web.php 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635
  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', \DB::raw('members.first_name as first_name'), \DB::raw('members.last_name as last_name')) // , \DB::raw('SUM(records.id) As total'))
  355. ->leftJoin('members', 'records.member_id', '=', 'members.id')
  356. ->leftJoin('records_rows', 'records.id', '=', 'records_rows.record_id')
  357. //->leftJoin('receipts', 'records.id', '=', 'receipts.record_id')
  358. ->where('records.type', 'IN');
  359. $hasFilter = false;
  360. if (isset($_GET["search"]["value"]))
  361. {
  362. if ($_GET["search"]["value"] != '')
  363. {
  364. $hasFilter = true;
  365. $v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
  366. $x = $x->where(function ($query) use ($v) {
  367. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $v . "%'")
  368. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $v . "%'");
  369. });
  370. $y = $y->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. }
  375. /*$v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
  376. $x = $x->where(function ($query) use ($v) {
  377. $query->where('first_name', 'like', '%' . $v . '%')
  378. ->orWhere('last_name', 'like', '%' . $v . '%');
  379. });
  380. $y = $y->where(function ($query) use ($v) {
  381. $query->where('first_name', 'like', '%' . $v . '%')
  382. ->orWhere('last_name', 'like', '%' . $v . '%');
  383. });
  384. }*/
  385. //where('first_name', 'like', '%' . $_GET["search"]["value"] . '%');
  386. }
  387. //$x = $x->where(function ($query) use ($v) {
  388. // $datas = \App\Models\Record::where('type', 'IN')->with('member', 'payment_method');
  389. if ($_GET["filterCommercial"] == 1)
  390. {
  391. $hasFilter = true;
  392. $x = $x->where('records.commercial', true );
  393. $y = $y->where('records.commercial', true );
  394. }
  395. if ($_GET["filterCommercial"] == 2)
  396. {
  397. $hasFilter = true;
  398. $x = $x->where('records.commercial', false);
  399. $y = $y->where('records.commercial', false);
  400. }
  401. if ($_GET["filterMember"] > 0)
  402. {
  403. $hasFilter = true;
  404. $x = $x->where('records.member_id', $_GET["filterMember"]);
  405. $y = $y->where('records.member_id', $_GET["filterMember"]);
  406. }
  407. if ($_GET["filterPaymentMethod"] != "null")
  408. {
  409. $hasFilter = true;
  410. $payments = explode(",", $_GET["filterPaymentMethod"]);
  411. $x = $x->whereIn('records.payment_method_id', $payments);
  412. $y = $y->whereIn('records.payment_method_id', $payments);
  413. }
  414. if ($_GET["filterCausals"] != "null")
  415. {
  416. $hasFilter = true;
  417. $causals = explode(",", $_GET["filterCausals"]);
  418. // Per ogni causale, se ha dei figli allora aggiungo le causali figlio
  419. foreach($causals as $c)
  420. {
  421. $childs = \App\Models\Causal::where('parent_id', $c)->get();
  422. foreach($childs as $cc)
  423. {
  424. $causals[] = $cc->id;
  425. $childss = \App\Models\Causal::where('parent_id', $cc->id)->get();
  426. foreach($childss as $ccc)
  427. {
  428. $causals[] = $ccc->id;
  429. }
  430. }
  431. }
  432. //$causals = \App\Models\RecordRow::where('causal_id', $_GET["filterCausals"])->pluck('record_id');
  433. $causals = \App\Models\RecordRow::whereIn('causal_id', $causals)->pluck('record_id');
  434. $x = $x->whereIn('records.id', $causals);
  435. $y = $y->whereIn('records.id', $causals);
  436. }
  437. if ($_GET["filterFrom"] != '')
  438. {
  439. $hasFilter = true;
  440. $x = $x->where('records.date', '>=', $_GET["filterFrom"]);
  441. $y = $y->where('date', '>=', $_GET["filterFrom"]);
  442. }
  443. if ($_GET["filterTo"] != '')
  444. {
  445. $hasFilter = true;
  446. $x = $x->where('records.date', '<=', $_GET["filterTo"]);
  447. $y = $y->where('date', '<=', $_GET["filterTo"]);
  448. }
  449. //});
  450. $start = 0;
  451. $limit = 100000;
  452. if (isset($_GET["start"]))
  453. {
  454. $start = $_GET["start"];
  455. $limit = $_GET["length"];
  456. }
  457. $excludeCausals = [];
  458. /*$borsellino = \App\Models\Causal::where('money', true)->first();
  459. if ($borsellino)
  460. $excludeCausals[] = $borsellino->id;*/
  461. // Aggiungo
  462. /*
  463. $excludes = \App\Models\Causal::where('no_records', true)->get();
  464. foreach($excludes as $e)
  465. {
  466. $excludeCausals[] = $e->id;
  467. }*/
  468. $exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
  469. // Pagamento money
  470. $moneys = \App\Models\PaymentMethod::where('money', true)->pluck('id')->toArray();
  471. // Causale money
  472. $moneysCausal = \App\Models\Causal::where('money', true)->pluck('id')->toArray();
  473. $total = 0;
  474. $causals = [];
  475. if ($_GET["filterCausals"] != "null")
  476. $causals = explode(",", $_GET["filterCausals"]);
  477. foreach($y->get() as $r)
  478. {
  479. if (!in_array($r->payment_method_id, $moneys))
  480. {
  481. 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))
  482. {
  483. if (sizeof($causals) == 0 || in_array($r->causal_id, $causals))
  484. {
  485. $total += $r->amount;
  486. if ($r->vat_id > 0)
  487. $total += getVatValue($r->amount, $r->vat_id);
  488. }
  489. }
  490. }
  491. }
  492. $count = $x->count();
  493. if (isset($_GET["order"]))
  494. {
  495. $column = '';
  496. if ($_GET["order"][0]["column"] == 0)
  497. $column = 'date';
  498. if ($_GET["order"][0]["column"] == 1)
  499. $column = 'records.amount';
  500. if ($_GET["order"][0]["column"] == 2)
  501. $column = 'last_name';
  502. if ($_GET["order"][0]["column"] == 3)
  503. $column = 'first_name';
  504. if ($_GET["order"][0]["column"] == 4)
  505. $column = 'commercial';
  506. if ($column != '')
  507. $x = $x->orderBy($column, $_GET["order"][0]["dir"])->orderBy('records.id', 'DESC');
  508. else
  509. $x = $x->orderBy('records.id', 'DESC');
  510. }
  511. else
  512. $x = $x->orderBy('records.date', 'DESC')->orderBy('records.id', 'DESC');
  513. $x = $x->offset($start)->limit($limit)->get();
  514. foreach($x as $idx => $r)
  515. {
  516. $causals = '';
  517. foreach($r->rows as $row)
  518. {
  519. $causals .= $row->causal->getTree() . "<br>";
  520. }
  521. $datas[] = array(
  522. //'id' => $r->id,
  523. 'date' => $r->date,
  524. //'date' => $r->receipt_date != null ? $r->receipt_date : "",
  525. 'total' => formatPrice($r->getTotal()),
  526. 'first_name' => $r->first_name,
  527. 'last_name' => $r->last_name,
  528. 'commercial' => $r->financial_movement ? 'Movimento finanziario' : ($r->commercial ? 'SI' : 'NO'),
  529. 'causals' => $causals,
  530. 'payment' => $r->payment_method->name,
  531. //'payment_date' => date("d/m/Y", strtotime($r->date)),
  532. 'status' => $r->deleted ? 'Annullato' : '',
  533. 'action' => $r->id . "||" . ($r->deleted ? 'x' : '')
  534. );
  535. }
  536. /*$datas[] = array(
  537. //'id' => $r->id,
  538. 'date' => '',
  539. 'total' => formatPrice($total),
  540. 'first_name' => '',
  541. 'last_name' => '',
  542. 'commercial' => '',
  543. 'causals' => '',
  544. 'payment' => '',
  545. 'status' => '',
  546. 'action' => ''
  547. );*/
  548. if ($hasFilter)
  549. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count, "totals" => formatPrice($total)));
  550. else
  551. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count));
  552. });
  553. Route::get('/get_record_out', function(){
  554. $datas = [];
  555. $hasFilter = false;
  556. $x = \App\Models\Record::where('type', 'OUT')->with('supplier', 'payment_method');
  557. if ($_GET["filterSupplier"] > 0)
  558. {
  559. $x = $x->where('supplier_id', $_GET["filterSupplier"]);
  560. $hasFilter = true;
  561. }
  562. /*if ($_GET["filterPaymentMethod"] > 0)
  563. {
  564. $x = $x->where('payment_method_id', $_GET["filterPaymentMethod"]);
  565. }
  566. if ($_GET["filterCausals"] > 0)
  567. {
  568. $causals = \App\Models\RecordRow::where('causal_id', $_GET["filterCausals"])->pluck('record_id');
  569. $x = $x->whereIn('records.id', $causals);
  570. }*/
  571. if ($_GET["filterPaymentMethod"] != "null")
  572. {
  573. $payments = explode(",", $_GET["filterPaymentMethod"]);
  574. $x = $x->whereIn('payment_method_id', $payments);
  575. $hasFilter = true;
  576. }
  577. if ($_GET["filterCausals"] != "null")
  578. {
  579. $causals = explode(",", $_GET["filterCausals"]);
  580. // Per ogni causale, se ha dei figli allora aggiungo le causali figlio
  581. foreach($causals as $c)
  582. {
  583. $childs = \App\Models\Causal::where('parent_id', $c)->get();
  584. foreach($childs as $cc)
  585. {
  586. $causals[] = $cc->id;
  587. $childss = \App\Models\Causal::where('parent_id', $cc->id)->get();
  588. foreach($childss as $ccc)
  589. {
  590. $causals[] = $ccc->id;
  591. }
  592. }
  593. }
  594. //$causals = \App\Models\RecordRow::where('causal_id', $_GET["filterCausals"])->pluck('record_id');
  595. $causals = \App\Models\RecordRow::whereIn('causal_id', $causals)->pluck('record_id');
  596. $x = $x->whereIn('records.id', $causals);
  597. $hasFilter = true;
  598. }
  599. if ($_GET["filterFrom"] != '')
  600. {
  601. $x = $x->where('date', '>=', $_GET["filterFrom"]);
  602. $hasFilter = true;
  603. }
  604. if ($_GET["filterTo"] != '')
  605. {
  606. $x = $x->where('date', '<=', $_GET["filterTo"]);
  607. $hasFilter = true;
  608. }
  609. $total = 0;
  610. foreach($x->get() as $r)
  611. {
  612. foreach($r->rows as $rr)
  613. {
  614. $total += $rr->amount;
  615. if ($rr->vat_id > 0)
  616. $total += getVatValue($rr->amount, $rr->vat_id);
  617. }
  618. }
  619. $x = $x->get();
  620. foreach($x as $idx => $r)
  621. {
  622. $causals = '';
  623. foreach($r->rows as $row)
  624. {
  625. $causals .= $row->causal->getTree() . "<br>";
  626. }
  627. $datas[] = array(
  628. //'id' => $r->id,
  629. 'date' => $r->date,
  630. 'total' => formatPrice($r->getTotal()),
  631. 'supplier' => $r->supplier->name,
  632. 'causals' => $causals,
  633. 'payment' => $r->payment_method->name,
  634. 'action' => $r->id . "|"
  635. );
  636. }
  637. /*
  638. $datas[] = array(
  639. //'id' => $r->id,
  640. 'date' => '',
  641. 'total' => formatPrice($total),
  642. 'supplier' => '',
  643. 'causals' => '',
  644. 'payment' => '',
  645. 'action' => ''
  646. );
  647. */
  648. if ($hasFilter)
  649. return json_encode(array("data" => $datas, "totals" => formatPrice($total)));
  650. else
  651. return json_encode(array("data" => $datas));
  652. });
  653. Route::get('/get_course_list', function(){
  654. $member_course = \App\Models\MemberCourse::with('member')->with('course');
  655. if (isset($_GET["search"]["value"]))
  656. {
  657. if ($_GET["search"]["value"] != '')
  658. {
  659. $v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
  660. $member_ids = \App\Models\Member::where(function ($query) use ($v) {
  661. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $v . "%'")
  662. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $v . "%'");
  663. })->pluck('id');
  664. /*
  665. $v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
  666. $member_ids = \App\Models\Member::where(function ($query) use ($v) {
  667. $query->where('first_name', 'like', '%' . $v . '%')
  668. ->orWhere('last_name', 'like', '%' . $v . '%');
  669. })->pluck('id');*/
  670. $member_course = $member_course->whereIn('member_id', $member_ids);
  671. }
  672. }
  673. if ($_GET["filterCourse"] != "null")
  674. {
  675. $course_ids = [];
  676. $courses = explode(",", $_GET["filterCourse"]);
  677. foreach($courses as $c)
  678. {
  679. $all = \App\Models\Course::where('name', 'like', '%' . $c . "%")->get();
  680. foreach($all as $a)
  681. {
  682. $course_ids[] = $a->id;
  683. }
  684. }
  685. $member_course = $member_course->whereIn('course_id', $course_ids);
  686. }
  687. if ($_GET["filterYear"] != "")
  688. {
  689. $course_ids = \App\Models\Course::where('year', $_GET["filterYear"])->pluck('id');
  690. $member_course = $member_course->whereIn('course_id', $course_ids);
  691. }
  692. if ($_GET["filterLevel"] != "null")
  693. {
  694. $levels = explode(",", $_GET["filterLevel"]);
  695. $course_ids = \App\Models\Course::whereIn('course_level_id', $levels)->pluck('id');
  696. $member_course = $member_course->whereIn('course_id', $course_ids);
  697. }
  698. if ($_GET["filterFrequency"] != "null")
  699. {
  700. $frequencies = explode(",", $_GET["filterFrequency"]);
  701. $course_ids = \App\Models\Course::whereIn('course_frequency_id', $frequencies)->pluck('id');
  702. $member_course = $member_course->whereIn('course_id', $course_ids);
  703. }
  704. if ($_GET["filterType"] != "null")
  705. {
  706. $types = explode(",", $_GET["filterType"]);
  707. $course_ids = \App\Models\Course::whereIn('course_type_id', $types)->pluck('id');
  708. $member_course = $member_course->whereIn('course_id', $course_ids);
  709. }
  710. if ($_GET["filterDuration"] != "null")
  711. {
  712. $durations = explode(",", $_GET["filterDuration"]);
  713. $course_ids = \App\Models\Course::whereIn('course_duration_id', $durations)->pluck('id');
  714. $member_course = $member_course->whereIn('course_id', $course_ids);
  715. }
  716. $totals = [];
  717. $totalIsc = [];
  718. $datas = [];
  719. $xxx = 1;
  720. /*
  721. $sortColumn = '';
  722. if (isset($_GET["order"]))
  723. {
  724. }
  725. */
  726. $column = '';
  727. $sort_value = 0;
  728. if (isset($_GET["order"]))
  729. {
  730. $f = $_GET["order"][0]["column"];
  731. $d = $_GET["order"][0]["dir"];
  732. if ($f >= 5 && $f <= 16)
  733. {
  734. $column = 'column_' . ($f - 2);
  735. if (session()->get('sort_column'))
  736. {
  737. if (session()->get('sort_column') != $f)
  738. {
  739. session()->put('sort_column', $f);
  740. session()->put('sort_order', $d);
  741. session()->put('sort_value', 0);
  742. $sort_value = 0;
  743. }
  744. else
  745. {
  746. if (session()->get('sort_order') == $d)
  747. {
  748. //session()->put('sort_value', 0);
  749. $sort_value = session()->get('sort_value', 0);
  750. }
  751. else
  752. {
  753. if (session()->get('sort_value', 0) == 0)
  754. {
  755. $sort_value = 1;
  756. }
  757. if (session()->get('sort_value', 0) == 1)
  758. {
  759. $sort_value = 2;
  760. }
  761. if (session()->get('sort_value', 0) == 2)
  762. {
  763. $sort_value = 3;
  764. }
  765. if (session()->get('sort_value', 0) == 3)
  766. {
  767. $sort_value = 0;
  768. }
  769. session()->put('sort_value', $sort_value);
  770. }
  771. session()->put('sort_order', $d);
  772. }
  773. }
  774. else
  775. {
  776. session()->put('sort_column', $f);
  777. session()->put('sort_order', $d);
  778. session()->put('sort_value', 0);
  779. $sort_value = 0;
  780. }
  781. }
  782. }
  783. //print $sort_value;
  784. $member_course_totals = $member_course->get();
  785. foreach($member_course_totals as $x)
  786. {
  787. $price = 0;
  788. $price = $x->price; // $x->course->price;
  789. $subPrice = $x->subscription_price; // $x->course->subscription_price;
  790. $records = \App\Models\Record::where('member_course_id', $x->id)->where('deleted', 0)->get();
  791. $prices = [];
  792. foreach ($records as $record)
  793. {
  794. foreach ($record->rows as $row)
  795. {
  796. if ($row->causal_id == $x->course->sub_causal_id) // || str_contains(strtolower($row->note), 'iscrizione'))
  797. //if (str_contains(strtolower($row->note), 'iscrizione'))
  798. {
  799. $subPrice = $row->amount;
  800. }
  801. if ($row->causal_id == $x->course->causal_id && !str_contains(strtolower($row->note), 'iscrizione'))
  802. {
  803. $tot = sizeof(json_decode($row->when));
  804. foreach(json_decode($row->when) as $m)
  805. {
  806. $prices[$m->month] = $row->amount / $tot;
  807. }
  808. }
  809. }
  810. }
  811. for($i=1; $i<=12; $i++)
  812. {
  813. $cls = getColor($x->months, $i);
  814. if ($cls != 'wgrey')
  815. {
  816. if (!isset($totals[$i]))
  817. {
  818. $totals[$i]['green'] = 0;
  819. $totals[$i]['orange'] = 0;
  820. $totals[$i]['yellow'] = 0;
  821. }
  822. if ($cls == 'yellow')
  823. {
  824. $totals[$i][$cls] += 1;
  825. }
  826. else
  827. {
  828. $p = isset($prices[$i]) ? $prices[$i] : $price;
  829. $totals[$i][$cls] += $p;
  830. }
  831. }
  832. }
  833. $sub = $x->subscribed ? "Y" : "N";
  834. if (isset($totalIsc[$sub]))
  835. $totalIsc[$sub] += $subPrice;
  836. else
  837. $totalIsc[$sub] = $subPrice;
  838. $s = 0;
  839. if ($column != '')
  840. {
  841. $z = 0;
  842. switch ($column) {
  843. case 'column_3':
  844. $z = 9;
  845. break;
  846. case 'column_4':
  847. $z = 10;
  848. break;
  849. case 'column_5':
  850. $z = 11;
  851. break;
  852. case 'column_6':
  853. $z = 12;
  854. break;
  855. case 'column_7':
  856. $z = 1;
  857. break;
  858. case 'column_8':
  859. $z = 2;
  860. break;
  861. case 'column_9':
  862. $z = 3;
  863. break;
  864. case 'column_10':
  865. $z = 4;
  866. break;
  867. case 'column_11':
  868. $z = 5;
  869. break;
  870. case 'column_12':
  871. $z = 6;
  872. break;
  873. case 'column_13':
  874. $z = 7;
  875. break;
  876. case 'column_14':
  877. $z = 8;
  878. break;
  879. default:
  880. $z = 0;
  881. break;
  882. }
  883. $c = getColor($x->months, $z);
  884. if ($sort_value == 0)
  885. {
  886. switch ($c) {
  887. case 'wgrey':
  888. $s = 0;
  889. break;
  890. case 'orange':
  891. $s = 1;
  892. break;
  893. case 'green':
  894. $s = 2;
  895. break;
  896. case 'yellow':
  897. $s = 3;
  898. break;
  899. default:
  900. $s = 0;
  901. break;
  902. }
  903. }
  904. if ($sort_value == 1)
  905. {
  906. switch ($c) {
  907. case 'wgrey':
  908. $s = 3;
  909. break;
  910. case 'orange':
  911. $s = 0;
  912. break;
  913. case 'green':
  914. $s = 1;
  915. break;
  916. case 'yellow':
  917. $s = 2;
  918. break;
  919. default:
  920. $s = 0;
  921. break;
  922. }
  923. }
  924. if ($sort_value == 2)
  925. {
  926. switch ($c) {
  927. case 'wgrey':
  928. $s = 2;
  929. break;
  930. case 'orange':
  931. $s = 3;
  932. break;
  933. case 'green':
  934. $s = 0;
  935. break;
  936. case 'yellow':
  937. $s = 1;
  938. break;
  939. default:
  940. $s = 0;
  941. break;
  942. }
  943. }
  944. if ($sort_value == 3)
  945. {
  946. switch ($c) {
  947. case 'wgrey':
  948. $s = 1;
  949. break;
  950. case 'orange':
  951. $s = 2;
  952. break;
  953. case 'green':
  954. $s = 3;
  955. break;
  956. case 'yellow':
  957. $s = 0;
  958. break;
  959. default:
  960. $s = 0;
  961. break;
  962. }
  963. }
  964. }
  965. $datas[] = array(
  966. "column_19" => $x->course->name,
  967. "column_0" => $x->member->last_name,
  968. "column_1" => $x->member->first_name,
  969. "column_2" => $x->subscribed . "§" . formatPrice($subPrice),
  970. "column_3" => getColor($x->months, 9) . "§" . formatPrice(isset($prices[9]) ? $prices[9] : $price),
  971. "column_4" => getColor($x->months, 10) . "§" . formatPrice(isset($prices[10]) ? $prices[10] : $price),
  972. "column_5" => getColor($x->months, 11) . "§" . formatPrice(isset($prices[11]) ? $prices[11] : $price),
  973. "column_6" => getColor($x->months, 12) . "§" . formatPrice(isset($prices[12]) ? $prices[12] : $price),
  974. "column_7" => getColor($x->months, 1) . "§" . formatPrice(isset($prices[1]) ? $prices[1] : $price),
  975. "column_8" => getColor($x->months, 2) . "§" . formatPrice(isset($prices[2]) ? $prices[2] : $price),
  976. "column_9" => getColor($x->months, 3) . "§" . formatPrice(isset($prices[3]) ? $prices[3] : $price),
  977. "column_10" => getColor($x->months, 4) . "§" . formatPrice(isset($prices[4]) ? $prices[4] : $price),
  978. "column_11" => getColor($x->months, 5) . "§" . formatPrice(isset($prices[5]) ? $prices[5] : $price),
  979. "column_12" => getColor($x->months, 6) . "§" . formatPrice(isset($prices[6]) ? $prices[6] : $price),
  980. "column_13" => getColor($x->months, 7) . "§" . formatPrice(isset($prices[7]) ? $prices[7] : $price),
  981. "column_14" => getColor($x->months, 8) . "§" . formatPrice(isset($prices[8]) ? $prices[8] : $price),
  982. "column_15" => $x->course_id,
  983. "column_16" => $x->id,
  984. "column_17" => $x->member_id,
  985. "column_18" => $xxx++,
  986. "column_20" => $s
  987. );
  988. }
  989. $count = $member_course->count();
  990. $js = '';
  991. $xx = 4;
  992. $str = '';
  993. if ($count > 0)
  994. {
  995. $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>";
  996. $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>";
  997. $str .= "<a style='width:100%;float:right; text-align:right; display:block;' class=yellow><small>0</small></a><br>";
  998. }
  999. $js .= $xx . "§" . $str . "_";
  1000. $str = "";
  1001. foreach($totals as $z => $t)
  1002. {
  1003. if ($z == 1) $xx = 9;
  1004. if ($z == 2) $xx = 10;
  1005. if ($z == 3) $xx = 11;
  1006. if ($z == 4) $xx = 12;
  1007. if ($z == 5) $xx = 13;
  1008. if ($z == 6) $xx = 14;
  1009. if ($z == 7) $xx = 15;
  1010. if ($z == 8) $xx = 16;
  1011. if ($z == 9) $xx = 5;
  1012. if ($z == 10) $xx = 6;
  1013. if ($z == 11) $xx = 7;
  1014. if ($z == 12) $xx = 8;
  1015. $str = '';
  1016. foreach($t as $x => $c)
  1017. {
  1018. $y = $x == 'yellow' ? $c : formatPrice($c);
  1019. $str .= "<a style='width:100%;float:right; text-align:right; display:block;' class=" . $x . "><small>" . $y . "</small></a><br>";
  1020. }
  1021. $js .= $xx . "§" . $str . "_";
  1022. $xx += 1;
  1023. }
  1024. if (isset($_GET["order"]))
  1025. {
  1026. $s = $_GET["order"][0]["column"];
  1027. if ($s == 1) $s = 21;
  1028. if ($column != '')
  1029. array_multisort(array_column($datas, 'column_20'), SORT_ASC, SORT_NATURAL|SORT_FLAG_CASE, $datas);
  1030. else
  1031. array_multisort(array_column($datas, 'column_' . ($s - 2)), $_GET["order"][0]["dir"] == "asc" ? SORT_ASC : SORT_DESC, SORT_NATURAL|SORT_FLAG_CASE, $datas);
  1032. }
  1033. $xxx = 1;
  1034. foreach($datas as $yyy => $d)
  1035. {
  1036. $datas[$yyy]["column_18"] = $xxx++;
  1037. }
  1038. if (isset($_GET["start"]))
  1039. $datas = array_slice($datas, $_GET["start"], $_GET["length"]);
  1040. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count, "totals" => $js));
  1041. });
  1042. Route::get('/get_course_members', function(){
  1043. //$datas = \App\Models\MemberCourse::with('member');
  1044. $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')
  1045. ->leftJoin('courses', 'member_courses.course_id', '=', 'courses.id')
  1046. ->leftJoin('members', 'member_courses.member_id', '=', 'members.id');
  1047. if (isset($_GET["search"]["value"]))
  1048. {
  1049. $v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
  1050. $member_ids = \App\Models\Member::where(function ($query) use ($v) {
  1051. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $v . "%'")
  1052. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $v . "%'");
  1053. })->pluck('id');
  1054. /*$member_ids = \App\Models\Member::where(function ($query) use ($v) {
  1055. $query->where('first_name', 'like', '%' . $v . '%')
  1056. ->orWhere('last_name', 'like', '%' . $v . '%');
  1057. })->pluck('id');*/
  1058. $datas = $datas->whereIn('member_id', $member_ids);
  1059. }
  1060. if ($_GET["filterCourse"] != "null")
  1061. {
  1062. $course_ids = [];
  1063. $courses = explode(",", $_GET["filterCourse"]);
  1064. foreach($courses as $c)
  1065. {
  1066. $all = \App\Models\Course::where('name', 'like', '%' . $c . "%")->get();
  1067. foreach($all as $a)
  1068. {
  1069. $course_ids[] = $a->id;
  1070. }
  1071. }
  1072. $datas = $datas->whereIn('course_id', $course_ids);
  1073. }
  1074. if ($_GET["filterLevel"] != "null")
  1075. {
  1076. $levels = explode(",", $_GET["filterLevel"]);
  1077. $course_ids = \App\Models\Course::whereIn('course_level_id', $levels)->pluck('id');
  1078. $datas = $datas->whereIn('course_id', $course_ids);
  1079. }
  1080. if ($_GET["filterFrequency"] != "null")
  1081. {
  1082. $frequencies = explode(",", $_GET["filterFrequency"]);
  1083. $course_ids = \App\Models\Course::whereIn('course_frequency_id', $frequencies)->pluck('id');
  1084. $datas = $datas->whereIn('course_id', $course_ids);
  1085. }
  1086. if ($_GET["filterType"] != "null")
  1087. {
  1088. $types = explode(",", $_GET["filterType"]);
  1089. $course_ids = \App\Models\Course::whereIn('course_type_id', $types)->pluck('id');
  1090. $datas = $datas->whereIn('course_id', $course_ids);
  1091. }
  1092. if ($_GET["filterDuration"] != "null")
  1093. {
  1094. $durations = explode(",", $_GET["filterDuration"]);
  1095. $course_ids = \App\Models\Course::whereIn('course_duration_id', $durations)->pluck('id');
  1096. $datas = $datas->whereIn('course_id', $course_ids);
  1097. }
  1098. if ($_GET["filterDays"] != "null")
  1099. {
  1100. $ids = [];
  1101. $days = explode(",", $_GET["filterDays"]);
  1102. foreach($days as $d)
  1103. {
  1104. $all = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")->get();
  1105. foreach($all as $a)
  1106. {
  1107. $ids[] = $a->id;
  1108. }
  1109. }
  1110. $datas = $datas->whereIn('member_courses.id', $ids);
  1111. }
  1112. if ($_GET["filterHours"] != "null")
  1113. {
  1114. $ids = [];
  1115. $hours = explode(",", $_GET["filterHours"]);
  1116. foreach($hours as $h)
  1117. {
  1118. $all = \App\Models\MemberCourse::where('when', 'like', '%"from":"' . $h . "%")->get();
  1119. foreach($all as $a)
  1120. {
  1121. $ids[] = $a->id;
  1122. }
  1123. }
  1124. $datas = $datas->whereIn('member_courses.id', $ids);
  1125. }
  1126. if ($_GET["filterSubscription"] != "")
  1127. {
  1128. $ids = \App\Models\MemberCourse::where('subscribed', $_GET["filterSubscription"] == 1 ? true : false)->pluck('id');
  1129. $datas = $datas->whereIn('member_courses.id', $ids);
  1130. //$this->filter .= $this->filter != '' ? ', ' : '';
  1131. //$this->filter .= "Pagata sottoscrizione : " . ($this->filterSubscription == 1 ? "SI" : "NO") . " ";
  1132. }
  1133. if ($_GET["filterCertificateScadenza"] != "null") {
  1134. $scadenzaValues = explode(",", $_GET["filterCertificateScadenza"]);
  1135. $allScadIds = [];
  1136. foreach ($scadenzaValues as $filterValue) {
  1137. if ($filterValue == "1") {
  1138. $scadIds = \App\Models\MemberCertificate::where('expire_date', '<', date("Y-m-d"))
  1139. ->pluck('member_id')
  1140. ->toArray();
  1141. $allScadIds = array_merge($allScadIds, $scadIds);
  1142. }
  1143. else if ($filterValue == "2") {
  1144. $scadIds = \App\Models\MemberCertificate::where('expire_date', '>=', date("Y-m-d"))
  1145. ->where('expire_date', '<=', date("Y-m-d", strtotime("+1 month")))
  1146. ->pluck('member_id')
  1147. ->toArray();
  1148. $allScadIds = array_merge($allScadIds, $scadIds);
  1149. }
  1150. else if ($filterValue == "3") {
  1151. $scadIds = \App\Models\Member::whereNotIn('id', \App\Models\MemberCertificate::pluck('member_id'))
  1152. ->pluck('id')
  1153. ->toArray();
  1154. $allScadIds = array_merge($allScadIds, $scadIds);
  1155. }
  1156. else if ($filterValue == "4") {
  1157. $scadIds = \App\Models\MemberCertificate::where('expire_date', '>', date("Y-m-d", strtotime("+1 month")))
  1158. ->pluck('member_id')
  1159. ->toArray();
  1160. $allScadIds = array_merge($allScadIds, $scadIds);
  1161. }
  1162. }
  1163. // Remove duplicates
  1164. $allScadIds = array_unique($allScadIds);
  1165. // Apply filter using the collected IDs
  1166. $datas = $datas->whereIn('member_id', $allScadIds);
  1167. }
  1168. if ($_GET["fromYear"] != "")
  1169. {
  1170. $m_ids = \App\Models\Member::where('birth_date', '<', date("Y-m-d", strtotime("-" . $_GET["fromYear"] . " year", time())))->pluck('id');
  1171. $datas = $datas->whereIn('member_id', $m_ids);
  1172. }
  1173. if ($_GET["toYear"] != "")
  1174. {
  1175. $m_ids = \App\Models\Member::where('birth_date', '>', date("Y-m-d", strtotime("-" . $_GET["toYear"] . " year", time())))->pluck('id');
  1176. $datas = $datas->whereIn('member_id', $m_ids);
  1177. }
  1178. if ($_GET["fromFromYear"] != "")
  1179. {
  1180. $m_ids = \App\Models\Member::whereYear('birth_date', '>=', $_GET["fromFromYear"])->pluck('id');
  1181. $datas = $datas->whereIn('member_id', $m_ids);
  1182. }
  1183. if ($_GET["toToYear"] != "")
  1184. {
  1185. $m_ids = \App\Models\Member::whereYear('birth_date', '<=', $_GET["toToYear"])->pluck('id');
  1186. $datas = $datas->whereIn('member_id', $m_ids);
  1187. }
  1188. if ($_GET["filterCards"] != "null")
  1189. {
  1190. $cards = explode(",", $_GET["filterCards"]);
  1191. $card_ids = \App\Models\MemberCard::whereIn('card_id', $cards)->pluck('member_id');
  1192. $datas = $datas->whereIn('member_id', $card_ids);
  1193. }
  1194. if ($_GET["filterYear"] != "")
  1195. {
  1196. $course_ids = \App\Models\Course::where('year', $_GET["filterYear"])->pluck('id');
  1197. $datas = $datas->whereIn('course_id', $course_ids);
  1198. //$this->filter .= $this->filter != '' ? ', ' : '';
  1199. //$this->filter .= "Anno : " . $this->filterYear . " ";
  1200. }
  1201. $aRet = [];
  1202. if (isset($_GET["order"]))
  1203. {
  1204. $column = '';
  1205. if ($_GET["order"][0]["column"] == 1)
  1206. $column = 'course_name';
  1207. if ($_GET["order"][0]["column"] == 2)
  1208. $column = 'last_name';
  1209. if ($_GET["order"][0]["column"] == 3)
  1210. $column = 'first_name';
  1211. if ($_GET["order"][0]["column"] == 4)
  1212. $column = 'birth_date';
  1213. if ($_GET["order"][0]["column"] == 5)
  1214. $column = 'birth_date';
  1215. if ($_GET["order"][0]["column"] == 6)
  1216. $column = 'birth_date';
  1217. if ($_GET["order"][0]["column"] == 7)
  1218. $column = 'phone';
  1219. if ($_GET["order"][0]["column"] == 8)
  1220. $column = 'certificate_expire_date';
  1221. if ($column != '')
  1222. $datas = $datas->orderBy($column, $_GET["order"][0]["dir"]);
  1223. else
  1224. $datas = $datas->orderBy('last_name', 'ASC')->orderBy('first_name', 'ASC');
  1225. }
  1226. else
  1227. $datas = $datas->orderBy('last_name', 'ASC')->orderBy('first_name', 'ASC');
  1228. if ($_GET["filterStatus"] != "null")
  1229. {
  1230. $status = explode(",", $_GET["filterStatus"]);
  1231. foreach($status as $s)
  1232. {
  1233. foreach($datas->get() as $aaa)
  1234. {
  1235. $state = \App\Models\Member::findOrFail($aaa->member_id)->isActive();
  1236. if ($state["status"] == $s)
  1237. $aRet[] = $aaa;
  1238. }
  1239. }
  1240. }
  1241. else
  1242. $aRet = $datas->get();
  1243. $ret = [];
  1244. foreach($aRet as $idx => $r)
  1245. {
  1246. $date1 = new DateTime($r->birth_date);
  1247. $date2 = new DateTime("now");
  1248. $interval = $date1->diff($date2);
  1249. $certificate = \App\Models\MemberCertificate::where('member_id', $r->member_id)
  1250. ->orderBy('expire_date', 'desc')
  1251. ->first();
  1252. $certificateInfo = "";
  1253. if ($certificate) {
  1254. // Format: status|expire_date|type
  1255. // Status: 0 = expired, 1 = expiring soon, 2 = valid
  1256. $today = new DateTime();
  1257. $expireDate = new DateTime($certificate->expire_date);
  1258. $oneMonthFromNow = (new DateTime())->modify('+1 month');
  1259. $status = "2"; // Default to valid
  1260. if ($expireDate < $today) {
  1261. $status = "0"; // Expired
  1262. } elseif ($expireDate < $oneMonthFromNow) {
  1263. $status = "1"; // Expiring soon
  1264. }
  1265. $certificateInfo = $status . "|" . date("d/m/Y", strtotime($certificate->expire_date)) . "|" . $certificate->type;
  1266. }
  1267. $ret[] = array(
  1268. "column_0" => $idx + 1,
  1269. "column_8" => $r->course_name,
  1270. "column_1" => $r->last_name,
  1271. "column_2" => $r->first_name,
  1272. "column_3" => strval($interval->y),
  1273. "column_4" => date("Y", strtotime($r->birth_date)),
  1274. "column_5" => $r->phone,
  1275. "column_6" => $certificateInfo,
  1276. "column_7" => $r->member_id
  1277. );
  1278. }
  1279. if (isset($_GET["start"]))
  1280. $ret = array_slice($ret, $_GET["start"], $_GET["length"]);
  1281. return json_encode(array("data" => $ret, "recordsTotal" => sizeof($aRet), "recordsFiltered" => sizeof($aRet)));
  1282. });
  1283. Route::get('/get_receipts', function(){
  1284. $x = \App\Models\Receipt::select('receipts.*', 'members.first_name', 'members.last_name')->leftJoin('members', 'receipts.member_id', '=', 'members.id');
  1285. if (isset($_GET["search"]["value"]))
  1286. {
  1287. $v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
  1288. $member_ids = \App\Models\Member::where(function ($query) use ($v) {
  1289. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $v . "%'")
  1290. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $v . "%'");
  1291. })->pluck('id');
  1292. /*$member_ids = \App\Models\Member::where(function ($query) use ($v) {
  1293. $query->where('first_name', 'like', '%' . $v . '%')
  1294. ->orWhere('last_name', 'like', '%' . $v . '%');
  1295. })->pluck('id');*/
  1296. $x = $x->whereIn('member_id', $member_ids);
  1297. }
  1298. if ($_GET["filterStatus"] != '')
  1299. $x = $x->where('receipts.status', $_GET["filterStatus"]);
  1300. if ($_GET["filterFrom"] != "")
  1301. $x = $x->where('date', '>=', $_GET["filterFrom"]);
  1302. if ($_GET["filterTo"] != "")
  1303. $x = $x->where('date', '<=', $_GET["filterTo"]);
  1304. $count = $x->count();
  1305. if (isset($_GET["order"]))
  1306. {
  1307. $column = '';
  1308. if ($_GET["order"][0]["column"] == 0)
  1309. $column = 'year';
  1310. if ($_GET["order"][0]["column"] == 1)
  1311. $column = 'number';
  1312. if ($_GET["order"][0]["column"] == 2)
  1313. $column = 'last_name';
  1314. if ($_GET["order"][0]["column"] == 3)
  1315. $column = 'first_name';
  1316. if ($_GET["order"][0]["column"] == 4)
  1317. $column = 'status';
  1318. if ($_GET["order"][0]["column"] == 5)
  1319. $column = 'date';
  1320. if ($column != '')
  1321. $x = $x->orderBy($column, $_GET["order"][0]["dir"])->orderBy('id', 'DESC');
  1322. else
  1323. $x = $x->orderBy('id', 'DESC');
  1324. }
  1325. else
  1326. $x = $x->orderBy('id', 'DESC');
  1327. if (isset($_GET["start"]))
  1328. $x = $x->offset($_GET["start"])->limit($_GET["length"])->get();
  1329. else
  1330. $x = $x->get();
  1331. $datas = [];
  1332. foreach($x as $idx => $r)
  1333. {
  1334. $ids = $r->id . "|" . $r->record_id;
  1335. $datas[] = array(
  1336. 'year' => $r->year,
  1337. 'number' => $r->number,
  1338. 'last_name' => $r->member->last_name,
  1339. 'first_name' => $r->member->first_name,
  1340. 'status' => $r->status,
  1341. 'date' => date("d/m/Y", strtotime($r->date)),
  1342. 'totals' => formatPrice($r->rows->sum('amount')),
  1343. 'action' => $ids
  1344. );
  1345. }
  1346. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count));
  1347. });
  1348. function getColor($months, $m)
  1349. {
  1350. $class = "wgrey";
  1351. foreach(json_decode($months) as $mm)
  1352. {
  1353. if ($mm->m == $m)
  1354. {
  1355. if ($mm->status == "")
  1356. {
  1357. $class = "orange";
  1358. }
  1359. if ($mm->status == "1")
  1360. {
  1361. $class = "green";
  1362. }
  1363. if ($mm->status == "2")
  1364. {
  1365. $class = "yellow";
  1366. }
  1367. }
  1368. }
  1369. return $class;
  1370. }
  1371. Route::get('/migrate', function(){
  1372. \Artisan::call('migrate');
  1373. dd('migrated!');
  1374. });
  1375. Route::get('/updateData', function()
  1376. {
  1377. // Call and Artisan command from within your application.
  1378. Artisan::call('update:data');
  1379. });
  1380. Route::get('/seed', function()
  1381. {
  1382. // Call and Artisan command from within your application.
  1383. Artisan::call('db:seed');
  1384. });
  1385. Route::get('/updateCourseCausal', function(){
  1386. $member_courses = \App\Models\MemberCourse::all();
  1387. foreach($member_courses as $x)
  1388. {
  1389. $records = \App\Models\Record::where('member_course_id', $x->id)->get();
  1390. foreach ($records as $record)
  1391. {
  1392. foreach ($record->rows as $row)
  1393. {
  1394. //if ($row->causal_id == $x->course->sub_causal_id || str_contains(strtolower($row->note), 'iscrizione'))
  1395. if (str_contains(strtolower($row->note), 'iscrizione'))
  1396. {
  1397. $row->causal_id = $x->course->sub_causal_id;
  1398. $row->save();
  1399. }
  1400. }
  1401. }
  1402. }
  1403. });
  1404. Route::get('/test_sms', function()
  1405. {
  1406. $expire_date = date("Y-m-d", strtotime("+1 month"));
  1407. $expire_date_it = date("d/m/Y", strtotime("+1 month"));
  1408. $certificates = \App\Models\MemberCertificate::where('expire_date', $expire_date)->get();
  1409. foreach($certificates as $certificate)
  1410. {
  1411. //$phone = $certificate->member->phone;
  1412. $phone = '3893163265';
  1413. $params = array(
  1414. 'to' => '+39' . $phone,
  1415. 'from' => env('SMS_FROM', 'Test'),
  1416. 'message' => $certificate->member->first_name . ', il tuo certificato medico scadrà il ' . $expire_date_it,
  1417. 'format' => 'json',
  1418. );
  1419. sms_send($params);
  1420. print "Inviato";
  1421. }
  1422. /*
  1423. $params = array(
  1424. 'to' => '+393893163265',
  1425. 'from' => env('SMS_FROM', 'Test'),
  1426. 'message' => 'Prova SMS da IAO',
  1427. 'format' => 'json',
  1428. );
  1429. echo sms_send($params);
  1430. */
  1431. });