web.php 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639
  1. <?php
  2. use Illuminate\Support\Facades\Route;
  3. use Barryvdh\DomPDF\Facade\Pdf;
  4. /*
  5. |--------------------------------------------------------------------------
  6. | Web Routes
  7. |--------------------------------------------------------------------------
  8. |
  9. | Here is where you can register web routes for your application. These
  10. | routes are loaded by the RouteServiceProvider within a group which
  11. | contains the "web" middleware group. Now create something great!
  12. |
  13. */
  14. Route::get('/', function () {
  15. return view('login');
  16. // return Redirect::to('/dashboard');
  17. })->name('login');
  18. Route::get('/login', function () {
  19. return Redirect::to('/');
  20. // return Redirect::to('/dashboard');
  21. });
  22. Route::post('/login', function () {
  23. if(\Auth::attempt(array('email' => $_POST["email"], 'password' => $_POST["password"])))
  24. {
  25. return Redirect::to('/dashboard');
  26. }
  27. else
  28. {
  29. return Redirect::to('/?error=1');
  30. }
  31. })->name('login');
  32. Route::get('/logout', function(){
  33. Auth::logout();
  34. return redirect('/');
  35. });
  36. Route::group(['middleware' => 'auth'],function(){
  37. //Route::get('/', \App\Http\Livewire\Login::class);
  38. Route::get('/dashboard', \App\Http\Livewire\Dashboard::class);
  39. Route::get('/settings', \App\Http\Livewire\Setting::class);
  40. Route::get('/courses', \App\Http\Livewire\Course::class);
  41. Route::get('/categories', \App\Http\Livewire\Category::class);
  42. Route::get('/nations_list', \App\Http\Livewire\Nation::class);
  43. Route::get('/provinces', \App\Http\Livewire\Province::class);
  44. Route::get('/cities', \App\Http\Livewire\City::class);
  45. Route::get('/banks', \App\Http\Livewire\Bank::class);
  46. Route::get('/vats', \App\Http\Livewire\Vat::class);
  47. Route::get('/disciplines', \App\Http\Livewire\Discipline::class);
  48. Route::get('/course_types', \App\Http\Livewire\CourseType::class);
  49. Route::get('/course_subscriptions', \App\Http\Livewire\CourseSubscription::class);
  50. Route::get('/course_durations', \App\Http\Livewire\CourseDuration::class);
  51. Route::get('/course_levels', \App\Http\Livewire\CourseLevel::class);
  52. Route::get('/course_frequencies', \App\Http\Livewire\CourseFrequency::class);
  53. Route::get('/course_list', \App\Http\Livewire\CourseList::class);
  54. Route::get('/course_member', \App\Http\Livewire\CourseMember::class);
  55. Route::get('/receipts', \App\Http\Livewire\Receipt::class);
  56. Route::get('/cards', \App\Http\Livewire\Card::class);
  57. Route::get('/causals', \App\Http\Livewire\Causal::class);
  58. Route::get('/payment_methods', \App\Http\Livewire\PaymentMethod::class);
  59. Route::get('/members', \App\Http\Livewire\Member::class);
  60. Route::get('/suppliers', \App\Http\Livewire\Supplier::class);
  61. Route::get('/sponsors', \App\Http\Livewire\Sponsor::class);
  62. Route::get('/records', \App\Http\Livewire\Record::class);
  63. Route::get('/reminders', \App\Http\Livewire\Reminder::class);
  64. Route::get('/in', \App\Http\Livewire\RecordIN::class);
  65. Route::get('/out', \App\Http\Livewire\RecordOUT::class);
  66. Route::get('/records_in_out', \App\Http\Livewire\RecordINOUT::class);
  67. Route::get('/users', \App\Http\Livewire\User::class);
  68. Route::get('/profile', \App\Http\Livewire\Profile::class);
  69. });
  70. Route::get('/receipt/{id}', function($id){
  71. $receipt = \App\Models\Receipt::findOrFail($id);
  72. $pdf = PDF::loadView('receipt', array('receipt' => $receipt));
  73. $pdfName = "Ricevuta_" . $receipt->member->last_name . "_" . $receipt->number . "_" . $receipt->year . ".pdf";
  74. return $pdf->stream($pdfName);
  75. /*return response()->streamDownload(
  76. fn () => print($pdf),
  77. "ricevuta_" . $receipt->number . "_" . $receipt->year . ".pdf"
  78. );*/
  79. });
  80. Route::get('/receipt/mail/{id}', function($id){
  81. $receipt = \App\Models\Receipt::findOrFail($id);
  82. if ($receipt->status == 99)
  83. sendReceiptDeleteEmail($receipt);
  84. else
  85. sendReceiptEmail($receipt);
  86. /*
  87. $pdf = PDF::loadView('receipt', array('receipt' => $receipt));
  88. $pdfName = "ricevuta_" . $receipt->number . "_" . $receipt->year . ".pdf";
  89. Storage::put('public/pdf/' . $pdfName, $pdf->output());
  90. $email = \App\Models\Member::findOrFail($receipt->member_id)->email;
  91. if ($email != '')
  92. {
  93. Mail::to($email)->send(new \App\Mail\ReceipEmail([
  94. 'name' => 'Luca',
  95. 'pdf' => 'public/pdf/' . $pdfName,
  96. 'number' => $receipt->number . "/" . $receipt->year
  97. ]));
  98. }
  99. */
  100. return true;
  101. //return $pdf->stream();
  102. /*return response()->streamDownload(
  103. fn () => print($pdf),
  104. "ricevuta_" . $receipt->number . "_" . $receipt->year . ".pdf"
  105. );*/
  106. });
  107. Route::get('/nations', function(){
  108. if (isset($_GET["q"]))
  109. $datas = \App\Models\Nation::where('name', 'like', $_GET["q"] . '%')->orderBy('name')->get();
  110. else
  111. $datas = \App\Models\Nation::orderBy('name')->get();
  112. $data = array();
  113. foreach($datas as $d)
  114. {
  115. $data[] = array("id" => $d->id, "text" => $d->name);
  116. }
  117. return array("results" => $data);
  118. });
  119. Route::get('/provinces/{nation_id}', function($nation_id){
  120. if (isset($_GET["q"]))
  121. $datas = \App\Models\Province::where('nation_id', $nation_id)->where('name', 'like', $_GET["q"] . '%')->orderBy('name')->get();
  122. else
  123. $datas = \App\Models\Province::where('nation_id', $nation_id)->orderBy('name')->get();
  124. $data = array();
  125. foreach($datas as $d)
  126. {
  127. $data[] = array("id" => $d->id, "text" => $d->name);
  128. }
  129. return array("results" => $data);
  130. });
  131. Route::get('/cities/{province_id}', function($province_id){
  132. if (isset($_GET["q"]))
  133. $datas = \App\Models\City::where('province_id', $province_id)->where('name', 'like', $_GET["q"] . '%')->orderBy('name')->get();
  134. else
  135. $datas = \App\Models\City::where('province_id', $province_id)->orderBy('name')->get();
  136. $data = array();
  137. foreach($datas as $d)
  138. {
  139. $data[] = array("id" => $d->id, "text" => $d->name);
  140. }
  141. return array("results" => $data);
  142. });
  143. Route::get('/get_members', function(){
  144. $datas = [];
  145. // $datas = \App\Models\Member::select('members.*')->where('id', '>', 0);
  146. $x = \App\Models\Member::select('id', 'first_name', 'last_name', 'phone', 'birth_date', 'to_complete', 'current_status', 'certificate', 'certificate_date')->where('id', '>', 0);
  147. if (isset($_GET["search"]["value"]))
  148. {
  149. $v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
  150. $x = $x->where(function ($query) use ($v) {
  151. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $v . "%'")
  152. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $v . "%'");
  153. });
  154. //where('first_name', 'like', '%' . $_GET["search"]["value"] . '%');
  155. }
  156. if ($_GET["cards"] != "")
  157. {
  158. $card_ids = \App\Models\MemberCard::whereIn('card_id', explode(",", $_GET["cards"]))->pluck('member_id');
  159. $x = $x->whereIn('id', $card_ids);
  160. }
  161. if ($_GET["filterCategories"] != "null")
  162. {
  163. $categories = [];
  164. $cats = explode(",", $_GET["filterCategories"]);
  165. // Per ogni causale, se ha dei figli allora aggiungo le causali figlio
  166. foreach($cats as $c)
  167. {
  168. $categories[] = $c;
  169. $childs = \App\Models\Category::where('parent_id', $c)->get();
  170. foreach($childs as $_cc)
  171. {
  172. $categories[] = $_cc->id;
  173. $childss = \App\Models\Category::where('parent_id', $_cc->id)->get();
  174. foreach($childss as $ccc)
  175. {
  176. $categories[] = $ccc->id;
  177. }
  178. }
  179. }
  180. $cc = array();
  181. foreach($categories as $c)
  182. {
  183. $m_ids = \App\Models\MemberCategory::where('category_id', $c)->pluck('member_id')->toArray();
  184. /*if (sizeof($cc) > 0)
  185. $cc = array_intersect($cc, $m_ids);
  186. else
  187. $cc = $m_ids;
  188. */
  189. $cc = array_merge($cc, $m_ids);
  190. }
  191. $x = $x->whereIn('id', $cc);
  192. //$cats_ids = \App\Models\MemberCategory::whereIn('category_id', explode(",", $_GET["filterCategories"]))->pluck('member_id');
  193. //$x = $x->whereIn('id', $cats_ids);
  194. }
  195. if ($_GET["fromYear"] != "")
  196. {
  197. $x = $x->where('birth_date', '<', date("Y-m-d", strtotime("-" . $_GET["fromYear"] . " year", time())));
  198. }
  199. if ($_GET["toYear"] != "")
  200. {
  201. $x = $x->where('birth_date', '>', date("Y-m-d", strtotime("-" . $_GET["toYear"] . " year", time())));
  202. }
  203. if ($_GET["fromYearYear"] != "")
  204. {
  205. $x = $x->whereYear('birth_date', '>=', $_GET["fromYearYear"]);
  206. }
  207. if ($_GET["toYearYear"] != "")
  208. {
  209. $x = $x->whereYear('birth_date', '<=', $_GET["toYearYear"]);
  210. }
  211. $ids = [];
  212. if ($_GET["filterCertificateType"] != "null")
  213. {
  214. $types = \App\Models\MemberCertificate::where('type', $_GET["filterCertificateType"])->where('expire_date', '>', date("Y-m-d"))->pluck('member_id');
  215. $x = $x->whereIn('id', $types->toArray());;
  216. //$ids = array_merge($ids, $types->toArray());
  217. }
  218. if ($_GET["filterScadenza"] != "null") {
  219. $scadenzaValues = explode(",", $_GET["filterScadenza"]);
  220. $allScadIds = [];
  221. foreach ($scadenzaValues as $filterValue) {
  222. if ($filterValue == "1")
  223. $scadIds = \App\Models\MemberCertificate::where('expire_date', '<', date("Y-m-d"))->pluck('member_id')->toArray();
  224. else if ($filterValue == "2")
  225. $scadIds = \App\Models\MemberCertificate::where('expire_date', '>=', date("Y-m-d"))
  226. ->where('expire_date', '<=', date("Y-m-d", strtotime("+1 month")))
  227. ->pluck('member_id')->toArray();
  228. else if ($filterValue == "3")
  229. $scadIds = \App\Models\Member::whereNotIn('id', \App\Models\MemberCertificate::pluck('member_id'))->pluck('id')->toArray();
  230. else if ($filterValue == "4")
  231. $scadIds = \App\Models\MemberCertificate::where('expire_date', '>', date("Y-m-d", strtotime("+1 month")))->pluck('member_id')->toArray();
  232. $allScadIds = array_merge($allScadIds, $scadIds);
  233. }
  234. $allScadIds = array_unique($allScadIds);
  235. $x = $x->whereIn('id', $allScadIds);
  236. }
  237. if ($_GET["filterStatus"] != "null")
  238. {
  239. $status = explode(",", $_GET["filterStatus"]);
  240. $members = \App\Models\Member::all();
  241. foreach($status as $s)
  242. {
  243. foreach($members as $m)
  244. {
  245. $state = $m->isActive();
  246. if ($state["status"] == $s)
  247. $ids[] = $m->id;
  248. }
  249. }
  250. }
  251. if (sizeof($ids) > 0)
  252. {
  253. $x = $x->whereIn('id', $ids);
  254. }
  255. else
  256. {
  257. if ($_GET["filterStatus"] != "null")
  258. $x = $x->whereIn('id', [-1]);
  259. }
  260. $count = $x->count();
  261. $x = $x->orderBy('to_complete', 'DESC');
  262. if (isset($_GET["order"]))
  263. {
  264. $column = '';
  265. if ($_GET["order"][0]["column"] == 0)
  266. $column = 'last_name';
  267. if ($_GET["order"][0]["column"] == 1)
  268. $column = 'first_name';
  269. if ($_GET["order"][0]["column"] == 2)
  270. $column = 'phone';
  271. if ($_GET["order"][0]["column"] == 3)
  272. $column = 'birth_date';
  273. if ($_GET["order"][0]["column"] == 4)
  274. $column = 'birth_date';
  275. if ($_GET["order"][0]["column"] == 5)
  276. $column = 'current_status';
  277. if ($_GET["order"][0]["column"] == 6)
  278. $column = 'certificate';
  279. if ($column != '')
  280. {
  281. $x = $x->orderBy($column, $_GET["order"][0]["dir"]);
  282. if ($column == 'certificate')
  283. $x = $x->orderBy('certificate_date', $_GET["order"][0]["dir"]);
  284. }
  285. else
  286. $x = $x->orderBy('last_name', 'ASC')->orderBy('first_name', 'ASC');
  287. }
  288. else
  289. $x = $x->orderBy('last_name', 'ASC')->orderBy('first_name', 'ASC');
  290. if (isset($_GET["start"]))
  291. $x = $x->offset($_GET["start"])->limit($_GET["length"])->get();
  292. else
  293. $x = $x->get();
  294. foreach($x as $idx => $r)
  295. {
  296. // $status = $r->getStatus();
  297. // $status = $status["status"];
  298. $status = $r->current_status;
  299. $class = $status > 0 ? ($status == 2 ? 'active' : 'due') : 'suspended';
  300. $text = $status > 0 ? ($status == 2 ? 'Tesserato' : 'Sospeso') : 'Non tesserato';
  301. if ($r->to_complete)
  302. {
  303. $text = 'Da completare';
  304. $class = "complete";
  305. }
  306. // $has_certificate = $r->hasCertificate();
  307. $y = '';
  308. if ($r->certificate_date != '') {
  309. $status = '';
  310. if ($r->certificate_date < date("Y-m-d")) {
  311. $status = "0";
  312. } else if ($r->certificate_date >= date("Y-m-d") && $r->certificate_date <= date("Y-m-d", strtotime("+1 month"))) {
  313. $status = "1";
  314. } else if ($r->certificate_date > date("Y-m-d", strtotime("+1 month"))) {
  315. $status = "2";
  316. }
  317. $y = $status . "|" . date("d/m/Y", strtotime($r->certificate_date));
  318. }
  319. /*
  320. if($has_certificate["date"] != '')
  321. {
  322. if($has_certificate["date"] < date("Y-m-d"))
  323. $y .= '0';
  324. if($has_certificate["date"] >= date("Y-m-d") && $has_certificate["date"] < date("Y-m-d", strtotime("+1 month")))
  325. $y .= '1';
  326. if($has_certificate["date"] >= date("Y-m-d", strtotime("+1 month")))
  327. $y .= '2';
  328. $y .= '|';
  329. $y .= $has_certificate["date"] != '' ? date("d/m/Y", strtotime($has_certificate["date"])) : '';
  330. }*/
  331. $datas[] = array(
  332. //'c' => $idx + 1,
  333. //'id' => "ID" . str_pad($r->id, 5, "0", STR_PAD_LEFT),
  334. 'last_name' => $r->last_name . "|" . $r->id,
  335. 'first_name' => $r->first_name . "|" . $r->id,
  336. 'phone' => $r->phone,
  337. 'age' => $r->getAge(),
  338. 'year' => date("Y", strtotime($r->birth_date)),
  339. 'status' => $class . "|" . $text,
  340. // 'state' => $x,
  341. 'certificate' => $y,
  342. 'action' => $r->id
  343. );
  344. }
  345. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count));
  346. });
  347. Route::get('/get_record_in', function(){
  348. $datas = [];
  349. $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('receipts.created_at as receipt_date')) // , \DB::raw('SUM(records.id) As total'))
  350. ->leftJoin('members', 'records.member_id', '=', 'members.id')
  351. ->leftJoin('payment_methods', 'records.payment_method_id', '=', 'payment_methods.id')
  352. ->leftJoin('receipts', 'records.id', '=', 'receipts.record_id')
  353. ->where('records.type', 'IN');
  354. //$y = \App\Models\Record::select('records_rows.amount', 'records.member_id', 'records.corrispettivo_fiscale', 'records.deleted', 'records.financial_movement', 'records_rows.causal_id', 'records.payment_method_id', \DB::raw('members.first_name as first_name'), \DB::raw('members.last_name as last_name')) // , \DB::raw('SUM(records.id) As total'))
  355. $y = \App\Models\Record::select('records_rows.amount', 'records.member_id', 'records.corrispettivo_fiscale', 'records.deleted', 'records.financial_movement', 'records_rows.causal_id', \DB::raw('members.first_name as first_name'), \DB::raw('members.last_name as last_name')) // , \DB::raw('SUM(records.id) As total'))
  356. ->leftJoin('members', 'records.member_id', '=', 'members.id')
  357. ->leftJoin('records_rows', 'records.id', '=', 'records_rows.record_id')
  358. //->leftJoin('receipts', 'records.id', '=', 'receipts.record_id')
  359. ->where('records.type', 'IN');
  360. $hasFilter = false;
  361. if (isset($_GET["search"]["value"]))
  362. {
  363. if ($_GET["search"]["value"] != '')
  364. {
  365. $hasFilter = true;
  366. $v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
  367. $x = $x->where(function ($query) use ($v) {
  368. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $v . "%'")
  369. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $v . "%'");
  370. });
  371. $y = $y->where(function ($query) use ($v) {
  372. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $v . "%'")
  373. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $v . "%'");
  374. });
  375. }
  376. /*$v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
  377. $x = $x->where(function ($query) use ($v) {
  378. $query->where('first_name', 'like', '%' . $v . '%')
  379. ->orWhere('last_name', 'like', '%' . $v . '%');
  380. });
  381. $y = $y->where(function ($query) use ($v) {
  382. $query->where('first_name', 'like', '%' . $v . '%')
  383. ->orWhere('last_name', 'like', '%' . $v . '%');
  384. });
  385. }*/
  386. //where('first_name', 'like', '%' . $_GET["search"]["value"] . '%');
  387. }
  388. //$x = $x->where(function ($query) use ($v) {
  389. // $datas = \App\Models\Record::where('type', 'IN')->with('member', 'payment_method');
  390. if ($_GET["filterCommercial"] == 1)
  391. {
  392. $hasFilter = true;
  393. $x = $x->where('records.commercial', true );
  394. $y = $y->where('records.commercial', true );
  395. }
  396. if ($_GET["filterCommercial"] == 2)
  397. {
  398. $hasFilter = true;
  399. $x = $x->where('records.commercial', false);
  400. $y = $y->where('records.commercial', false);
  401. }
  402. if ($_GET["filterMember"] > 0)
  403. {
  404. $hasFilter = true;
  405. $x = $x->where('records.member_id', $_GET["filterMember"]);
  406. $y = $y->where('records.member_id', $_GET["filterMember"]);
  407. }
  408. if ($_GET["filterPaymentMethod"] != "null")
  409. {
  410. $hasFilter = true;
  411. $payments = explode(",", $_GET["filterPaymentMethod"]);
  412. $x = $x->whereIn('records.payment_method_id', $payments);
  413. $y = $y->whereIn('records.payment_method_id', $payments);
  414. }
  415. if ($_GET["filterCausals"] != "null")
  416. {
  417. $hasFilter = true;
  418. $causals = explode(",", $_GET["filterCausals"]);
  419. // Per ogni causale, se ha dei figli allora aggiungo le causali figlio
  420. foreach($causals as $c)
  421. {
  422. $childs = \App\Models\Causal::where('parent_id', $c)->get();
  423. foreach($childs as $cc)
  424. {
  425. $causals[] = $cc->id;
  426. $childss = \App\Models\Causal::where('parent_id', $cc->id)->get();
  427. foreach($childss as $ccc)
  428. {
  429. $causals[] = $ccc->id;
  430. }
  431. }
  432. }
  433. //$causals = \App\Models\RecordRow::where('causal_id', $_GET["filterCausals"])->pluck('record_id');
  434. $causals = \App\Models\RecordRow::whereIn('causal_id', $causals)->pluck('record_id');
  435. $x = $x->whereIn('records.id', $causals);
  436. $y = $y->whereIn('records.id', $causals);
  437. }
  438. if ($_GET["filterFrom"] != '')
  439. {
  440. $hasFilter = true;
  441. $x = $x->where('records.date', '>=', $_GET["filterFrom"]);
  442. $y = $y->where('date', '>=', $_GET["filterFrom"]);
  443. }
  444. if ($_GET["filterTo"] != '')
  445. {
  446. $hasFilter = true;
  447. $x = $x->where('records.date', '<=', $_GET["filterTo"]);
  448. $y = $y->where('date', '<=', $_GET["filterTo"]);
  449. }
  450. //});
  451. $start = 0;
  452. $limit = 100000;
  453. if (isset($_GET["start"]))
  454. {
  455. $start = $_GET["start"];
  456. $limit = $_GET["length"];
  457. }
  458. $excludeCausals = [];
  459. /*$borsellino = \App\Models\Causal::where('money', true)->first();
  460. if ($borsellino)
  461. $excludeCausals[] = $borsellino->id;*/
  462. // Aggiungo
  463. /*
  464. $excludes = \App\Models\Causal::where('no_records', true)->get();
  465. foreach($excludes as $e)
  466. {
  467. $excludeCausals[] = $e->id;
  468. }*/
  469. $exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
  470. // Pagamento money
  471. $moneys = \App\Models\PaymentMethod::where('money', true)->pluck('id')->toArray();
  472. // Causale money
  473. $moneysCausal = \App\Models\Causal::where('money', true)->pluck('id')->toArray();
  474. $total = 0;
  475. $causals = [];
  476. if ($_GET["filterCausals"] != "null")
  477. $causals = explode(",", $_GET["filterCausals"]);
  478. foreach($y->get() as $r)
  479. {
  480. if (!in_array($r->payment_method_id, $moneys))
  481. {
  482. if ((!in_array($r->member_id, $exclude_from_records) || in_array($r->causal_id, $moneysCausal)) && (!$r->deleted || $r->deleted == null) && (!in_array($r->causal_id, $excludeCausals) || in_array($r->causal_id, $moneysCausal)) && (!$r->financial_movement || $r->financial_movement == null) && (!$r->corrispettivo_fiscale || $r->corrispettivo_fiscale == null))
  483. {
  484. if (sizeof($causals) == 0 || in_array($r->causal_id, $causals))
  485. {
  486. $total += $r->amount;
  487. if ($r->vat_id > 0)
  488. $total += getVatValue($r->amount, $r->vat_id);
  489. }
  490. }
  491. }
  492. }
  493. $count = $x->count();
  494. if (isset($_GET["order"]))
  495. {
  496. $column = '';
  497. if ($_GET["order"][0]["column"] == 0)
  498. $column = 'date';
  499. if ($_GET["order"][0]["column"] == 1)
  500. $column = 'records.amount';
  501. if ($_GET["order"][0]["column"] == 2)
  502. $column = 'last_name';
  503. if ($_GET["order"][0]["column"] == 3)
  504. $column = 'first_name';
  505. if ($_GET["order"][0]["column"] == 4)
  506. $column = 'commercial';
  507. if ($column != '')
  508. $x = $x->orderBy($column, $_GET["order"][0]["dir"])->orderBy('records.id', 'DESC');
  509. else
  510. $x = $x->orderBy('records.id', 'DESC');
  511. }
  512. else
  513. $x = $x->orderBy('records.date', 'DESC')->orderBy('records.id', 'DESC');
  514. $x = $x->offset($start)->limit($limit)->get();
  515. foreach($x as $idx => $r)
  516. {
  517. $causals = '';
  518. foreach($r->rows as $row)
  519. {
  520. $causals .= $row->causal->getTree() . "<br>";
  521. }
  522. $datas[] = array(
  523. //'id' => $r->id,
  524. 'date' => $r->date,
  525. //'date' => $r->receipt_date != null ? $r->receipt_date : "",
  526. 'total' => formatPrice($r->getTotal()),
  527. 'first_name' => $r->first_name,
  528. 'last_name' => $r->last_name,
  529. 'commercial' => $r->financial_movement ? 'Movimento finanziario' : ($r->commercial ? 'SI' : 'NO'),
  530. 'causals' => $causals,
  531. 'payment' => $r->payment_method->name,
  532. //'payment_date' => date("d/m/Y", strtotime($r->date)),
  533. 'status' => $r->deleted ? 'Annullato' : '',
  534. 'action' => $r->id . "||" . ($r->deleted ? 'x' : '')
  535. );
  536. }
  537. /*$datas[] = array(
  538. //'id' => $r->id,
  539. 'date' => '',
  540. 'total' => formatPrice($total),
  541. 'first_name' => '',
  542. 'last_name' => '',
  543. 'commercial' => '',
  544. 'causals' => '',
  545. 'payment' => '',
  546. 'status' => '',
  547. 'action' => ''
  548. );*/
  549. if ($hasFilter)
  550. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count, "totals" => formatPrice($total)));
  551. else
  552. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count));
  553. });
  554. Route::get('/get_record_out', function(){
  555. $datas = [];
  556. $hasFilter = false;
  557. $x = \App\Models\Record::where('type', 'OUT')->with('supplier', 'payment_method');
  558. if ($_GET["filterSupplier"] > 0)
  559. {
  560. $x = $x->where('supplier_id', $_GET["filterSupplier"]);
  561. $hasFilter = true;
  562. }
  563. /*if ($_GET["filterPaymentMethod"] > 0)
  564. {
  565. $x = $x->where('payment_method_id', $_GET["filterPaymentMethod"]);
  566. }
  567. if ($_GET["filterCausals"] > 0)
  568. {
  569. $causals = \App\Models\RecordRow::where('causal_id', $_GET["filterCausals"])->pluck('record_id');
  570. $x = $x->whereIn('records.id', $causals);
  571. }*/
  572. if ($_GET["filterPaymentMethod"] != "null")
  573. {
  574. $payments = explode(",", $_GET["filterPaymentMethod"]);
  575. $x = $x->whereIn('payment_method_id', $payments);
  576. $hasFilter = true;
  577. }
  578. if ($_GET["filterCausals"] != "null")
  579. {
  580. $causals = explode(",", $_GET["filterCausals"]);
  581. // Per ogni causale, se ha dei figli allora aggiungo le causali figlio
  582. foreach($causals as $c)
  583. {
  584. $childs = \App\Models\Causal::where('parent_id', $c)->get();
  585. foreach($childs as $cc)
  586. {
  587. $causals[] = $cc->id;
  588. $childss = \App\Models\Causal::where('parent_id', $cc->id)->get();
  589. foreach($childss as $ccc)
  590. {
  591. $causals[] = $ccc->id;
  592. }
  593. }
  594. }
  595. //$causals = \App\Models\RecordRow::where('causal_id', $_GET["filterCausals"])->pluck('record_id');
  596. $causals = \App\Models\RecordRow::whereIn('causal_id', $causals)->pluck('record_id');
  597. $x = $x->whereIn('records.id', $causals);
  598. $hasFilter = true;
  599. }
  600. if ($_GET["filterFrom"] != '')
  601. {
  602. $x = $x->where('date', '>=', $_GET["filterFrom"]);
  603. $hasFilter = true;
  604. }
  605. if ($_GET["filterTo"] != '')
  606. {
  607. $x = $x->where('date', '<=', $_GET["filterTo"]);
  608. $hasFilter = true;
  609. }
  610. $total = 0;
  611. foreach($x->get() as $r)
  612. {
  613. foreach($r->rows as $rr)
  614. {
  615. $total += $rr->amount;
  616. if ($rr->vat_id > 0)
  617. $total += getVatValue($rr->amount, $rr->vat_id);
  618. }
  619. }
  620. $x = $x->get();
  621. foreach($x as $idx => $r)
  622. {
  623. $causals = '';
  624. foreach($r->rows as $row)
  625. {
  626. $causals .= $row->causal->getTree() . "<br>";
  627. }
  628. $datas[] = array(
  629. //'id' => $r->id,
  630. 'date' => $r->date,
  631. 'total' => formatPrice($r->getTotal()),
  632. 'supplier' => $r->supplier->name,
  633. 'causals' => $causals,
  634. 'payment' => $r->payment_method->name,
  635. 'action' => $r->id . "|"
  636. );
  637. }
  638. /*
  639. $datas[] = array(
  640. //'id' => $r->id,
  641. 'date' => '',
  642. 'total' => formatPrice($total),
  643. 'supplier' => '',
  644. 'causals' => '',
  645. 'payment' => '',
  646. 'action' => ''
  647. );
  648. */
  649. if ($hasFilter)
  650. return json_encode(array("data" => $datas, "totals" => formatPrice($total)));
  651. else
  652. return json_encode(array("data" => $datas));
  653. });
  654. Route::get('/get_course_list', function(){
  655. $member_course = \App\Models\MemberCourse::with('member')->with('course');
  656. if (isset($_GET["search"]["value"]))
  657. {
  658. if ($_GET["search"]["value"] != '')
  659. {
  660. $v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
  661. $member_ids = \App\Models\Member::where(function ($query) use ($v) {
  662. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $v . "%'")
  663. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $v . "%'");
  664. })->pluck('id');
  665. /*
  666. $v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
  667. $member_ids = \App\Models\Member::where(function ($query) use ($v) {
  668. $query->where('first_name', 'like', '%' . $v . '%')
  669. ->orWhere('last_name', 'like', '%' . $v . '%');
  670. })->pluck('id');*/
  671. $member_course = $member_course->whereIn('member_id', $member_ids);
  672. }
  673. }
  674. if ($_GET["filterCourse"] != "null")
  675. {
  676. $course_ids = [];
  677. $courses = explode(",", $_GET["filterCourse"]);
  678. foreach($courses as $c)
  679. {
  680. $all = \App\Models\Course::where('name', 'like', '%' . $c . "%")->get();
  681. foreach($all as $a)
  682. {
  683. $course_ids[] = $a->id;
  684. }
  685. }
  686. $member_course = $member_course->whereIn('course_id', $course_ids);
  687. }
  688. if ($_GET["filterYear"] != "")
  689. {
  690. $course_ids = \App\Models\Course::where('year', $_GET["filterYear"])->pluck('id');
  691. $member_course = $member_course->whereIn('course_id', $course_ids);
  692. }
  693. if ($_GET["filterLevel"] != "null")
  694. {
  695. $levels = explode(",", $_GET["filterLevel"]);
  696. $course_ids = \App\Models\Course::whereIn('course_level_id', $levels)->pluck('id');
  697. $member_course = $member_course->whereIn('course_id', $course_ids);
  698. }
  699. if ($_GET["filterFrequency"] != "null")
  700. {
  701. $frequencies = explode(",", $_GET["filterFrequency"]);
  702. $course_ids = \App\Models\Course::whereIn('course_frequency_id', $frequencies)->pluck('id');
  703. $member_course = $member_course->whereIn('course_id', $course_ids);
  704. }
  705. if ($_GET["filterType"] != "null")
  706. {
  707. $types = explode(",", $_GET["filterType"]);
  708. $course_ids = \App\Models\Course::whereIn('course_type_id', $types)->pluck('id');
  709. $member_course = $member_course->whereIn('course_id', $course_ids);
  710. }
  711. if ($_GET["filterDuration"] != "null")
  712. {
  713. $durations = explode(",", $_GET["filterDuration"]);
  714. $course_ids = \App\Models\Course::whereIn('course_duration_id', $durations)->pluck('id');
  715. $member_course = $member_course->whereIn('course_id', $course_ids);
  716. }
  717. $totals = [];
  718. $totalIsc = [];
  719. $datas = [];
  720. $xxx = 1;
  721. /*
  722. $sortColumn = '';
  723. if (isset($_GET["order"]))
  724. {
  725. }
  726. */
  727. $column = '';
  728. $sort_value = 0;
  729. if (isset($_GET["order"]))
  730. {
  731. $f = $_GET["order"][0]["column"];
  732. $d = $_GET["order"][0]["dir"];
  733. if ($f >= 5 && $f <= 16)
  734. {
  735. $column = 'column_' . ($f - 2);
  736. if (session()->get('sort_column'))
  737. {
  738. if (session()->get('sort_column') != $f)
  739. {
  740. session()->put('sort_column', $f);
  741. session()->put('sort_order', $d);
  742. session()->put('sort_value', 0);
  743. $sort_value = 0;
  744. }
  745. else
  746. {
  747. if (session()->get('sort_order') == $d)
  748. {
  749. //session()->put('sort_value', 0);
  750. $sort_value = session()->get('sort_value', 0);
  751. }
  752. else
  753. {
  754. if (session()->get('sort_value', 0) == 0)
  755. {
  756. $sort_value = 1;
  757. }
  758. if (session()->get('sort_value', 0) == 1)
  759. {
  760. $sort_value = 2;
  761. }
  762. if (session()->get('sort_value', 0) == 2)
  763. {
  764. $sort_value = 3;
  765. }
  766. if (session()->get('sort_value', 0) == 3)
  767. {
  768. $sort_value = 0;
  769. }
  770. session()->put('sort_value', $sort_value);
  771. }
  772. session()->put('sort_order', $d);
  773. }
  774. }
  775. else
  776. {
  777. session()->put('sort_column', $f);
  778. session()->put('sort_order', $d);
  779. session()->put('sort_value', 0);
  780. $sort_value = 0;
  781. }
  782. }
  783. }
  784. //print $sort_value;
  785. $member_course_totals = $member_course->get();
  786. foreach($member_course_totals as $x)
  787. {
  788. $price = 0;
  789. $price = $x->price; // $x->course->price;
  790. $subPrice = $x->subscription_price; // $x->course->subscription_price;
  791. $records = \App\Models\Record::where('member_course_id', $x->id)->where('deleted', 0)->get();
  792. $prices = [];
  793. foreach ($records as $record)
  794. {
  795. foreach ($record->rows as $row)
  796. {
  797. if ($row->causal_id == $x->course->sub_causal_id) // || str_contains(strtolower($row->note), 'iscrizione'))
  798. //if (str_contains(strtolower($row->note), 'iscrizione'))
  799. {
  800. $subPrice = $row->amount;
  801. }
  802. if ($row->causal_id == $x->course->causal_id && !str_contains(strtolower($row->note), 'iscrizione'))
  803. {
  804. $tot = sizeof(json_decode($row->when));
  805. foreach(json_decode($row->when) as $m)
  806. {
  807. $prices[$m->month] = $row->amount / $tot;
  808. }
  809. }
  810. }
  811. }
  812. for($i=1; $i<=12; $i++)
  813. {
  814. $cls = getColor($x->months, $i);
  815. if ($cls != 'wgrey')
  816. {
  817. if (!isset($totals[$i]))
  818. {
  819. $totals[$i]['green'] = 0;
  820. $totals[$i]['orange'] = 0;
  821. $totals[$i]['yellow'] = 0;
  822. }
  823. if ($cls == 'yellow')
  824. {
  825. $totals[$i][$cls] += 1;
  826. }
  827. else
  828. {
  829. $p = isset($prices[$i]) ? $prices[$i] : $price;
  830. $totals[$i][$cls] += $p;
  831. }
  832. }
  833. }
  834. $sub = $x->subscribed ? "Y" : "N";
  835. if (isset($totalIsc[$sub]))
  836. $totalIsc[$sub] += $subPrice;
  837. else
  838. $totalIsc[$sub] = $subPrice;
  839. $s = 0;
  840. if ($column != '')
  841. {
  842. $z = 0;
  843. switch ($column) {
  844. case 'column_3':
  845. $z = 9;
  846. break;
  847. case 'column_4':
  848. $z = 10;
  849. break;
  850. case 'column_5':
  851. $z = 11;
  852. break;
  853. case 'column_6':
  854. $z = 12;
  855. break;
  856. case 'column_7':
  857. $z = 1;
  858. break;
  859. case 'column_8':
  860. $z = 2;
  861. break;
  862. case 'column_9':
  863. $z = 3;
  864. break;
  865. case 'column_10':
  866. $z = 4;
  867. break;
  868. case 'column_11':
  869. $z = 5;
  870. break;
  871. case 'column_12':
  872. $z = 6;
  873. break;
  874. case 'column_13':
  875. $z = 7;
  876. break;
  877. case 'column_14':
  878. $z = 8;
  879. break;
  880. default:
  881. $z = 0;
  882. break;
  883. }
  884. $c = getColor($x->months, $z);
  885. if ($sort_value == 0)
  886. {
  887. switch ($c) {
  888. case 'wgrey':
  889. $s = 0;
  890. break;
  891. case 'orange':
  892. $s = 1;
  893. break;
  894. case 'green':
  895. $s = 2;
  896. break;
  897. case 'yellow':
  898. $s = 3;
  899. break;
  900. default:
  901. $s = 0;
  902. break;
  903. }
  904. }
  905. if ($sort_value == 1)
  906. {
  907. switch ($c) {
  908. case 'wgrey':
  909. $s = 3;
  910. break;
  911. case 'orange':
  912. $s = 0;
  913. break;
  914. case 'green':
  915. $s = 1;
  916. break;
  917. case 'yellow':
  918. $s = 2;
  919. break;
  920. default:
  921. $s = 0;
  922. break;
  923. }
  924. }
  925. if ($sort_value == 2)
  926. {
  927. switch ($c) {
  928. case 'wgrey':
  929. $s = 2;
  930. break;
  931. case 'orange':
  932. $s = 3;
  933. break;
  934. case 'green':
  935. $s = 0;
  936. break;
  937. case 'yellow':
  938. $s = 1;
  939. break;
  940. default:
  941. $s = 0;
  942. break;
  943. }
  944. }
  945. if ($sort_value == 3)
  946. {
  947. switch ($c) {
  948. case 'wgrey':
  949. $s = 1;
  950. break;
  951. case 'orange':
  952. $s = 2;
  953. break;
  954. case 'green':
  955. $s = 3;
  956. break;
  957. case 'yellow':
  958. $s = 0;
  959. break;
  960. default:
  961. $s = 0;
  962. break;
  963. }
  964. }
  965. }
  966. $datas[] = array(
  967. "column_19" => $x->course->name,
  968. "column_0" => $x->member->last_name,
  969. "column_1" => $x->member->first_name,
  970. "column_2" => $x->subscribed . "§" . formatPrice($subPrice),
  971. "column_3" => getColor($x->months, 9) . "§" . formatPrice(isset($prices[9]) ? $prices[9] : $price),
  972. "column_4" => getColor($x->months, 10) . "§" . formatPrice(isset($prices[10]) ? $prices[10] : $price),
  973. "column_5" => getColor($x->months, 11) . "§" . formatPrice(isset($prices[11]) ? $prices[11] : $price),
  974. "column_6" => getColor($x->months, 12) . "§" . formatPrice(isset($prices[12]) ? $prices[12] : $price),
  975. "column_7" => getColor($x->months, 1) . "§" . formatPrice(isset($prices[1]) ? $prices[1] : $price),
  976. "column_8" => getColor($x->months, 2) . "§" . formatPrice(isset($prices[2]) ? $prices[2] : $price),
  977. "column_9" => getColor($x->months, 3) . "§" . formatPrice(isset($prices[3]) ? $prices[3] : $price),
  978. "column_10" => getColor($x->months, 4) . "§" . formatPrice(isset($prices[4]) ? $prices[4] : $price),
  979. "column_11" => getColor($x->months, 5) . "§" . formatPrice(isset($prices[5]) ? $prices[5] : $price),
  980. "column_12" => getColor($x->months, 6) . "§" . formatPrice(isset($prices[6]) ? $prices[6] : $price),
  981. "column_13" => getColor($x->months, 7) . "§" . formatPrice(isset($prices[7]) ? $prices[7] : $price),
  982. "column_14" => getColor($x->months, 8) . "§" . formatPrice(isset($prices[8]) ? $prices[8] : $price),
  983. "column_15" => $x->course_id,
  984. "column_16" => $x->id,
  985. "column_17" => $x->member_id,
  986. "column_18" => $xxx++,
  987. "column_20" => $s
  988. );
  989. }
  990. $count = $member_course->count();
  991. $js = '';
  992. $xx = 4;
  993. $str = '';
  994. if ($count > 0)
  995. {
  996. $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>";
  997. $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>";
  998. $str .= "<a style='width:100%;float:right; text-align:right; display:block;' class=yellow><small>0</small></a><br>";
  999. }
  1000. $js .= $xx . "§" . $str . "_";
  1001. $str = "";
  1002. foreach($totals as $z => $t)
  1003. {
  1004. if ($z == 1) $xx = 9;
  1005. if ($z == 2) $xx = 10;
  1006. if ($z == 3) $xx = 11;
  1007. if ($z == 4) $xx = 12;
  1008. if ($z == 5) $xx = 13;
  1009. if ($z == 6) $xx = 14;
  1010. if ($z == 7) $xx = 15;
  1011. if ($z == 8) $xx = 16;
  1012. if ($z == 9) $xx = 5;
  1013. if ($z == 10) $xx = 6;
  1014. if ($z == 11) $xx = 7;
  1015. if ($z == 12) $xx = 8;
  1016. $str = '';
  1017. foreach($t as $x => $c)
  1018. {
  1019. $y = $x == 'yellow' ? $c : formatPrice($c);
  1020. $str .= "<a style='width:100%;float:right; text-align:right; display:block;' class=" . $x . "><small>" . $y . "</small></a><br>";
  1021. }
  1022. $js .= $xx . "§" . $str . "_";
  1023. $xx += 1;
  1024. }
  1025. if (isset($_GET["order"]))
  1026. {
  1027. $s = $_GET["order"][0]["column"];
  1028. if ($s == 1) $s = 21;
  1029. if ($column != '')
  1030. array_multisort(array_column($datas, 'column_20'), SORT_ASC, SORT_NATURAL|SORT_FLAG_CASE, $datas);
  1031. else
  1032. array_multisort(array_column($datas, 'column_' . ($s - 2)), $_GET["order"][0]["dir"] == "asc" ? SORT_ASC : SORT_DESC, SORT_NATURAL|SORT_FLAG_CASE, $datas);
  1033. }
  1034. $xxx = 1;
  1035. foreach($datas as $yyy => $d)
  1036. {
  1037. $datas[$yyy]["column_18"] = $xxx++;
  1038. }
  1039. if (isset($_GET["start"]))
  1040. $datas = array_slice($datas, $_GET["start"], $_GET["length"]);
  1041. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count, "totals" => $js));
  1042. });
  1043. Route::get('/get_course_members', function(){
  1044. //$datas = \App\Models\MemberCourse::with('member');
  1045. $datas = \App\Models\MemberCourse::select('member_courses.*', 'courses.name as course_name', 'members.first_name', 'members.last_name', 'members.email', 'members.phone', 'members.birth_date')
  1046. ->leftJoin('courses', 'member_courses.course_id', '=', 'courses.id')
  1047. ->leftJoin('members', 'member_courses.member_id', '=', 'members.id');
  1048. if (isset($_GET["search"]["value"]))
  1049. {
  1050. $v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
  1051. $member_ids = \App\Models\Member::where(function ($query) use ($v) {
  1052. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $v . "%'")
  1053. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $v . "%'");
  1054. })->pluck('id');
  1055. /*$member_ids = \App\Models\Member::where(function ($query) use ($v) {
  1056. $query->where('first_name', 'like', '%' . $v . '%')
  1057. ->orWhere('last_name', 'like', '%' . $v . '%');
  1058. })->pluck('id');*/
  1059. $datas = $datas->whereIn('member_id', $member_ids);
  1060. }
  1061. if ($_GET["filterCourse"] != "null")
  1062. {
  1063. $course_ids = [];
  1064. $courses = explode(",", $_GET["filterCourse"]);
  1065. foreach($courses as $c)
  1066. {
  1067. $all = \App\Models\Course::where('name', 'like', '%' . $c . "%")->get();
  1068. foreach($all as $a)
  1069. {
  1070. $course_ids[] = $a->id;
  1071. }
  1072. }
  1073. $datas = $datas->whereIn('course_id', $course_ids);
  1074. }
  1075. if ($_GET["filterLevel"] != "null")
  1076. {
  1077. $levels = explode(",", $_GET["filterLevel"]);
  1078. $course_ids = \App\Models\Course::whereIn('course_level_id', $levels)->pluck('id');
  1079. $datas = $datas->whereIn('course_id', $course_ids);
  1080. }
  1081. if ($_GET["filterFrequency"] != "null")
  1082. {
  1083. $frequencies = explode(",", $_GET["filterFrequency"]);
  1084. $course_ids = \App\Models\Course::whereIn('course_frequency_id', $frequencies)->pluck('id');
  1085. $datas = $datas->whereIn('course_id', $course_ids);
  1086. }
  1087. if ($_GET["filterType"] != "null")
  1088. {
  1089. $types = explode(",", $_GET["filterType"]);
  1090. $course_ids = \App\Models\Course::whereIn('course_type_id', $types)->pluck('id');
  1091. $datas = $datas->whereIn('course_id', $course_ids);
  1092. }
  1093. if ($_GET["filterDuration"] != "null")
  1094. {
  1095. $durations = explode(",", $_GET["filterDuration"]);
  1096. $course_ids = \App\Models\Course::whereIn('course_duration_id', $durations)->pluck('id');
  1097. $datas = $datas->whereIn('course_id', $course_ids);
  1098. }
  1099. if ($_GET["filterDays"] != "null")
  1100. {
  1101. $ids = [];
  1102. $days = explode(",", $_GET["filterDays"]);
  1103. foreach($days as $d)
  1104. {
  1105. $all = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")->get();
  1106. foreach($all as $a)
  1107. {
  1108. $ids[] = $a->id;
  1109. }
  1110. }
  1111. $datas = $datas->whereIn('member_courses.id', $ids);
  1112. }
  1113. if ($_GET["filterHours"] != "null")
  1114. {
  1115. $ids = [];
  1116. $hours = explode(",", $_GET["filterHours"]);
  1117. foreach($hours as $h)
  1118. {
  1119. $all = \App\Models\MemberCourse::where('when', 'like', '%"from":"' . $h . "%")->get();
  1120. foreach($all as $a)
  1121. {
  1122. $ids[] = $a->id;
  1123. }
  1124. }
  1125. $datas = $datas->whereIn('member_courses.id', $ids);
  1126. }
  1127. if ($_GET["filterSubscription"] != "")
  1128. {
  1129. $ids = \App\Models\MemberCourse::where('subscribed', $_GET["filterSubscription"] == 1 ? true : false)->pluck('id');
  1130. $datas = $datas->whereIn('member_courses.id', $ids);
  1131. //$this->filter .= $this->filter != '' ? ', ' : '';
  1132. //$this->filter .= "Pagata sottoscrizione : " . ($this->filterSubscription == 1 ? "SI" : "NO") . " ";
  1133. }
  1134. if ($_GET["filterCertificateScadenza"] != "null") {
  1135. $scadenzaValues = explode(",", $_GET["filterCertificateScadenza"]);
  1136. $allScadIds = [];
  1137. foreach ($scadenzaValues as $filterValue) {
  1138. if ($filterValue == "1") {
  1139. $scadIds = \App\Models\MemberCertificate::where('expire_date', '<', date("Y-m-d"))
  1140. ->pluck('member_id')
  1141. ->toArray();
  1142. $allScadIds = array_merge($allScadIds, $scadIds);
  1143. }
  1144. else if ($filterValue == "2") {
  1145. $scadIds = \App\Models\MemberCertificate::where('expire_date', '>=', date("Y-m-d"))
  1146. ->where('expire_date', '<=', date("Y-m-d", strtotime("+1 month")))
  1147. ->pluck('member_id')
  1148. ->toArray();
  1149. $allScadIds = array_merge($allScadIds, $scadIds);
  1150. }
  1151. else if ($filterValue == "3") {
  1152. $scadIds = \App\Models\Member::whereNotIn('id', \App\Models\MemberCertificate::pluck('member_id'))
  1153. ->pluck('id')
  1154. ->toArray();
  1155. $allScadIds = array_merge($allScadIds, $scadIds);
  1156. }
  1157. else if ($filterValue == "4") {
  1158. $scadIds = \App\Models\MemberCertificate::where('expire_date', '>', date("Y-m-d", strtotime("+1 month")))
  1159. ->pluck('member_id')
  1160. ->toArray();
  1161. $allScadIds = array_merge($allScadIds, $scadIds);
  1162. }
  1163. }
  1164. // Remove duplicates
  1165. $allScadIds = array_unique($allScadIds);
  1166. // Apply filter using the collected IDs
  1167. $datas = $datas->whereIn('member_id', $allScadIds);
  1168. }
  1169. if ($_GET["fromYear"] != "")
  1170. {
  1171. $m_ids = \App\Models\Member::where('birth_date', '<', date("Y-m-d", strtotime("-" . $_GET["fromYear"] . " year", time())))->pluck('id');
  1172. $datas = $datas->whereIn('member_id', $m_ids);
  1173. }
  1174. if ($_GET["toYear"] != "")
  1175. {
  1176. $m_ids = \App\Models\Member::where('birth_date', '>', date("Y-m-d", strtotime("-" . $_GET["toYear"] . " year", time())))->pluck('id');
  1177. $datas = $datas->whereIn('member_id', $m_ids);
  1178. }
  1179. if ($_GET["fromFromYear"] != "")
  1180. {
  1181. $m_ids = \App\Models\Member::whereYear('birth_date', '>=', $_GET["fromFromYear"])->pluck('id');
  1182. $datas = $datas->whereIn('member_id', $m_ids);
  1183. }
  1184. if ($_GET["toToYear"] != "")
  1185. {
  1186. $m_ids = \App\Models\Member::whereYear('birth_date', '<=', $_GET["toToYear"])->pluck('id');
  1187. $datas = $datas->whereIn('member_id', $m_ids);
  1188. }
  1189. if ($_GET["filterCards"] != "null")
  1190. {
  1191. $cards = explode(",", $_GET["filterCards"]);
  1192. $card_ids = \App\Models\MemberCard::whereIn('card_id', $cards)->pluck('member_id');
  1193. $datas = $datas->whereIn('member_id', $card_ids);
  1194. }
  1195. if ($_GET["filterYear"] != "")
  1196. {
  1197. $course_ids = \App\Models\Course::where('year', $_GET["filterYear"])->pluck('id');
  1198. $datas = $datas->whereIn('course_id', $course_ids);
  1199. //$this->filter .= $this->filter != '' ? ', ' : '';
  1200. //$this->filter .= "Anno : " . $this->filterYear . " ";
  1201. }
  1202. $aRet = [];
  1203. if (isset($_GET["order"]))
  1204. {
  1205. $column = '';
  1206. if ($_GET["order"][0]["column"] == 1)
  1207. $column = 'course_name';
  1208. if ($_GET["order"][0]["column"] == 2)
  1209. $column = 'last_name';
  1210. if ($_GET["order"][0]["column"] == 3)
  1211. $column = 'first_name';
  1212. if ($_GET["order"][0]["column"] == 4)
  1213. $column = 'birth_date';
  1214. if ($_GET["order"][0]["column"] == 5)
  1215. $column = 'birth_date';
  1216. if ($_GET["order"][0]["column"] == 6)
  1217. $column = 'birth_date';
  1218. if ($_GET["order"][0]["column"] == 7)
  1219. $column = 'phone';
  1220. if ($_GET["order"][0]["column"] == 8)
  1221. $column = 'certificate_expire_date';
  1222. if ($column != '')
  1223. $datas = $datas->orderBy($column, $_GET["order"][0]["dir"]);
  1224. else
  1225. $datas = $datas->orderBy('last_name', 'ASC')->orderBy('first_name', 'ASC');
  1226. }
  1227. else
  1228. $datas = $datas->orderBy('last_name', 'ASC')->orderBy('first_name', 'ASC');
  1229. if ($_GET["filterStatus"] != "null")
  1230. {
  1231. $status = explode(",", $_GET["filterStatus"]);
  1232. foreach($status as $s)
  1233. {
  1234. foreach($datas->get() as $aaa)
  1235. {
  1236. $state = \App\Models\Member::findOrFail($aaa->member_id)->isActive();
  1237. if ($state["status"] == $s)
  1238. $aRet[] = $aaa;
  1239. }
  1240. }
  1241. }
  1242. else
  1243. $aRet = $datas->get();
  1244. $ret = [];
  1245. foreach($aRet as $idx => $r)
  1246. {
  1247. $date1 = new DateTime($r->birth_date);
  1248. $date2 = new DateTime("now");
  1249. $interval = $date1->diff($date2);
  1250. $certificate = \App\Models\MemberCertificate::where('member_id', $r->member_id)
  1251. ->orderBy('expire_date', 'desc')
  1252. ->first();
  1253. $certificateInfo = "";
  1254. if ($certificate) {
  1255. // Format: status|expire_date|type
  1256. // Status: 0 = expired, 1 = expiring soon, 2 = valid
  1257. $today = new DateTime();
  1258. $expireDate = new DateTime($certificate->expire_date);
  1259. $oneMonthFromNow = (new DateTime())->modify('+1 month');
  1260. $status = "2"; // Default to valid
  1261. if ($expireDate < $today) {
  1262. $status = "0"; // Expired
  1263. } elseif ($expireDate < $oneMonthFromNow) {
  1264. $status = "1"; // Expiring soon
  1265. }
  1266. $certificateInfo = $status . "|" . date("d/m/Y", strtotime($certificate->expire_date)) . "|" . $certificate->type;
  1267. }
  1268. $ret[] = array(
  1269. "column_0" => $idx + 1,
  1270. "column_8" => $r->course_name,
  1271. "column_1" => $r->last_name,
  1272. "column_2" => $r->first_name,
  1273. "column_3" => strval($interval->y),
  1274. "column_4" => date("Y", strtotime($r->birth_date)),
  1275. "column_5" => $r->phone,
  1276. "column_6" => $certificateInfo,
  1277. "column_7" => $r->member_id
  1278. );
  1279. }
  1280. if (isset($_GET["start"]))
  1281. $ret = array_slice($ret, $_GET["start"], $_GET["length"]);
  1282. return json_encode(array("data" => $ret, "recordsTotal" => sizeof($aRet), "recordsFiltered" => sizeof($aRet)));
  1283. });
  1284. Route::get('/get_receipts', function(){
  1285. $x = \App\Models\Receipt::select('receipts.*', 'members.first_name', 'members.last_name')->leftJoin('members', 'receipts.member_id', '=', 'members.id');
  1286. if (isset($_GET["search"]["value"]))
  1287. {
  1288. $v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
  1289. $member_ids = \App\Models\Member::where(function ($query) use ($v) {
  1290. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $v . "%'")
  1291. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $v . "%'");
  1292. })->pluck('id');
  1293. /*$member_ids = \App\Models\Member::where(function ($query) use ($v) {
  1294. $query->where('first_name', 'like', '%' . $v . '%')
  1295. ->orWhere('last_name', 'like', '%' . $v . '%');
  1296. })->pluck('id');*/
  1297. $x = $x->whereIn('member_id', $member_ids);
  1298. }
  1299. if ($_GET["filterStatus"] != '')
  1300. $x = $x->where('receipts.status', $_GET["filterStatus"]);
  1301. if ($_GET["filterFrom"] != "")
  1302. $x = $x->where('date', '>=', $_GET["filterFrom"]);
  1303. if ($_GET["filterTo"] != "")
  1304. $x = $x->where('date', '<=', $_GET["filterTo"]);
  1305. $count = $x->count();
  1306. if (isset($_GET["order"]))
  1307. {
  1308. $column = '';
  1309. if ($_GET["order"][0]["column"] == 0)
  1310. $column = 'year';
  1311. if ($_GET["order"][0]["column"] == 1)
  1312. $column = 'number';
  1313. if ($_GET["order"][0]["column"] == 2)
  1314. $column = 'last_name';
  1315. if ($_GET["order"][0]["column"] == 3)
  1316. $column = 'first_name';
  1317. if ($_GET["order"][0]["column"] == 4)
  1318. $column = 'status';
  1319. if ($_GET["order"][0]["column"] == 5)
  1320. $column = 'date';
  1321. if ($column != '')
  1322. $x = $x->orderBy($column, $_GET["order"][0]["dir"])->orderBy('id', 'DESC');
  1323. else
  1324. $x = $x->orderBy('id', 'DESC');
  1325. }
  1326. else
  1327. $x = $x->orderBy('id', 'DESC');
  1328. if (isset($_GET["start"]))
  1329. $x = $x->offset($_GET["start"])->limit($_GET["length"])->get();
  1330. else
  1331. $x = $x->get();
  1332. $datas = [];
  1333. foreach($x as $idx => $r)
  1334. {
  1335. $ids = $r->id . "|" . $r->record_id;
  1336. $datas[] = array(
  1337. 'year' => $r->year,
  1338. 'number' => $r->number,
  1339. 'last_name' => $r->member->last_name,
  1340. 'first_name' => $r->member->first_name,
  1341. 'status' => $r->status,
  1342. 'date' => date("d/m/Y", strtotime($r->created_at)),
  1343. 'totals' => formatPrice($r->rows->sum('amount')),
  1344. 'action' => $ids
  1345. );
  1346. }
  1347. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count));
  1348. });
  1349. function getColor($months, $m)
  1350. {
  1351. $class = "wgrey";
  1352. foreach(json_decode($months) as $mm)
  1353. {
  1354. if ($mm->m == $m)
  1355. {
  1356. if ($mm->status == "")
  1357. {
  1358. $class = "orange";
  1359. }
  1360. if ($mm->status == "1")
  1361. {
  1362. $class = "green";
  1363. }
  1364. if ($mm->status == "2")
  1365. {
  1366. $class = "yellow";
  1367. }
  1368. }
  1369. }
  1370. return $class;
  1371. }
  1372. Route::get('/migrate', function(){
  1373. \Artisan::call('migrate');
  1374. dd('migrated!');
  1375. });
  1376. Route::get('/updateData', function()
  1377. {
  1378. // Call and Artisan command from within your application.
  1379. Artisan::call('update:data');
  1380. });
  1381. Route::get('/seed', function()
  1382. {
  1383. // Call and Artisan command from within your application.
  1384. Artisan::call('db:seed');
  1385. });
  1386. Route::get('/updateCourseCausal', function(){
  1387. $member_courses = \App\Models\MemberCourse::all();
  1388. foreach($member_courses as $x)
  1389. {
  1390. $records = \App\Models\Record::where('member_course_id', $x->id)->get();
  1391. foreach ($records as $record)
  1392. {
  1393. foreach ($record->rows as $row)
  1394. {
  1395. //if ($row->causal_id == $x->course->sub_causal_id || str_contains(strtolower($row->note), 'iscrizione'))
  1396. if (str_contains(strtolower($row->note), 'iscrizione'))
  1397. {
  1398. $row->causal_id = $x->course->sub_causal_id;
  1399. $row->save();
  1400. }
  1401. }
  1402. }
  1403. }
  1404. });
  1405. Route::get('/test_sms', function()
  1406. {
  1407. $expire_date = date("Y-m-d", strtotime("+1 month"));
  1408. $expire_date_it = date("d/m/Y", strtotime("+1 month"));
  1409. $certificates = \App\Models\MemberCertificate::where('expire_date', $expire_date)->get();
  1410. foreach($certificates as $certificate)
  1411. {
  1412. //$phone = $certificate->member->phone;
  1413. $phone = '3893163265';
  1414. $params = array(
  1415. 'to' => '+39' . $phone,
  1416. 'from' => env('SMS_FROM', 'Test'),
  1417. 'message' => $certificate->member->first_name . ', il tuo certificato medico scadrà il ' . $expire_date_it,
  1418. 'format' => 'json',
  1419. );
  1420. sms_send($params);
  1421. print "Inviato";
  1422. }
  1423. /*
  1424. $params = array(
  1425. 'to' => '+393893163265',
  1426. 'from' => env('SMS_FROM', 'Test'),
  1427. 'message' => 'Prova SMS da IAO',
  1428. 'format' => 'json',
  1429. );
  1430. echo sms_send($params);
  1431. */
  1432. });