web.php 61 KB

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