web.php 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  1. <?php
  2. use Illuminate\Support\Facades\Route;
  3. use Barryvdh\DomPDF\Facade\Pdf;
  4. /*
  5. |--------------------------------------------------------------------------
  6. | Web Routes
  7. |--------------------------------------------------------------------------
  8. |
  9. | Here is where you can register web routes for your application. These
  10. | routes are loaded by the RouteServiceProvider within a group which
  11. | contains the "web" middleware group. Now create something great!
  12. |
  13. */
  14. Route::get('/', function () {
  15. return view('login');
  16. // return Redirect::to('/dashboard');
  17. });
  18. //Route::get('/', \App\Http\Livewire\Login::class);
  19. Route::get('/dashboard', \App\Http\Livewire\Dashboard::class);
  20. Route::get('/settings', \App\Http\Livewire\Setting::class);
  21. Route::get('/courses', \App\Http\Livewire\Course::class);
  22. Route::get('/categories', \App\Http\Livewire\Category::class);
  23. Route::get('/nations_list', \App\Http\Livewire\Nation::class);
  24. Route::get('/provinces', \App\Http\Livewire\Province::class);
  25. Route::get('/cities', \App\Http\Livewire\City::class);
  26. Route::get('/banks', \App\Http\Livewire\Bank::class);
  27. Route::get('/vats', \App\Http\Livewire\Vat::class);
  28. Route::get('/disciplines', \App\Http\Livewire\Discipline::class);
  29. Route::get('/course_types', \App\Http\Livewire\CourseType::class);
  30. Route::get('/course_subscriptions', \App\Http\Livewire\CourseSubscription::class);
  31. Route::get('/course_durations', \App\Http\Livewire\CourseDuration::class);
  32. Route::get('/course_levels', \App\Http\Livewire\CourseLevel::class);
  33. Route::get('/course_frequencies', \App\Http\Livewire\CourseFrequency::class);
  34. Route::get('/course_list', \App\Http\Livewire\CourseList::class);
  35. Route::get('/course_member', \App\Http\Livewire\CourseMember::class);
  36. Route::get('/receipts', \App\Http\Livewire\Receipt::class);
  37. Route::get('/cards', \App\Http\Livewire\Card::class);
  38. Route::get('/causals', \App\Http\Livewire\Causal::class);
  39. Route::get('/payment_methods', \App\Http\Livewire\PaymentMethod::class);
  40. Route::get('/members', \App\Http\Livewire\Member::class);
  41. Route::get('/suppliers', \App\Http\Livewire\Supplier::class);
  42. Route::get('/sponsors', \App\Http\Livewire\Sponsor::class);
  43. Route::get('/records', \App\Http\Livewire\Record::class);
  44. Route::get('/reminders', \App\Http\Livewire\Reminder::class);
  45. Route::get('/in', \App\Http\Livewire\RecordIN::class);
  46. Route::get('/out', \App\Http\Livewire\RecordOUT::class);
  47. Route::get('/records_in_out', \App\Http\Livewire\RecordINOUT::class);
  48. Route::get('/receipt/{id}', function($id){
  49. $receipt = \App\Models\Receipt::findOrFail($id);
  50. $pdf = PDF::loadView('receipt', array('receipt' => $receipt));
  51. $pdfName = "Ricevuta_" . $receipt->member->last_name . "_" . $receipt->number . "_" . $receipt->year . ".pdf";
  52. return $pdf->stream($pdfName);
  53. /*return response()->streamDownload(
  54. fn () => print($pdf),
  55. "ricevuta_" . $receipt->number . "_" . $receipt->year . ".pdf"
  56. );*/
  57. });
  58. Route::get('/receipt/mail/{id}', function($id){
  59. $receipt = \App\Models\Receipt::findOrFail($id);
  60. if ($receipt->status == 99)
  61. sendReceiptDeleteEmail($receipt);
  62. else
  63. sendReceiptEmail($receipt);
  64. /*
  65. $pdf = PDF::loadView('receipt', array('receipt' => $receipt));
  66. $pdfName = "ricevuta_" . $receipt->number . "_" . $receipt->year . ".pdf";
  67. Storage::put('public/pdf/' . $pdfName, $pdf->output());
  68. $email = \App\Models\Member::findOrFail($receipt->member_id)->email;
  69. if ($email != '')
  70. {
  71. Mail::to($email)->send(new \App\Mail\ReceipEmail([
  72. 'name' => 'Luca',
  73. 'pdf' => 'public/pdf/' . $pdfName,
  74. 'number' => $receipt->number . "/" . $receipt->year
  75. ]));
  76. }
  77. */
  78. return true;
  79. //return $pdf->stream();
  80. /*return response()->streamDownload(
  81. fn () => print($pdf),
  82. "ricevuta_" . $receipt->number . "_" . $receipt->year . ".pdf"
  83. );*/
  84. });
  85. Route::get('/nations', function(){
  86. if (isset($_GET["q"]))
  87. $datas = \App\Models\Nation::where('name', 'like', $_GET["q"] . '%')->orderBy('name')->get();
  88. else
  89. $datas = \App\Models\Nation::orderBy('name')->get();
  90. $data = array();
  91. foreach($datas as $d)
  92. {
  93. $data[] = array("id" => $d->id, "text" => $d->name);
  94. }
  95. return array("results" => $data);
  96. });
  97. Route::get('/provinces/{nation_id}', function($nation_id){
  98. if (isset($_GET["q"]))
  99. $datas = \App\Models\Province::where('nation_id', $nation_id)->where('name', 'like', $_GET["q"] . '%')->orderBy('name')->get();
  100. else
  101. $datas = \App\Models\Province::where('nation_id', $nation_id)->orderBy('name')->get();
  102. $data = array();
  103. foreach($datas as $d)
  104. {
  105. $data[] = array("id" => $d->id, "text" => $d->name);
  106. }
  107. return array("results" => $data);
  108. });
  109. Route::get('/cities/{province_id}', function($province_id){
  110. if (isset($_GET["q"]))
  111. $datas = \App\Models\City::where('province_id', $province_id)->where('name', 'like', $_GET["q"] . '%')->orderBy('name')->get();
  112. else
  113. $datas = \App\Models\City::where('province_id', $province_id)->orderBy('name')->get();
  114. $data = array();
  115. foreach($datas as $d)
  116. {
  117. $data[] = array("id" => $d->id, "text" => $d->name);
  118. }
  119. return array("results" => $data);
  120. });
  121. Route::get('/get_members', function(){
  122. $datas = [];
  123. // $datas = \App\Models\Member::select('members.*')->where('id', '>', 0);
  124. $x = \App\Models\Member::select('id', 'first_name', 'last_name', 'phone', 'birth_date', 'to_complete')->where('id', '>', 0);
  125. if (isset($_GET["search"]["value"]))
  126. {
  127. $v = str_replace("'", "''", stripcslashes($_GET["search"]["value"]));
  128. $x = $x->where(function ($query) use ($v) {
  129. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $v . "%'")
  130. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $v . "%'");
  131. });
  132. //where('first_name', 'like', '%' . $_GET["search"]["value"] . '%');
  133. }
  134. if ($_GET["cards"] != "")
  135. {
  136. $card_ids = \App\Models\MemberCard::whereIn('card_id', explode(",", $_GET["cards"]))->pluck('member_id');
  137. $x = $x->whereIn('id', $card_ids);
  138. }
  139. if ($_GET["filterCategories"] != "null")
  140. {
  141. $cats_ids = \App\Models\MemberCategory::whereIn('category_id', explode(",", $_GET["filterCategories"]))->pluck('member_id');
  142. $x = $x->whereIn('id', $cats_ids);
  143. }
  144. if ($_GET["fromYear"] != "")
  145. {
  146. $x = $x->where('birth_date', '<', date("Y-m-d", strtotime("-" . $_GET["fromYear"] . " year", time())));
  147. }
  148. if ($_GET["toYear"] != "")
  149. {
  150. $x = $x->where('birth_date', '>', date("Y-m-d", strtotime("-" . $_GET["toYear"] . " year", time())));
  151. }
  152. if ($_GET["fromYearYear"] != "")
  153. {
  154. $x = $x->whereYear('birth_date', '>=', $_GET["fromYearYear"]);
  155. }
  156. if ($_GET["toYearYear"] != "")
  157. {
  158. $x = $x->whereYear('birth_date', '<=', $_GET["toYearYear"]);
  159. }
  160. $ids = [];
  161. if ($_GET["filterCertificateType"] != "null")
  162. {
  163. $types = \App\Models\MemberCertificate::where('type', $_GET["filterCertificateType"])->pluck('member_id');
  164. $x = $x->whereIn('id', $types->toArray());;
  165. //$ids = array_merge($ids, $types->toArray());
  166. }
  167. if ($_GET["filterScadenza"] != "null")
  168. {
  169. if ($_GET["filterScadenza"] == "1")
  170. $scad = \App\Models\MemberCertificate::where('expire_date', '<', date("Y-m-d"))->pluck('member_id');
  171. if ($_GET["filterScadenza"] == "2")
  172. $scad = \App\Models\MemberCertificate::whereBetween('expire_date', [date("Y-m-d"), date("Y-m-d", strtotime("+1 month"))])->pluck('member_id');
  173. //$ids = array_merge($ids, $scad->toArray());
  174. $x = $x->whereIn('id', $scad->toArray());;
  175. //$x = $x->whereIn('id', $scadenza);
  176. }
  177. if ($_GET["filterStatus"] != "null")
  178. {
  179. $status = explode(",", $_GET["filterStatus"]);
  180. $members = \App\Models\Member::all();
  181. foreach($status as $s)
  182. {
  183. foreach($members as $m)
  184. {
  185. $state = $m->isActive();
  186. if ($state["status"] == $s)
  187. $ids[] = $m->id;
  188. }
  189. }
  190. }
  191. if (sizeof($ids) > 0)
  192. {
  193. $x = $x->whereIn('id', $ids);
  194. }
  195. else
  196. {
  197. if ($_GET["filterStatus"] != "null")
  198. $x = $x->whereIn('id', [-1]);
  199. }
  200. $count = $x->count();
  201. $x = $x->orderBy('to_complete', 'DESC');
  202. if (isset($_GET["order"]))
  203. {
  204. $column = '';
  205. if ($_GET["order"][0]["column"] == 0)
  206. $column = 'last_name';
  207. if ($_GET["order"][0]["column"] == 1)
  208. $column = 'first_name';
  209. if ($_GET["order"][0]["column"] == 2)
  210. $column = 'phone';
  211. if ($_GET["order"][0]["column"] == 3)
  212. $column = 'birth_date';
  213. if ($_GET["order"][0]["column"] == 4)
  214. $column = 'birth_date';
  215. if ($column != '')
  216. $x = $x->orderBy($column, $_GET["order"][0]["dir"]);
  217. else
  218. $x = $x->orderBy('last_name', 'ASC')->orderBy('first_name', 'ASC');
  219. }
  220. else
  221. $x = $x->orderBy('last_name', 'ASC')->orderBy('first_name', 'ASC');
  222. if (isset($_GET["start"]))
  223. $x = $x->offset($_GET["start"])->limit($_GET["length"])->get();
  224. else
  225. $x = $x->get();
  226. foreach($x as $idx => $r)
  227. {
  228. $status = $r->getStatus();
  229. $status = $status["status"];
  230. $class = $status > 0 ? ($status == 2 ? 'active' : 'due') : 'suspended';
  231. $text = $status > 0 ? ($status == 2 ? 'Tesserato' : 'Sospeso') : 'Non tesserato';
  232. if ($r->to_complete)
  233. {
  234. $text = 'Da completare';
  235. $class = "complete";
  236. }
  237. $has_certificate = $r->hasCertificate();
  238. $y = '';
  239. if($has_certificate["date"] != '')
  240. {
  241. if($has_certificate["date"] < date("Y-m-d"))
  242. $y .= '0';
  243. if($has_certificate["date"] >= date("Y-m-d") && $has_certificate["date"] < date("Y-m-d", strtotime("+1 month")))
  244. $y .= '1';
  245. if($has_certificate["date"] >= date("Y-m-d", strtotime("+1 month")))
  246. $y .= '2';
  247. $y .= '|';
  248. $y .= $has_certificate["date"] != '' ? date("d/m/Y", strtotime($has_certificate["date"])) : '';
  249. }
  250. $datas[] = array(
  251. //'c' => $idx + 1,
  252. //'id' => "ID" . str_pad($r->id, 5, "0", STR_PAD_LEFT),
  253. 'last_name' => $r->last_name . "|" . $r->id,
  254. 'first_name' => $r->first_name . "|" . $r->id,
  255. 'phone' => $r->phone,
  256. 'age' => $r->getAge(),
  257. 'year' => date("Y", strtotime($r->birth_date)),
  258. 'status' => $class . "|" . $text,
  259. // 'state' => $x,
  260. 'certificate' => $y,
  261. 'action' => $r->id
  262. );
  263. }
  264. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count));
  265. });
  266. Route::get('/get_record_in', function(){
  267. $datas = [];
  268. $x = \App\Models\Record::select('records.*', \DB::raw('members.first_name as first_name'), \DB::raw('members.last_name as last_name'), \DB::raw('payment_methods.name as payment')) // , \DB::raw('SUM(records.id) As total'))
  269. ->leftJoin('members', 'records.member_id', '=', 'members.id')
  270. ->leftJoin('payment_methods', 'records.payment_method_id', '=', 'payment_methods.id')
  271. ->where('records.type', 'IN');
  272. if (isset($_GET["search"]["value"]))
  273. {
  274. $v = str_replace("'", "''", stripcslashes($_GET["search"]["value"]));
  275. $x = $x->where(function ($query) use ($v) {
  276. $query->where('first_name', 'like', '%' . $v . '%')
  277. ->orWhere('last_name', 'like', '%' . $v . '%');
  278. });
  279. //where('first_name', 'like', '%' . $_GET["search"]["value"] . '%');
  280. }
  281. //$x = $x->where(function ($query) use ($v) {
  282. // $datas = \App\Models\Record::where('type', 'IN')->with('member', 'payment_method');
  283. if ($_GET["filterCommercial"] == 1)
  284. {
  285. $x = $x->where('commercial', true );
  286. }
  287. if ($_GET["filterCommercial"] == 2)
  288. {
  289. $x = $x->where('commercial', false);
  290. }
  291. if ($_GET["filterMember"] > 0)
  292. {
  293. $x = $x->where('member_id', $_GET["filterMember"]);
  294. }
  295. if ($_GET["filterPaymentMethod"] != "null")
  296. {
  297. $payments = explode(",", $_GET["filterPaymentMethod"]);
  298. $x = $x->whereIn('payment_method_id', $payments);
  299. }
  300. if ($_GET["filterCausals"] != "null")
  301. {
  302. $causals = explode(",", $_GET["filterCausals"]);
  303. //$causals = \App\Models\RecordRow::where('causal_id', $_GET["filterCausals"])->pluck('record_id');
  304. $causals = \App\Models\RecordRow::whereIn('causal_id', $causals)->pluck('record_id');
  305. $x = $x->whereIn('records.id', $causals);
  306. }
  307. if ($_GET["filterFrom"] != '')
  308. {
  309. $x = $x->where('date', '>=', $_GET["filterFrom"]);
  310. }
  311. if ($_GET["filterTo"] != '')
  312. {
  313. $x = $x->where('date', '<=', $_GET["filterTo"]);
  314. }
  315. //});
  316. $start = 0;
  317. $limit = 100000;
  318. if (isset($_GET["start"]))
  319. {
  320. $start = $_GET["start"];
  321. $limit = $_GET["length"];
  322. }
  323. $excludeCausals = [];
  324. /*$borsellino = \App\Models\Causal::where('money', true)->first();
  325. if ($borsellino)
  326. $excludeCausals[] = $borsellino->id;*/
  327. // Aggiungo
  328. $excludes = \App\Models\Causal::where('no_records', true)->get();
  329. foreach($excludes as $e)
  330. {
  331. $excludeCausals[] = $e->id;
  332. }
  333. $exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
  334. // Pagamento money
  335. $moneys = \App\Models\PaymentMethod::where('money', true)->pluck('id')->toArray();
  336. // Causale money
  337. $moneysCausal = \App\Models\Causal::where('money', true)->pluck('id')->toArray();
  338. $total = 0;
  339. /*
  340. foreach($x->get() as $r)
  341. {
  342. if (!in_array($r->payment_method_id, $moneys))
  343. {
  344. foreach($r->rows as $rr)
  345. {
  346. if ((!in_array($rr->member_id, $exclude_from_records) || in_array($r->causal_id, $moneysCausal)) && (!$r->deleted || $r->deleted == null) && (!in_array($rr->causal_id, $excludeCausals) || in_array($r->causal_id, $moneysCausal)) && (!$r->financial_movement || $r->financial_movement == null) && (!$r->corrispettivo_fiscale || $r->corrispettivo_fiscale == null))
  347. {
  348. $total += $rr->amount;
  349. if ($rr->vat_id > 0)
  350. $total += getVatValue($rr->amount, $rr->vat_id);
  351. }
  352. }
  353. }
  354. }
  355. */
  356. $count = $x->count();
  357. if (isset($_GET["order"]))
  358. {
  359. $column = '';
  360. if ($_GET["order"][0]["column"] == 0)
  361. $column = 'date';
  362. if ($_GET["order"][0]["column"] == 1)
  363. $column = 'records.amount';
  364. if ($_GET["order"][0]["column"] == 2)
  365. $column = 'last_name';
  366. if ($_GET["order"][0]["column"] == 3)
  367. $column = 'first_name';
  368. if ($_GET["order"][0]["column"] == 4)
  369. $column = 'commercial';
  370. if ($column != '')
  371. $x = $x->orderBy($column, $_GET["order"][0]["dir"])->orderBy('records.id', 'DESC');
  372. else
  373. $x = $x->orderBy('records.id', 'DESC');
  374. }
  375. else
  376. $x = $x->orderBy('records.date', 'DESC')->orderBy('records.id', 'DESC');
  377. $x = $x->offset($start)->limit($limit)->get();
  378. foreach($x as $idx => $r)
  379. {
  380. $causals = '';
  381. foreach($r->rows as $row)
  382. {
  383. $causals .= $row->causal->getTree() . "<br>";
  384. }
  385. $datas[] = array(
  386. //'id' => $r->id,
  387. 'date' => $r->date,
  388. 'total' => formatPrice($r->getTotal()),
  389. 'first_name' => $r->first_name,
  390. 'last_name' => $r->last_name,
  391. 'commercial' => $r->financial_movement ? 'Movimento finanziario' : ($r->commercial ? 'SI' : 'NO'),
  392. 'causals' => $causals,
  393. 'payment' => $r->payment_method->name,
  394. 'status' => $r->deleted ? 'Annullato' : '',
  395. 'action' => $r->id . "|" . formatPrice($total) . "|" . ($r->deleted ? 'x' : '')
  396. );
  397. }
  398. /*$datas[] = array(
  399. //'id' => $r->id,
  400. 'date' => '',
  401. 'total' => formatPrice($total),
  402. 'first_name' => '',
  403. 'last_name' => '',
  404. 'commercial' => '',
  405. 'causals' => '',
  406. 'payment' => '',
  407. 'status' => '',
  408. 'action' => ''
  409. );*/
  410. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count));
  411. });
  412. Route::get('/get_record_out', function(){
  413. $datas = [];
  414. $x = \App\Models\Record::where('type', 'OUT')->with('supplier', 'payment_method');
  415. if ($_GET["filterSupplier"] > 0)
  416. {
  417. $x = $x->where('supplier_id', $_GET["filterSupplier"]);
  418. }
  419. /*if ($_GET["filterPaymentMethod"] > 0)
  420. {
  421. $x = $x->where('payment_method_id', $_GET["filterPaymentMethod"]);
  422. }
  423. if ($_GET["filterCausals"] > 0)
  424. {
  425. $causals = \App\Models\RecordRow::where('causal_id', $_GET["filterCausals"])->pluck('record_id');
  426. $x = $x->whereIn('records.id', $causals);
  427. }*/
  428. if ($_GET["filterPaymentMethod"] != "null")
  429. {
  430. $payments = explode(",", $_GET["filterPaymentMethod"]);
  431. $x = $x->whereIn('payment_method_id', $payments);
  432. }
  433. if ($_GET["filterCausals"] != "null")
  434. {
  435. $causals = explode(",", $_GET["filterCausals"]);
  436. //$causals = \App\Models\RecordRow::where('causal_id', $_GET["filterCausals"])->pluck('record_id');
  437. $causals = \App\Models\RecordRow::whereIn('causal_id', $causals)->pluck('record_id');
  438. $x = $x->whereIn('records.id', $causals);
  439. }
  440. if ($_GET["filterFrom"] != '')
  441. {
  442. $x = $x->where('date', '>=', $_GET["filterFrom"]);
  443. }
  444. if ($_GET["filterTo"] != '')
  445. {
  446. $x = $x->where('date', '<=', $_GET["filterTo"]);
  447. }
  448. $total = 0;
  449. foreach($x->get() as $r)
  450. {
  451. foreach($r->rows as $rr)
  452. {
  453. $total += $rr->amount;
  454. if ($rr->vat_id > 0)
  455. $total += getVatValue($rr->amount, $rr->vat_id);
  456. }
  457. }
  458. $x = $x->get();
  459. foreach($x as $idx => $r)
  460. {
  461. $causals = '';
  462. foreach($r->rows as $row)
  463. {
  464. $causals .= $row->causal->getTree() . "<br>";
  465. }
  466. $datas[] = array(
  467. //'id' => $r->id,
  468. 'date' => $r->date,
  469. 'total' => formatPrice($r->getTotal()),
  470. 'supplier' => $r->supplier->name,
  471. 'causals' => $causals,
  472. 'payment' => $r->payment_method->name,
  473. 'action' => $r->id . "|" . formatPrice($total)
  474. );
  475. }
  476. /*
  477. $datas[] = array(
  478. //'id' => $r->id,
  479. 'date' => '',
  480. 'total' => formatPrice($total),
  481. 'supplier' => '',
  482. 'causals' => '',
  483. 'payment' => '',
  484. 'action' => ''
  485. );
  486. */
  487. return json_encode(array("data" => $datas));
  488. });
  489. Route::get('/get_course_list', function(){
  490. $member_course = \App\Models\MemberCourse::with('member');
  491. if (isset($_GET["search"]["value"]))
  492. {
  493. $v = str_replace("'", "''", stripcslashes($_GET["search"]["value"]));
  494. $member_ids = \App\Models\Member::where(function ($query) use ($v) {
  495. $query->where('first_name', 'like', '%' . $v . '%')
  496. ->orWhere('last_name', 'like', '%' . $v . '%');
  497. })->pluck('id');
  498. $member_course = $member_course->whereIn('member_id', $member_ids);
  499. }
  500. if ($_GET["filterCourse"] != "null")
  501. {
  502. $course_ids = [];
  503. $courses = explode(",", $_GET["filterCourse"]);
  504. foreach($courses as $c)
  505. {
  506. $all = \App\Models\Course::where('name', 'like', '%' . $c . "%")->get();
  507. foreach($all as $a)
  508. {
  509. $course_ids[] = $a->id;
  510. }
  511. }
  512. $member_course = $member_course->whereIn('course_id', $course_ids);
  513. }
  514. if ($_GET["filterLevel"] != "null")
  515. {
  516. $levels = explode(",", $_GET["filterLevel"]);
  517. $course_ids = \App\Models\Course::whereIn('course_level_id', $levels)->pluck('id');
  518. $member_course = $member_course->whereIn('course_id', $course_ids);
  519. }
  520. if ($_GET["filterFrequency"] != "null")
  521. {
  522. $frequencies = explode(",", $_GET["filterFrequency"]);
  523. $course_ids = \App\Models\Course::whereIn('course_type_id', $frequencies)->pluck('id');
  524. $member_course = $member_course->whereIn('course_id', $course_ids);
  525. }
  526. if ($_GET["filterType"] != "null")
  527. {
  528. $types = explode(",", $_GET["filterType"]);
  529. $course_ids = \App\Models\Course::whereIn('course_frequency_id', $types)->pluck('id');
  530. $member_course = $member_course->whereIn('course_id', $course_ids);
  531. }
  532. if ($_GET["filterDuration"] != "null")
  533. {
  534. $durations = explode(",", $_GET["filterDuration"]);
  535. $course_ids = \App\Models\Course::whereIn('course_duration_id', $durations)->pluck('id');
  536. $member_course = $member_course->whereIn('course_id', $course_ids);
  537. }
  538. $totals = [];
  539. $totalIsc = [];
  540. $member_course_totals = $member_course->get();
  541. foreach($member_course_totals as $x)
  542. {
  543. $price = 0;
  544. $price = $x->course->price;
  545. $subPrice = $x->course->subscription_price;
  546. $records = \App\Models\Record::where('member_course_id', $x->id)->where('deleted', 0)->get();
  547. $prices = [];
  548. foreach ($records as $record)
  549. {
  550. foreach ($record->rows as $row)
  551. {
  552. //if ($row->causal_id == $x->course->sub_causal_id || str_contains(strtolower($row->note), 'iscrizione'))
  553. if (str_contains(strtolower($row->note), 'iscrizione'))
  554. {
  555. $subPrice = $row->amount;
  556. }
  557. if ($row->causal_id == $x->course->causal_id && !str_contains(strtolower($row->note), 'iscrizione'))
  558. {
  559. $tot = sizeof(json_decode($row->when));
  560. foreach(json_decode($row->when) as $m)
  561. {
  562. $prices[$m->month] = $row->amount / $tot;
  563. }
  564. }
  565. }
  566. }
  567. for($i=1; $i<=12; $i++)
  568. {
  569. $cls = getColor($x->months, $i);
  570. if ($cls != 'grey')
  571. {
  572. if (!isset($totals[$i]))
  573. {
  574. $totals[$i]['green'] = 0;
  575. $totals[$i]['orange'] = 0;
  576. $totals[$i]['yellow'] = 0;
  577. }
  578. if ($cls == 'yellow')
  579. {
  580. $totals[$i][$cls] += 1;
  581. }
  582. else
  583. {
  584. $p = isset($prices[$i]) ? $prices[$i] : $price;
  585. $totals[$i][$cls] += $p;
  586. }
  587. }
  588. }
  589. $sub = $x->subscribed ? "Y" : "N";
  590. if (isset($totalIsc[$sub]))
  591. $totalIsc[$sub] += $subPrice;
  592. else
  593. $totalIsc[$sub] = $subPrice;
  594. }
  595. $js = '';
  596. $xx = 2;
  597. $str = '';
  598. $str .= "<a class=green><small>" . (isset($totalIsc["Y"]) ? formatPrice($totalIsc["Y"]) : 0) . "</small></a><br>";
  599. $str .= "<a class=orange><small>" . (isset($totalIsc["N"]) ? formatPrice($totalIsc["N"]) : 0) . "</small></a><br>";
  600. $str .= "<a class=yellow><small>0</small></a><br>";
  601. $js .= "2§" . $str . "_";
  602. $str = "";
  603. foreach($totals as $z => $t)
  604. {
  605. if ($z == 1) $xx = 7;
  606. if ($z == 2) $xx = 8;
  607. if ($z == 3) $xx = 9;
  608. if ($z == 4) $xx = 10;
  609. if ($z == 5) $xx = 11;
  610. if ($z == 6) $xx = 12;
  611. if ($z == 7) $xx = 13;
  612. if ($z == 8) $xx = 14;
  613. if ($z == 9) $xx = 3;
  614. if ($z == 10) $xx = 4;
  615. if ($z == 11) $xx = 5;
  616. if ($z == 12) $xx = 6;
  617. $str = '';
  618. foreach($t as $x => $c)
  619. {
  620. $y = $x == 'yellow' ? $c : formatPrice($c);
  621. $str .= "<a class=" . $x . "><small>" . $y . "</small></a><br>";
  622. }
  623. $js .= $xx . "§" . $str . "_";
  624. $xx += 1;
  625. }
  626. $count = $member_course->count();
  627. //$member_course = $member_course->where('course_id', 999999);
  628. $start = 0;
  629. $limit = 100000;
  630. if (isset($_GET["start"]))
  631. {
  632. $start = $_GET["start"];
  633. $limit = $_GET["length"];
  634. }
  635. $member_course = $member_course->offset($start)->limit($limit)->get();
  636. $datas = [];
  637. foreach($member_course as $x)
  638. {
  639. $price = 0;
  640. $price = $x->course->price;
  641. $subPrice = $x->course->subscription_price;
  642. $records = \App\Models\Record::where('member_course_id', $x->id)->where('deleted', 0)->get();
  643. $prices = [];
  644. foreach ($records as $record)
  645. {
  646. foreach ($record->rows as $row)
  647. {
  648. //if ($row->causal_id == $x->course->sub_causal_id || str_contains(strtolower($row->note), 'iscrizione'))
  649. if (str_contains(strtolower($row->note), 'iscrizione'))
  650. {
  651. $subPrice = $row->amount;
  652. }
  653. if ($row->causal_id == $x->course->causal_id && !str_contains(strtolower($row->note), 'iscrizione'))
  654. {
  655. $tot = sizeof(json_decode($row->when));
  656. foreach(json_decode($row->when) as $m)
  657. {
  658. $prices[$m->month] = $row->amount / $tot;
  659. }
  660. }
  661. }
  662. }
  663. for($i=1; $i<=12; $i++)
  664. {
  665. $cls = getColor($x->months, $i);
  666. if ($cls != 'grey')
  667. {
  668. if (!isset($totals[$i]))
  669. {
  670. $totals[$i]['green'] = 0;
  671. $totals[$i]['orange'] = 0;
  672. $totals[$i]['yellow'] = 0;
  673. }
  674. if ($cls == 'yellow')
  675. {
  676. $totals[$i][$cls] += 1;
  677. }
  678. else
  679. {
  680. $p = isset($prices[$i]) ? $prices[$i] : $price;
  681. $totals[$i][$cls] += $p;
  682. }
  683. }
  684. }
  685. $sub = $x->subscribed ? "Y" : "N";
  686. if (isset($totalIsc[$sub]))
  687. $totalIsc[$sub] += $subPrice;
  688. else
  689. $totalIsc[$sub] = $subPrice;
  690. $datas[] = array(
  691. "column_0" => $x->member->last_name,
  692. "column_1" => $x->member->first_name,
  693. "column_2" => $x->subscribed . "§" . formatPrice($subPrice),
  694. "column_3" => getColor($x->months, 9) . "§" . formatPrice(isset($prices[9]) ? $prices[9] : $price),
  695. "column_4" => getColor($x->months, 10) . "§" . formatPrice(isset($prices[10]) ? $prices[10] : $price),
  696. "column_5" => getColor($x->months, 11) . "§" . formatPrice(isset($prices[11]) ? $prices[11] : $price),
  697. "column_6" => getColor($x->months, 12) . "§" . formatPrice(isset($prices[12]) ? $prices[12] : $price),
  698. "column_7" => getColor($x->months, 1) . "§" . formatPrice(isset($prices[1]) ? $prices[1] : $price),
  699. "column_8" => getColor($x->months, 2) . "§" . formatPrice(isset($prices[2]) ? $prices[2] : $price),
  700. "column_9" => getColor($x->months, 3) . "§" . formatPrice(isset($prices[3]) ? $prices[3] : $price),
  701. "column_10" => getColor($x->months, 4) . "§" . formatPrice(isset($prices[4]) ? $prices[4] : $price),
  702. "column_11" => getColor($x->months, 5) . "§" . formatPrice(isset($prices[5]) ? $prices[5] : $price),
  703. "column_12" => getColor($x->months, 6) . "§" . formatPrice(isset($prices[6]) ? $prices[6] : $price),
  704. "column_13" => getColor($x->months, 7) . "§" . formatPrice(isset($prices[7]) ? $prices[7] : $price),
  705. "column_14" => getColor($x->months, 8) . "§" . formatPrice(isset($prices[8]) ? $prices[8] : $price),
  706. "column_15" => $x->course_id,
  707. "column_16" => $x->id,
  708. "column_17" => $x->member_id
  709. );
  710. }
  711. // Sort data
  712. /*
  713. if (isset($_GET["order"]))
  714. array_multisort(array_column($datas, 'column_' . $_GET["order"][0]["column"]), $_GET["order"][0]["dir"] == "asc" ? SORT_ASC : SORT_DESC, SORT_NATURAL|SORT_FLAG_CASE, $datas);
  715. else
  716. array_multisort(array_column($datas, 'column_0'), SORT_DESC, SORT_NATURAL|SORT_FLAG_CASE, $datas);
  717. //$x = $x->orderBy('records.date', 'DESC')->orderBy('records.id', 'DESC');
  718. */
  719. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count, "totals" => $js));
  720. });
  721. Route::get('/get_course_members', function(){
  722. //$datas = \App\Models\MemberCourse::with('member');
  723. $datas = \App\Models\MemberCourse::select('member_courses.*', 'members.first_name', 'members.last_name', 'members.email', 'members.phone', 'members.birth_date')->leftJoin('members', 'member_courses.member_id', '=', 'members.id');
  724. if ($_GET["filterCourse"] != "null")
  725. {
  726. $course_ids = [];
  727. $courses = explode(",", $_GET["filterCourse"]);
  728. foreach($courses as $c)
  729. {
  730. $all = \App\Models\Course::where('name', 'like', '%' . $c . "%")->get();
  731. foreach($all as $a)
  732. {
  733. $course_ids[] = $a->id;
  734. }
  735. }
  736. $datas = $datas->whereIn('course_id', $course_ids);
  737. }
  738. if ($_GET["filterLevel"] != "null")
  739. {
  740. $levels = explode(",", $_GET["filterLevel"]);
  741. $course_ids = \App\Models\Course::whereIn('course_level_id', $levels)->pluck('id');
  742. $datas = $datas->whereIn('course_id', $course_ids);
  743. }
  744. if ($_GET["filterFrequency"] != "null")
  745. {
  746. $frequencies = explode(",", $_GET["filterFrequency"]);
  747. $course_ids = \App\Models\Course::whereIn('course_type_id', $frequencies)->pluck('id');
  748. $datas = $datas->whereIn('course_id', $course_ids);
  749. }
  750. if ($_GET["filterType"] != "null")
  751. {
  752. $types = explode(",", $_GET["filterType"]);
  753. $course_ids = \App\Models\Course::whereIn('course_frequency_id', $types)->pluck('id');
  754. $datas = $datas->whereIn('course_id', $course_ids);
  755. }
  756. if ($_GET["filterDuration"] != "null")
  757. {
  758. $durations = explode(",", $_GET["filterDuration"]);
  759. $course_ids = \App\Models\Course::whereIn('course_duration_id', $durations)->pluck('id');
  760. $datas = $datas->whereIn('course_id', $course_ids);
  761. }
  762. if ($_GET["filterDays"] != "null")
  763. {
  764. $ids = [];
  765. $days = explode(",", $_GET["filterDays"]);
  766. foreach($days as $d)
  767. {
  768. $all = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")->get();
  769. foreach($all as $a)
  770. {
  771. $ids[] = $a->id;
  772. }
  773. }
  774. $datas = $datas->whereIn('id', $ids);
  775. }
  776. if ($_GET["filterHours"] != "null")
  777. {
  778. $ids = [];
  779. $hours = explode(",", $_GET["filterHours"]);
  780. foreach($hours as $h)
  781. {
  782. $all = \App\Models\MemberCourse::where('when', 'like', '%"from":"' . $h . "%")->get();
  783. foreach($all as $a)
  784. {
  785. $ids[] = $a->id;
  786. }
  787. }
  788. $datas = $datas->whereIn('member_courses.id', $ids);
  789. }
  790. if ($_GET["filterSubscription"] != "")
  791. {
  792. $ids = \App\Models\MemberCourse::where('subscribed', $_GET["filterSubscription"] == 1 ? true : false)->pluck('id');
  793. $datas = $datas->whereIn('member_courses.id', $ids);
  794. //$this->filter .= $this->filter != '' ? ', ' : '';
  795. //$this->filter .= "Pagata sottoscrizione : " . ($this->filterSubscription == 1 ? "SI" : "NO") . " ";
  796. }
  797. if ($_GET["filterCertificateType"] != "null")
  798. {
  799. $types = \App\Models\MemberCertificate::where('type', $_GET["filterCertificateType"])->pluck('member_id');
  800. $datas = $datas->whereIn('member_id', $types);
  801. }
  802. if ($_GET["filterCertificateScadenza"] != "null")
  803. {
  804. if ($_GET["filterCertificateScadenza"] == "1")
  805. $scad = \App\Models\MemberCertificate::where('expire_date', '<', date("Y-m-d"))->pluck('member_id');
  806. if ($_GET["filterCertificateScadenza"] == "2")
  807. $scad = \App\Models\MemberCertificate::whereBetween('expire_date', [date("Y-m-d"), date("Y-m-d", strtotime("+1 month"))])->pluck('member_id');
  808. $datas = $datas->whereIn('member_id', $scad);
  809. }
  810. if ($_GET["fromYear"] != "")
  811. {
  812. $m_ids = \App\Models\Member::where('birth_date', '<', date("Y-m-d", strtotime("-" . $_GET["fromYear"] . " year", time())))->pluck('id');
  813. $datas = $datas->whereIn('member_id', $m_ids);
  814. }
  815. if ($_GET["toYear"] != "")
  816. {
  817. $m_ids = \App\Models\Member::where('birth_date', '>', date("Y-m-d", strtotime("-" . $_GET["toYear"] . " year", time())))->pluck('id');
  818. $datas = $datas->whereIn('member_id', $m_ids);
  819. }
  820. if ($_GET["fromFromYear"] != "")
  821. {
  822. $m_ids = \App\Models\Member::whereYear('birth_date', '>=', $_GET["fromFromYear"])->pluck('id');
  823. $datas = $datas->whereIn('member_id', $m_ids);
  824. }
  825. if ($_GET["toToYear"] != "")
  826. {
  827. $m_ids = \App\Models\Member::whereYear('birth_date', '<=', $_GET["toToYear"])->pluck('id');
  828. $datas = $datas->whereIn('member_id', $m_ids);
  829. }
  830. if ($_GET["filterCards"] != "null")
  831. {
  832. $card_ids = \App\Models\MemberCard::whereIn('card_id', $_GET["filterCards"])->pluck('member_id');
  833. $datas = $datas->whereIn('member_id', $card_ids);
  834. }
  835. if ($_GET["filterYear"] != "")
  836. {
  837. $course_ids = \App\Models\Course::where('year', $_GET["filterYear"])->pluck('id');
  838. $datas = $datas->whereIn('course_id', $course_ids);
  839. //$this->filter .= $this->filter != '' ? ', ' : '';
  840. //$this->filter .= "Anno : " . $this->filterYear . " ";
  841. }
  842. $aRet = [];
  843. if (isset($_GET["order"]))
  844. {
  845. $column = '';
  846. if ($_GET["order"][0]["column"] == 1)
  847. $column = 'last_name';
  848. if ($_GET["order"][0]["column"] == 2)
  849. $column = 'first_name';
  850. if ($_GET["order"][0]["column"] == 2)
  851. $column = 'birth_date';
  852. if ($_GET["order"][0]["column"] == 3)
  853. $column = 'birth_date';
  854. if ($_GET["order"][0]["column"] == 4)
  855. $column = 'birth_date';
  856. if ($_GET["order"][0]["column"] == 5)
  857. $column = 'phone';
  858. if ($_GET["order"][0]["column"] == 6)
  859. $column = 'email';
  860. if ($column != '')
  861. $datas = $datas->orderBy($column, $_GET["order"][0]["dir"]);
  862. else
  863. $datas = $datas->orderBy('last_name', 'ASC')->orderBy('first_name', 'ASC');
  864. }
  865. else
  866. $datas = $datas->orderBy('last_name', 'ASC')->orderBy('first_name', 'ASC');
  867. if ($_GET["filterStatus"] != "null")
  868. {
  869. $status = explode(",", $_GET["filterStatus"]);
  870. foreach($status as $s)
  871. {
  872. foreach($datas->get() as $aaa)
  873. {
  874. $state = \App\Models\Member::findOrFail($aaa->member_id)->isActive();
  875. if ($state["status"] == $s)
  876. $aRet[] = $aaa;
  877. }
  878. }
  879. }
  880. else
  881. $aRet = $datas->get();
  882. $ret = [];
  883. foreach($aRet as $idx => $r)
  884. {
  885. $date1 = new DateTime($r->birth_date);
  886. $date2 = new DateTime("now");
  887. $interval = $date1->diff($date2);
  888. $ret[] = array(
  889. "column_0" => $idx,
  890. "column_1" => $r->last_name,
  891. "column_2" => $r->first_name,
  892. "column_3" => strval($interval->y),
  893. "column_4" => date("Y", strtotime($r->birth_date)),
  894. "column_5" => $r->phone,
  895. "column_6" => $r->email,
  896. "column_7" => $r->member_id
  897. );
  898. }
  899. if (isset($_GET["start"]))
  900. $ret = array_slice($ret, $_GET["start"], $_GET["length"]);
  901. return json_encode(array("data" => $ret, "recordsTotal" => sizeof($aRet), "recordsFiltered" => sizeof($aRet)));
  902. });
  903. function getColor($months, $m)
  904. {
  905. $class = "grey";
  906. foreach(json_decode($months) as $mm)
  907. {
  908. if ($mm->m == $m)
  909. {
  910. if ($mm->status == "")
  911. {
  912. $class = "orange";
  913. }
  914. if ($mm->status == "1")
  915. {
  916. $class = "green";
  917. }
  918. if ($mm->status == "2")
  919. {
  920. $class = "yellow";
  921. }
  922. }
  923. }
  924. return $class;
  925. }
  926. Route::get('/migrate', function(){
  927. \Artisan::call('migrate');
  928. dd('migrated!');
  929. });