web.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. return $pdf->stream();
  52. /*return response()->streamDownload(
  53. fn () => print($pdf),
  54. "ricevuta_" . $receipt->number . "_" . $receipt->year . ".pdf"
  55. );*/
  56. });
  57. Route::get('/receipt/mail/{id}', function($id){
  58. $receipt = \App\Models\Receipt::findOrFail($id);
  59. if ($receipt->status == 99)
  60. sendReceiptDeleteEmail($receipt);
  61. else
  62. sendReceiptEmail($receipt);
  63. /*
  64. $pdf = PDF::loadView('receipt', array('receipt' => $receipt));
  65. $pdfName = "ricevuta_" . $receipt->number . "_" . $receipt->year . ".pdf";
  66. Storage::put('public/pdf/' . $pdfName, $pdf->output());
  67. $email = \App\Models\Member::findOrFail($receipt->member_id)->email;
  68. if ($email != '')
  69. {
  70. Mail::to($email)->send(new \App\Mail\ReceipEmail([
  71. 'name' => 'Luca',
  72. 'pdf' => 'public/pdf/' . $pdfName,
  73. 'number' => $receipt->number . "/" . $receipt->year
  74. ]));
  75. }
  76. */
  77. return true;
  78. //return $pdf->stream();
  79. /*return response()->streamDownload(
  80. fn () => print($pdf),
  81. "ricevuta_" . $receipt->number . "_" . $receipt->year . ".pdf"
  82. );*/
  83. });
  84. Route::get('/nations', function(){
  85. if (isset($_GET["q"]))
  86. $datas = \App\Models\Nation::where('name', 'like', $_GET["q"] . '%')->orderBy('name')->get();
  87. else
  88. $datas = \App\Models\Nation::orderBy('name')->get();
  89. $data = array();
  90. foreach($datas as $d)
  91. {
  92. $data[] = array("id" => $d->id, "text" => $d->name);
  93. }
  94. return array("results" => $data);
  95. });
  96. Route::get('/provinces/{nation_id}', function($nation_id){
  97. if (isset($_GET["q"]))
  98. $datas = \App\Models\Province::where('nation_id', $nation_id)->where('name', 'like', $_GET["q"] . '%')->orderBy('name')->get();
  99. else
  100. $datas = \App\Models\Province::where('nation_id', $nation_id)->orderBy('name')->get();
  101. $data = array();
  102. foreach($datas as $d)
  103. {
  104. $data[] = array("id" => $d->id, "text" => $d->name);
  105. }
  106. return array("results" => $data);
  107. });
  108. Route::get('/cities/{province_id}', function($province_id){
  109. if (isset($_GET["q"]))
  110. $datas = \App\Models\City::where('province_id', $province_id)->where('name', 'like', $_GET["q"] . '%')->orderBy('name')->get();
  111. else
  112. $datas = \App\Models\City::where('province_id', $province_id)->orderBy('name')->get();
  113. $data = array();
  114. foreach($datas as $d)
  115. {
  116. $data[] = array("id" => $d->id, "text" => $d->name);
  117. }
  118. return array("results" => $data);
  119. });
  120. Route::get('/get_members', function(){
  121. $datas = [];
  122. // $datas = \App\Models\Member::select('members.*')->where('id', '>', 0);
  123. $x = \App\Models\Member::select('id', 'first_name', 'last_name', 'phone', 'birth_date')->where('id', '>', 0);
  124. if (isset($_GET["search"]["value"]))
  125. {
  126. $v = str_replace("'", "''", stripcslashes($_GET["search"]["value"]));
  127. $x = $x->where(function ($query) use ($v) {
  128. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $v . "%'")
  129. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $v . "%'");
  130. });
  131. //where('first_name', 'like', '%' . $_GET["search"]["value"] . '%');
  132. }
  133. if ($_GET["cards"] != "")
  134. {
  135. $card_ids = \App\Models\MemberCard::whereIn('card_id', explode(",", $_GET["cards"]))->pluck('member_id');
  136. $x = $x->whereIn('id', $card_ids);
  137. }
  138. if ($_GET["categories"] != "")
  139. {
  140. $cats_ids = \App\Models\MemberCategory::whereIn('category_id', explode(",", $_GET["categories"]))->pluck('member_id');
  141. $x = $x->whereIn('id', $cats_ids);
  142. }
  143. if ($_GET["fromYear"] != "")
  144. {
  145. $x = $x->where('birth_date', '<', date("Y-m-d", strtotime("-" . $_GET["fromYear"] . " year", time())));
  146. }
  147. if ($_GET["toYear"] != "")
  148. {
  149. $x = $x->where('birth_date', '>', date("Y-m-d", strtotime("-" . $_GET["toYear"] . " year", time())));
  150. }
  151. $ids = [];
  152. if ($_GET["chkCertificateNormal"] != "")
  153. {
  154. $normal = \App\Models\MemberCertificate::where('type', 'N')->pluck('member_id');
  155. $ids = array_merge($ids, $normal->toArray());
  156. //$x = $x->whereIn('id', $normal);;
  157. }
  158. if ($_GET["chkCertificateAgonistico"] != "")
  159. {
  160. $agonistic = \App\Models\MemberCertificate::where('type', 'A')->pluck('member_id');
  161. $ids = array_merge($ids, $agonistic->toArray());
  162. //$x = $x->whereIn('id', $agonistic);
  163. }
  164. if ($_GET["chkCertificateScaduti"] != "")
  165. {
  166. $scaduto = \App\Models\MemberCertificate::where('expire_date', '<', date("Y-m-d"))->pluck('member_id');
  167. $ids = array_merge($ids, $scaduto->toArray());
  168. //$x = $x->whereIn('id', $scaduto);
  169. }
  170. if ($_GET["chkCertificateScadenza"] != "")
  171. {
  172. $scadenza = \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, $scadenza->toArray());
  174. //$x = $x->whereIn('id', $scadenza);
  175. }
  176. //$filterStatus = isset($_GET["status"]) ? $_GET["status"] : -1;
  177. $chkStatus = [];
  178. $chkStatus0 = isset($_GET["chkStatus0"]) ? $_GET["chkStatus0"] : 0;
  179. if($chkStatus0 > 0)
  180. $chkStatus[] = 0;
  181. $chkStatus1 = isset($_GET["chkStatus1"]) ? $_GET["chkStatus1"] : 0;
  182. if($chkStatus1 > 0)
  183. $chkStatus[] = 1;
  184. $chkStatus2 = isset($_GET["chkStatus2"]) ? $_GET["chkStatus2"] : 0;
  185. if($chkStatus2 > 0)
  186. $chkStatus[] = 2;
  187. if (sizeof($chkStatus) > 0)
  188. {
  189. $members = \App\Models\Member::all();
  190. foreach($members as $m)
  191. {
  192. $state = $m->isActive();
  193. if (in_array($state["status"], $chkStatus))
  194. $ids[] = $m->id;
  195. }
  196. }
  197. if (sizeof($ids) > 0)
  198. {
  199. $x = $x->whereIn('id', $ids);
  200. }
  201. else
  202. {
  203. if (sizeof($chkStatus) > 0)
  204. $x = $x->whereIn('id', [-1]);
  205. }
  206. $count = $x->count();
  207. $x = $x->orderBy('last_name', 'ASC')->orderBy('first_name', 'ASC');
  208. if (isset($_GET["start"]))
  209. $x = $x->offset($_GET["start"])->limit($_GET["length"])->get();
  210. else
  211. $x = $x->get();
  212. foreach($x as $idx => $r)
  213. {
  214. $status = $r->getStatus();
  215. $status = $status["status"];
  216. // $state = $r->isActive();
  217. $class = $status > 0 ? ($status == 2 ? 'active' : 'suspended') : 'due';
  218. $text = $status > 0 ? ($status == 2 ? 'Tesserato' : 'Sospeso') : 'Non tesserato';
  219. /*$x = $state["status"] > 0 ? ($state["status"] == 2 ? 'active' : 'suspended') : '';
  220. $x .= "|";
  221. $x .= $state["status"] > 0 ? ($state["status"] == 2 ? 'Attivo' : 'Sospesa') : '';
  222. $x .= "|";
  223. $x .= $state["status"] ? 'Scadenza : ' : ($state["date"] != '' ? 'Scaduto : ' : '');
  224. $x .= "|";
  225. $x .= $state["date"] != '' ? date("d/m/Y", strtotime($state["date"])) : '';*/
  226. $has_certificate = $r->hasCertificate();
  227. $y = '';
  228. if($has_certificate["date"] != '')
  229. {
  230. if($has_certificate["date"] < date("Y-m-d"))
  231. $y .= '0';
  232. if($has_certificate["date"] >= date("Y-m-d") && $has_certificate["date"] < date("Y-m-d", strtotime("+1 month")))
  233. $y .= '1';
  234. if($has_certificate["date"] >= date("Y-m-d", strtotime("+1 month")))
  235. $y .= '2';
  236. $y .= '|';
  237. $y .= $has_certificate["date"] != '' ? date("d/m/Y", strtotime($has_certificate["date"])) : '';
  238. }
  239. $datas[] = array(
  240. //'c' => $idx + 1,
  241. //'id' => "ID" . str_pad($r->id, 5, "0", STR_PAD_LEFT),
  242. 'last_name' => $r->last_name . "|" . $r->id,
  243. 'first_name' => $r->first_name . "|" . $r->id,
  244. 'phone' => $r->phone,
  245. 'age' => $r->getAge(),
  246. 'year' => date("Y", strtotime($r->birth_date)),
  247. 'status' => $class . "|" . $text,
  248. // 'state' => $x,
  249. 'certificate' => $y,
  250. 'action' => $r->id
  251. );
  252. }
  253. /*
  254. $r->age = $r->getAge();
  255. $active = $r->isActive();
  256. $r->status = $active["status"];
  257. $r->date = $active["date"] . "|" . $r->hasCertificate()["date"];
  258. $r->state = $r->getStatus()["status"];
  259. $r->action = '';*/
  260. /*
  261. if ($this->sortAsc)
  262. $this->records = $this->records->sortBy($this->sortField);
  263. else
  264. $this->records = $this->records->sortByDesc($this->sortField);
  265. */
  266. // $datas = $x; // ->orderBy($this->sortField, $this->sortAsc ? 'ASC' : 'DESC')->paginate(10);
  267. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count));
  268. });
  269. Route::get('/get_record_in', function(){
  270. $datas = [];
  271. $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'))
  272. ->leftJoin('members', 'records.member_id', '=', 'members.id')
  273. ->leftJoin('payment_methods', 'records.payment_method_id', '=', 'payment_methods.id')
  274. ->where('records.type', 'IN');
  275. // $datas = \App\Models\Record::where('type', 'IN')->with('member', 'payment_method');
  276. if ($_GET["filterCommercial"] > 0)
  277. {
  278. $x = $x->where('commercial', $_GET["filterCommercial"] == 1 ? true : false);
  279. }
  280. if ($_GET["filterMember"] > 0)
  281. {
  282. $x = $x->where('member_id', $_GET["filterMember"]);
  283. }
  284. if ($_GET["filterPaymentMethod"] > 0)
  285. {
  286. $x = $x->where('payment_method_id', $_GET["filterPaymentMethod"]);
  287. }
  288. if ($_GET["filterCausals"] > 0)
  289. {
  290. $causals = \App\Models\RecordRow::where('causal_id', $_GET["filterCausals"])->pluck('record_id');
  291. $x = $x->whereIn('records.id', $causals);
  292. }
  293. if ($_GET["filterFrom"] != '')
  294. {
  295. $x = $x->where('date', '>=', $_GET["filterFrom"]);
  296. }
  297. if ($_GET["filterTo"] != '')
  298. {
  299. $x = $x->where('date', '<=', $_GET["filterTo"]);
  300. }
  301. if (isset($_GET["search"]["value"]))
  302. {
  303. $v = str_replace("'", "''", stripcslashes($_GET["search"]["value"]));
  304. $x = $x->where(function ($query) use ($v) {
  305. $query->where('first_name', 'like', '%' . $v . '%')
  306. ->orWhere('last_name', 'like', '%' . $v . '%');
  307. });
  308. //where('first_name', 'like', '%' . $_GET["search"]["value"] . '%');
  309. }
  310. $start = 0;
  311. $limit = 100000;
  312. if (isset($_GET["start"]))
  313. {
  314. $start = $_GET["start"];
  315. $limit = $_GET["length"];
  316. }
  317. $excludeCausals = [];
  318. $borsellino = \App\Models\Causal::where('money', true)->first();
  319. if ($borsellino)
  320. $excludeCausals[] = $borsellino->id;
  321. // Aggiungo
  322. $excludes = \App\Models\Causal::where('no_records', true)->get();
  323. foreach($excludes as $e)
  324. {
  325. $excludeCausals[] = $e->id;
  326. }
  327. $exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
  328. $total = 0;
  329. foreach($x->get() as $r)
  330. {
  331. foreach($r->rows as $rr)
  332. {
  333. if (!in_array($rr->member_id, $exclude_from_records) && (!$r->deleted || $r->deleted == null) && !in_array($rr->causal_id, $excludeCausals) && (!$r->financial_movement || $r->financial_movement == null) && (!$r->corrispettivo_fiscale || $r->corrispettivo_fiscale == null))
  334. {
  335. $total += $rr->amount;
  336. if ($rr->vat_id > 0)
  337. $total += getVatValue($rr->amount, $rr->vat_id);
  338. }
  339. }
  340. }
  341. $count = $x->count();
  342. $x = $x->orderBy('date', 'DESC')->orderBy('id', 'DESC');
  343. $x = $x->offset($start)->limit($limit)->get();
  344. foreach($x as $idx => $r)
  345. {
  346. $causals = '';
  347. foreach($r->rows as $row)
  348. {
  349. $causals .= $row->causal->getTree() . "<br>";
  350. }
  351. $datas[] = array(
  352. //'id' => $r->id,
  353. 'date' => $r->date,
  354. 'total' => formatPrice($r->getTotal()),
  355. 'first_name' => $r->first_name,
  356. 'last_name' => $r->last_name,
  357. 'commercial' => $r->financial_movement ? 'Movimento finanziario' : ($r->commercial ? 'SI' : 'NO'),
  358. 'causals' => $causals,
  359. 'payment' => $r->payment_method->name,
  360. 'status' => $r->deleted ? 'Annullato' : '',
  361. 'action' => $r->id . "|" . formatPrice($total) . "|" . ($r->deleted ? 'x' : '')
  362. );
  363. }
  364. /*$datas[] = array(
  365. //'id' => $r->id,
  366. 'date' => '',
  367. 'total' => formatPrice($total),
  368. 'first_name' => '',
  369. 'last_name' => '',
  370. 'commercial' => '',
  371. 'causals' => '',
  372. 'payment' => '',
  373. 'status' => '',
  374. 'action' => ''
  375. );*/
  376. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count));
  377. });
  378. Route::get('/get_record_out', function(){
  379. $datas = [];
  380. $x = \App\Models\Record::where('type', 'OUT')->with('supplier', 'payment_method');
  381. if ($_GET["filterSupplier"] > 0)
  382. {
  383. $x = $x->where('supplier_id', $_GET["filterSupplier"]);
  384. }
  385. if ($_GET["filterPaymentMethod"] > 0)
  386. {
  387. $x = $x->where('payment_method_id', $_GET["filterPaymentMethod"]);
  388. }
  389. if ($_GET["filterCausals"] > 0)
  390. {
  391. $causals = \App\Models\RecordRow::where('causal_id', $_GET["filterCausals"])->pluck('record_id');
  392. $x = $x->whereIn('records.id', $causals);
  393. }
  394. if ($_GET["filterFrom"] != '')
  395. {
  396. $x = $x->where('date', '>=', $_GET["filterFrom"]);
  397. }
  398. if ($_GET["filterTo"] != '')
  399. {
  400. $x = $x->where('date', '<=', $_GET["filterTo"]);
  401. }
  402. $total = 0;
  403. foreach($x->get() as $r)
  404. {
  405. foreach($r->rows as $rr)
  406. {
  407. $total += $rr->amount;
  408. if ($rr->vat_id > 0)
  409. $total += getVatValue($rr->amount, $rr->vat_id);
  410. }
  411. }
  412. $x = $x->get();
  413. foreach($x as $idx => $r)
  414. {
  415. $causals = '';
  416. foreach($r->rows as $row)
  417. {
  418. $causals .= $row->causal->getTree() . "<br>";
  419. }
  420. $datas[] = array(
  421. //'id' => $r->id,
  422. 'date' => $r->date,
  423. 'total' => formatPrice($r->getTotal()),
  424. 'supplier' => $r->supplier->name,
  425. 'causals' => $causals,
  426. 'payment' => $r->payment_method->name,
  427. 'action' => $r->id . "|" . formatPrice($total)
  428. );
  429. }
  430. /*
  431. $datas[] = array(
  432. //'id' => $r->id,
  433. 'date' => '',
  434. 'total' => formatPrice($total),
  435. 'supplier' => '',
  436. 'causals' => '',
  437. 'payment' => '',
  438. 'action' => ''
  439. );
  440. */
  441. return json_encode(array("data" => $datas));
  442. });
  443. Route::get('/migrate', function(){
  444. \Artisan::call('migrate');
  445. dd('migrated!');
  446. });