web.php 36 KB

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