web.php 61 KB

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