web.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  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')->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 ($column != '')
  216. $x = $x->orderBy($column, $_GET["order"][0]["dir"]);
  217. else
  218. $x = $x->orderBy('last_name', 'ASC')->orderBy('first_name', 'ASC');
  219. }
  220. else
  221. $x = $x->orderBy('last_name', 'ASC')->orderBy('first_name', 'ASC');
  222. if (isset($_GET["start"]))
  223. $x = $x->offset($_GET["start"])->limit($_GET["length"])->get();
  224. else
  225. $x = $x->get();
  226. foreach($x as $idx => $r)
  227. {
  228. $status = $r->getStatus();
  229. $status = $status["status"];
  230. $class = $status > 0 ? ($status == 2 ? 'active' : 'due') : 'suspended';
  231. $text = $status > 0 ? ($status == 2 ? 'Tesserato' : 'Sospeso') : 'Non tesserato';
  232. if ($r->to_complete)
  233. {
  234. $text = 'Da completare';
  235. $class = "complete";
  236. }
  237. $has_certificate = $r->hasCertificate();
  238. $y = '';
  239. if($has_certificate["date"] != '')
  240. {
  241. if($has_certificate["date"] < date("Y-m-d"))
  242. $y .= '0';
  243. if($has_certificate["date"] >= date("Y-m-d") && $has_certificate["date"] < date("Y-m-d", strtotime("+1 month")))
  244. $y .= '1';
  245. if($has_certificate["date"] >= date("Y-m-d", strtotime("+1 month")))
  246. $y .= '2';
  247. $y .= '|';
  248. $y .= $has_certificate["date"] != '' ? date("d/m/Y", strtotime($has_certificate["date"])) : '';
  249. }
  250. $datas[] = array(
  251. //'c' => $idx + 1,
  252. //'id' => "ID" . str_pad($r->id, 5, "0", STR_PAD_LEFT),
  253. 'last_name' => $r->last_name . "|" . $r->id,
  254. 'first_name' => $r->first_name . "|" . $r->id,
  255. 'phone' => $r->phone,
  256. 'age' => $r->getAge(),
  257. 'year' => date("Y", strtotime($r->birth_date)),
  258. 'status' => $class . "|" . $text,
  259. // 'state' => $x,
  260. 'certificate' => $y,
  261. 'action' => $r->id
  262. );
  263. }
  264. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count));
  265. });
  266. Route::get('/get_record_in', function(){
  267. $datas = [];
  268. $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'))
  269. ->leftJoin('members', 'records.member_id', '=', 'members.id')
  270. ->leftJoin('payment_methods', 'records.payment_method_id', '=', 'payment_methods.id')
  271. ->where('records.type', 'IN');
  272. if (isset($_GET["search"]["value"]))
  273. {
  274. $v = str_replace("'", "''", stripcslashes($_GET["search"]["value"]));
  275. $x = $x->where(function ($query) use ($v) {
  276. $query->where('first_name', 'like', '%' . $v . '%')
  277. ->orWhere('last_name', 'like', '%' . $v . '%');
  278. });
  279. //where('first_name', 'like', '%' . $_GET["search"]["value"] . '%');
  280. }
  281. //$x = $x->where(function ($query) use ($v) {
  282. // $datas = \App\Models\Record::where('type', 'IN')->with('member', 'payment_method');
  283. if ($_GET["filterCommercial"] == 1)
  284. {
  285. $x = $x->where('commercial', true );
  286. }
  287. if ($_GET["filterCommercial"] == 2)
  288. {
  289. $x = $x->where('commercial', false);
  290. }
  291. if ($_GET["filterMember"] > 0)
  292. {
  293. $x = $x->where('member_id', $_GET["filterMember"]);
  294. }
  295. if ($_GET["filterPaymentMethod"] != "null")
  296. {
  297. $payments = explode(",", $_GET["filterPaymentMethod"]);
  298. $x = $x->whereIn('payment_method_id', $payments);
  299. }
  300. if ($_GET["filterCausals"] != "null")
  301. {
  302. $causals = explode(",", $_GET["filterCausals"]);
  303. //$causals = \App\Models\RecordRow::where('causal_id', $_GET["filterCausals"])->pluck('record_id');
  304. $causals = \App\Models\RecordRow::whereIn('causal_id', $causals)->pluck('record_id');
  305. $x = $x->whereIn('records.id', $causals);
  306. }
  307. if ($_GET["filterFrom"] != '')
  308. {
  309. $x = $x->where('date', '>=', $_GET["filterFrom"]);
  310. }
  311. if ($_GET["filterTo"] != '')
  312. {
  313. $x = $x->where('date', '<=', $_GET["filterTo"]);
  314. }
  315. //});
  316. $start = 0;
  317. $limit = 100000;
  318. if (isset($_GET["start"]))
  319. {
  320. $start = $_GET["start"];
  321. $limit = $_GET["length"];
  322. }
  323. $excludeCausals = [];
  324. /*$borsellino = \App\Models\Causal::where('money', true)->first();
  325. if ($borsellino)
  326. $excludeCausals[] = $borsellino->id;*/
  327. // Aggiungo
  328. $excludes = \App\Models\Causal::where('no_records', true)->get();
  329. foreach($excludes as $e)
  330. {
  331. $excludeCausals[] = $e->id;
  332. }
  333. $exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
  334. // Pagamento money
  335. $moneys = \App\Models\PaymentMethod::where('money', true)->pluck('id')->toArray();
  336. // Causale money
  337. $moneysCausal = \App\Models\Causal::where('money', true)->pluck('id')->toArray();
  338. $total = 0;
  339. /*
  340. foreach($x->get() as $r)
  341. {
  342. if (!in_array($r->payment_method_id, $moneys))
  343. {
  344. foreach($r->rows as $rr)
  345. {
  346. 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))
  347. {
  348. $total += $rr->amount;
  349. if ($rr->vat_id > 0)
  350. $total += getVatValue($rr->amount, $rr->vat_id);
  351. }
  352. }
  353. }
  354. }
  355. */
  356. $count = $x->count();
  357. if (isset($_GET["order"]))
  358. {
  359. $column = '';
  360. if ($_GET["order"][0]["column"] == 0)
  361. $column = 'date';
  362. if ($_GET["order"][0]["column"] == 1)
  363. $column = 'records.amount';
  364. if ($_GET["order"][0]["column"] == 2)
  365. $column = 'last_name';
  366. if ($_GET["order"][0]["column"] == 3)
  367. $column = 'first_name';
  368. if ($_GET["order"][0]["column"] == 4)
  369. $column = 'commercial';
  370. if ($column != '')
  371. $x = $x->orderBy($column, $_GET["order"][0]["dir"])->orderBy('records.id', 'DESC');
  372. else
  373. $x = $x->orderBy('records.id', 'DESC');
  374. }
  375. else
  376. $x = $x->orderBy('records.date', 'DESC')->orderBy('records.id', 'DESC');
  377. $x = $x->offset($start)->limit($limit)->get();
  378. foreach($x as $idx => $r)
  379. {
  380. $causals = '';
  381. foreach($r->rows as $row)
  382. {
  383. $causals .= $row->causal->getTree() . "<br>";
  384. }
  385. $datas[] = array(
  386. //'id' => $r->id,
  387. 'date' => $r->date,
  388. 'total' => formatPrice($r->getTotal()),
  389. 'first_name' => $r->first_name,
  390. 'last_name' => $r->last_name,
  391. 'commercial' => $r->financial_movement ? 'Movimento finanziario' : ($r->commercial ? 'SI' : 'NO'),
  392. 'causals' => $causals,
  393. 'payment' => $r->payment_method->name,
  394. 'status' => $r->deleted ? 'Annullato' : '',
  395. 'action' => $r->id . "|" . formatPrice($total) . "|" . ($r->deleted ? 'x' : '')
  396. );
  397. }
  398. /*$datas[] = array(
  399. //'id' => $r->id,
  400. 'date' => '',
  401. 'total' => formatPrice($total),
  402. 'first_name' => '',
  403. 'last_name' => '',
  404. 'commercial' => '',
  405. 'causals' => '',
  406. 'payment' => '',
  407. 'status' => '',
  408. 'action' => ''
  409. );*/
  410. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count));
  411. });
  412. Route::get('/get_record_out', function(){
  413. $datas = [];
  414. $x = \App\Models\Record::where('type', 'OUT')->with('supplier', 'payment_method');
  415. if ($_GET["filterSupplier"] > 0)
  416. {
  417. $x = $x->where('supplier_id', $_GET["filterSupplier"]);
  418. }
  419. /*if ($_GET["filterPaymentMethod"] > 0)
  420. {
  421. $x = $x->where('payment_method_id', $_GET["filterPaymentMethod"]);
  422. }
  423. if ($_GET["filterCausals"] > 0)
  424. {
  425. $causals = \App\Models\RecordRow::where('causal_id', $_GET["filterCausals"])->pluck('record_id');
  426. $x = $x->whereIn('records.id', $causals);
  427. }*/
  428. if ($_GET["filterPaymentMethod"] != "null")
  429. {
  430. $payments = explode(",", $_GET["filterPaymentMethod"]);
  431. $x = $x->whereIn('payment_method_id', $payments);
  432. }
  433. if ($_GET["filterCausals"] != "null")
  434. {
  435. $causals = explode(",", $_GET["filterCausals"]);
  436. //$causals = \App\Models\RecordRow::where('causal_id', $_GET["filterCausals"])->pluck('record_id');
  437. $causals = \App\Models\RecordRow::whereIn('causal_id', $causals)->pluck('record_id');
  438. $x = $x->whereIn('records.id', $causals);
  439. }
  440. if ($_GET["filterFrom"] != '')
  441. {
  442. $x = $x->where('date', '>=', $_GET["filterFrom"]);
  443. }
  444. if ($_GET["filterTo"] != '')
  445. {
  446. $x = $x->where('date', '<=', $_GET["filterTo"]);
  447. }
  448. $total = 0;
  449. /*foreach($x->get() as $r)
  450. {
  451. foreach($r->rows as $rr)
  452. {
  453. $total += $rr->amount;
  454. if ($rr->vat_id > 0)
  455. $total += getVatValue($rr->amount, $rr->vat_id);
  456. }
  457. }*/
  458. $x = $x->get();
  459. foreach($x as $idx => $r)
  460. {
  461. $causals = '';
  462. foreach($r->rows as $row)
  463. {
  464. $causals .= $row->causal->getTree() . "<br>";
  465. }
  466. $datas[] = array(
  467. //'id' => $r->id,
  468. 'date' => $r->date,
  469. 'total' => formatPrice($r->getTotal()),
  470. 'supplier' => $r->supplier->name,
  471. 'causals' => $causals,
  472. 'payment' => $r->payment_method->name,
  473. 'action' => $r->id . "|" . formatPrice($total)
  474. );
  475. }
  476. /*
  477. $datas[] = array(
  478. //'id' => $r->id,
  479. 'date' => '',
  480. 'total' => formatPrice($total),
  481. 'supplier' => '',
  482. 'causals' => '',
  483. 'payment' => '',
  484. 'action' => ''
  485. );
  486. */
  487. return json_encode(array("data" => $datas));
  488. });
  489. Route::get('/get_course_list', function(){
  490. $member_course = \App\Models\MemberCourse::with('member');
  491. if (isset($_GET["search"]["value"]))
  492. {
  493. $v = str_replace("'", "''", stripcslashes($_GET["search"]["value"]));
  494. $member_ids = \App\Models\Member::where(function ($query) use ($v) {
  495. $query->where('first_name', 'like', '%' . $v . '%')
  496. ->orWhere('last_name', 'like', '%' . $v . '%');
  497. })->pluck('id');
  498. $member_course = $member_course->whereIn('member_id', $member_ids);
  499. }
  500. if ($_GET["filterCourse"] != "null")
  501. {
  502. $course_ids = [];
  503. $courses = explode(",", $_GET["filterCourse"]);
  504. foreach($courses as $c)
  505. {
  506. $all = \App\Models\Course::where('name', 'like', '%' . $c . "%")->get();
  507. foreach($all as $a)
  508. {
  509. $course_ids[] = $a->id;
  510. }
  511. }
  512. $member_course = $member_course->whereIn('course_id', $course_ids);
  513. }
  514. if ($_GET["filterLevel"] != "null")
  515. {
  516. $levels = explode(",", $_GET["filterLevel"]);
  517. $course_ids = \App\Models\Course::whereIn('course_level_id', $levels)->pluck('id');
  518. $member_course = $member_course->whereIn('course_id', $course_ids);
  519. }
  520. if ($_GET["filterFrequency"] != "null")
  521. {
  522. $frequencies = explode(",", $_GET["filterFrequency"]);
  523. $course_ids = \App\Models\Course::whereIn('course_type_id', $frequencies)->pluck('id');
  524. $member_course = $member_course->whereIn('course_id', $course_ids);
  525. }
  526. if ($_GET["filterType"] != "null")
  527. {
  528. $types = explode(",", $_GET["filterType"]);
  529. $course_ids = \App\Models\Course::whereIn('course_frequency_id', $types)->pluck('id');
  530. $member_course = $member_course->whereIn('course_id', $course_ids);
  531. }
  532. if ($_GET["filterDuration"] != "null")
  533. {
  534. $durations = explode(",", $_GET["filterDuration"]);
  535. $course_ids = \App\Models\Course::whereIn('course_duration_id', $durations)->pluck('id');
  536. $member_course = $member_course->whereIn('course_id', $course_ids);
  537. }
  538. $totals = [];
  539. $totalIsc = [];
  540. $member_course_totals = $member_course->get();
  541. foreach($member_course_totals as $x)
  542. {
  543. $price = 0;
  544. $price = $x->course->price;
  545. $subPrice = $x->course->subscription_price;
  546. $records = \App\Models\Record::where('member_course_id', $x->id)->where('deleted', 0)->get();
  547. $prices = [];
  548. foreach ($records as $record)
  549. {
  550. foreach ($record->rows as $row)
  551. {
  552. //if ($row->causal_id == $x->course->sub_causal_id || str_contains(strtolower($row->note), 'iscrizione'))
  553. if (str_contains(strtolower($row->note), 'iscrizione'))
  554. {
  555. $subPrice = $row->amount;
  556. }
  557. if ($row->causal_id == $x->course->causal_id && !str_contains(strtolower($row->note), 'iscrizione'))
  558. {
  559. $tot = sizeof(json_decode($row->when));
  560. foreach(json_decode($row->when) as $m)
  561. {
  562. $prices[$m->month] = $row->amount / $tot;
  563. }
  564. }
  565. }
  566. }
  567. for($i=1; $i<=12; $i++)
  568. {
  569. $cls = getColor($x->months, $i);
  570. if ($cls != 'grey')
  571. {
  572. if (!isset($totals[$i]))
  573. {
  574. $totals[$i]['green'] = 0;
  575. $totals[$i]['orange'] = 0;
  576. $totals[$i]['yellow'] = 0;
  577. }
  578. if ($cls == 'yellow')
  579. {
  580. $totals[$i][$cls] += 1;
  581. }
  582. else
  583. {
  584. $p = isset($prices[$i]) ? $prices[$i] : $price;
  585. $totals[$i][$cls] += $p;
  586. }
  587. }
  588. }
  589. $sub = $x->subscribed ? "Y" : "N";
  590. if (isset($totalIsc[$sub]))
  591. $totalIsc[$sub] += $subPrice;
  592. else
  593. $totalIsc[$sub] = $subPrice;
  594. }
  595. $js = '';
  596. $xx = 2;
  597. $str = '';
  598. $str .= "<a class=green><small>" . (isset($totalIsc["Y"]) ? formatPrice($totalIsc["Y"]) : 0) . "</small></a><br>";
  599. $str .= "<a class=orange><small>" . (isset($totalIsc["N"]) ? formatPrice($totalIsc["N"]) : 0) . "</small></a><br>";
  600. $str .= "<a class=yellow><small>0</small></a><br>";
  601. $js .= "2§" . $str . "_";
  602. $str = "";
  603. foreach($totals as $z => $t)
  604. {
  605. if ($z == 1) $xx = 7;
  606. if ($z == 2) $xx = 8;
  607. if ($z == 3) $xx = 9;
  608. if ($z == 4) $xx = 10;
  609. if ($z == 5) $xx = 11;
  610. if ($z == 6) $xx = 12;
  611. if ($z == 7) $xx = 13;
  612. if ($z == 8) $xx = 14;
  613. if ($z == 9) $xx = 3;
  614. if ($z == 10) $xx = 4;
  615. if ($z == 11) $xx = 5;
  616. if ($z == 12) $xx = 6;
  617. $str = '';
  618. foreach($t as $x => $c)
  619. {
  620. $y = $x == 'yellow' ? $c : formatPrice($c);
  621. $str .= "<a class=" . $x . "><small>" . $y . "</small></a><br>";
  622. }
  623. $js .= $xx . "§" . $str . "_";
  624. $xx += 1;
  625. }
  626. $count = $member_course->count();
  627. //$member_course = $member_course->where('course_id', 999999);
  628. $start = 0;
  629. $limit = 100000;
  630. if (isset($_GET["start"]))
  631. {
  632. $start = $_GET["start"];
  633. $limit = $_GET["length"];
  634. }
  635. $member_course = $member_course->offset($start)->limit($limit)->get();
  636. $datas = [];
  637. foreach($member_course as $x)
  638. {
  639. $price = 0;
  640. $price = $x->course->price;
  641. $subPrice = $x->course->subscription_price;
  642. $records = \App\Models\Record::where('member_course_id', $x->id)->where('deleted', 0)->get();
  643. $prices = [];
  644. foreach ($records as $record)
  645. {
  646. foreach ($record->rows as $row)
  647. {
  648. //if ($row->causal_id == $x->course->sub_causal_id || str_contains(strtolower($row->note), 'iscrizione'))
  649. if (str_contains(strtolower($row->note), 'iscrizione'))
  650. {
  651. $subPrice = $row->amount;
  652. }
  653. if ($row->causal_id == $x->course->causal_id && !str_contains(strtolower($row->note), 'iscrizione'))
  654. {
  655. $tot = sizeof(json_decode($row->when));
  656. foreach(json_decode($row->when) as $m)
  657. {
  658. $prices[$m->month] = $row->amount / $tot;
  659. }
  660. }
  661. }
  662. }
  663. for($i=1; $i<=12; $i++)
  664. {
  665. $cls = getColor($x->months, $i);
  666. if ($cls != 'grey')
  667. {
  668. if (!isset($totals[$i]))
  669. {
  670. $totals[$i]['green'] = 0;
  671. $totals[$i]['orange'] = 0;
  672. $totals[$i]['yellow'] = 0;
  673. }
  674. if ($cls == 'yellow')
  675. {
  676. $totals[$i][$cls] += 1;
  677. }
  678. else
  679. {
  680. $p = isset($prices[$i]) ? $prices[$i] : $price;
  681. $totals[$i][$cls] += $p;
  682. }
  683. }
  684. }
  685. $sub = $x->subscribed ? "Y" : "N";
  686. if (isset($totalIsc[$sub]))
  687. $totalIsc[$sub] += $subPrice;
  688. else
  689. $totalIsc[$sub] = $subPrice;
  690. $datas[] = array(
  691. "column_0" => $x->member->last_name,
  692. "column_1" => $x->member->first_name,
  693. "column_2" => $x->subscribed . "§" . formatPrice($subPrice),
  694. "column_3" => getColor($x->months, 9) . "§" . formatPrice(isset($prices[9]) ? $prices[9] : $price),
  695. "column_4" => getColor($x->months, 10) . "§" . formatPrice(isset($prices[10]) ? $prices[10] : $price),
  696. "column_5" => getColor($x->months, 11) . "§" . formatPrice(isset($prices[11]) ? $prices[11] : $price),
  697. "column_6" => getColor($x->months, 12) . "§" . formatPrice(isset($prices[12]) ? $prices[12] : $price),
  698. "column_7" => getColor($x->months, 1) . "§" . formatPrice(isset($prices[1]) ? $prices[1] : $price),
  699. "column_8" => getColor($x->months, 2) . "§" . formatPrice(isset($prices[2]) ? $prices[2] : $price),
  700. "column_9" => getColor($x->months, 3) . "§" . formatPrice(isset($prices[3]) ? $prices[3] : $price),
  701. "column_10" => getColor($x->months, 4) . "§" . formatPrice(isset($prices[4]) ? $prices[4] : $price),
  702. "column_11" => getColor($x->months, 5) . "§" . formatPrice(isset($prices[5]) ? $prices[5] : $price),
  703. "column_12" => getColor($x->months, 6) . "§" . formatPrice(isset($prices[6]) ? $prices[6] : $price),
  704. "column_13" => getColor($x->months, 7) . "§" . formatPrice(isset($prices[7]) ? $prices[7] : $price),
  705. "column_14" => getColor($x->months, 8) . "§" . formatPrice(isset($prices[8]) ? $prices[8] : $price),
  706. "column_15" => $x->course_id,
  707. "column_16" => $x->id,
  708. "column_17" => $x->member_id
  709. );
  710. }
  711. // Sort data
  712. /*
  713. if (isset($_GET["order"]))
  714. 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);
  715. else
  716. array_multisort(array_column($datas, 'column_0'), SORT_DESC, SORT_NATURAL|SORT_FLAG_CASE, $datas);
  717. //$x = $x->orderBy('records.date', 'DESC')->orderBy('records.id', 'DESC');
  718. */
  719. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count, "totals" => $js));
  720. });
  721. Route::get('/get_course_members', function(){
  722. //$datas = \App\Models\MemberCourse::with('member');
  723. $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');
  724. if ($_GET["filterCourse"] != "null")
  725. {
  726. $course_ids = [];
  727. $courses = explode(",", $_GET["filterCourse"]);
  728. foreach($courses as $c)
  729. {
  730. $all = \App\Models\Course::where('name', 'like', '%' . $c . "%")->get();
  731. foreach($all as $a)
  732. {
  733. $course_ids[] = $a->id;
  734. }
  735. }
  736. $datas = $datas->whereIn('course_id', $course_ids);
  737. }
  738. if ($_GET["filterLevel"] != "null")
  739. {
  740. $levels = explode(",", $_GET["filterLevel"]);
  741. $course_ids = \App\Models\Course::whereIn('course_level_id', $levels)->pluck('id');
  742. $datas = $datas->whereIn('course_id', $course_ids);
  743. }
  744. if ($_GET["filterFrequency"] != "null")
  745. {
  746. $frequencies = explode(",", $_GET["filterFrequency"]);
  747. $course_ids = \App\Models\Course::whereIn('course_type_id', $frequencies)->pluck('id');
  748. $datas = $datas->whereIn('course_id', $course_ids);
  749. }
  750. if ($_GET["filterType"] != "null")
  751. {
  752. $types = explode(",", $_GET["filterType"]);
  753. $course_ids = \App\Models\Course::whereIn('course_frequency_id', $types)->pluck('id');
  754. $datas = $datas->whereIn('course_id', $course_ids);
  755. }
  756. if ($_GET["filterDuration"] != "null")
  757. {
  758. $durations = explode(",", $_GET["filterDuration"]);
  759. $course_ids = \App\Models\Course::whereIn('course_duration_id', $durations)->pluck('id');
  760. $datas = $datas->whereIn('course_id', $course_ids);
  761. }
  762. if ($_GET["filterDays"] != "null")
  763. {
  764. $ids = [];
  765. $days = explode(",", $_GET["filterDays"]);
  766. foreach($days as $d)
  767. {
  768. $all = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")->get();
  769. foreach($all as $a)
  770. {
  771. $ids[] = $a->id;
  772. }
  773. }
  774. $datas = $datas->whereIn('id', $ids);
  775. }
  776. if ($_GET["filterHours"] != "null")
  777. {
  778. $ids = [];
  779. $hours = explode(",", $_GET["filterHours"]);
  780. foreach($hours as $h)
  781. {
  782. $all = \App\Models\MemberCourse::where('when', 'like', '%"from":"' . $h . "%")->get();
  783. foreach($all as $a)
  784. {
  785. $ids[] = $a->id;
  786. }
  787. }
  788. $datas = $datas->whereIn('member_courses.id', $ids);
  789. }
  790. if ($_GET["filterSubscription"] != "")
  791. {
  792. $ids = \App\Models\MemberCourse::where('subscribed', $_GET["filterSubscription"] == 1 ? true : false)->pluck('id');
  793. $datas = $datas->whereIn('member_courses.id', $ids);
  794. //$this->filter .= $this->filter != '' ? ', ' : '';
  795. //$this->filter .= "Pagata sottoscrizione : " . ($this->filterSubscription == 1 ? "SI" : "NO") . " ";
  796. }
  797. if ($_GET["filterCertificateType"] != "null")
  798. {
  799. $types = \App\Models\MemberCertificate::where('type', $_GET["filterCertificateType"])->pluck('member_id');
  800. $datas = $datas->whereIn('member_id', $types);
  801. }
  802. if ($_GET["filterCertificateScadenza"] != "null")
  803. {
  804. if ($_GET["filterCertificateScadenza"] == "1")
  805. $scad = \App\Models\MemberCertificate::where('expire_date', '<', date("Y-m-d"))->pluck('member_id');
  806. if ($_GET["filterCertificateScadenza"] == "2")
  807. $scad = \App\Models\MemberCertificate::whereBetween('expire_date', [date("Y-m-d"), date("Y-m-d", strtotime("+1 month"))])->pluck('member_id');
  808. $datas = $datas->whereIn('member_id', $scad);
  809. }
  810. if ($_GET["fromYear"] != "")
  811. {
  812. $m_ids = \App\Models\Member::where('birth_date', '<', date("Y-m-d", strtotime("-" . $_GET["fromYear"] . " year", time())))->pluck('id');
  813. $datas = $datas->whereIn('member_id', $m_ids);
  814. }
  815. if ($_GET["toYear"] != "")
  816. {
  817. $m_ids = \App\Models\Member::where('birth_date', '>', date("Y-m-d", strtotime("-" . $_GET["toYear"] . " year", time())))->pluck('id');
  818. $datas = $datas->whereIn('member_id', $m_ids);
  819. }
  820. if ($_GET["fromFromYear"] != "")
  821. {
  822. $m_ids = \App\Models\Member::whereYear('birth_date', '>=', $_GET["fromFromYear"])->pluck('id');
  823. $datas = $datas->whereIn('member_id', $m_ids);
  824. }
  825. if ($_GET["toToYear"] != "")
  826. {
  827. $m_ids = \App\Models\Member::whereYear('birth_date', '<=', $_GET["toToYear"])->pluck('id');
  828. $datas = $datas->whereIn('member_id', $m_ids);
  829. }
  830. if ($_GET["filterCards"] != "null")
  831. {
  832. $card_ids = \App\Models\MemberCard::whereIn('card_id', $_GET["filterCards"])->pluck('member_id');
  833. $datas = $datas->whereIn('member_id', $card_ids);
  834. }
  835. if ($_GET["filterYear"] != "")
  836. {
  837. $course_ids = \App\Models\Course::where('year', $_GET["filterYear"])->pluck('id');
  838. $datas = $datas->whereIn('course_id', $course_ids);
  839. //$this->filter .= $this->filter != '' ? ', ' : '';
  840. //$this->filter .= "Anno : " . $this->filterYear . " ";
  841. }
  842. $aRet = [];
  843. if (isset($_GET["order"]))
  844. {
  845. $column = '';
  846. if ($_GET["order"][0]["column"] == 1)
  847. $column = 'last_name';
  848. if ($_GET["order"][0]["column"] == 2)
  849. $column = 'first_name';
  850. if ($_GET["order"][0]["column"] == 2)
  851. $column = 'birth_date';
  852. if ($_GET["order"][0]["column"] == 3)
  853. $column = 'birth_date';
  854. if ($_GET["order"][0]["column"] == 4)
  855. $column = 'birth_date';
  856. if ($_GET["order"][0]["column"] == 5)
  857. $column = 'phone';
  858. if ($_GET["order"][0]["column"] == 6)
  859. $column = 'email';
  860. if ($column != '')
  861. $datas = $datas->orderBy($column, $_GET["order"][0]["dir"]);
  862. else
  863. $datas = $datas->orderBy('last_name', 'ASC')->orderBy('first_name', 'ASC');
  864. }
  865. else
  866. $datas = $datas->orderBy('last_name', 'ASC')->orderBy('first_name', 'ASC');
  867. if ($_GET["filterStatus"] != "null")
  868. {
  869. $status = explode(",", $_GET["filterStatus"]);
  870. foreach($status as $s)
  871. {
  872. foreach($datas->get() as $aaa)
  873. {
  874. $state = \App\Models\Member::findOrFail($aaa->member_id)->isActive();
  875. if ($state["status"] == $s)
  876. $aRet[] = $aaa;
  877. }
  878. }
  879. }
  880. else
  881. $aRet = $datas->get();
  882. $ret = [];
  883. foreach($aRet as $idx => $r)
  884. {
  885. $date1 = new DateTime($r->birth_date);
  886. $date2 = new DateTime("now");
  887. $interval = $date1->diff($date2);
  888. $ret[] = array(
  889. "column_0" => $idx,
  890. "column_1" => $r->last_name,
  891. "column_2" => $r->first_name,
  892. "column_3" => strval($interval->y),
  893. "column_4" => date("Y", strtotime($r->birth_date)),
  894. "column_5" => $r->phone,
  895. "column_6" => $r->email,
  896. "column_7" => $r->member_id
  897. );
  898. }
  899. if (isset($_GET["start"]))
  900. $ret = array_slice($ret, $_GET["start"], $_GET["length"]);
  901. return json_encode(array("data" => $ret, "recordsTotal" => sizeof($aRet), "recordsFiltered" => sizeof($aRet)));
  902. });
  903. Route::get('/get_receipts', function(){
  904. $x = \App\Models\Receipt::select('receipts.*', 'members.first_name', 'members.last_name')->leftJoin('members', 'receipts.member_id', '=', 'members.id');
  905. if (isset($_GET["search"]["value"]))
  906. {
  907. $v = str_replace("'", "''", stripcslashes($_GET["search"]["value"]));
  908. $member_ids = \App\Models\Member::where(function ($query) use ($v) {
  909. $query->where('first_name', 'like', '%' . $v . '%')
  910. ->orWhere('last_name', 'like', '%' . $v . '%');
  911. })->pluck('id');
  912. $x = $x->whereIn('member_id', $member_ids);
  913. }
  914. if ($_GET["filterStatus"] != '')
  915. $x = $x->where('receipts.status', $_GET["filterStatus"]);
  916. if ($_GET["filterFrom"] != "")
  917. $x = $x->where('date', '>=', $_GET["filterFrom"]);
  918. if ($_GET["filterTo"] != "")
  919. $x = $x->where('date', '<=', $_GET["filterTo"]);
  920. $count = $x->count();
  921. if (isset($_GET["order"]))
  922. {
  923. $column = '';
  924. if ($_GET["order"][0]["column"] == 0)
  925. $column = 'year';
  926. if ($_GET["order"][0]["column"] == 1)
  927. $column = 'number';
  928. if ($_GET["order"][0]["column"] == 2)
  929. $column = 'last_name';
  930. if ($_GET["order"][0]["column"] == 3)
  931. $column = 'first_name';
  932. if ($_GET["order"][0]["column"] == 4)
  933. $column = 'status';
  934. if ($_GET["order"][0]["column"] == 5)
  935. $column = 'date';
  936. if ($column != '')
  937. $x = $x->orderBy($column, $_GET["order"][0]["dir"])->orderBy('id', 'DESC');
  938. else
  939. $x = $x->orderBy('id', 'DESC');
  940. }
  941. else
  942. $x = $x->orderBy('id', 'DESC');
  943. if (isset($_GET["start"]))
  944. $x = $x->offset($_GET["start"])->limit($_GET["length"])->get();
  945. else
  946. $x = $x->get();
  947. $datas = [];
  948. foreach($x as $idx => $r)
  949. {
  950. $datas[] = array(
  951. 'year' => $r->year,
  952. 'number' => $r->number,
  953. 'last_name' => $r->member->last_name,
  954. 'first_name' => $r->member->first_name,
  955. 'status' => $r->status,
  956. 'date' => date("d/m/Y", strtotime($r->date)),
  957. 'totals' => formatPrice($r->rows->sum('amount')),
  958. 'action' => $r->id
  959. );
  960. }
  961. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count));
  962. });
  963. function getColor($months, $m)
  964. {
  965. $class = "grey";
  966. foreach(json_decode($months) as $mm)
  967. {
  968. if ($mm->m == $m)
  969. {
  970. if ($mm->status == "")
  971. {
  972. $class = "orange";
  973. }
  974. if ($mm->status == "1")
  975. {
  976. $class = "green";
  977. }
  978. if ($mm->status == "2")
  979. {
  980. $class = "yellow";
  981. }
  982. }
  983. }
  984. return $class;
  985. }
  986. Route::get('/migrate', function(){
  987. \Artisan::call('migrate');
  988. dd('migrated!');
  989. });