web.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. <?php
  2. use Illuminate\Support\Facades\Route;
  3. use Barryvdh\DomPDF\Facade\Pdf;
  4. /*
  5. |--------------------------------------------------------------------------
  6. | Web Routes
  7. |--------------------------------------------------------------------------
  8. |
  9. | Here is where you can register web routes for your application. These
  10. | routes are loaded by the RouteServiceProvider within a group which
  11. | contains the "web" middleware group. Now create something great!
  12. |
  13. */
  14. Route::get('/', function () {
  15. return view('login');
  16. // return Redirect::to('/dashboard');
  17. })->name('login');
  18. Route::post('/login', function () {
  19. if(\Auth::attempt(array('email' => $_POST["email"], 'password' => $_POST["password"])))
  20. {
  21. return Redirect::to('/dashboard');
  22. }
  23. else
  24. {
  25. return Redirect::to('/?error=1');
  26. }
  27. })->name('login');
  28. Route::get('/logout', function(){
  29. Auth::logout();
  30. return redirect('/');
  31. });
  32. Route::group(['middleware' => 'auth'],function(){
  33. //Route::get('/', \App\Http\Livewire\Login::class);
  34. Route::get('/dashboard', \App\Http\Livewire\Dashboard::class);
  35. Route::get('/settings', \App\Http\Livewire\Setting::class);
  36. Route::get('/courses', \App\Http\Livewire\Course::class);
  37. Route::get('/categories', \App\Http\Livewire\Category::class);
  38. Route::get('/nations_list', \App\Http\Livewire\Nation::class);
  39. Route::get('/provinces', \App\Http\Livewire\Province::class);
  40. Route::get('/cities', \App\Http\Livewire\City::class);
  41. Route::get('/banks', \App\Http\Livewire\Bank::class);
  42. Route::get('/vats', \App\Http\Livewire\Vat::class);
  43. Route::get('/disciplines', \App\Http\Livewire\Discipline::class);
  44. Route::get('/course_types', \App\Http\Livewire\CourseType::class);
  45. Route::get('/course_subscriptions', \App\Http\Livewire\CourseSubscription::class);
  46. Route::get('/course_durations', \App\Http\Livewire\CourseDuration::class);
  47. Route::get('/course_levels', \App\Http\Livewire\CourseLevel::class);
  48. Route::get('/course_frequencies', \App\Http\Livewire\CourseFrequency::class);
  49. Route::get('/course_list', \App\Http\Livewire\CourseList::class);
  50. Route::get('/course_member', \App\Http\Livewire\CourseMember::class);
  51. Route::get('/receipts', \App\Http\Livewire\Receipt::class);
  52. Route::get('/cards', \App\Http\Livewire\Card::class);
  53. Route::get('/causals', \App\Http\Livewire\Causal::class);
  54. Route::get('/payment_methods', \App\Http\Livewire\PaymentMethod::class);
  55. Route::get('/members', \App\Http\Livewire\Member::class);
  56. Route::get('/suppliers', \App\Http\Livewire\Supplier::class);
  57. Route::get('/sponsors', \App\Http\Livewire\Sponsor::class);
  58. Route::get('/records', \App\Http\Livewire\Record::class);
  59. Route::get('/reminders', \App\Http\Livewire\Reminder::class);
  60. Route::get('/in', \App\Http\Livewire\RecordIN::class);
  61. Route::get('/out', \App\Http\Livewire\RecordOUT::class);
  62. Route::get('/records_in_out', \App\Http\Livewire\RecordINOUT::class);
  63. Route::get('/users', \App\Http\Livewire\User::class);
  64. Route::get('/profile', \App\Http\Livewire\Profile::class);
  65. });
  66. Route::get('/receipt/{id}', function($id){
  67. $receipt = \App\Models\Receipt::findOrFail($id);
  68. $pdf = PDF::loadView('receipt', array('receipt' => $receipt));
  69. $pdfName = "Ricevuta_" . $receipt->member->last_name . "_" . $receipt->number . "_" . $receipt->year . ".pdf";
  70. return $pdf->stream($pdfName);
  71. /*return response()->streamDownload(
  72. fn () => print($pdf),
  73. "ricevuta_" . $receipt->number . "_" . $receipt->year . ".pdf"
  74. );*/
  75. });
  76. Route::get('/receipt/mail/{id}', function($id){
  77. $receipt = \App\Models\Receipt::findOrFail($id);
  78. if ($receipt->status == 99)
  79. sendReceiptDeleteEmail($receipt);
  80. else
  81. sendReceiptEmail($receipt);
  82. /*
  83. $pdf = PDF::loadView('receipt', array('receipt' => $receipt));
  84. $pdfName = "ricevuta_" . $receipt->number . "_" . $receipt->year . ".pdf";
  85. Storage::put('public/pdf/' . $pdfName, $pdf->output());
  86. $email = \App\Models\Member::findOrFail($receipt->member_id)->email;
  87. if ($email != '')
  88. {
  89. Mail::to($email)->send(new \App\Mail\ReceipEmail([
  90. 'name' => 'Luca',
  91. 'pdf' => 'public/pdf/' . $pdfName,
  92. 'number' => $receipt->number . "/" . $receipt->year
  93. ]));
  94. }
  95. */
  96. return true;
  97. //return $pdf->stream();
  98. /*return response()->streamDownload(
  99. fn () => print($pdf),
  100. "ricevuta_" . $receipt->number . "_" . $receipt->year . ".pdf"
  101. );*/
  102. });
  103. Route::get('/nations', function(){
  104. if (isset($_GET["q"]))
  105. $datas = \App\Models\Nation::where('name', 'like', $_GET["q"] . '%')->orderBy('name')->get();
  106. else
  107. $datas = \App\Models\Nation::orderBy('name')->get();
  108. $data = array();
  109. foreach($datas as $d)
  110. {
  111. $data[] = array("id" => $d->id, "text" => $d->name);
  112. }
  113. return array("results" => $data);
  114. });
  115. Route::get('/provinces/{nation_id}', function($nation_id){
  116. if (isset($_GET["q"]))
  117. $datas = \App\Models\Province::where('nation_id', $nation_id)->where('name', 'like', $_GET["q"] . '%')->orderBy('name')->get();
  118. else
  119. $datas = \App\Models\Province::where('nation_id', $nation_id)->orderBy('name')->get();
  120. $data = array();
  121. foreach($datas as $d)
  122. {
  123. $data[] = array("id" => $d->id, "text" => $d->name);
  124. }
  125. return array("results" => $data);
  126. });
  127. Route::get('/cities/{province_id}', function($province_id){
  128. if (isset($_GET["q"]))
  129. $datas = \App\Models\City::where('province_id', $province_id)->where('name', 'like', $_GET["q"] . '%')->orderBy('name')->get();
  130. else
  131. $datas = \App\Models\City::where('province_id', $province_id)->orderBy('name')->get();
  132. $data = array();
  133. foreach($datas as $d)
  134. {
  135. $data[] = array("id" => $d->id, "text" => $d->name);
  136. }
  137. return array("results" => $data);
  138. });
  139. Route::get('/get_members', function(){
  140. $datas = [];
  141. // $datas = \App\Models\Member::select('members.*')->where('id', '>', 0);
  142. $x = \App\Models\Member::select('id', 'first_name', 'last_name', 'phone', 'birth_date', 'to_complete', 'current_status', 'certificate', 'certificate_date')->where('id', '>', 0);
  143. if (isset($_GET["search"]["value"]))
  144. {
  145. $v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
  146. $x = $x->where(function ($query) use ($v) {
  147. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $v . "%'")
  148. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $v . "%'");
  149. });
  150. //where('first_name', 'like', '%' . $_GET["search"]["value"] . '%');
  151. }
  152. if ($_GET["cards"] != "")
  153. {
  154. $card_ids = \App\Models\MemberCard::whereIn('card_id', explode(",", $_GET["cards"]))->pluck('member_id');
  155. $x = $x->whereIn('id', $card_ids);
  156. }
  157. if ($_GET["filterCategories"] != "null")
  158. {
  159. $cats_ids = \App\Models\MemberCategory::whereIn('category_id', explode(",", $_GET["filterCategories"]))->pluck('member_id');
  160. $x = $x->whereIn('id', $cats_ids);
  161. }
  162. if ($_GET["fromYear"] != "")
  163. {
  164. $x = $x->where('birth_date', '<', date("Y-m-d", strtotime("-" . $_GET["fromYear"] . " year", time())));
  165. }
  166. if ($_GET["toYear"] != "")
  167. {
  168. $x = $x->where('birth_date', '>', date("Y-m-d", strtotime("-" . $_GET["toYear"] . " year", time())));
  169. }
  170. if ($_GET["fromYearYear"] != "")
  171. {
  172. $x = $x->whereYear('birth_date', '>=', $_GET["fromYearYear"]);
  173. }
  174. if ($_GET["toYearYear"] != "")
  175. {
  176. $x = $x->whereYear('birth_date', '<=', $_GET["toYearYear"]);
  177. }
  178. $ids = [];
  179. if ($_GET["filterCertificateType"] != "null")
  180. {
  181. $types = \App\Models\MemberCertificate::where('type', $_GET["filterCertificateType"])->where('expire_date', '>', date("Y-m-d"))->pluck('member_id');
  182. $x = $x->whereIn('id', $types->toArray());;
  183. //$ids = array_merge($ids, $types->toArray());
  184. }
  185. if ($_GET["filterScadenza"] != "null")
  186. {
  187. if ($_GET["filterScadenza"] == "1")
  188. $scad = \App\Models\MemberCertificate::where('expire_date', '<', date("Y-m-d"))->pluck('member_id');
  189. if ($_GET["filterScadenza"] == "2")
  190. $scad = \App\Models\MemberCertificate::whereBetween('expire_date', [date("Y-m-d"), date("Y-m-d", strtotime("+1 month"))])->pluck('member_id');
  191. //$ids = array_merge($ids, $scad->toArray());
  192. $x = $x->whereIn('id', $scad->toArray());;
  193. //$x = $x->whereIn('id', $scadenza);
  194. }
  195. if ($_GET["filterStatus"] != "null")
  196. {
  197. $status = explode(",", $_GET["filterStatus"]);
  198. $members = \App\Models\Member::all();
  199. foreach($status as $s)
  200. {
  201. foreach($members as $m)
  202. {
  203. $state = $m->isActive();
  204. if ($state["status"] == $s)
  205. $ids[] = $m->id;
  206. }
  207. }
  208. }
  209. if (sizeof($ids) > 0)
  210. {
  211. $x = $x->whereIn('id', $ids);
  212. }
  213. else
  214. {
  215. if ($_GET["filterStatus"] != "null")
  216. $x = $x->whereIn('id', [-1]);
  217. }
  218. $count = $x->count();
  219. $x = $x->orderBy('to_complete', 'DESC');
  220. if (isset($_GET["order"]))
  221. {
  222. $column = '';
  223. if ($_GET["order"][0]["column"] == 0)
  224. $column = 'last_name';
  225. if ($_GET["order"][0]["column"] == 1)
  226. $column = 'first_name';
  227. if ($_GET["order"][0]["column"] == 2)
  228. $column = 'phone';
  229. if ($_GET["order"][0]["column"] == 3)
  230. $column = 'birth_date';
  231. if ($_GET["order"][0]["column"] == 4)
  232. $column = 'birth_date';
  233. if ($_GET["order"][0]["column"] == 5)
  234. $column = 'current_status';
  235. if ($_GET["order"][0]["column"] == 6)
  236. $column = 'certificate';
  237. if ($column != '')
  238. $x = $x->orderBy($column, $_GET["order"][0]["dir"]);
  239. else
  240. $x = $x->orderBy('last_name', 'ASC')->orderBy('first_name', 'ASC');
  241. }
  242. else
  243. $x = $x->orderBy('last_name', 'ASC')->orderBy('first_name', 'ASC');
  244. if (isset($_GET["start"]))
  245. $x = $x->offset($_GET["start"])->limit($_GET["length"])->get();
  246. else
  247. $x = $x->get();
  248. foreach($x as $idx => $r)
  249. {
  250. // $status = $r->getStatus();
  251. // $status = $status["status"];
  252. $status = $r->current_status;
  253. $class = $status > 0 ? ($status == 2 ? 'active' : 'due') : 'suspended';
  254. $text = $status > 0 ? ($status == 2 ? 'Tesserato' : 'Sospeso') : 'Non tesserato';
  255. if ($r->to_complete)
  256. {
  257. $text = 'Da completare';
  258. $class = "complete";
  259. }
  260. // $has_certificate = $r->hasCertificate();
  261. $y = '';
  262. if ($r->certificate_date != '')
  263. {
  264. $y = $r->certificate . "|" . date("d/m/Y", strtotime($r->certificate_date));
  265. }
  266. /*
  267. if($has_certificate["date"] != '')
  268. {
  269. if($has_certificate["date"] < date("Y-m-d"))
  270. $y .= '0';
  271. if($has_certificate["date"] >= date("Y-m-d") && $has_certificate["date"] < date("Y-m-d", strtotime("+1 month")))
  272. $y .= '1';
  273. if($has_certificate["date"] >= date("Y-m-d", strtotime("+1 month")))
  274. $y .= '2';
  275. $y .= '|';
  276. $y .= $has_certificate["date"] != '' ? date("d/m/Y", strtotime($has_certificate["date"])) : '';
  277. }*/
  278. $datas[] = array(
  279. //'c' => $idx + 1,
  280. //'id' => "ID" . str_pad($r->id, 5, "0", STR_PAD_LEFT),
  281. 'last_name' => $r->last_name . "|" . $r->id,
  282. 'first_name' => $r->first_name . "|" . $r->id,
  283. 'phone' => $r->phone,
  284. 'age' => $r->getAge(),
  285. 'year' => date("Y", strtotime($r->birth_date)),
  286. 'status' => $class . "|" . $text,
  287. // 'state' => $x,
  288. 'certificate' => $y,
  289. 'action' => $r->id
  290. );
  291. }
  292. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count));
  293. });
  294. Route::get('/get_record_in', function(){
  295. $datas = [];
  296. $x = \App\Models\Record::select('records.*', \DB::raw('members.first_name as first_name'), \DB::raw('members.last_name as last_name'), \DB::raw('payment_methods.name as payment')) // , \DB::raw('SUM(records.id) As total'))
  297. ->leftJoin('members', 'records.member_id', '=', 'members.id')
  298. ->leftJoin('payment_methods', 'records.payment_method_id', '=', 'payment_methods.id')
  299. ->where('records.type', 'IN');
  300. if (isset($_GET["search"]["value"]))
  301. {
  302. $v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
  303. $x = $x->where(function ($query) use ($v) {
  304. $query->where('first_name', 'like', '%' . $v . '%')
  305. ->orWhere('last_name', 'like', '%' . $v . '%');
  306. });
  307. //where('first_name', 'like', '%' . $_GET["search"]["value"] . '%');
  308. }
  309. //$x = $x->where(function ($query) use ($v) {
  310. // $datas = \App\Models\Record::where('type', 'IN')->with('member', 'payment_method');
  311. if ($_GET["filterCommercial"] == 1)
  312. {
  313. $x = $x->where('commercial', true );
  314. }
  315. if ($_GET["filterCommercial"] == 2)
  316. {
  317. $x = $x->where('commercial', false);
  318. }
  319. if ($_GET["filterMember"] > 0)
  320. {
  321. $x = $x->where('member_id', $_GET["filterMember"]);
  322. }
  323. if ($_GET["filterPaymentMethod"] != "null")
  324. {
  325. $payments = explode(",", $_GET["filterPaymentMethod"]);
  326. $x = $x->whereIn('payment_method_id', $payments);
  327. }
  328. if ($_GET["filterCausals"] != "null")
  329. {
  330. $causals = explode(",", $_GET["filterCausals"]);
  331. //$causals = \App\Models\RecordRow::where('causal_id', $_GET["filterCausals"])->pluck('record_id');
  332. $causals = \App\Models\RecordRow::whereIn('causal_id', $causals)->pluck('record_id');
  333. $x = $x->whereIn('records.id', $causals);
  334. }
  335. if ($_GET["filterFrom"] != '')
  336. {
  337. $x = $x->where('date', '>=', $_GET["filterFrom"]);
  338. }
  339. if ($_GET["filterTo"] != '')
  340. {
  341. $x = $x->where('date', '<=', $_GET["filterTo"]);
  342. }
  343. //});
  344. $start = 0;
  345. $limit = 100000;
  346. if (isset($_GET["start"]))
  347. {
  348. $start = $_GET["start"];
  349. $limit = $_GET["length"];
  350. }
  351. $excludeCausals = [];
  352. /*$borsellino = \App\Models\Causal::where('money', true)->first();
  353. if ($borsellino)
  354. $excludeCausals[] = $borsellino->id;*/
  355. // Aggiungo
  356. $excludes = \App\Models\Causal::where('no_records', true)->get();
  357. foreach($excludes as $e)
  358. {
  359. $excludeCausals[] = $e->id;
  360. }
  361. $exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
  362. // Pagamento money
  363. $moneys = \App\Models\PaymentMethod::where('money', true)->pluck('id')->toArray();
  364. // Causale money
  365. $moneysCausal = \App\Models\Causal::where('money', true)->pluck('id')->toArray();
  366. $total = 0;
  367. /*
  368. foreach($x->get() as $r)
  369. {
  370. if (!in_array($r->payment_method_id, $moneys))
  371. {
  372. foreach($r->rows as $rr)
  373. {
  374. if ((!in_array($rr->member_id, $exclude_from_records) || in_array($r->causal_id, $moneysCausal)) && (!$r->deleted || $r->deleted == null) && (!in_array($rr->causal_id, $excludeCausals) || in_array($r->causal_id, $moneysCausal)) && (!$r->financial_movement || $r->financial_movement == null) && (!$r->corrispettivo_fiscale || $r->corrispettivo_fiscale == null))
  375. {
  376. $total += $rr->amount;
  377. if ($rr->vat_id > 0)
  378. $total += getVatValue($rr->amount, $rr->vat_id);
  379. }
  380. }
  381. }
  382. }
  383. */
  384. $count = $x->count();
  385. if (isset($_GET["order"]))
  386. {
  387. $column = '';
  388. if ($_GET["order"][0]["column"] == 0)
  389. $column = 'date';
  390. if ($_GET["order"][0]["column"] == 1)
  391. $column = 'records.amount';
  392. if ($_GET["order"][0]["column"] == 2)
  393. $column = 'last_name';
  394. if ($_GET["order"][0]["column"] == 3)
  395. $column = 'first_name';
  396. if ($_GET["order"][0]["column"] == 4)
  397. $column = 'commercial';
  398. if ($column != '')
  399. $x = $x->orderBy($column, $_GET["order"][0]["dir"])->orderBy('records.id', 'DESC');
  400. else
  401. $x = $x->orderBy('records.id', 'DESC');
  402. }
  403. else
  404. $x = $x->orderBy('records.date', 'DESC')->orderBy('records.id', 'DESC');
  405. $x = $x->offset($start)->limit($limit)->get();
  406. foreach($x as $idx => $r)
  407. {
  408. $causals = '';
  409. foreach($r->rows as $row)
  410. {
  411. $causals .= $row->causal->getTree() . "<br>";
  412. }
  413. $datas[] = array(
  414. //'id' => $r->id,
  415. 'date' => $r->date,
  416. 'total' => formatPrice($r->getTotal()),
  417. 'first_name' => $r->first_name,
  418. 'last_name' => $r->last_name,
  419. 'commercial' => $r->financial_movement ? 'Movimento finanziario' : ($r->commercial ? 'SI' : 'NO'),
  420. 'causals' => $causals,
  421. 'payment' => $r->payment_method->name,
  422. 'status' => $r->deleted ? 'Annullato' : '',
  423. 'action' => $r->id . "|" . formatPrice($total) . "|" . ($r->deleted ? 'x' : '')
  424. );
  425. }
  426. /*$datas[] = array(
  427. //'id' => $r->id,
  428. 'date' => '',
  429. 'total' => formatPrice($total),
  430. 'first_name' => '',
  431. 'last_name' => '',
  432. 'commercial' => '',
  433. 'causals' => '',
  434. 'payment' => '',
  435. 'status' => '',
  436. 'action' => ''
  437. );*/
  438. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count));
  439. });
  440. Route::get('/get_record_out', function(){
  441. $datas = [];
  442. $x = \App\Models\Record::where('type', 'OUT')->with('supplier', 'payment_method');
  443. if ($_GET["filterSupplier"] > 0)
  444. {
  445. $x = $x->where('supplier_id', $_GET["filterSupplier"]);
  446. }
  447. /*if ($_GET["filterPaymentMethod"] > 0)
  448. {
  449. $x = $x->where('payment_method_id', $_GET["filterPaymentMethod"]);
  450. }
  451. if ($_GET["filterCausals"] > 0)
  452. {
  453. $causals = \App\Models\RecordRow::where('causal_id', $_GET["filterCausals"])->pluck('record_id');
  454. $x = $x->whereIn('records.id', $causals);
  455. }*/
  456. if ($_GET["filterPaymentMethod"] != "null")
  457. {
  458. $payments = explode(",", $_GET["filterPaymentMethod"]);
  459. $x = $x->whereIn('payment_method_id', $payments);
  460. }
  461. if ($_GET["filterCausals"] != "null")
  462. {
  463. $causals = explode(",", $_GET["filterCausals"]);
  464. //$causals = \App\Models\RecordRow::where('causal_id', $_GET["filterCausals"])->pluck('record_id');
  465. $causals = \App\Models\RecordRow::whereIn('causal_id', $causals)->pluck('record_id');
  466. $x = $x->whereIn('records.id', $causals);
  467. }
  468. if ($_GET["filterFrom"] != '')
  469. {
  470. $x = $x->where('date', '>=', $_GET["filterFrom"]);
  471. }
  472. if ($_GET["filterTo"] != '')
  473. {
  474. $x = $x->where('date', '<=', $_GET["filterTo"]);
  475. }
  476. $total = 0;
  477. /*foreach($x->get() as $r)
  478. {
  479. foreach($r->rows as $rr)
  480. {
  481. $total += $rr->amount;
  482. if ($rr->vat_id > 0)
  483. $total += getVatValue($rr->amount, $rr->vat_id);
  484. }
  485. }*/
  486. $x = $x->get();
  487. foreach($x as $idx => $r)
  488. {
  489. $causals = '';
  490. foreach($r->rows as $row)
  491. {
  492. $causals .= $row->causal->getTree() . "<br>";
  493. }
  494. $datas[] = array(
  495. //'id' => $r->id,
  496. 'date' => $r->date,
  497. 'total' => formatPrice($r->getTotal()),
  498. 'supplier' => $r->supplier->name,
  499. 'causals' => $causals,
  500. 'payment' => $r->payment_method->name,
  501. 'action' => $r->id . "|" . formatPrice($total)
  502. );
  503. }
  504. /*
  505. $datas[] = array(
  506. //'id' => $r->id,
  507. 'date' => '',
  508. 'total' => formatPrice($total),
  509. 'supplier' => '',
  510. 'causals' => '',
  511. 'payment' => '',
  512. 'action' => ''
  513. );
  514. */
  515. return json_encode(array("data" => $datas));
  516. });
  517. Route::get('/get_course_list', function(){
  518. $member_course = \App\Models\MemberCourse::with('member');
  519. if (isset($_GET["search"]["value"]))
  520. {
  521. $v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
  522. $member_ids = \App\Models\Member::where(function ($query) use ($v) {
  523. $query->where('first_name', 'like', '%' . $v . '%')
  524. ->orWhere('last_name', 'like', '%' . $v . '%');
  525. })->pluck('id');
  526. $member_course = $member_course->whereIn('member_id', $member_ids);
  527. }
  528. if ($_GET["filterCourse"] != "null")
  529. {
  530. $course_ids = [];
  531. $courses = explode(",", $_GET["filterCourse"]);
  532. foreach($courses as $c)
  533. {
  534. $all = \App\Models\Course::where('name', 'like', '%' . $c . "%")->get();
  535. foreach($all as $a)
  536. {
  537. $course_ids[] = $a->id;
  538. }
  539. }
  540. $member_course = $member_course->whereIn('course_id', $course_ids);
  541. }
  542. if ($_GET["filterYear"] != "")
  543. {
  544. $course_ids = \App\Models\Course::where('year', $_GET["filterYear"])->pluck('id');
  545. $member_course = $member_course->whereIn('course_id', $course_ids);
  546. }
  547. if ($_GET["filterLevel"] != "null")
  548. {
  549. $levels = explode(",", $_GET["filterLevel"]);
  550. $course_ids = \App\Models\Course::whereIn('course_level_id', $levels)->pluck('id');
  551. $member_course = $member_course->whereIn('course_id', $course_ids);
  552. }
  553. if ($_GET["filterFrequency"] != "null")
  554. {
  555. $frequencies = explode(",", $_GET["filterFrequency"]);
  556. $course_ids = \App\Models\Course::whereIn('course_frequency_id', $frequencies)->pluck('id');
  557. $member_course = $member_course->whereIn('course_id', $course_ids);
  558. }
  559. if ($_GET["filterType"] != "null")
  560. {
  561. $types = explode(",", $_GET["filterType"]);
  562. $course_ids = \App\Models\Course::whereIn('course_type_id', $types)->pluck('id');
  563. $member_course = $member_course->whereIn('course_id', $course_ids);
  564. }
  565. if ($_GET["filterDuration"] != "null")
  566. {
  567. $durations = explode(",", $_GET["filterDuration"]);
  568. $course_ids = \App\Models\Course::whereIn('course_duration_id', $durations)->pluck('id');
  569. $member_course = $member_course->whereIn('course_id', $course_ids);
  570. }
  571. $totals = [];
  572. $totalIsc = [];
  573. $datas = [];
  574. $xxx = 1;
  575. $member_course_totals = $member_course->get();
  576. foreach($member_course_totals as $x)
  577. {
  578. $price = 0;
  579. $price = $x->course->price;
  580. $subPrice = $x->course->subscription_price;
  581. $records = \App\Models\Record::where('member_course_id', $x->id)->where('deleted', 0)->get();
  582. $prices = [];
  583. foreach ($records as $record)
  584. {
  585. foreach ($record->rows as $row)
  586. {
  587. if ($row->causal_id == $x->course->sub_causal_id) // || str_contains(strtolower($row->note), 'iscrizione'))
  588. //if (str_contains(strtolower($row->note), 'iscrizione'))
  589. {
  590. $subPrice = $row->amount;
  591. }
  592. if ($row->causal_id == $x->course->causal_id && !str_contains(strtolower($row->note), 'iscrizione'))
  593. {
  594. $tot = sizeof(json_decode($row->when));
  595. foreach(json_decode($row->when) as $m)
  596. {
  597. $prices[$m->month] = $row->amount / $tot;
  598. }
  599. }
  600. }
  601. }
  602. for($i=1; $i<=12; $i++)
  603. {
  604. $cls = getColor($x->months, $i);
  605. if ($cls != 'grey')
  606. {
  607. if (!isset($totals[$i]))
  608. {
  609. $totals[$i]['green'] = 0;
  610. $totals[$i]['orange'] = 0;
  611. $totals[$i]['yellow'] = 0;
  612. }
  613. if ($cls == 'yellow')
  614. {
  615. $totals[$i][$cls] += 1;
  616. }
  617. else
  618. {
  619. $p = isset($prices[$i]) ? $prices[$i] : $price;
  620. $totals[$i][$cls] += $p;
  621. }
  622. }
  623. }
  624. $sub = $x->subscribed ? "Y" : "N";
  625. if (isset($totalIsc[$sub]))
  626. $totalIsc[$sub] += $subPrice;
  627. else
  628. $totalIsc[$sub] = $subPrice;
  629. $datas[] = array(
  630. "column_0" => $x->member->last_name,
  631. "column_1" => $x->member->first_name,
  632. "column_2" => $x->subscribed . "§" . formatPrice($subPrice),
  633. "column_3" => getColor($x->months, 9) . "§" . formatPrice(isset($prices[9]) ? $prices[9] : $price),
  634. "column_4" => getColor($x->months, 10) . "§" . formatPrice(isset($prices[10]) ? $prices[10] : $price),
  635. "column_5" => getColor($x->months, 11) . "§" . formatPrice(isset($prices[11]) ? $prices[11] : $price),
  636. "column_6" => getColor($x->months, 12) . "§" . formatPrice(isset($prices[12]) ? $prices[12] : $price),
  637. "column_7" => getColor($x->months, 1) . "§" . formatPrice(isset($prices[1]) ? $prices[1] : $price),
  638. "column_8" => getColor($x->months, 2) . "§" . formatPrice(isset($prices[2]) ? $prices[2] : $price),
  639. "column_9" => getColor($x->months, 3) . "§" . formatPrice(isset($prices[3]) ? $prices[3] : $price),
  640. "column_10" => getColor($x->months, 4) . "§" . formatPrice(isset($prices[4]) ? $prices[4] : $price),
  641. "column_11" => getColor($x->months, 5) . "§" . formatPrice(isset($prices[5]) ? $prices[5] : $price),
  642. "column_12" => getColor($x->months, 6) . "§" . formatPrice(isset($prices[6]) ? $prices[6] : $price),
  643. "column_13" => getColor($x->months, 7) . "§" . formatPrice(isset($prices[7]) ? $prices[7] : $price),
  644. "column_14" => getColor($x->months, 8) . "§" . formatPrice(isset($prices[8]) ? $prices[8] : $price),
  645. "column_15" => $x->course_id,
  646. "column_16" => $x->id,
  647. "column_17" => $x->member_id,
  648. "column_18" => $xxx++
  649. );
  650. }
  651. $count = $member_course->count();
  652. $js = '';
  653. $xx = 3;
  654. $str = '';
  655. if ($count > 0)
  656. {
  657. $str .= "<a style='width:100%;float:right; text-align:right; display:block;' class=green><small>" . (isset($totalIsc["Y"]) ? formatPrice($totalIsc["Y"]) : 0) . "</small></a><br>";
  658. $str .= "<a style='width:100%;float:right; text-align:right; display:block;' class=orange><small>" . (isset($totalIsc["N"]) ? formatPrice($totalIsc["N"]) : 0) . "</small></a><br>";
  659. $str .= "<a style='width:100%;float:right; text-align:right; display:block;' class=yellow><small>0</small></a><br>";
  660. }
  661. $js .= $xx . "§" . $str . "_";
  662. $str = "";
  663. foreach($totals as $z => $t)
  664. {
  665. if ($z == 1) $xx = 8;
  666. if ($z == 2) $xx = 9;
  667. if ($z == 3) $xx = 10;
  668. if ($z == 4) $xx = 11;
  669. if ($z == 5) $xx = 12;
  670. if ($z == 6) $xx = 13;
  671. if ($z == 7) $xx = 14;
  672. if ($z == 8) $xx = 15;
  673. if ($z == 9) $xx = 4;
  674. if ($z == 10) $xx = 5;
  675. if ($z == 11) $xx = 6;
  676. if ($z == 12) $xx = 7;
  677. $str = '';
  678. foreach($t as $x => $c)
  679. {
  680. $y = $x == 'yellow' ? $c : formatPrice($c);
  681. $str .= "<a style='width:100%;float:right; text-align:right; display:block;' class=" . $x . "><small>" . $y . "</small></a><br>";
  682. }
  683. $js .= $xx . "§" . $str . "_";
  684. $xx += 1;
  685. }
  686. if (isset($_GET["order"]))
  687. array_multisort(array_column($datas, 'column_' . ($_GET["order"][0]["column"] - 1)), $_GET["order"][0]["dir"] == "asc" ? SORT_ASC : SORT_DESC, SORT_NATURAL|SORT_FLAG_CASE, $datas);
  688. $xxx = 1;
  689. foreach($datas as $yyy => $d)
  690. {
  691. $datas[$yyy]["column_18"] = $xxx++;
  692. }
  693. if (isset($_GET["start"]))
  694. $datas = array_slice($datas, $_GET["start"], $_GET["length"]);
  695. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count, "totals" => $js));
  696. });
  697. Route::get('/get_course_members', function(){
  698. //$datas = \App\Models\MemberCourse::with('member');
  699. $datas = \App\Models\MemberCourse::select('member_courses.*', 'members.first_name', 'members.last_name', 'members.email', 'members.phone', 'members.birth_date')->leftJoin('members', 'member_courses.member_id', '=', 'members.id');
  700. if (isset($_GET["search"]["value"]))
  701. {
  702. $v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
  703. $member_ids = \App\Models\Member::where(function ($query) use ($v) {
  704. $query->where('first_name', 'like', '%' . $v . '%')
  705. ->orWhere('last_name', 'like', '%' . $v . '%');
  706. })->pluck('id');
  707. $datas = $datas->whereIn('member_id', $member_ids);
  708. }
  709. if ($_GET["filterCourse"] != "null")
  710. {
  711. $course_ids = [];
  712. $courses = explode(",", $_GET["filterCourse"]);
  713. foreach($courses as $c)
  714. {
  715. $all = \App\Models\Course::where('name', 'like', '%' . $c . "%")->get();
  716. foreach($all as $a)
  717. {
  718. $course_ids[] = $a->id;
  719. }
  720. }
  721. $datas = $datas->whereIn('course_id', $course_ids);
  722. }
  723. if ($_GET["filterLevel"] != "null")
  724. {
  725. $levels = explode(",", $_GET["filterLevel"]);
  726. $course_ids = \App\Models\Course::whereIn('course_level_id', $levels)->pluck('id');
  727. $datas = $datas->whereIn('course_id', $course_ids);
  728. }
  729. if ($_GET["filterFrequency"] != "null")
  730. {
  731. $frequencies = explode(",", $_GET["filterFrequency"]);
  732. $course_ids = \App\Models\Course::whereIn('course_frequency_id', $frequencies)->pluck('id');
  733. $datas = $datas->whereIn('course_id', $course_ids);
  734. }
  735. if ($_GET["filterType"] != "null")
  736. {
  737. $types = explode(",", $_GET["filterType"]);
  738. $course_ids = \App\Models\Course::whereIn('course_type_id', $types)->pluck('id');
  739. $datas = $datas->whereIn('course_id', $course_ids);
  740. }
  741. if ($_GET["filterDuration"] != "null")
  742. {
  743. $durations = explode(",", $_GET["filterDuration"]);
  744. $course_ids = \App\Models\Course::whereIn('course_duration_id', $durations)->pluck('id');
  745. $datas = $datas->whereIn('course_id', $course_ids);
  746. }
  747. if ($_GET["filterDays"] != "null")
  748. {
  749. $ids = [];
  750. $days = explode(",", $_GET["filterDays"]);
  751. foreach($days as $d)
  752. {
  753. $all = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")->get();
  754. foreach($all as $a)
  755. {
  756. $ids[] = $a->id;
  757. }
  758. }
  759. $datas = $datas->whereIn('member_courses.id', $ids);
  760. }
  761. if ($_GET["filterHours"] != "null")
  762. {
  763. $ids = [];
  764. $hours = explode(",", $_GET["filterHours"]);
  765. foreach($hours as $h)
  766. {
  767. $all = \App\Models\MemberCourse::where('when', 'like', '%"from":"' . $h . "%")->get();
  768. foreach($all as $a)
  769. {
  770. $ids[] = $a->id;
  771. }
  772. }
  773. $datas = $datas->whereIn('member_courses.id', $ids);
  774. }
  775. if ($_GET["filterSubscription"] != "")
  776. {
  777. $ids = \App\Models\MemberCourse::where('subscribed', $_GET["filterSubscription"] == 1 ? true : false)->pluck('id');
  778. $datas = $datas->whereIn('member_courses.id', $ids);
  779. //$this->filter .= $this->filter != '' ? ', ' : '';
  780. //$this->filter .= "Pagata sottoscrizione : " . ($this->filterSubscription == 1 ? "SI" : "NO") . " ";
  781. }
  782. if ($_GET["filterCertificateType"] != "null")
  783. {
  784. $ctypes = \App\Models\MemberCertificate::where('type', $_GET["filterCertificateType"])->where('expire_date', '>', date("Y-m-d"))->pluck('member_id');
  785. $datas = $datas->whereIn('member_id', $ctypes);
  786. }
  787. if ($_GET["filterCertificateScadenza"] != "null")
  788. {
  789. if ($_GET["filterCertificateScadenza"] == "1")
  790. $scad = \App\Models\MemberCertificate::where('expire_date', '<', date("Y-m-d"))->pluck('member_id');
  791. if ($_GET["filterCertificateScadenza"] == "2")
  792. $scad = \App\Models\MemberCertificate::whereBetween('expire_date', [date("Y-m-d"), date("Y-m-d", strtotime("+1 month"))])->pluck('member_id');
  793. $datas = $datas->whereIn('member_id', $scad);
  794. }
  795. if ($_GET["fromYear"] != "")
  796. {
  797. $m_ids = \App\Models\Member::where('birth_date', '<', date("Y-m-d", strtotime("-" . $_GET["fromYear"] . " year", time())))->pluck('id');
  798. $datas = $datas->whereIn('member_id', $m_ids);
  799. }
  800. if ($_GET["toYear"] != "")
  801. {
  802. $m_ids = \App\Models\Member::where('birth_date', '>', date("Y-m-d", strtotime("-" . $_GET["toYear"] . " year", time())))->pluck('id');
  803. $datas = $datas->whereIn('member_id', $m_ids);
  804. }
  805. if ($_GET["fromFromYear"] != "")
  806. {
  807. $m_ids = \App\Models\Member::whereYear('birth_date', '>=', $_GET["fromFromYear"])->pluck('id');
  808. $datas = $datas->whereIn('member_id', $m_ids);
  809. }
  810. if ($_GET["toToYear"] != "")
  811. {
  812. $m_ids = \App\Models\Member::whereYear('birth_date', '<=', $_GET["toToYear"])->pluck('id');
  813. $datas = $datas->whereIn('member_id', $m_ids);
  814. }
  815. if ($_GET["filterCards"] != "null")
  816. {
  817. $cards = explode(",", $_GET["filterCards"]);
  818. $card_ids = \App\Models\MemberCard::whereIn('card_id', $cards)->pluck('member_id');
  819. $datas = $datas->whereIn('member_id', $card_ids);
  820. }
  821. if ($_GET["filterYear"] != "")
  822. {
  823. $course_ids = \App\Models\Course::where('year', $_GET["filterYear"])->pluck('id');
  824. $datas = $datas->whereIn('course_id', $course_ids);
  825. //$this->filter .= $this->filter != '' ? ', ' : '';
  826. //$this->filter .= "Anno : " . $this->filterYear . " ";
  827. }
  828. $aRet = [];
  829. if (isset($_GET["order"]))
  830. {
  831. $column = '';
  832. if ($_GET["order"][0]["column"] == 1)
  833. $column = 'last_name';
  834. if ($_GET["order"][0]["column"] == 2)
  835. $column = 'first_name';
  836. if ($_GET["order"][0]["column"] == 2)
  837. $column = 'birth_date';
  838. if ($_GET["order"][0]["column"] == 3)
  839. $column = 'birth_date';
  840. if ($_GET["order"][0]["column"] == 4)
  841. $column = 'birth_date';
  842. if ($_GET["order"][0]["column"] == 5)
  843. $column = 'phone';
  844. if ($_GET["order"][0]["column"] == 6)
  845. $column = 'email';
  846. if ($column != '')
  847. $datas = $datas->orderBy($column, $_GET["order"][0]["dir"]);
  848. else
  849. $datas = $datas->orderBy('last_name', 'ASC')->orderBy('first_name', 'ASC');
  850. }
  851. else
  852. $datas = $datas->orderBy('last_name', 'ASC')->orderBy('first_name', 'ASC');
  853. if ($_GET["filterStatus"] != "null")
  854. {
  855. $status = explode(",", $_GET["filterStatus"]);
  856. foreach($status as $s)
  857. {
  858. foreach($datas->get() as $aaa)
  859. {
  860. $state = \App\Models\Member::findOrFail($aaa->member_id)->isActive();
  861. if ($state["status"] == $s)
  862. $aRet[] = $aaa;
  863. }
  864. }
  865. }
  866. else
  867. $aRet = $datas->get();
  868. $ret = [];
  869. foreach($aRet as $idx => $r)
  870. {
  871. $date1 = new DateTime($r->birth_date);
  872. $date2 = new DateTime("now");
  873. $interval = $date1->diff($date2);
  874. $ret[] = array(
  875. "column_0" => $idx + 1,
  876. "column_1" => $r->last_name,
  877. "column_2" => $r->first_name,
  878. "column_3" => strval($interval->y),
  879. "column_4" => date("Y", strtotime($r->birth_date)),
  880. "column_5" => $r->phone,
  881. "column_6" => $r->email,
  882. "column_7" => $r->member_id
  883. );
  884. }
  885. if (isset($_GET["start"]))
  886. $ret = array_slice($ret, $_GET["start"], $_GET["length"]);
  887. return json_encode(array("data" => $ret, "recordsTotal" => sizeof($aRet), "recordsFiltered" => sizeof($aRet)));
  888. });
  889. Route::get('/get_receipts', function(){
  890. $x = \App\Models\Receipt::select('receipts.*', 'members.first_name', 'members.last_name')->leftJoin('members', 'receipts.member_id', '=', 'members.id');
  891. if (isset($_GET["search"]["value"]))
  892. {
  893. $v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
  894. $member_ids = \App\Models\Member::where(function ($query) use ($v) {
  895. $query->where('first_name', 'like', '%' . $v . '%')
  896. ->orWhere('last_name', 'like', '%' . $v . '%');
  897. })->pluck('id');
  898. $x = $x->whereIn('member_id', $member_ids);
  899. }
  900. if ($_GET["filterStatus"] != '')
  901. $x = $x->where('receipts.status', $_GET["filterStatus"]);
  902. if ($_GET["filterFrom"] != "")
  903. $x = $x->where('date', '>=', $_GET["filterFrom"]);
  904. if ($_GET["filterTo"] != "")
  905. $x = $x->where('date', '<=', $_GET["filterTo"]);
  906. $count = $x->count();
  907. if (isset($_GET["order"]))
  908. {
  909. $column = '';
  910. if ($_GET["order"][0]["column"] == 0)
  911. $column = 'year';
  912. if ($_GET["order"][0]["column"] == 1)
  913. $column = 'number';
  914. if ($_GET["order"][0]["column"] == 2)
  915. $column = 'last_name';
  916. if ($_GET["order"][0]["column"] == 3)
  917. $column = 'first_name';
  918. if ($_GET["order"][0]["column"] == 4)
  919. $column = 'status';
  920. if ($_GET["order"][0]["column"] == 5)
  921. $column = 'date';
  922. if ($column != '')
  923. $x = $x->orderBy($column, $_GET["order"][0]["dir"])->orderBy('id', 'DESC');
  924. else
  925. $x = $x->orderBy('id', 'DESC');
  926. }
  927. else
  928. $x = $x->orderBy('id', 'DESC');
  929. if (isset($_GET["start"]))
  930. $x = $x->offset($_GET["start"])->limit($_GET["length"])->get();
  931. else
  932. $x = $x->get();
  933. $datas = [];
  934. foreach($x as $idx => $r)
  935. {
  936. $datas[] = array(
  937. 'year' => $r->year,
  938. 'number' => $r->number,
  939. 'last_name' => $r->member->last_name,
  940. 'first_name' => $r->member->first_name,
  941. 'status' => $r->status,
  942. 'date' => date("d/m/Y", strtotime($r->date)),
  943. 'totals' => formatPrice($r->rows->sum('amount')),
  944. 'action' => $r->id
  945. );
  946. }
  947. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count));
  948. });
  949. function getColor($months, $m)
  950. {
  951. $class = "grey";
  952. foreach(json_decode($months) as $mm)
  953. {
  954. if ($mm->m == $m)
  955. {
  956. if ($mm->status == "")
  957. {
  958. $class = "orange";
  959. }
  960. if ($mm->status == "1")
  961. {
  962. $class = "green";
  963. }
  964. if ($mm->status == "2")
  965. {
  966. $class = "yellow";
  967. }
  968. }
  969. }
  970. return $class;
  971. }
  972. Route::get('/migrate', function(){
  973. \Artisan::call('migrate');
  974. dd('migrated!');
  975. });
  976. Route::get('/updateData', function()
  977. {
  978. // Call and Artisan command from within your application.
  979. Artisan::call('update:data');
  980. });
  981. Route::get('/seed', function()
  982. {
  983. // Call and Artisan command from within your application.
  984. Artisan::call('db:seed');
  985. });
  986. Route::get('/updateCourseCausal', function(){
  987. $member_courses = \App\Models\MemberCourse::all();
  988. foreach($member_courses as $x)
  989. {
  990. $records = \App\Models\Record::where('member_course_id', $x->id)->get();
  991. foreach ($records as $record)
  992. {
  993. foreach ($record->rows as $row)
  994. {
  995. //if ($row->causal_id == $x->course->sub_causal_id || str_contains(strtolower($row->note), 'iscrizione'))
  996. if (str_contains(strtolower($row->note), 'iscrizione'))
  997. {
  998. $row->causal_id = $x->course->sub_causal_id;
  999. $row->save();
  1000. }
  1001. }
  1002. }
  1003. }
  1004. });