web.php 43 KB

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