web.php 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634
  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::get('/login', function () {
  19. return Redirect::to('/');
  20. // return Redirect::to('/dashboard');
  21. });
  22. Route::post('/login', function () {
  23. if(\Auth::attempt(array('email' => $_POST["email"], 'password' => $_POST["password"])))
  24. {
  25. return Redirect::to('/dashboard');
  26. }
  27. else
  28. {
  29. return Redirect::to('/?error=1');
  30. }
  31. })->name('login');
  32. Route::get('/logout', function(){
  33. Auth::logout();
  34. return redirect('/');
  35. });
  36. Route::group(['middleware' => 'auth'],function(){
  37. //Route::get('/', \App\Http\Livewire\Login::class);
  38. Route::get('/dashboard', \App\Http\Livewire\Dashboard::class);
  39. Route::get('/settings', \App\Http\Livewire\Setting::class);
  40. Route::get('/courses', \App\Http\Livewire\Course::class);
  41. Route::get('/categories', \App\Http\Livewire\Category::class);
  42. Route::get('/nations_list', \App\Http\Livewire\Nation::class);
  43. Route::get('/provinces', \App\Http\Livewire\Province::class);
  44. Route::get('/cities', \App\Http\Livewire\City::class);
  45. Route::get('/banks', \App\Http\Livewire\Bank::class);
  46. Route::get('/vats', \App\Http\Livewire\Vat::class);
  47. Route::get('/disciplines', \App\Http\Livewire\Discipline::class);
  48. Route::get('/course_types', \App\Http\Livewire\CourseType::class);
  49. Route::get('/course_subscriptions', \App\Http\Livewire\CourseSubscription::class);
  50. Route::get('/course_durations', \App\Http\Livewire\CourseDuration::class);
  51. Route::get('/course_levels', \App\Http\Livewire\CourseLevel::class);
  52. Route::get('/course_frequencies', \App\Http\Livewire\CourseFrequency::class);
  53. Route::get('/course_list', \App\Http\Livewire\CourseList::class);
  54. Route::get('/course_member', \App\Http\Livewire\CourseMember::class);
  55. Route::get('/receipts', \App\Http\Livewire\Receipt::class);
  56. Route::get('/cards', \App\Http\Livewire\Card::class);
  57. Route::get('/causals', \App\Http\Livewire\Causal::class);
  58. Route::get('/payment_methods', \App\Http\Livewire\PaymentMethod::class);
  59. Route::get('/members', \App\Http\Livewire\Member::class);
  60. Route::get('/suppliers', \App\Http\Livewire\Supplier::class);
  61. Route::get('/sponsors', \App\Http\Livewire\Sponsor::class);
  62. Route::get('/records', \App\Http\Livewire\Record::class);
  63. Route::get('/reminders', \App\Http\Livewire\Reminder::class);
  64. Route::get('/in', \App\Http\Livewire\RecordIN::class);
  65. Route::get('/out', \App\Http\Livewire\RecordOUT::class);
  66. Route::get('/records_in_out', \App\Http\Livewire\RecordINOUT::class);
  67. Route::get('/users', \App\Http\Livewire\User::class);
  68. Route::get('/profile', \App\Http\Livewire\Profile::class);
  69. });
  70. Route::get('/receipt/{id}', function($id){
  71. $receipt = \App\Models\Receipt::findOrFail($id);
  72. $pdf = PDF::loadView('receipt', array('receipt' => $receipt));
  73. $pdfName = "Ricevuta_" . $receipt->member->last_name . "_" . $receipt->number . "_" . $receipt->year . ".pdf";
  74. return $pdf->stream($pdfName);
  75. /*return response()->streamDownload(
  76. fn () => print($pdf),
  77. "ricevuta_" . $receipt->number . "_" . $receipt->year . ".pdf"
  78. );*/
  79. });
  80. Route::get('/receipt/mail/{id}', function($id){
  81. $receipt = \App\Models\Receipt::findOrFail($id);
  82. if ($receipt->status == 99)
  83. sendReceiptDeleteEmail($receipt);
  84. else
  85. sendReceiptEmail($receipt);
  86. /*
  87. $pdf = PDF::loadView('receipt', array('receipt' => $receipt));
  88. $pdfName = "ricevuta_" . $receipt->number . "_" . $receipt->year . ".pdf";
  89. Storage::put('public/pdf/' . $pdfName, $pdf->output());
  90. $email = \App\Models\Member::findOrFail($receipt->member_id)->email;
  91. if ($email != '')
  92. {
  93. Mail::to($email)->send(new \App\Mail\ReceipEmail([
  94. 'name' => 'Luca',
  95. 'pdf' => 'public/pdf/' . $pdfName,
  96. 'number' => $receipt->number . "/" . $receipt->year
  97. ]));
  98. }
  99. */
  100. return true;
  101. //return $pdf->stream();
  102. /*return response()->streamDownload(
  103. fn () => print($pdf),
  104. "ricevuta_" . $receipt->number . "_" . $receipt->year . ".pdf"
  105. );*/
  106. });
  107. Route::get('/nations', function(){
  108. if (isset($_GET["q"]))
  109. $datas = \App\Models\Nation::where('name', 'like', $_GET["q"] . '%')->orderBy('name')->get();
  110. else
  111. $datas = \App\Models\Nation::orderBy('name')->get();
  112. $data = array();
  113. foreach($datas as $d)
  114. {
  115. $data[] = array("id" => $d->id, "text" => $d->name);
  116. }
  117. return array("results" => $data);
  118. });
  119. Route::get('/provinces/{nation_id}', function($nation_id){
  120. if (isset($_GET["q"]))
  121. $datas = \App\Models\Province::where('nation_id', $nation_id)->where('name', 'like', $_GET["q"] . '%')->orderBy('name')->get();
  122. else
  123. $datas = \App\Models\Province::where('nation_id', $nation_id)->orderBy('name')->get();
  124. $data = array();
  125. foreach($datas as $d)
  126. {
  127. $data[] = array("id" => $d->id, "text" => $d->name);
  128. }
  129. return array("results" => $data);
  130. });
  131. Route::get('/cities/{province_id}', function($province_id){
  132. if (isset($_GET["q"]))
  133. $datas = \App\Models\City::where('province_id', $province_id)->where('name', 'like', $_GET["q"] . '%')->orderBy('name')->get();
  134. else
  135. $datas = \App\Models\City::where('province_id', $province_id)->orderBy('name')->get();
  136. $data = array();
  137. foreach($datas as $d)
  138. {
  139. $data[] = array("id" => $d->id, "text" => $d->name);
  140. }
  141. return array("results" => $data);
  142. });
  143. Route::get('/get_members', function(){
  144. $datas = [];
  145. // $datas = \App\Models\Member::select('members.*')->where('id', '>', 0);
  146. $x = \App\Models\Member::select('id', 'first_name', 'last_name', 'phone', 'birth_date', 'to_complete', 'current_status', 'certificate', 'certificate_date')->where('id', '>', 0);
  147. if (isset($_GET["search"]["value"]))
  148. {
  149. $v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
  150. $x = $x->where(function ($query) use ($v) {
  151. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $v . "%'")
  152. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $v . "%'");
  153. });
  154. //where('first_name', 'like', '%' . $_GET["search"]["value"] . '%');
  155. }
  156. if ($_GET["cards"] != "")
  157. {
  158. $card_ids = \App\Models\MemberCard::whereIn('card_id', explode(",", $_GET["cards"]))->pluck('member_id');
  159. $x = $x->whereIn('id', $card_ids);
  160. }
  161. if ($_GET["filterCategories"] != "null")
  162. {
  163. $categories = [];
  164. $cats = explode(",", $_GET["filterCategories"]);
  165. // Per ogni causale, se ha dei figli allora aggiungo le causali figlio
  166. foreach($cats as $c)
  167. {
  168. $categories[] = $c;
  169. $childs = \App\Models\Category::where('parent_id', $c)->get();
  170. foreach($childs as $_cc)
  171. {
  172. $categories[] = $_cc->id;
  173. $childss = \App\Models\Category::where('parent_id', $_cc->id)->get();
  174. foreach($childss as $ccc)
  175. {
  176. $categories[] = $ccc->id;
  177. }
  178. }
  179. }
  180. $cc = array();
  181. foreach($categories as $c)
  182. {
  183. $m_ids = \App\Models\MemberCategory::where('category_id', $c)->pluck('member_id')->toArray();
  184. /*if (sizeof($cc) > 0)
  185. $cc = array_intersect($cc, $m_ids);
  186. else
  187. $cc = $m_ids;
  188. */
  189. $cc = array_merge($cc, $m_ids);
  190. }
  191. $x = $x->whereIn('id', $cc);
  192. //$cats_ids = \App\Models\MemberCategory::whereIn('category_id', explode(",", $_GET["filterCategories"]))->pluck('member_id');
  193. //$x = $x->whereIn('id', $cats_ids);
  194. }
  195. if ($_GET["fromYear"] != "")
  196. {
  197. $x = $x->where('birth_date', '<', date("Y-m-d", strtotime("-" . $_GET["fromYear"] . " year", time())));
  198. }
  199. if ($_GET["toYear"] != "")
  200. {
  201. $x = $x->where('birth_date', '>', date("Y-m-d", strtotime("-" . $_GET["toYear"] . " year", time())));
  202. }
  203. if ($_GET["fromYearYear"] != "")
  204. {
  205. $x = $x->whereYear('birth_date', '>=', $_GET["fromYearYear"]);
  206. }
  207. if ($_GET["toYearYear"] != "")
  208. {
  209. $x = $x->whereYear('birth_date', '<=', $_GET["toYearYear"]);
  210. }
  211. $ids = [];
  212. if ($_GET["filterCertificateType"] != "null")
  213. {
  214. $types = \App\Models\MemberCertificate::where('type', $_GET["filterCertificateType"])->where('expire_date', '>', date("Y-m-d"))->pluck('member_id');
  215. $x = $x->whereIn('id', $types->toArray());;
  216. //$ids = array_merge($ids, $types->toArray());
  217. }
  218. if ($_GET["filterScadenza"] != "null") {
  219. $scadenzaValues = explode(",", $_GET["filterScadenza"]);
  220. $allScadIds = [];
  221. foreach ($scadenzaValues as $filterValue) {
  222. if ($filterValue == "1")
  223. $scadIds = \App\Models\MemberCertificate::where('expire_date', '<', date("Y-m-d"))->pluck('member_id')->toArray();
  224. else if ($filterValue == "2")
  225. $scadIds = \App\Models\MemberCertificate::where('expire_date', '>=', date("Y-m-d"))
  226. ->where('expire_date', '<=', date("Y-m-d", strtotime("+1 month")))
  227. ->pluck('member_id')->toArray();
  228. else if ($filterValue == "3")
  229. $scadIds = \App\Models\Member::whereNotIn('id', \App\Models\MemberCertificate::pluck('member_id'))->pluck('id')->toArray();
  230. else if ($filterValue == "4")
  231. $scadIds = \App\Models\MemberCertificate::where('expire_date', '>', date("Y-m-d", strtotime("+1 month")))->pluck('member_id')->toArray();
  232. $allScadIds = array_merge($allScadIds, $scadIds);
  233. }
  234. $allScadIds = array_unique($allScadIds);
  235. $x = $x->whereIn('id', $allScadIds);
  236. }
  237. if ($_GET["filterStatus"] != "null")
  238. {
  239. $status = explode(",", $_GET["filterStatus"]);
  240. $members = \App\Models\Member::all();
  241. foreach($status as $s)
  242. {
  243. foreach($members as $m)
  244. {
  245. $state = $m->isActive();
  246. if ($state["status"] == $s)
  247. $ids[] = $m->id;
  248. }
  249. }
  250. }
  251. if (sizeof($ids) > 0)
  252. {
  253. $x = $x->whereIn('id', $ids);
  254. }
  255. else
  256. {
  257. if ($_GET["filterStatus"] != "null")
  258. $x = $x->whereIn('id', [-1]);
  259. }
  260. $count = $x->count();
  261. $x = $x->orderBy('to_complete', 'DESC');
  262. if (isset($_GET["order"]))
  263. {
  264. $column = '';
  265. if ($_GET["order"][0]["column"] == 0)
  266. $column = 'last_name';
  267. if ($_GET["order"][0]["column"] == 1)
  268. $column = 'first_name';
  269. if ($_GET["order"][0]["column"] == 2)
  270. $column = 'phone';
  271. if ($_GET["order"][0]["column"] == 3)
  272. $column = 'birth_date';
  273. if ($_GET["order"][0]["column"] == 4)
  274. $column = 'birth_date';
  275. if ($_GET["order"][0]["column"] == 5)
  276. $column = 'current_status';
  277. if ($_GET["order"][0]["column"] == 6)
  278. $column = 'certificate';
  279. if ($column != '')
  280. {
  281. $x = $x->orderBy($column, $_GET["order"][0]["dir"]);
  282. if ($column == 'certificate')
  283. $x = $x->orderBy('certificate_date', $_GET["order"][0]["dir"]);
  284. }
  285. else
  286. $x = $x->orderBy('last_name', 'ASC')->orderBy('first_name', 'ASC');
  287. }
  288. else
  289. $x = $x->orderBy('last_name', 'ASC')->orderBy('first_name', 'ASC');
  290. if (isset($_GET["start"]))
  291. $x = $x->offset($_GET["start"])->limit($_GET["length"])->get();
  292. else
  293. $x = $x->get();
  294. foreach($x as $idx => $r)
  295. {
  296. // $status = $r->getStatus();
  297. // $status = $status["status"];
  298. $status = $r->current_status;
  299. $class = $status > 0 ? ($status == 2 ? 'active' : 'due') : 'suspended';
  300. $text = $status > 0 ? ($status == 2 ? 'Tesserato' : 'Sospeso') : 'Non tesserato';
  301. if ($r->to_complete)
  302. {
  303. $text = 'Da completare';
  304. $class = "complete";
  305. }
  306. // $has_certificate = $r->hasCertificate();
  307. $y = '';
  308. if ($r->certificate_date != '') {
  309. $status = '';
  310. if ($r->certificate_date < date("Y-m-d")) {
  311. $status = "0";
  312. } else if ($r->certificate_date >= date("Y-m-d") && $r->certificate_date <= date("Y-m-d", strtotime("+1 month"))) {
  313. $status = "1";
  314. } else if ($r->certificate_date > date("Y-m-d", strtotime("+1 month"))) {
  315. $status = "2";
  316. }
  317. $y = $status . "|" . date("d/m/Y", strtotime($r->certificate_date));
  318. }
  319. /*
  320. if($has_certificate["date"] != '')
  321. {
  322. if($has_certificate["date"] < date("Y-m-d"))
  323. $y .= '0';
  324. if($has_certificate["date"] >= date("Y-m-d") && $has_certificate["date"] < date("Y-m-d", strtotime("+1 month")))
  325. $y .= '1';
  326. if($has_certificate["date"] >= date("Y-m-d", strtotime("+1 month")))
  327. $y .= '2';
  328. $y .= '|';
  329. $y .= $has_certificate["date"] != '' ? date("d/m/Y", strtotime($has_certificate["date"])) : '';
  330. }*/
  331. $datas[] = array(
  332. //'c' => $idx + 1,
  333. //'id' => "ID" . str_pad($r->id, 5, "0", STR_PAD_LEFT),
  334. 'last_name' => $r->last_name . "|" . $r->id,
  335. 'first_name' => $r->first_name . "|" . $r->id,
  336. 'phone' => $r->phone,
  337. 'age' => $r->getAge(),
  338. 'year' => date("Y", strtotime($r->birth_date)),
  339. 'status' => $class . "|" . $text,
  340. // 'state' => $x,
  341. 'certificate' => $y,
  342. 'action' => $r->id
  343. );
  344. }
  345. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count));
  346. });
  347. Route::get('/get_record_in', function(){
  348. $datas = [];
  349. $x = \App\Models\Record::select('records.*', \DB::raw('members.first_name as first_name'), \DB::raw('members.last_name as last_name'), \DB::raw('payment_methods.name as payment'), \DB::raw('receipts.created_at as receipt_date')) // , \DB::raw('SUM(records.id) As total'))
  350. ->leftJoin('members', 'records.member_id', '=', 'members.id')
  351. ->leftJoin('payment_methods', 'records.payment_method_id', '=', 'payment_methods.id')
  352. ->leftJoin('receipts', 'records.id', '=', 'receipts.record_id')
  353. ->where('records.type', 'IN');
  354. $y = \App\Models\Record::select('records_rows.amount', 'records.member_id', 'records.corrispettivo_fiscale', 'records.deleted', 'records.financial_movement', 'records_rows.causal_id', \DB::raw('members.first_name as first_name'), \DB::raw('members.last_name as last_name')) // , \DB::raw('SUM(records.id) As total'))
  355. ->leftJoin('members', 'records.member_id', '=', 'members.id')
  356. ->leftJoin('records_rows', 'records.id', '=', 'records_rows.record_id')
  357. //->leftJoin('receipts', 'records.id', '=', 'receipts.record_id')
  358. ->where('records.type', 'IN');
  359. $hasFilter = false;
  360. if (isset($_GET["search"]["value"]))
  361. {
  362. if ($_GET["search"]["value"] != '')
  363. {
  364. $hasFilter = true;
  365. $v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
  366. $x = $x->where(function ($query) use ($v) {
  367. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $v . "%'")
  368. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $v . "%'");
  369. });
  370. $y = $y->where(function ($query) use ($v) {
  371. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $v . "%'")
  372. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $v . "%'");
  373. });
  374. }
  375. /*$v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
  376. $x = $x->where(function ($query) use ($v) {
  377. $query->where('first_name', 'like', '%' . $v . '%')
  378. ->orWhere('last_name', 'like', '%' . $v . '%');
  379. });
  380. $y = $y->where(function ($query) use ($v) {
  381. $query->where('first_name', 'like', '%' . $v . '%')
  382. ->orWhere('last_name', 'like', '%' . $v . '%');
  383. });
  384. }*/
  385. //where('first_name', 'like', '%' . $_GET["search"]["value"] . '%');
  386. }
  387. //$x = $x->where(function ($query) use ($v) {
  388. // $datas = \App\Models\Record::where('type', 'IN')->with('member', 'payment_method');
  389. if ($_GET["filterCommercial"] == 1)
  390. {
  391. $hasFilter = true;
  392. $x = $x->where('records.commercial', true );
  393. $y = $y->where('records.commercial', true );
  394. }
  395. if ($_GET["filterCommercial"] == 2)
  396. {
  397. $hasFilter = true;
  398. $x = $x->where('records.commercial', false);
  399. $y = $y->where('records.commercial', false);
  400. }
  401. if ($_GET["filterMember"] > 0)
  402. {
  403. $hasFilter = true;
  404. $x = $x->where('records.member_id', $_GET["filterMember"]);
  405. $y = $y->where('records.member_id', $_GET["filterMember"]);
  406. }
  407. if ($_GET["filterPaymentMethod"] != "null")
  408. {
  409. $hasFilter = true;
  410. $payments = explode(",", $_GET["filterPaymentMethod"]);
  411. $x = $x->whereIn('records.payment_method_id', $payments);
  412. $y = $y->whereIn('records.payment_method_id', $payments);
  413. }
  414. if ($_GET["filterCausals"] != "null")
  415. {
  416. $hasFilter = true;
  417. $causals = explode(",", $_GET["filterCausals"]);
  418. // Per ogni causale, se ha dei figli allora aggiungo le causali figlio
  419. foreach($causals as $c)
  420. {
  421. $childs = \App\Models\Causal::where('parent_id', $c)->get();
  422. foreach($childs as $cc)
  423. {
  424. $causals[] = $cc->id;
  425. $childss = \App\Models\Causal::where('parent_id', $cc->id)->get();
  426. foreach($childss as $ccc)
  427. {
  428. $causals[] = $ccc->id;
  429. }
  430. }
  431. }
  432. //$causals = \App\Models\RecordRow::where('causal_id', $_GET["filterCausals"])->pluck('record_id');
  433. $causals = \App\Models\RecordRow::whereIn('causal_id', $causals)->pluck('record_id');
  434. $x = $x->whereIn('records.id', $causals);
  435. $y = $y->whereIn('records.id', $causals);
  436. }
  437. if ($_GET["filterFrom"] != '')
  438. {
  439. $hasFilter = true;
  440. $x = $x->where('records.date', '>=', $_GET["filterFrom"]);
  441. $y = $y->where('date', '>=', $_GET["filterFrom"]);
  442. }
  443. if ($_GET["filterTo"] != '')
  444. {
  445. $hasFilter = true;
  446. $x = $x->where('records.date', '<=', $_GET["filterTo"]);
  447. $y = $y->where('date', '<=', $_GET["filterTo"]);
  448. }
  449. //});
  450. $start = 0;
  451. $limit = 100000;
  452. if (isset($_GET["start"]))
  453. {
  454. $start = $_GET["start"];
  455. $limit = $_GET["length"];
  456. }
  457. $excludeCausals = [];
  458. /*$borsellino = \App\Models\Causal::where('money', true)->first();
  459. if ($borsellino)
  460. $excludeCausals[] = $borsellino->id;*/
  461. // Aggiungo
  462. $excludes = \App\Models\Causal::where('no_records', true)->get();
  463. foreach($excludes as $e)
  464. {
  465. $excludeCausals[] = $e->id;
  466. }
  467. $exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
  468. // Pagamento money
  469. $moneys = \App\Models\PaymentMethod::where('money', true)->pluck('id')->toArray();
  470. // Causale money
  471. $moneysCausal = \App\Models\Causal::where('money', true)->pluck('id')->toArray();
  472. $total = 0;
  473. $causals = [];
  474. if ($_GET["filterCausals"] != "null")
  475. $causals = explode(",", $_GET["filterCausals"]);
  476. foreach($y->get() as $r)
  477. {
  478. if (!in_array($r->payment_method_id, $moneys))
  479. {
  480. 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))
  481. {
  482. if (sizeof($causals) == 0 || in_array($r->causal_id, $causals))
  483. {
  484. $total += $r->amount;
  485. if ($r->vat_id > 0)
  486. $total += getVatValue($r->amount, $r->vat_id);
  487. }
  488. }
  489. }
  490. }
  491. $count = $x->count();
  492. if (isset($_GET["order"]))
  493. {
  494. $column = '';
  495. if ($_GET["order"][0]["column"] == 0)
  496. $column = 'date';
  497. if ($_GET["order"][0]["column"] == 1)
  498. $column = 'records.amount';
  499. if ($_GET["order"][0]["column"] == 2)
  500. $column = 'last_name';
  501. if ($_GET["order"][0]["column"] == 3)
  502. $column = 'first_name';
  503. if ($_GET["order"][0]["column"] == 4)
  504. $column = 'commercial';
  505. if ($column != '')
  506. $x = $x->orderBy($column, $_GET["order"][0]["dir"])->orderBy('records.id', 'DESC');
  507. else
  508. $x = $x->orderBy('records.id', 'DESC');
  509. }
  510. else
  511. $x = $x->orderBy('records.date', 'DESC')->orderBy('records.id', 'DESC');
  512. $x = $x->offset($start)->limit($limit)->get();
  513. foreach($x as $idx => $r)
  514. {
  515. $causals = '';
  516. foreach($r->rows as $row)
  517. {
  518. $causals .= $row->causal->getTree() . "<br>";
  519. }
  520. $datas[] = array(
  521. //'id' => $r->id,
  522. 'date' => $r->date,
  523. //'date' => $r->receipt_date != null ? $r->receipt_date : "",
  524. 'total' => formatPrice($r->getTotal()),
  525. 'first_name' => $r->first_name,
  526. 'last_name' => $r->last_name,
  527. 'commercial' => $r->financial_movement ? 'Movimento finanziario' : ($r->commercial ? 'SI' : 'NO'),
  528. 'causals' => $causals,
  529. 'payment' => $r->payment_method->name,
  530. //'payment_date' => date("d/m/Y", strtotime($r->date)),
  531. 'status' => $r->deleted ? 'Annullato' : '',
  532. 'action' => $r->id . "||" . ($r->deleted ? 'x' : '')
  533. );
  534. }
  535. /*$datas[] = array(
  536. //'id' => $r->id,
  537. 'date' => '',
  538. 'total' => formatPrice($total),
  539. 'first_name' => '',
  540. 'last_name' => '',
  541. 'commercial' => '',
  542. 'causals' => '',
  543. 'payment' => '',
  544. 'status' => '',
  545. 'action' => ''
  546. );*/
  547. if ($hasFilter)
  548. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count, "totals" => formatPrice($total)));
  549. else
  550. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count));
  551. });
  552. Route::get('/get_record_out', function(){
  553. $datas = [];
  554. $hasFilter = false;
  555. $x = \App\Models\Record::where('type', 'OUT')->with('supplier', 'payment_method');
  556. if ($_GET["filterSupplier"] > 0)
  557. {
  558. $x = $x->where('supplier_id', $_GET["filterSupplier"]);
  559. $hasFilter = true;
  560. }
  561. /*if ($_GET["filterPaymentMethod"] > 0)
  562. {
  563. $x = $x->where('payment_method_id', $_GET["filterPaymentMethod"]);
  564. }
  565. if ($_GET["filterCausals"] > 0)
  566. {
  567. $causals = \App\Models\RecordRow::where('causal_id', $_GET["filterCausals"])->pluck('record_id');
  568. $x = $x->whereIn('records.id', $causals);
  569. }*/
  570. if ($_GET["filterPaymentMethod"] != "null")
  571. {
  572. $payments = explode(",", $_GET["filterPaymentMethod"]);
  573. $x = $x->whereIn('payment_method_id', $payments);
  574. $hasFilter = true;
  575. }
  576. if ($_GET["filterCausals"] != "null")
  577. {
  578. $causals = explode(",", $_GET["filterCausals"]);
  579. // Per ogni causale, se ha dei figli allora aggiungo le causali figlio
  580. foreach($causals as $c)
  581. {
  582. $childs = \App\Models\Causal::where('parent_id', $c)->get();
  583. foreach($childs as $cc)
  584. {
  585. $causals[] = $cc->id;
  586. $childss = \App\Models\Causal::where('parent_id', $cc->id)->get();
  587. foreach($childss as $ccc)
  588. {
  589. $causals[] = $ccc->id;
  590. }
  591. }
  592. }
  593. //$causals = \App\Models\RecordRow::where('causal_id', $_GET["filterCausals"])->pluck('record_id');
  594. $causals = \App\Models\RecordRow::whereIn('causal_id', $causals)->pluck('record_id');
  595. $x = $x->whereIn('records.id', $causals);
  596. $hasFilter = true;
  597. }
  598. if ($_GET["filterFrom"] != '')
  599. {
  600. $x = $x->where('date', '>=', $_GET["filterFrom"]);
  601. $hasFilter = true;
  602. }
  603. if ($_GET["filterTo"] != '')
  604. {
  605. $x = $x->where('date', '<=', $_GET["filterTo"]);
  606. $hasFilter = true;
  607. }
  608. $total = 0;
  609. foreach($x->get() as $r)
  610. {
  611. foreach($r->rows as $rr)
  612. {
  613. $total += $rr->amount;
  614. if ($rr->vat_id > 0)
  615. $total += getVatValue($rr->amount, $rr->vat_id);
  616. }
  617. }
  618. $x = $x->get();
  619. foreach($x as $idx => $r)
  620. {
  621. $causals = '';
  622. foreach($r->rows as $row)
  623. {
  624. $causals .= $row->causal->getTree() . "<br>";
  625. }
  626. $datas[] = array(
  627. //'id' => $r->id,
  628. 'date' => $r->date,
  629. 'total' => formatPrice($r->getTotal()),
  630. 'supplier' => $r->supplier->name,
  631. 'causals' => $causals,
  632. 'payment' => $r->payment_method->name,
  633. 'action' => $r->id . "|"
  634. );
  635. }
  636. /*
  637. $datas[] = array(
  638. //'id' => $r->id,
  639. 'date' => '',
  640. 'total' => formatPrice($total),
  641. 'supplier' => '',
  642. 'causals' => '',
  643. 'payment' => '',
  644. 'action' => ''
  645. );
  646. */
  647. if ($hasFilter)
  648. return json_encode(array("data" => $datas, "totals" => formatPrice($total)));
  649. else
  650. return json_encode(array("data" => $datas));
  651. });
  652. Route::get('/get_course_list', function(){
  653. $member_course = \App\Models\MemberCourse::with('member')->with('course');
  654. if (isset($_GET["search"]["value"]))
  655. {
  656. if ($_GET["search"]["value"] != '')
  657. {
  658. $v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
  659. $member_ids = \App\Models\Member::where(function ($query) use ($v) {
  660. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $v . "%'")
  661. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $v . "%'");
  662. })->pluck('id');
  663. /*
  664. $v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
  665. $member_ids = \App\Models\Member::where(function ($query) use ($v) {
  666. $query->where('first_name', 'like', '%' . $v . '%')
  667. ->orWhere('last_name', 'like', '%' . $v . '%');
  668. })->pluck('id');*/
  669. $member_course = $member_course->whereIn('member_id', $member_ids);
  670. }
  671. }
  672. if ($_GET["filterCourse"] != "null")
  673. {
  674. $course_ids = [];
  675. $courses = explode(",", $_GET["filterCourse"]);
  676. foreach($courses as $c)
  677. {
  678. $all = \App\Models\Course::where('name', 'like', '%' . $c . "%")->get();
  679. foreach($all as $a)
  680. {
  681. $course_ids[] = $a->id;
  682. }
  683. }
  684. $member_course = $member_course->whereIn('course_id', $course_ids);
  685. }
  686. if ($_GET["filterYear"] != "")
  687. {
  688. $course_ids = \App\Models\Course::where('year', $_GET["filterYear"])->pluck('id');
  689. $member_course = $member_course->whereIn('course_id', $course_ids);
  690. }
  691. if ($_GET["filterLevel"] != "null")
  692. {
  693. $levels = explode(",", $_GET["filterLevel"]);
  694. $course_ids = \App\Models\Course::whereIn('course_level_id', $levels)->pluck('id');
  695. $member_course = $member_course->whereIn('course_id', $course_ids);
  696. }
  697. if ($_GET["filterFrequency"] != "null")
  698. {
  699. $frequencies = explode(",", $_GET["filterFrequency"]);
  700. $course_ids = \App\Models\Course::whereIn('course_frequency_id', $frequencies)->pluck('id');
  701. $member_course = $member_course->whereIn('course_id', $course_ids);
  702. }
  703. if ($_GET["filterType"] != "null")
  704. {
  705. $types = explode(",", $_GET["filterType"]);
  706. $course_ids = \App\Models\Course::whereIn('course_type_id', $types)->pluck('id');
  707. $member_course = $member_course->whereIn('course_id', $course_ids);
  708. }
  709. if ($_GET["filterDuration"] != "null")
  710. {
  711. $durations = explode(",", $_GET["filterDuration"]);
  712. $course_ids = \App\Models\Course::whereIn('course_duration_id', $durations)->pluck('id');
  713. $member_course = $member_course->whereIn('course_id', $course_ids);
  714. }
  715. $totals = [];
  716. $totalIsc = [];
  717. $datas = [];
  718. $xxx = 1;
  719. /*
  720. $sortColumn = '';
  721. if (isset($_GET["order"]))
  722. {
  723. }
  724. */
  725. $column = '';
  726. $sort_value = 0;
  727. if (isset($_GET["order"]))
  728. {
  729. $f = $_GET["order"][0]["column"];
  730. $d = $_GET["order"][0]["dir"];
  731. if ($f >= 5 && $f <= 16)
  732. {
  733. $column = 'column_' . ($f - 2);
  734. if (session()->get('sort_column'))
  735. {
  736. if (session()->get('sort_column') != $f)
  737. {
  738. session()->put('sort_column', $f);
  739. session()->put('sort_order', $d);
  740. session()->put('sort_value', 0);
  741. $sort_value = 0;
  742. }
  743. else
  744. {
  745. if (session()->get('sort_order') == $d)
  746. {
  747. //session()->put('sort_value', 0);
  748. $sort_value = session()->get('sort_value', 0);
  749. }
  750. else
  751. {
  752. if (session()->get('sort_value', 0) == 0)
  753. {
  754. $sort_value = 1;
  755. }
  756. if (session()->get('sort_value', 0) == 1)
  757. {
  758. $sort_value = 2;
  759. }
  760. if (session()->get('sort_value', 0) == 2)
  761. {
  762. $sort_value = 3;
  763. }
  764. if (session()->get('sort_value', 0) == 3)
  765. {
  766. $sort_value = 0;
  767. }
  768. session()->put('sort_value', $sort_value);
  769. }
  770. session()->put('sort_order', $d);
  771. }
  772. }
  773. else
  774. {
  775. session()->put('sort_column', $f);
  776. session()->put('sort_order', $d);
  777. session()->put('sort_value', 0);
  778. $sort_value = 0;
  779. }
  780. }
  781. }
  782. //print $sort_value;
  783. $member_course_totals = $member_course->get();
  784. foreach($member_course_totals as $x)
  785. {
  786. $price = 0;
  787. $price = $x->price; // $x->course->price;
  788. $subPrice = $x->subscription_price; // $x->course->subscription_price;
  789. $records = \App\Models\Record::where('member_course_id', $x->id)->where('deleted', 0)->get();
  790. $prices = [];
  791. foreach ($records as $record)
  792. {
  793. foreach ($record->rows as $row)
  794. {
  795. if ($row->causal_id == $x->course->sub_causal_id) // || str_contains(strtolower($row->note), 'iscrizione'))
  796. //if (str_contains(strtolower($row->note), 'iscrizione'))
  797. {
  798. $subPrice = $row->amount;
  799. }
  800. if ($row->causal_id == $x->course->causal_id && !str_contains(strtolower($row->note), 'iscrizione'))
  801. {
  802. $tot = sizeof(json_decode($row->when));
  803. foreach(json_decode($row->when) as $m)
  804. {
  805. $prices[$m->month] = $row->amount / $tot;
  806. }
  807. }
  808. }
  809. }
  810. for($i=1; $i<=12; $i++)
  811. {
  812. $cls = getColor($x->months, $i);
  813. if ($cls != 'wgrey')
  814. {
  815. if (!isset($totals[$i]))
  816. {
  817. $totals[$i]['green'] = 0;
  818. $totals[$i]['orange'] = 0;
  819. $totals[$i]['yellow'] = 0;
  820. }
  821. if ($cls == 'yellow')
  822. {
  823. $totals[$i][$cls] += 1;
  824. }
  825. else
  826. {
  827. $p = isset($prices[$i]) ? $prices[$i] : $price;
  828. $totals[$i][$cls] += $p;
  829. }
  830. }
  831. }
  832. $sub = $x->subscribed ? "Y" : "N";
  833. if (isset($totalIsc[$sub]))
  834. $totalIsc[$sub] += $subPrice;
  835. else
  836. $totalIsc[$sub] = $subPrice;
  837. $s = 0;
  838. if ($column != '')
  839. {
  840. $z = 0;
  841. switch ($column) {
  842. case 'column_3':
  843. $z = 9;
  844. break;
  845. case 'column_4':
  846. $z = 10;
  847. break;
  848. case 'column_5':
  849. $z = 11;
  850. break;
  851. case 'column_6':
  852. $z = 12;
  853. break;
  854. case 'column_7':
  855. $z = 1;
  856. break;
  857. case 'column_8':
  858. $z = 2;
  859. break;
  860. case 'column_9':
  861. $z = 3;
  862. break;
  863. case 'column_10':
  864. $z = 4;
  865. break;
  866. case 'column_11':
  867. $z = 5;
  868. break;
  869. case 'column_12':
  870. $z = 6;
  871. break;
  872. case 'column_13':
  873. $z = 7;
  874. break;
  875. case 'column_14':
  876. $z = 8;
  877. break;
  878. default:
  879. $z = 0;
  880. break;
  881. }
  882. $c = getColor($x->months, $z);
  883. if ($sort_value == 0)
  884. {
  885. switch ($c) {
  886. case 'wgrey':
  887. $s = 0;
  888. break;
  889. case 'orange':
  890. $s = 1;
  891. break;
  892. case 'green':
  893. $s = 2;
  894. break;
  895. case 'yellow':
  896. $s = 3;
  897. break;
  898. default:
  899. $s = 0;
  900. break;
  901. }
  902. }
  903. if ($sort_value == 1)
  904. {
  905. switch ($c) {
  906. case 'wgrey':
  907. $s = 3;
  908. break;
  909. case 'orange':
  910. $s = 0;
  911. break;
  912. case 'green':
  913. $s = 1;
  914. break;
  915. case 'yellow':
  916. $s = 2;
  917. break;
  918. default:
  919. $s = 0;
  920. break;
  921. }
  922. }
  923. if ($sort_value == 2)
  924. {
  925. switch ($c) {
  926. case 'wgrey':
  927. $s = 2;
  928. break;
  929. case 'orange':
  930. $s = 3;
  931. break;
  932. case 'green':
  933. $s = 0;
  934. break;
  935. case 'yellow':
  936. $s = 1;
  937. break;
  938. default:
  939. $s = 0;
  940. break;
  941. }
  942. }
  943. if ($sort_value == 3)
  944. {
  945. switch ($c) {
  946. case 'wgrey':
  947. $s = 1;
  948. break;
  949. case 'orange':
  950. $s = 2;
  951. break;
  952. case 'green':
  953. $s = 3;
  954. break;
  955. case 'yellow':
  956. $s = 0;
  957. break;
  958. default:
  959. $s = 0;
  960. break;
  961. }
  962. }
  963. }
  964. $datas[] = array(
  965. "column_19" => $x->course->name,
  966. "column_0" => $x->member->last_name,
  967. "column_1" => $x->member->first_name,
  968. "column_2" => $x->subscribed . "§" . formatPrice($subPrice),
  969. "column_3" => getColor($x->months, 9) . "§" . formatPrice(isset($prices[9]) ? $prices[9] : $price),
  970. "column_4" => getColor($x->months, 10) . "§" . formatPrice(isset($prices[10]) ? $prices[10] : $price),
  971. "column_5" => getColor($x->months, 11) . "§" . formatPrice(isset($prices[11]) ? $prices[11] : $price),
  972. "column_6" => getColor($x->months, 12) . "§" . formatPrice(isset($prices[12]) ? $prices[12] : $price),
  973. "column_7" => getColor($x->months, 1) . "§" . formatPrice(isset($prices[1]) ? $prices[1] : $price),
  974. "column_8" => getColor($x->months, 2) . "§" . formatPrice(isset($prices[2]) ? $prices[2] : $price),
  975. "column_9" => getColor($x->months, 3) . "§" . formatPrice(isset($prices[3]) ? $prices[3] : $price),
  976. "column_10" => getColor($x->months, 4) . "§" . formatPrice(isset($prices[4]) ? $prices[4] : $price),
  977. "column_11" => getColor($x->months, 5) . "§" . formatPrice(isset($prices[5]) ? $prices[5] : $price),
  978. "column_12" => getColor($x->months, 6) . "§" . formatPrice(isset($prices[6]) ? $prices[6] : $price),
  979. "column_13" => getColor($x->months, 7) . "§" . formatPrice(isset($prices[7]) ? $prices[7] : $price),
  980. "column_14" => getColor($x->months, 8) . "§" . formatPrice(isset($prices[8]) ? $prices[8] : $price),
  981. "column_15" => $x->course_id,
  982. "column_16" => $x->id,
  983. "column_17" => $x->member_id,
  984. "column_18" => $xxx++,
  985. "column_20" => $s
  986. );
  987. }
  988. $count = $member_course->count();
  989. $js = '';
  990. $xx = 4;
  991. $str = '';
  992. if ($count > 0)
  993. {
  994. $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>";
  995. $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>";
  996. $str .= "<a style='width:100%;float:right; text-align:right; display:block;' class=yellow><small>0</small></a><br>";
  997. }
  998. $js .= $xx . "§" . $str . "_";
  999. $str = "";
  1000. foreach($totals as $z => $t)
  1001. {
  1002. if ($z == 1) $xx = 9;
  1003. if ($z == 2) $xx = 10;
  1004. if ($z == 3) $xx = 11;
  1005. if ($z == 4) $xx = 12;
  1006. if ($z == 5) $xx = 13;
  1007. if ($z == 6) $xx = 14;
  1008. if ($z == 7) $xx = 15;
  1009. if ($z == 8) $xx = 16;
  1010. if ($z == 9) $xx = 5;
  1011. if ($z == 10) $xx = 6;
  1012. if ($z == 11) $xx = 7;
  1013. if ($z == 12) $xx = 8;
  1014. $str = '';
  1015. foreach($t as $x => $c)
  1016. {
  1017. $y = $x == 'yellow' ? $c : formatPrice($c);
  1018. $str .= "<a style='width:100%;float:right; text-align:right; display:block;' class=" . $x . "><small>" . $y . "</small></a><br>";
  1019. }
  1020. $js .= $xx . "§" . $str . "_";
  1021. $xx += 1;
  1022. }
  1023. if (isset($_GET["order"]))
  1024. {
  1025. $s = $_GET["order"][0]["column"];
  1026. if ($s == 1) $s = 21;
  1027. if ($column != '')
  1028. array_multisort(array_column($datas, 'column_20'), SORT_ASC, SORT_NATURAL|SORT_FLAG_CASE, $datas);
  1029. else
  1030. array_multisort(array_column($datas, 'column_' . ($s - 2)), $_GET["order"][0]["dir"] == "asc" ? SORT_ASC : SORT_DESC, SORT_NATURAL|SORT_FLAG_CASE, $datas);
  1031. }
  1032. $xxx = 1;
  1033. foreach($datas as $yyy => $d)
  1034. {
  1035. $datas[$yyy]["column_18"] = $xxx++;
  1036. }
  1037. if (isset($_GET["start"]))
  1038. $datas = array_slice($datas, $_GET["start"], $_GET["length"]);
  1039. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count, "totals" => $js));
  1040. });
  1041. Route::get('/get_course_members', function(){
  1042. //$datas = \App\Models\MemberCourse::with('member');
  1043. $datas = \App\Models\MemberCourse::select('member_courses.*', 'courses.name as course_name', 'members.first_name', 'members.last_name', 'members.email', 'members.phone', 'members.birth_date')
  1044. ->leftJoin('courses', 'member_courses.course_id', '=', 'courses.id')
  1045. ->leftJoin('members', 'member_courses.member_id', '=', 'members.id');
  1046. if (isset($_GET["search"]["value"]))
  1047. {
  1048. $v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
  1049. $member_ids = \App\Models\Member::where(function ($query) use ($v) {
  1050. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $v . "%'")
  1051. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $v . "%'");
  1052. })->pluck('id');
  1053. /*$member_ids = \App\Models\Member::where(function ($query) use ($v) {
  1054. $query->where('first_name', 'like', '%' . $v . '%')
  1055. ->orWhere('last_name', 'like', '%' . $v . '%');
  1056. })->pluck('id');*/
  1057. $datas = $datas->whereIn('member_id', $member_ids);
  1058. }
  1059. if ($_GET["filterCourse"] != "null")
  1060. {
  1061. $course_ids = [];
  1062. $courses = explode(",", $_GET["filterCourse"]);
  1063. foreach($courses as $c)
  1064. {
  1065. $all = \App\Models\Course::where('name', 'like', '%' . $c . "%")->get();
  1066. foreach($all as $a)
  1067. {
  1068. $course_ids[] = $a->id;
  1069. }
  1070. }
  1071. $datas = $datas->whereIn('course_id', $course_ids);
  1072. }
  1073. if ($_GET["filterLevel"] != "null")
  1074. {
  1075. $levels = explode(",", $_GET["filterLevel"]);
  1076. $course_ids = \App\Models\Course::whereIn('course_level_id', $levels)->pluck('id');
  1077. $datas = $datas->whereIn('course_id', $course_ids);
  1078. }
  1079. if ($_GET["filterFrequency"] != "null")
  1080. {
  1081. $frequencies = explode(",", $_GET["filterFrequency"]);
  1082. $course_ids = \App\Models\Course::whereIn('course_frequency_id', $frequencies)->pluck('id');
  1083. $datas = $datas->whereIn('course_id', $course_ids);
  1084. }
  1085. if ($_GET["filterType"] != "null")
  1086. {
  1087. $types = explode(",", $_GET["filterType"]);
  1088. $course_ids = \App\Models\Course::whereIn('course_type_id', $types)->pluck('id');
  1089. $datas = $datas->whereIn('course_id', $course_ids);
  1090. }
  1091. if ($_GET["filterDuration"] != "null")
  1092. {
  1093. $durations = explode(",", $_GET["filterDuration"]);
  1094. $course_ids = \App\Models\Course::whereIn('course_duration_id', $durations)->pluck('id');
  1095. $datas = $datas->whereIn('course_id', $course_ids);
  1096. }
  1097. if ($_GET["filterDays"] != "null")
  1098. {
  1099. $ids = [];
  1100. $days = explode(",", $_GET["filterDays"]);
  1101. foreach($days as $d)
  1102. {
  1103. $all = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")->get();
  1104. foreach($all as $a)
  1105. {
  1106. $ids[] = $a->id;
  1107. }
  1108. }
  1109. $datas = $datas->whereIn('member_courses.id', $ids);
  1110. }
  1111. if ($_GET["filterHours"] != "null")
  1112. {
  1113. $ids = [];
  1114. $hours = explode(",", $_GET["filterHours"]);
  1115. foreach($hours as $h)
  1116. {
  1117. $all = \App\Models\MemberCourse::where('when', 'like', '%"from":"' . $h . "%")->get();
  1118. foreach($all as $a)
  1119. {
  1120. $ids[] = $a->id;
  1121. }
  1122. }
  1123. $datas = $datas->whereIn('member_courses.id', $ids);
  1124. }
  1125. if ($_GET["filterSubscription"] != "")
  1126. {
  1127. $ids = \App\Models\MemberCourse::where('subscribed', $_GET["filterSubscription"] == 1 ? true : false)->pluck('id');
  1128. $datas = $datas->whereIn('member_courses.id', $ids);
  1129. //$this->filter .= $this->filter != '' ? ', ' : '';
  1130. //$this->filter .= "Pagata sottoscrizione : " . ($this->filterSubscription == 1 ? "SI" : "NO") . " ";
  1131. }
  1132. if ($_GET["filterCertificateScadenza"] != "null") {
  1133. $scadenzaValues = explode(",", $_GET["filterCertificateScadenza"]);
  1134. $allScadIds = [];
  1135. foreach ($scadenzaValues as $filterValue) {
  1136. if ($filterValue == "1") {
  1137. $scadIds = \App\Models\MemberCertificate::where('expire_date', '<', date("Y-m-d"))
  1138. ->pluck('member_id')
  1139. ->toArray();
  1140. $allScadIds = array_merge($allScadIds, $scadIds);
  1141. }
  1142. else if ($filterValue == "2") {
  1143. $scadIds = \App\Models\MemberCertificate::where('expire_date', '>=', date("Y-m-d"))
  1144. ->where('expire_date', '<=', date("Y-m-d", strtotime("+1 month")))
  1145. ->pluck('member_id')
  1146. ->toArray();
  1147. $allScadIds = array_merge($allScadIds, $scadIds);
  1148. }
  1149. else if ($filterValue == "3") {
  1150. $scadIds = \App\Models\Member::whereNotIn('id', \App\Models\MemberCertificate::pluck('member_id'))
  1151. ->pluck('id')
  1152. ->toArray();
  1153. $allScadIds = array_merge($allScadIds, $scadIds);
  1154. }
  1155. else if ($filterValue == "4") {
  1156. $scadIds = \App\Models\MemberCertificate::where('expire_date', '>', date("Y-m-d", strtotime("+1 month")))
  1157. ->pluck('member_id')
  1158. ->toArray();
  1159. $allScadIds = array_merge($allScadIds, $scadIds);
  1160. }
  1161. }
  1162. // Remove duplicates
  1163. $allScadIds = array_unique($allScadIds);
  1164. // Apply filter using the collected IDs
  1165. $datas = $datas->whereIn('member_id', $allScadIds);
  1166. }
  1167. if ($_GET["fromYear"] != "")
  1168. {
  1169. $m_ids = \App\Models\Member::where('birth_date', '<', date("Y-m-d", strtotime("-" . $_GET["fromYear"] . " year", time())))->pluck('id');
  1170. $datas = $datas->whereIn('member_id', $m_ids);
  1171. }
  1172. if ($_GET["toYear"] != "")
  1173. {
  1174. $m_ids = \App\Models\Member::where('birth_date', '>', date("Y-m-d", strtotime("-" . $_GET["toYear"] . " year", time())))->pluck('id');
  1175. $datas = $datas->whereIn('member_id', $m_ids);
  1176. }
  1177. if ($_GET["fromFromYear"] != "")
  1178. {
  1179. $m_ids = \App\Models\Member::whereYear('birth_date', '>=', $_GET["fromFromYear"])->pluck('id');
  1180. $datas = $datas->whereIn('member_id', $m_ids);
  1181. }
  1182. if ($_GET["toToYear"] != "")
  1183. {
  1184. $m_ids = \App\Models\Member::whereYear('birth_date', '<=', $_GET["toToYear"])->pluck('id');
  1185. $datas = $datas->whereIn('member_id', $m_ids);
  1186. }
  1187. if ($_GET["filterCards"] != "null")
  1188. {
  1189. $cards = explode(",", $_GET["filterCards"]);
  1190. $card_ids = \App\Models\MemberCard::whereIn('card_id', $cards)->pluck('member_id');
  1191. $datas = $datas->whereIn('member_id', $card_ids);
  1192. }
  1193. if ($_GET["filterYear"] != "")
  1194. {
  1195. $course_ids = \App\Models\Course::where('year', $_GET["filterYear"])->pluck('id');
  1196. $datas = $datas->whereIn('course_id', $course_ids);
  1197. //$this->filter .= $this->filter != '' ? ', ' : '';
  1198. //$this->filter .= "Anno : " . $this->filterYear . " ";
  1199. }
  1200. $aRet = [];
  1201. if (isset($_GET["order"]))
  1202. {
  1203. $column = '';
  1204. if ($_GET["order"][0]["column"] == 1)
  1205. $column = 'course_name';
  1206. if ($_GET["order"][0]["column"] == 2)
  1207. $column = 'last_name';
  1208. if ($_GET["order"][0]["column"] == 3)
  1209. $column = 'first_name';
  1210. if ($_GET["order"][0]["column"] == 4)
  1211. $column = 'birth_date';
  1212. if ($_GET["order"][0]["column"] == 5)
  1213. $column = 'birth_date';
  1214. if ($_GET["order"][0]["column"] == 6)
  1215. $column = 'birth_date';
  1216. if ($_GET["order"][0]["column"] == 7)
  1217. $column = 'phone';
  1218. if ($_GET["order"][0]["column"] == 8)
  1219. $column = 'certificate_expire_date';
  1220. if ($column != '')
  1221. $datas = $datas->orderBy($column, $_GET["order"][0]["dir"]);
  1222. else
  1223. $datas = $datas->orderBy('last_name', 'ASC')->orderBy('first_name', 'ASC');
  1224. }
  1225. else
  1226. $datas = $datas->orderBy('last_name', 'ASC')->orderBy('first_name', 'ASC');
  1227. if ($_GET["filterStatus"] != "null")
  1228. {
  1229. $status = explode(",", $_GET["filterStatus"]);
  1230. foreach($status as $s)
  1231. {
  1232. foreach($datas->get() as $aaa)
  1233. {
  1234. $state = \App\Models\Member::findOrFail($aaa->member_id)->isActive();
  1235. if ($state["status"] == $s)
  1236. $aRet[] = $aaa;
  1237. }
  1238. }
  1239. }
  1240. else
  1241. $aRet = $datas->get();
  1242. $ret = [];
  1243. foreach($aRet as $idx => $r)
  1244. {
  1245. $date1 = new DateTime($r->birth_date);
  1246. $date2 = new DateTime("now");
  1247. $interval = $date1->diff($date2);
  1248. $certificate = \App\Models\MemberCertificate::where('member_id', $r->member_id)
  1249. ->orderBy('expire_date', 'desc')
  1250. ->first();
  1251. $certificateInfo = "";
  1252. if ($certificate) {
  1253. // Format: status|expire_date|type
  1254. // Status: 0 = expired, 1 = expiring soon, 2 = valid
  1255. $today = new DateTime();
  1256. $expireDate = new DateTime($certificate->expire_date);
  1257. $oneMonthFromNow = (new DateTime())->modify('+1 month');
  1258. $status = "2"; // Default to valid
  1259. if ($expireDate < $today) {
  1260. $status = "0"; // Expired
  1261. } elseif ($expireDate < $oneMonthFromNow) {
  1262. $status = "1"; // Expiring soon
  1263. }
  1264. $certificateInfo = $status . "|" . date("d/m/Y", strtotime($certificate->expire_date)) . "|" . $certificate->type;
  1265. }
  1266. $ret[] = array(
  1267. "column_0" => $idx + 1,
  1268. "column_8" => $r->course_name,
  1269. "column_1" => $r->last_name,
  1270. "column_2" => $r->first_name,
  1271. "column_3" => strval($interval->y),
  1272. "column_4" => date("Y", strtotime($r->birth_date)),
  1273. "column_5" => $r->phone,
  1274. "column_6" => $certificateInfo,
  1275. "column_7" => $r->member_id
  1276. );
  1277. }
  1278. if (isset($_GET["start"]))
  1279. $ret = array_slice($ret, $_GET["start"], $_GET["length"]);
  1280. return json_encode(array("data" => $ret, "recordsTotal" => sizeof($aRet), "recordsFiltered" => sizeof($aRet)));
  1281. });
  1282. Route::get('/get_receipts', function(){
  1283. $x = \App\Models\Receipt::select('receipts.*', 'members.first_name', 'members.last_name')->leftJoin('members', 'receipts.member_id', '=', 'members.id');
  1284. if (isset($_GET["search"]["value"]))
  1285. {
  1286. $v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
  1287. $member_ids = \App\Models\Member::where(function ($query) use ($v) {
  1288. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $v . "%'")
  1289. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $v . "%'");
  1290. })->pluck('id');
  1291. /*$member_ids = \App\Models\Member::where(function ($query) use ($v) {
  1292. $query->where('first_name', 'like', '%' . $v . '%')
  1293. ->orWhere('last_name', 'like', '%' . $v . '%');
  1294. })->pluck('id');*/
  1295. $x = $x->whereIn('member_id', $member_ids);
  1296. }
  1297. if ($_GET["filterStatus"] != '')
  1298. $x = $x->where('receipts.status', $_GET["filterStatus"]);
  1299. if ($_GET["filterFrom"] != "")
  1300. $x = $x->where('date', '>=', $_GET["filterFrom"]);
  1301. if ($_GET["filterTo"] != "")
  1302. $x = $x->where('date', '<=', $_GET["filterTo"]);
  1303. $count = $x->count();
  1304. if (isset($_GET["order"]))
  1305. {
  1306. $column = '';
  1307. if ($_GET["order"][0]["column"] == 0)
  1308. $column = 'year';
  1309. if ($_GET["order"][0]["column"] == 1)
  1310. $column = 'number';
  1311. if ($_GET["order"][0]["column"] == 2)
  1312. $column = 'last_name';
  1313. if ($_GET["order"][0]["column"] == 3)
  1314. $column = 'first_name';
  1315. if ($_GET["order"][0]["column"] == 4)
  1316. $column = 'status';
  1317. if ($_GET["order"][0]["column"] == 5)
  1318. $column = 'date';
  1319. if ($column != '')
  1320. $x = $x->orderBy($column, $_GET["order"][0]["dir"])->orderBy('id', 'DESC');
  1321. else
  1322. $x = $x->orderBy('id', 'DESC');
  1323. }
  1324. else
  1325. $x = $x->orderBy('id', 'DESC');
  1326. if (isset($_GET["start"]))
  1327. $x = $x->offset($_GET["start"])->limit($_GET["length"])->get();
  1328. else
  1329. $x = $x->get();
  1330. $datas = [];
  1331. foreach($x as $idx => $r)
  1332. {
  1333. $ids = $r->id . "|" . $r->record_id;
  1334. $datas[] = array(
  1335. 'year' => $r->year,
  1336. 'number' => $r->number,
  1337. 'last_name' => $r->member->last_name,
  1338. 'first_name' => $r->member->first_name,
  1339. 'status' => $r->status,
  1340. 'date' => date("d/m/Y", strtotime($r->date)),
  1341. 'totals' => formatPrice($r->rows->sum('amount')),
  1342. 'action' => $ids
  1343. );
  1344. }
  1345. return json_encode(array("data" => $datas, "recordsTotal" => $count, "recordsFiltered" => $count));
  1346. });
  1347. function getColor($months, $m)
  1348. {
  1349. $class = "wgrey";
  1350. foreach(json_decode($months) as $mm)
  1351. {
  1352. if ($mm->m == $m)
  1353. {
  1354. if ($mm->status == "")
  1355. {
  1356. $class = "orange";
  1357. }
  1358. if ($mm->status == "1")
  1359. {
  1360. $class = "green";
  1361. }
  1362. if ($mm->status == "2")
  1363. {
  1364. $class = "yellow";
  1365. }
  1366. }
  1367. }
  1368. return $class;
  1369. }
  1370. Route::get('/migrate', function(){
  1371. \Artisan::call('migrate');
  1372. dd('migrated!');
  1373. });
  1374. Route::get('/updateData', function()
  1375. {
  1376. // Call and Artisan command from within your application.
  1377. Artisan::call('update:data');
  1378. });
  1379. Route::get('/seed', function()
  1380. {
  1381. // Call and Artisan command from within your application.
  1382. Artisan::call('db:seed');
  1383. });
  1384. Route::get('/updateCourseCausal', function(){
  1385. $member_courses = \App\Models\MemberCourse::all();
  1386. foreach($member_courses as $x)
  1387. {
  1388. $records = \App\Models\Record::where('member_course_id', $x->id)->get();
  1389. foreach ($records as $record)
  1390. {
  1391. foreach ($record->rows as $row)
  1392. {
  1393. //if ($row->causal_id == $x->course->sub_causal_id || str_contains(strtolower($row->note), 'iscrizione'))
  1394. if (str_contains(strtolower($row->note), 'iscrizione'))
  1395. {
  1396. $row->causal_id = $x->course->sub_causal_id;
  1397. $row->save();
  1398. }
  1399. }
  1400. }
  1401. }
  1402. });
  1403. Route::get('/test_sms', function()
  1404. {
  1405. $expire_date = date("Y-m-d", strtotime("+1 month"));
  1406. $expire_date_it = date("d/m/Y", strtotime("+1 month"));
  1407. $certificates = \App\Models\MemberCertificate::where('expire_date', $expire_date)->get();
  1408. foreach($certificates as $certificate)
  1409. {
  1410. //$phone = $certificate->member->phone;
  1411. $phone = '3893163265';
  1412. $params = array(
  1413. 'to' => '+39' . $phone,
  1414. 'from' => env('SMS_FROM', 'Test'),
  1415. 'message' => $certificate->member->first_name . ', il tuo certificato medico scadrà il ' . $expire_date_it,
  1416. 'format' => 'json',
  1417. );
  1418. sms_send($params);
  1419. print "Inviato";
  1420. }
  1421. /*
  1422. $params = array(
  1423. 'to' => '+393893163265',
  1424. 'from' => env('SMS_FROM', 'Test'),
  1425. 'message' => 'Prova SMS da IAO',
  1426. 'format' => 'json',
  1427. );
  1428. echo sms_send($params);
  1429. */
  1430. });