web.php 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  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', 'current_status', 'certificate', 'certificate_date')->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 ($_GET["order"][0]["column"] == 5)
  216. $column = 'current_status';
  217. if ($_GET["order"][0]["column"] == 6)
  218. $column = 'certificate';
  219. if ($column != '')
  220. $x = $x->orderBy($column, $_GET["order"][0]["dir"]);
  221. else
  222. $x = $x->orderBy('last_name', 'ASC')->orderBy('first_name', 'ASC');
  223. }
  224. else
  225. $x = $x->orderBy('last_name', 'ASC')->orderBy('first_name', 'ASC');
  226. if (isset($_GET["start"]))
  227. $x = $x->offset($_GET["start"])->limit($_GET["length"])->get();
  228. else
  229. $x = $x->get();
  230. foreach($x as $idx => $r)
  231. {
  232. // $status = $r->getStatus();
  233. // $status = $status["status"];
  234. $status = $r->current_status;
  235. $class = $status > 0 ? ($status == 2 ? 'active' : 'due') : 'suspended';
  236. $text = $status > 0 ? ($status == 2 ? 'Tesserato' : 'Sospeso') : 'Non tesserato';
  237. if ($r->to_complete)
  238. {
  239. $text = 'Da completare';
  240. $class = "complete";
  241. }
  242. // $has_certificate = $r->hasCertificate();
  243. $y = '';
  244. if ($r->certificate_date != '')
  245. {
  246. $y = $r->certificate . "|" . date("d/m/Y", strtotime($r->certificate_date));
  247. }
  248. /*
  249. if($has_certificate["date"] != '')
  250. {
  251. if($has_certificate["date"] < date("Y-m-d"))
  252. $y .= '0';
  253. if($has_certificate["date"] >= date("Y-m-d") && $has_certificate["date"] < date("Y-m-d", strtotime("+1 month")))
  254. $y .= '1';
  255. if($has_certificate["date"] >= date("Y-m-d", strtotime("+1 month")))
  256. $y .= '2';
  257. $y .= '|';
  258. $y .= $has_certificate["date"] != '' ? date("d/m/Y", strtotime($has_certificate["date"])) : '';
  259. }*/
  260. $datas[] = array(
  261. //'c' => $idx + 1,
  262. //'id' => "ID" . str_pad($r->id, 5, "0", STR_PAD_LEFT),
  263. 'last_name' => $r->last_name . "|" . $r->id,
  264. 'first_name' => $r->first_name . "|" . $r->id,
  265. 'phone' => $r->phone,
  266. 'age' => $r->getAge(),
  267. 'year' => date("Y", strtotime($r->birth_date)),
  268. 'status' => $class . "|" . $text,
  269. // 'state' => $x,
  270. 'certificate' => $y,
  271. 'action' => $r->id
  272. );
  273. }
  274. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count));
  275. });
  276. Route::get('/get_record_in', function(){
  277. $datas = [];
  278. $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'))
  279. ->leftJoin('members', 'records.member_id', '=', 'members.id')
  280. ->leftJoin('payment_methods', 'records.payment_method_id', '=', 'payment_methods.id')
  281. ->where('records.type', 'IN');
  282. if (isset($_GET["search"]["value"]))
  283. {
  284. $v = str_replace("'", "''", stripcslashes($_GET["search"]["value"]));
  285. $x = $x->where(function ($query) use ($v) {
  286. $query->where('first_name', 'like', '%' . $v . '%')
  287. ->orWhere('last_name', 'like', '%' . $v . '%');
  288. });
  289. //where('first_name', 'like', '%' . $_GET["search"]["value"] . '%');
  290. }
  291. //$x = $x->where(function ($query) use ($v) {
  292. // $datas = \App\Models\Record::where('type', 'IN')->with('member', 'payment_method');
  293. if ($_GET["filterCommercial"] == 1)
  294. {
  295. $x = $x->where('commercial', true );
  296. }
  297. if ($_GET["filterCommercial"] == 2)
  298. {
  299. $x = $x->where('commercial', false);
  300. }
  301. if ($_GET["filterMember"] > 0)
  302. {
  303. $x = $x->where('member_id', $_GET["filterMember"]);
  304. }
  305. if ($_GET["filterPaymentMethod"] != "null")
  306. {
  307. $payments = explode(",", $_GET["filterPaymentMethod"]);
  308. $x = $x->whereIn('payment_method_id', $payments);
  309. }
  310. if ($_GET["filterCausals"] != "null")
  311. {
  312. $causals = explode(",", $_GET["filterCausals"]);
  313. //$causals = \App\Models\RecordRow::where('causal_id', $_GET["filterCausals"])->pluck('record_id');
  314. $causals = \App\Models\RecordRow::whereIn('causal_id', $causals)->pluck('record_id');
  315. $x = $x->whereIn('records.id', $causals);
  316. }
  317. if ($_GET["filterFrom"] != '')
  318. {
  319. $x = $x->where('date', '>=', $_GET["filterFrom"]);
  320. }
  321. if ($_GET["filterTo"] != '')
  322. {
  323. $x = $x->where('date', '<=', $_GET["filterTo"]);
  324. }
  325. //});
  326. $start = 0;
  327. $limit = 100000;
  328. if (isset($_GET["start"]))
  329. {
  330. $start = $_GET["start"];
  331. $limit = $_GET["length"];
  332. }
  333. $excludeCausals = [];
  334. /*$borsellino = \App\Models\Causal::where('money', true)->first();
  335. if ($borsellino)
  336. $excludeCausals[] = $borsellino->id;*/
  337. // Aggiungo
  338. $excludes = \App\Models\Causal::where('no_records', true)->get();
  339. foreach($excludes as $e)
  340. {
  341. $excludeCausals[] = $e->id;
  342. }
  343. $exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
  344. // Pagamento money
  345. $moneys = \App\Models\PaymentMethod::where('money', true)->pluck('id')->toArray();
  346. // Causale money
  347. $moneysCausal = \App\Models\Causal::where('money', true)->pluck('id')->toArray();
  348. $total = 0;
  349. /*
  350. foreach($x->get() as $r)
  351. {
  352. if (!in_array($r->payment_method_id, $moneys))
  353. {
  354. foreach($r->rows as $rr)
  355. {
  356. 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))
  357. {
  358. $total += $rr->amount;
  359. if ($rr->vat_id > 0)
  360. $total += getVatValue($rr->amount, $rr->vat_id);
  361. }
  362. }
  363. }
  364. }
  365. */
  366. $count = $x->count();
  367. if (isset($_GET["order"]))
  368. {
  369. $column = '';
  370. if ($_GET["order"][0]["column"] == 0)
  371. $column = 'date';
  372. if ($_GET["order"][0]["column"] == 1)
  373. $column = 'records.amount';
  374. if ($_GET["order"][0]["column"] == 2)
  375. $column = 'last_name';
  376. if ($_GET["order"][0]["column"] == 3)
  377. $column = 'first_name';
  378. if ($_GET["order"][0]["column"] == 4)
  379. $column = 'commercial';
  380. if ($column != '')
  381. $x = $x->orderBy($column, $_GET["order"][0]["dir"])->orderBy('records.id', 'DESC');
  382. else
  383. $x = $x->orderBy('records.id', 'DESC');
  384. }
  385. else
  386. $x = $x->orderBy('records.date', 'DESC')->orderBy('records.id', 'DESC');
  387. $x = $x->offset($start)->limit($limit)->get();
  388. foreach($x as $idx => $r)
  389. {
  390. $causals = '';
  391. foreach($r->rows as $row)
  392. {
  393. $causals .= $row->causal->getTree() . "<br>";
  394. }
  395. $datas[] = array(
  396. //'id' => $r->id,
  397. 'date' => $r->date,
  398. 'total' => formatPrice($r->getTotal()),
  399. 'first_name' => $r->first_name,
  400. 'last_name' => $r->last_name,
  401. 'commercial' => $r->financial_movement ? 'Movimento finanziario' : ($r->commercial ? 'SI' : 'NO'),
  402. 'causals' => $causals,
  403. 'payment' => $r->payment_method->name,
  404. 'status' => $r->deleted ? 'Annullato' : '',
  405. 'action' => $r->id . "|" . formatPrice($total) . "|" . ($r->deleted ? 'x' : '')
  406. );
  407. }
  408. /*$datas[] = array(
  409. //'id' => $r->id,
  410. 'date' => '',
  411. 'total' => formatPrice($total),
  412. 'first_name' => '',
  413. 'last_name' => '',
  414. 'commercial' => '',
  415. 'causals' => '',
  416. 'payment' => '',
  417. 'status' => '',
  418. 'action' => ''
  419. );*/
  420. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count));
  421. });
  422. Route::get('/get_record_out', function(){
  423. $datas = [];
  424. $x = \App\Models\Record::where('type', 'OUT')->with('supplier', 'payment_method');
  425. if ($_GET["filterSupplier"] > 0)
  426. {
  427. $x = $x->where('supplier_id', $_GET["filterSupplier"]);
  428. }
  429. /*if ($_GET["filterPaymentMethod"] > 0)
  430. {
  431. $x = $x->where('payment_method_id', $_GET["filterPaymentMethod"]);
  432. }
  433. if ($_GET["filterCausals"] > 0)
  434. {
  435. $causals = \App\Models\RecordRow::where('causal_id', $_GET["filterCausals"])->pluck('record_id');
  436. $x = $x->whereIn('records.id', $causals);
  437. }*/
  438. if ($_GET["filterPaymentMethod"] != "null")
  439. {
  440. $payments = explode(",", $_GET["filterPaymentMethod"]);
  441. $x = $x->whereIn('payment_method_id', $payments);
  442. }
  443. if ($_GET["filterCausals"] != "null")
  444. {
  445. $causals = explode(",", $_GET["filterCausals"]);
  446. //$causals = \App\Models\RecordRow::where('causal_id', $_GET["filterCausals"])->pluck('record_id');
  447. $causals = \App\Models\RecordRow::whereIn('causal_id', $causals)->pluck('record_id');
  448. $x = $x->whereIn('records.id', $causals);
  449. }
  450. if ($_GET["filterFrom"] != '')
  451. {
  452. $x = $x->where('date', '>=', $_GET["filterFrom"]);
  453. }
  454. if ($_GET["filterTo"] != '')
  455. {
  456. $x = $x->where('date', '<=', $_GET["filterTo"]);
  457. }
  458. $total = 0;
  459. /*foreach($x->get() as $r)
  460. {
  461. foreach($r->rows as $rr)
  462. {
  463. $total += $rr->amount;
  464. if ($rr->vat_id > 0)
  465. $total += getVatValue($rr->amount, $rr->vat_id);
  466. }
  467. }*/
  468. $x = $x->get();
  469. foreach($x as $idx => $r)
  470. {
  471. $causals = '';
  472. foreach($r->rows as $row)
  473. {
  474. $causals .= $row->causal->getTree() . "<br>";
  475. }
  476. $datas[] = array(
  477. //'id' => $r->id,
  478. 'date' => $r->date,
  479. 'total' => formatPrice($r->getTotal()),
  480. 'supplier' => $r->supplier->name,
  481. 'causals' => $causals,
  482. 'payment' => $r->payment_method->name,
  483. 'action' => $r->id . "|" . formatPrice($total)
  484. );
  485. }
  486. /*
  487. $datas[] = array(
  488. //'id' => $r->id,
  489. 'date' => '',
  490. 'total' => formatPrice($total),
  491. 'supplier' => '',
  492. 'causals' => '',
  493. 'payment' => '',
  494. 'action' => ''
  495. );
  496. */
  497. return json_encode(array("data" => $datas));
  498. });
  499. Route::get('/get_course_list', function(){
  500. $member_course = \App\Models\MemberCourse::with('member');
  501. if (isset($_GET["search"]["value"]))
  502. {
  503. $v = str_replace("'", "''", stripcslashes($_GET["search"]["value"]));
  504. $member_ids = \App\Models\Member::where(function ($query) use ($v) {
  505. $query->where('first_name', 'like', '%' . $v . '%')
  506. ->orWhere('last_name', 'like', '%' . $v . '%');
  507. })->pluck('id');
  508. $member_course = $member_course->whereIn('member_id', $member_ids);
  509. }
  510. if ($_GET["filterCourse"] != "null")
  511. {
  512. $course_ids = [];
  513. $courses = explode(",", $_GET["filterCourse"]);
  514. foreach($courses as $c)
  515. {
  516. $all = \App\Models\Course::where('name', 'like', '%' . $c . "%")->get();
  517. foreach($all as $a)
  518. {
  519. $course_ids[] = $a->id;
  520. }
  521. }
  522. $member_course = $member_course->whereIn('course_id', $course_ids);
  523. }
  524. if ($_GET["filterLevel"] != "null")
  525. {
  526. $levels = explode(",", $_GET["filterLevel"]);
  527. $course_ids = \App\Models\Course::whereIn('course_level_id', $levels)->pluck('id');
  528. $member_course = $member_course->whereIn('course_id', $course_ids);
  529. }
  530. if ($_GET["filterFrequency"] != "null")
  531. {
  532. $frequencies = explode(",", $_GET["filterFrequency"]);
  533. $course_ids = \App\Models\Course::whereIn('course_type_id', $frequencies)->pluck('id');
  534. $member_course = $member_course->whereIn('course_id', $course_ids);
  535. }
  536. if ($_GET["filterType"] != "null")
  537. {
  538. $types = explode(",", $_GET["filterType"]);
  539. $course_ids = \App\Models\Course::whereIn('course_frequency_id', $types)->pluck('id');
  540. $member_course = $member_course->whereIn('course_id', $course_ids);
  541. }
  542. if ($_GET["filterDuration"] != "null")
  543. {
  544. $durations = explode(",", $_GET["filterDuration"]);
  545. $course_ids = \App\Models\Course::whereIn('course_duration_id', $durations)->pluck('id');
  546. $member_course = $member_course->whereIn('course_id', $course_ids);
  547. }
  548. $totals = [];
  549. $totalIsc = [];
  550. $member_course_totals = $member_course->get();
  551. foreach($member_course_totals as $x)
  552. {
  553. $price = 0;
  554. $price = $x->course->price;
  555. $subPrice = $x->course->subscription_price;
  556. $records = \App\Models\Record::where('member_course_id', $x->id)->where('deleted', 0)->get();
  557. $prices = [];
  558. foreach ($records as $record)
  559. {
  560. foreach ($record->rows as $row)
  561. {
  562. //if ($row->causal_id == $x->course->sub_causal_id || str_contains(strtolower($row->note), 'iscrizione'))
  563. if (str_contains(strtolower($row->note), 'iscrizione'))
  564. {
  565. $subPrice = $row->amount;
  566. }
  567. if ($row->causal_id == $x->course->causal_id && !str_contains(strtolower($row->note), 'iscrizione'))
  568. {
  569. $tot = sizeof(json_decode($row->when));
  570. foreach(json_decode($row->when) as $m)
  571. {
  572. $prices[$m->month] = $row->amount / $tot;
  573. }
  574. }
  575. }
  576. }
  577. for($i=1; $i<=12; $i++)
  578. {
  579. $cls = getColor($x->months, $i);
  580. if ($cls != 'grey')
  581. {
  582. if (!isset($totals[$i]))
  583. {
  584. $totals[$i]['green'] = 0;
  585. $totals[$i]['orange'] = 0;
  586. $totals[$i]['yellow'] = 0;
  587. }
  588. if ($cls == 'yellow')
  589. {
  590. $totals[$i][$cls] += 1;
  591. }
  592. else
  593. {
  594. $p = isset($prices[$i]) ? $prices[$i] : $price;
  595. $totals[$i][$cls] += $p;
  596. }
  597. }
  598. }
  599. $sub = $x->subscribed ? "Y" : "N";
  600. if (isset($totalIsc[$sub]))
  601. $totalIsc[$sub] += $subPrice;
  602. else
  603. $totalIsc[$sub] = $subPrice;
  604. }
  605. $count = $member_course->count();
  606. $js = '';
  607. $xx = 2;
  608. $str = '';
  609. if ($count > 0)
  610. {
  611. $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>";
  612. $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>";
  613. $str .= "<a style='width:100%;float:right; text-align:right; display:block;' class=yellow><small>02220</small></a><br>";
  614. }
  615. $js .= "2§" . $str . "_";
  616. $str = "";
  617. foreach($totals as $z => $t)
  618. {
  619. if ($z == 1) $xx = 7;
  620. if ($z == 2) $xx = 8;
  621. if ($z == 3) $xx = 9;
  622. if ($z == 4) $xx = 10;
  623. if ($z == 5) $xx = 11;
  624. if ($z == 6) $xx = 12;
  625. if ($z == 7) $xx = 13;
  626. if ($z == 8) $xx = 14;
  627. if ($z == 9) $xx = 3;
  628. if ($z == 10) $xx = 4;
  629. if ($z == 11) $xx = 5;
  630. if ($z == 12) $xx = 6;
  631. $str = '';
  632. foreach($t as $x => $c)
  633. {
  634. $y = $x == 'yellow' ? $c : formatPrice($c);
  635. $str .= "<a style='width:100%;float:right; text-align:right; display:block;' class=" . $x . "><small>" . $y . "</small></a><br>";
  636. }
  637. $js .= $xx . "§" . $str . "_";
  638. $xx += 1;
  639. }
  640. //$member_course = $member_course->where('course_id', 999999);
  641. $start = 0;
  642. $limit = 100000;
  643. if (isset($_GET["start"]))
  644. {
  645. $start = $_GET["start"];
  646. $limit = $_GET["length"];
  647. }
  648. $member_course = $member_course->offset($start)->limit($limit)->get();
  649. $datas = [];
  650. foreach($member_course as $x)
  651. {
  652. $price = 0;
  653. $price = $x->course->price;
  654. $subPrice = $x->course->subscription_price;
  655. $records = \App\Models\Record::where('member_course_id', $x->id)->where('deleted', 0)->get();
  656. $prices = [];
  657. foreach ($records as $record)
  658. {
  659. foreach ($record->rows as $row)
  660. {
  661. //if ($row->causal_id == $x->course->sub_causal_id || str_contains(strtolower($row->note), 'iscrizione'))
  662. if (str_contains(strtolower($row->note), 'iscrizione'))
  663. {
  664. $subPrice = $row->amount;
  665. }
  666. if ($row->causal_id == $x->course->causal_id && !str_contains(strtolower($row->note), 'iscrizione'))
  667. {
  668. $tot = sizeof(json_decode($row->when));
  669. foreach(json_decode($row->when) as $m)
  670. {
  671. $prices[$m->month] = $row->amount / $tot;
  672. }
  673. }
  674. }
  675. }
  676. for($i=1; $i<=12; $i++)
  677. {
  678. $cls = getColor($x->months, $i);
  679. if ($cls != 'grey')
  680. {
  681. if (!isset($totals[$i]))
  682. {
  683. $totals[$i]['green'] = 0;
  684. $totals[$i]['orange'] = 0;
  685. $totals[$i]['yellow'] = 0;
  686. }
  687. if ($cls == 'yellow')
  688. {
  689. $prices[$i] = 0;
  690. $totals[$i][$cls] += 1;
  691. }
  692. else
  693. {
  694. $p = isset($prices[$i]) ? $prices[$i] : $price;
  695. $totals[$i][$cls] += $p;
  696. }
  697. }
  698. }
  699. $sub = $x->subscribed ? "Y" : "N";
  700. if (isset($totalIsc[$sub]))
  701. $totalIsc[$sub] += $subPrice;
  702. else
  703. $totalIsc[$sub] = $subPrice;
  704. $datas[] = array(
  705. "column_0" => $x->member->last_name,
  706. "column_1" => $x->member->first_name,
  707. "column_2" => $x->subscribed . "§" . formatPrice($subPrice),
  708. "column_3" => getColor($x->months, 9) . "§" . formatPrice(isset($prices[9]) ? $prices[9] : $price),
  709. "column_4" => getColor($x->months, 10) . "§" . formatPrice(isset($prices[10]) ? $prices[10] : $price),
  710. "column_5" => getColor($x->months, 11) . "§" . formatPrice(isset($prices[11]) ? $prices[11] : $price),
  711. "column_6" => getColor($x->months, 12) . "§" . formatPrice(isset($prices[12]) ? $prices[12] : $price),
  712. "column_7" => getColor($x->months, 1) . "§" . formatPrice(isset($prices[1]) ? $prices[1] : $price),
  713. "column_8" => getColor($x->months, 2) . "§" . formatPrice(isset($prices[2]) ? $prices[2] : $price),
  714. "column_9" => getColor($x->months, 3) . "§" . formatPrice(isset($prices[3]) ? $prices[3] : $price),
  715. "column_10" => getColor($x->months, 4) . "§" . formatPrice(isset($prices[4]) ? $prices[4] : $price),
  716. "column_11" => getColor($x->months, 5) . "§" . formatPrice(isset($prices[5]) ? $prices[5] : $price),
  717. "column_12" => getColor($x->months, 6) . "§" . formatPrice(isset($prices[6]) ? $prices[6] : $price),
  718. "column_13" => getColor($x->months, 7) . "§" . formatPrice(isset($prices[7]) ? $prices[7] : $price),
  719. "column_14" => getColor($x->months, 8) . "§" . formatPrice(isset($prices[8]) ? $prices[8] : $price),
  720. "column_15" => $x->course_id,
  721. "column_16" => $x->id,
  722. "column_17" => $x->member_id
  723. );
  724. }
  725. // Sort data
  726. /*
  727. if (isset($_GET["order"]))
  728. 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);
  729. else
  730. array_multisort(array_column($datas, 'column_0'), SORT_DESC, SORT_NATURAL|SORT_FLAG_CASE, $datas);
  731. //$x = $x->orderBy('records.date', 'DESC')->orderBy('records.id', 'DESC');
  732. */
  733. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count, "totals" => $js));
  734. });
  735. Route::get('/get_course_members', function(){
  736. //$datas = \App\Models\MemberCourse::with('member');
  737. $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');
  738. if ($_GET["filterCourse"] != "null")
  739. {
  740. $course_ids = [];
  741. $courses = explode(",", $_GET["filterCourse"]);
  742. foreach($courses as $c)
  743. {
  744. $all = \App\Models\Course::where('name', 'like', '%' . $c . "%")->get();
  745. foreach($all as $a)
  746. {
  747. $course_ids[] = $a->id;
  748. }
  749. }
  750. $datas = $datas->whereIn('course_id', $course_ids);
  751. }
  752. if ($_GET["filterLevel"] != "null")
  753. {
  754. $levels = explode(",", $_GET["filterLevel"]);
  755. $course_ids = \App\Models\Course::whereIn('course_level_id', $levels)->pluck('id');
  756. $datas = $datas->whereIn('course_id', $course_ids);
  757. }
  758. if ($_GET["filterFrequency"] != "null")
  759. {
  760. $frequencies = explode(",", $_GET["filterFrequency"]);
  761. $course_ids = \App\Models\Course::whereIn('course_type_id', $frequencies)->pluck('id');
  762. $datas = $datas->whereIn('course_id', $course_ids);
  763. }
  764. if ($_GET["filterType"] != "null")
  765. {
  766. $types = explode(",", $_GET["filterType"]);
  767. $course_ids = \App\Models\Course::whereIn('course_frequency_id', $types)->pluck('id');
  768. $datas = $datas->whereIn('course_id', $course_ids);
  769. }
  770. if ($_GET["filterDuration"] != "null")
  771. {
  772. $durations = explode(",", $_GET["filterDuration"]);
  773. $course_ids = \App\Models\Course::whereIn('course_duration_id', $durations)->pluck('id');
  774. $datas = $datas->whereIn('course_id', $course_ids);
  775. }
  776. if ($_GET["filterDays"] != "null")
  777. {
  778. $ids = [];
  779. $days = explode(",", $_GET["filterDays"]);
  780. foreach($days as $d)
  781. {
  782. $all = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")->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["filterHours"] != "null")
  791. {
  792. $ids = [];
  793. $hours = explode(",", $_GET["filterHours"]);
  794. foreach($hours as $h)
  795. {
  796. $all = \App\Models\MemberCourse::where('when', 'like', '%"from":"' . $h . "%")->get();
  797. foreach($all as $a)
  798. {
  799. $ids[] = $a->id;
  800. }
  801. }
  802. $datas = $datas->whereIn('member_courses.id', $ids);
  803. }
  804. if ($_GET["filterSubscription"] != "")
  805. {
  806. $ids = \App\Models\MemberCourse::where('subscribed', $_GET["filterSubscription"] == 1 ? true : false)->pluck('id');
  807. $datas = $datas->whereIn('member_courses.id', $ids);
  808. //$this->filter .= $this->filter != '' ? ', ' : '';
  809. //$this->filter .= "Pagata sottoscrizione : " . ($this->filterSubscription == 1 ? "SI" : "NO") . " ";
  810. }
  811. if ($_GET["filterCertificateType"] != "null")
  812. {
  813. $types = \App\Models\MemberCertificate::where('type', $_GET["filterCertificateType"])->pluck('member_id');
  814. $datas = $datas->whereIn('member_id', $types);
  815. }
  816. if ($_GET["filterCertificateScadenza"] != "null")
  817. {
  818. if ($_GET["filterCertificateScadenza"] == "1")
  819. $scad = \App\Models\MemberCertificate::where('expire_date', '<', date("Y-m-d"))->pluck('member_id');
  820. if ($_GET["filterCertificateScadenza"] == "2")
  821. $scad = \App\Models\MemberCertificate::whereBetween('expire_date', [date("Y-m-d"), date("Y-m-d", strtotime("+1 month"))])->pluck('member_id');
  822. $datas = $datas->whereIn('member_id', $scad);
  823. }
  824. if ($_GET["fromYear"] != "")
  825. {
  826. $m_ids = \App\Models\Member::where('birth_date', '<', date("Y-m-d", strtotime("-" . $_GET["fromYear"] . " year", time())))->pluck('id');
  827. $datas = $datas->whereIn('member_id', $m_ids);
  828. }
  829. if ($_GET["toYear"] != "")
  830. {
  831. $m_ids = \App\Models\Member::where('birth_date', '>', date("Y-m-d", strtotime("-" . $_GET["toYear"] . " year", time())))->pluck('id');
  832. $datas = $datas->whereIn('member_id', $m_ids);
  833. }
  834. if ($_GET["fromFromYear"] != "")
  835. {
  836. $m_ids = \App\Models\Member::whereYear('birth_date', '>=', $_GET["fromFromYear"])->pluck('id');
  837. $datas = $datas->whereIn('member_id', $m_ids);
  838. }
  839. if ($_GET["toToYear"] != "")
  840. {
  841. $m_ids = \App\Models\Member::whereYear('birth_date', '<=', $_GET["toToYear"])->pluck('id');
  842. $datas = $datas->whereIn('member_id', $m_ids);
  843. }
  844. if ($_GET["filterCards"] != "null")
  845. {
  846. $cards = explode(",", $_GET["filterCards"]);
  847. $card_ids = \App\Models\MemberCard::whereIn('card_id', $cards)->pluck('member_id');
  848. $datas = $datas->whereIn('member_id', $card_ids);
  849. }
  850. if ($_GET["filterYear"] != "")
  851. {
  852. $course_ids = \App\Models\Course::where('year', $_GET["filterYear"])->pluck('id');
  853. $datas = $datas->whereIn('course_id', $course_ids);
  854. //$this->filter .= $this->filter != '' ? ', ' : '';
  855. //$this->filter .= "Anno : " . $this->filterYear . " ";
  856. }
  857. $aRet = [];
  858. if (isset($_GET["order"]))
  859. {
  860. $column = '';
  861. if ($_GET["order"][0]["column"] == 1)
  862. $column = 'last_name';
  863. if ($_GET["order"][0]["column"] == 2)
  864. $column = 'first_name';
  865. if ($_GET["order"][0]["column"] == 2)
  866. $column = 'birth_date';
  867. if ($_GET["order"][0]["column"] == 3)
  868. $column = 'birth_date';
  869. if ($_GET["order"][0]["column"] == 4)
  870. $column = 'birth_date';
  871. if ($_GET["order"][0]["column"] == 5)
  872. $column = 'phone';
  873. if ($_GET["order"][0]["column"] == 6)
  874. $column = 'email';
  875. if ($column != '')
  876. $datas = $datas->orderBy($column, $_GET["order"][0]["dir"]);
  877. else
  878. $datas = $datas->orderBy('last_name', 'ASC')->orderBy('first_name', 'ASC');
  879. }
  880. else
  881. $datas = $datas->orderBy('last_name', 'ASC')->orderBy('first_name', 'ASC');
  882. if ($_GET["filterStatus"] != "null")
  883. {
  884. $status = explode(",", $_GET["filterStatus"]);
  885. foreach($status as $s)
  886. {
  887. foreach($datas->get() as $aaa)
  888. {
  889. $state = \App\Models\Member::findOrFail($aaa->member_id)->isActive();
  890. if ($state["status"] == $s)
  891. $aRet[] = $aaa;
  892. }
  893. }
  894. }
  895. else
  896. $aRet = $datas->get();
  897. $ret = [];
  898. foreach($aRet as $idx => $r)
  899. {
  900. $date1 = new DateTime($r->birth_date);
  901. $date2 = new DateTime("now");
  902. $interval = $date1->diff($date2);
  903. $ret[] = array(
  904. "column_0" => $idx + 1,
  905. "column_1" => $r->last_name,
  906. "column_2" => $r->first_name,
  907. "column_3" => strval($interval->y),
  908. "column_4" => date("Y", strtotime($r->birth_date)),
  909. "column_5" => $r->phone,
  910. "column_6" => $r->email,
  911. "column_7" => $r->member_id
  912. );
  913. }
  914. if (isset($_GET["start"]))
  915. $ret = array_slice($ret, $_GET["start"], $_GET["length"]);
  916. return json_encode(array("data" => $ret, "recordsTotal" => sizeof($aRet), "recordsFiltered" => sizeof($aRet)));
  917. });
  918. Route::get('/get_receipts', function(){
  919. $x = \App\Models\Receipt::select('receipts.*', 'members.first_name', 'members.last_name')->leftJoin('members', 'receipts.member_id', '=', 'members.id');
  920. if (isset($_GET["search"]["value"]))
  921. {
  922. $v = str_replace("'", "''", stripcslashes($_GET["search"]["value"]));
  923. $member_ids = \App\Models\Member::where(function ($query) use ($v) {
  924. $query->where('first_name', 'like', '%' . $v . '%')
  925. ->orWhere('last_name', 'like', '%' . $v . '%');
  926. })->pluck('id');
  927. $x = $x->whereIn('member_id', $member_ids);
  928. }
  929. if ($_GET["filterStatus"] != '')
  930. $x = $x->where('receipts.status', $_GET["filterStatus"]);
  931. if ($_GET["filterFrom"] != "")
  932. $x = $x->where('date', '>=', $_GET["filterFrom"]);
  933. if ($_GET["filterTo"] != "")
  934. $x = $x->where('date', '<=', $_GET["filterTo"]);
  935. $count = $x->count();
  936. if (isset($_GET["order"]))
  937. {
  938. $column = '';
  939. if ($_GET["order"][0]["column"] == 0)
  940. $column = 'year';
  941. if ($_GET["order"][0]["column"] == 1)
  942. $column = 'number';
  943. if ($_GET["order"][0]["column"] == 2)
  944. $column = 'last_name';
  945. if ($_GET["order"][0]["column"] == 3)
  946. $column = 'first_name';
  947. if ($_GET["order"][0]["column"] == 4)
  948. $column = 'status';
  949. if ($_GET["order"][0]["column"] == 5)
  950. $column = 'date';
  951. if ($column != '')
  952. $x = $x->orderBy($column, $_GET["order"][0]["dir"])->orderBy('id', 'DESC');
  953. else
  954. $x = $x->orderBy('id', 'DESC');
  955. }
  956. else
  957. $x = $x->orderBy('id', 'DESC');
  958. if (isset($_GET["start"]))
  959. $x = $x->offset($_GET["start"])->limit($_GET["length"])->get();
  960. else
  961. $x = $x->get();
  962. $datas = [];
  963. foreach($x as $idx => $r)
  964. {
  965. $datas[] = array(
  966. 'year' => $r->year,
  967. 'number' => $r->number,
  968. 'last_name' => $r->member->last_name,
  969. 'first_name' => $r->member->first_name,
  970. 'status' => $r->status,
  971. 'date' => date("d/m/Y", strtotime($r->date)),
  972. 'totals' => formatPrice($r->rows->sum('amount')),
  973. 'action' => $r->id
  974. );
  975. }
  976. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count));
  977. });
  978. function getColor($months, $m)
  979. {
  980. $class = "grey";
  981. foreach(json_decode($months) as $mm)
  982. {
  983. if ($mm->m == $m)
  984. {
  985. if ($mm->status == "")
  986. {
  987. $class = "orange";
  988. }
  989. if ($mm->status == "1")
  990. {
  991. $class = "green";
  992. }
  993. if ($mm->status == "2")
  994. {
  995. $class = "yellow";
  996. }
  997. }
  998. }
  999. return $class;
  1000. }
  1001. Route::get('/migrate', function(){
  1002. \Artisan::call('migrate');
  1003. dd('migrated!');
  1004. });
  1005. Route::get('/updateData', function()
  1006. {
  1007. // Call and Artisan command from within your application.
  1008. Artisan::call('update:data');
  1009. });