web.php 39 KB

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