CourseList.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  5. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  6. use App\Models\MemberCourse;
  7. use Illuminate\Support\Facades\Log;
  8. class CourseList extends Component
  9. {
  10. public $records = array();
  11. public $recordsNoPaginate = array();
  12. public $courses = array();
  13. public $start = 0;
  14. public $totalRecords = 0;
  15. public $pages = 0;
  16. public $currentPage = 1;
  17. public $pageLength = 10;
  18. public $totS = [];
  19. public $totSExcel = [];
  20. public $sort = '';
  21. public $dir = '';
  22. public $hasFilter = false;
  23. public $courseId = 0;
  24. public $filterCourse = [];
  25. public $filterLevel = [];
  26. public $filterFrequency = [];
  27. public $filterType = [];
  28. public $filterDuration = [];
  29. public $course_durations = [];
  30. public $course_types = [];
  31. public $course_frequencies = [];
  32. public $course_levels = [];
  33. public $course_years = [];
  34. public $totals = [];
  35. public $totalIsc = [];
  36. public $aaa;
  37. public $months = array('Set', 'Ott', 'Nov', 'Dic', 'Gen', 'Feb', 'Mar', 'Apr', 'Mag', 'Giu', 'Lug', 'Ago');
  38. public function mount()
  39. {
  40. $this->selectedCourseId = 0;
  41. $this->selectedMemberId = 0;
  42. $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->get();
  43. $this->course_durations = \App\Models\CourseDuration::select('*')->where('enabled', true)->get();
  44. $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->get();
  45. $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->get();
  46. $this->course_years = \App\Models\Course::select('year')->where('year', '<>', '')->groupBy('year')->pluck('year');
  47. $this->courses = \App\Models\Course::orderBy('name')->groupBy('name')->pluck('name');
  48. //if (sizeof($this->courses) > 0)
  49. // $this->courseId = $this->courses[0]->id;
  50. }
  51. public function updatedfilterCourse($value){
  52. $this->emit('load-data-table');
  53. }
  54. public function updatedfilterLevel($value){
  55. $this->emit('load-data-table');
  56. }
  57. public function updatedfilterFrequency($value){
  58. $this->emit('load-data-table');
  59. }
  60. public function updatedfilterType($value){
  61. $this->emit('load-data-table');
  62. }
  63. public function updatedfilterDuration($value){
  64. $this->emit('load-data-table');
  65. }
  66. public function updatedpageLength($value){
  67. $this->emit('load-data-table');
  68. }
  69. public function render()
  70. {
  71. /*
  72. if ($this->hasFilter)
  73. {
  74. $member_course = \App\Models\MemberCourse::with('member');
  75. if (sizeof($this->filterCourse) > 0)
  76. {
  77. $course_ids = [];
  78. foreach($this->filterCourse as $c)
  79. {
  80. $all = \App\Models\Course::where('name', 'like', '%' . $c . "%")->get();
  81. foreach($all as $a)
  82. {
  83. $course_ids[] = $a->id;
  84. }
  85. //$course_ids = array_merge($course_ids, $a);
  86. }
  87. //$course_ids = \App\Models\Course::where('name', 'like', '%' . $this->filterCourse . "%")->pluck('id');
  88. $member_course = $member_course->whereIn('course_id', $course_ids);
  89. }
  90. if (sizeof($this->filterLevel) > 0)
  91. {
  92. $course_ids = \App\Models\Course::whereIn('course_level_id', $this->filterLevel)->pluck('id');
  93. $member_course = $member_course->whereIn('course_id', $course_ids);
  94. }
  95. if (sizeof($this->filterFrequency) > 0)
  96. {
  97. $course_ids = \App\Models\Course::whereIn('course_frequency_id', $this->filterFrequency)->pluck('id');
  98. $member_course = $member_course->whereIn('course_id', $course_ids);
  99. }
  100. if (sizeof($this->filterType) > 0)
  101. {
  102. $course_ids = \App\Models\Course::whereIn('course_type_id', $this->filterType)->pluck('id');
  103. $member_course = $member_course->whereIn('course_id', $course_ids);
  104. }
  105. if (sizeof($this->filterDuration) > 0)
  106. {
  107. $course_ids = \App\Models\Course::whereIn('course_duration_id', $this->filterDuration)->pluck('id');
  108. $member_course = $member_course->whereIn('course_id', $course_ids);
  109. }
  110. //$member_course = $member_course->where('course_id', 999999);
  111. $member_course = $member_course->get();
  112. }
  113. else
  114. {
  115. $member_course = \App\Models\MemberCourse::with('member')->get();
  116. }
  117. $this->records = [];
  118. $this->totals = [];
  119. $this->totalIsc = [];
  120. foreach($member_course as $x)
  121. {
  122. $price = 0;
  123. $price = $x->course->price;
  124. $subPrice = $x->course->subscription_price;
  125. $records = \App\Models\Record::where('member_course_id', $x->id)->where('deleted', 0)->get();
  126. $prices = [];
  127. foreach ($records as $record)
  128. {
  129. foreach ($record->rows as $row)
  130. {
  131. //if ($row->causal_id == $x->course->sub_causal_id || str_contains(strtolower($row->note), 'iscrizione'))
  132. if (str_contains(strtolower($row->note), 'iscrizione'))
  133. {
  134. $subPrice = $row->amount;
  135. }
  136. if ($row->causal_id == $x->course->causal_id && !str_contains(strtolower($row->note), 'iscrizione'))
  137. {
  138. $tot = sizeof(json_decode($row->when));
  139. foreach(json_decode($row->when) as $m)
  140. {
  141. $prices[$m->month] = $row->amount / $tot;
  142. }
  143. }
  144. }
  145. }
  146. for($i=1; $i<=12; $i++)
  147. {
  148. $cls = $this->getColor($x->months, $i);
  149. if ($cls != 'grey')
  150. {
  151. if (!isset($this->totals[$i]))
  152. {
  153. $this->totals[$i]['green'] = 0;
  154. $this->totals[$i]['orange'] = 0;
  155. $this->totals[$i]['yellow'] = 0;
  156. }
  157. if ($cls == 'yellow')
  158. {
  159. $this->totals[$i][$cls] += 1;
  160. }
  161. else
  162. {
  163. $p = isset($prices[$i]) ? $prices[$i] : $price;
  164. $this->totals[$i][$cls] += $p;
  165. }
  166. }
  167. }
  168. $sub = $x->subscribed ? "Y" : "N";
  169. if (isset($this->totalIsc[$sub]))
  170. $this->totalIsc[$sub] += $subPrice;
  171. else
  172. $this->totalIsc[$sub] = $subPrice;
  173. $this->records[] = array(
  174. $x->member_id . "§" . $x->member->first_name . "§" . $x->member->last_name,
  175. $this->getColor($x->months, 9) . "§" . (isset($prices[9]) ? $prices[9] : $price),
  176. $this->getColor($x->months, 10) . "§" . (isset($prices[10]) ? $prices[10] : $price),
  177. $this->getColor($x->months, 11) . "§" . (isset($prices[11]) ? $prices[11] : $price),
  178. $this->getColor($x->months, 12) . "§" . (isset($prices[12]) ? $prices[12] : $price),
  179. $this->getColor($x->months, 1) . "§" . (isset($prices[1]) ? $prices[1] : $price),
  180. $this->getColor($x->months, 2) . "§" . (isset($prices[2]) ? $prices[2] : $price),
  181. $this->getColor($x->months, 3) . "§" . (isset($prices[3]) ? $prices[3] : $price),
  182. $this->getColor($x->months, 4) . "§" . (isset($prices[4]) ? $prices[4] : $price),
  183. $this->getColor($x->months, 5) . "§" . (isset($prices[5]) ? $prices[5] : $price),
  184. $this->getColor($x->months, 6) . "§" . (isset($prices[6]) ? $prices[6] : $price),
  185. $this->getColor($x->months, 7) . "§" . (isset($prices[7]) ? $prices[7] : $price),
  186. $this->getColor($x->months, 8) . "§" . (isset($prices[8]) ? $prices[8] : $price),
  187. $x->course_id,
  188. $x->id,
  189. $x->subscribed . "§" . $subPrice
  190. );
  191. }
  192. $js = '';
  193. $xx = 2;
  194. $str = '';
  195. $str .= "<a class=green><small>" . (isset($this->totalIsc["Y"]) ? formatPrice($this->totalIsc["Y"]) : 0) . "</small></a><br>";
  196. $str .= "<a class=orange><small>" . (isset($this->totalIsc["N"]) ? formatPrice($this->totalIsc["N"]) : 0) . "</small></a><br>";
  197. $str .= "<a class=yellow><small>0</small></a><br>";
  198. $js .= "2§" . $str . "_";
  199. $str = "";
  200. foreach($this->totals as $z => $t)
  201. {
  202. if ($z == 1) $xx = 7;
  203. if ($z == 2) $xx = 8;
  204. if ($z == 3) $xx = 9;
  205. if ($z == 4) $xx = 10;
  206. if ($z == 5) $xx = 11;
  207. if ($z == 6) $xx = 12;
  208. if ($z == 7) $xx = 13;
  209. if ($z == 8) $xx = 14;
  210. if ($z == 9) $xx = 3;
  211. if ($z == 10) $xx = 4;
  212. if ($z == 11) $xx = 5;
  213. if ($z == 12) $xx = 6;
  214. $str = '';
  215. foreach($t as $x => $c)
  216. {
  217. $y = $x == 'yellow' ? $c : formatPrice($c);
  218. $str .= "<a class=" . $x . "><small>" . $y . "</small></a><br>";
  219. }
  220. $js .= $xx . "§" . $str . "_";
  221. $xx += 1;
  222. }
  223. //$this->aaa = $js;
  224. //$this->emit('setTotals', $js);
  225. $this->emit('load-data-table');
  226. */
  227. $member_course = \App\Models\MemberCourse::with('member')->with('course');
  228. if ($this->hasFilter)
  229. {
  230. if (isset($_GET["search"]["value"]))
  231. {
  232. if ($_GET["search"]["value"] != '')
  233. {
  234. $v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
  235. $member_ids = \App\Models\Member::where(function ($query) use ($v) {
  236. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $v . "%'")
  237. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $v . "%'");
  238. })->pluck('id');
  239. /*
  240. $v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
  241. $member_ids = \App\Models\Member::where(function ($query) use ($v) {
  242. $query->where('first_name', 'like', '%' . $v . '%')
  243. ->orWhere('last_name', 'like', '%' . $v . '%');
  244. })->pluck('id');*/
  245. $member_course = $member_course->whereIn('member_id', $member_ids);
  246. }
  247. }
  248. if (sizeof($this->filterCourse) > 0)
  249. {
  250. $course_ids = [];
  251. $courses = $this->filterCourse;
  252. foreach($courses as $c)
  253. {
  254. $all = \App\Models\Course::where('name', 'like', '%' . $c . "%")->get();
  255. foreach($all as $a)
  256. {
  257. $course_ids[] = $a->id;
  258. }
  259. }
  260. $member_course = $member_course->whereIn('course_id', $course_ids);
  261. }
  262. if (sizeof($this->filterLevel) > 0)
  263. {
  264. $course_ids = \App\Models\Course::whereIn('course_level_id', $this->filterLevel)->pluck('id');
  265. $member_course = $member_course->whereIn('course_id', $course_ids);
  266. }
  267. if (sizeof($this->filterFrequency) > 0)
  268. {
  269. $course_ids = \App\Models\Course::whereIn('course_frequency_id', $this->filterFrequency)->pluck('id');
  270. $member_course = $member_course->whereIn('course_id', $course_ids);
  271. }
  272. if (sizeof($this->filterType) > 0)
  273. {
  274. $course_ids = \App\Models\Course::whereIn('course_type_id', $this->filterType)->pluck('id');
  275. $member_course = $member_course->whereIn('course_id', $course_ids);
  276. }
  277. if (sizeof($this->filterDuration) > 0)
  278. {
  279. $course_ids = \App\Models\Course::whereIn('course_duration_id', $this->filterDuration)->pluck('id');
  280. $member_course = $member_course->whereIn('course_id', $course_ids);
  281. }
  282. }
  283. /*
  284. if ($_GET["filterCourse"] != "null")
  285. {
  286. $course_ids = [];
  287. $courses = explode(",", $_GET["filterCourse"]);
  288. foreach($courses as $c)
  289. {
  290. $all = \App\Models\Course::where('name', 'like', '%' . $c . "%")->get();
  291. foreach($all as $a)
  292. {
  293. $course_ids[] = $a->id;
  294. }
  295. }
  296. $member_course = $member_course->whereIn('course_id', $course_ids);
  297. }
  298. if ($_GET["filterYear"] != "")
  299. {
  300. $course_ids = \App\Models\Course::where('year', $_GET["filterYear"])->pluck('id');
  301. $member_course = $member_course->whereIn('course_id', $course_ids);
  302. }
  303. if ($_GET["filterLevel"] != "null")
  304. {
  305. $levels = explode(",", $_GET["filterLevel"]);
  306. $course_ids = \App\Models\Course::whereIn('course_level_id', $levels)->pluck('id');
  307. $member_course = $member_course->whereIn('course_id', $course_ids);
  308. }
  309. if ($_GET["filterFrequency"] != "null")
  310. {
  311. $frequencies = explode(",", $_GET["filterFrequency"]);
  312. $course_ids = \App\Models\Course::whereIn('course_frequency_id', $frequencies)->pluck('id');
  313. $member_course = $member_course->whereIn('course_id', $course_ids);
  314. }
  315. if ($_GET["filterType"] != "null")
  316. {
  317. $types = explode(",", $_GET["filterType"]);
  318. $course_ids = \App\Models\Course::whereIn('course_type_id', $types)->pluck('id');
  319. $member_course = $member_course->whereIn('course_id', $course_ids);
  320. }
  321. if ($_GET["filterDuration"] != "null")
  322. {
  323. $durations = explode(",", $_GET["filterDuration"]);
  324. $course_ids = \App\Models\Course::whereIn('course_duration_id', $durations)->pluck('id');
  325. $member_course = $member_course->whereIn('course_id', $course_ids);
  326. }
  327. */
  328. $totals = [];
  329. $totalIsc = [];
  330. $datas = [];
  331. $xxx = 1;
  332. /*
  333. $sortColumn = '';
  334. if (isset($_GET["order"]))
  335. {
  336. }
  337. */
  338. $column = '';
  339. $sort_value = 0;
  340. if ($this->sort != '')
  341. {
  342. $f = $this->sort;
  343. $d = $this->dir;
  344. if ($f >= 5 && $f <= 16)
  345. {
  346. $column = 'column_' . ($f - 2);
  347. if (session()->get('sort_column'))
  348. {
  349. if (session()->get('sort_column') != $f)
  350. {
  351. session()->put('sort_column', $f);
  352. session()->put('sort_order', $d);
  353. session()->put('sort_value', 0);
  354. $sort_value = 0;
  355. }
  356. else
  357. {
  358. if (session()->get('sort_order') == $d)
  359. {
  360. //session()->put('sort_value', 0);
  361. $sort_value = session()->get('sort_value', 0);
  362. }
  363. else
  364. {
  365. if (session()->get('sort_value', 0) == 0)
  366. {
  367. $sort_value = 1;
  368. }
  369. if (session()->get('sort_value', 0) == 1)
  370. {
  371. $sort_value = 2;
  372. }
  373. if (session()->get('sort_value', 0) == 2)
  374. {
  375. $sort_value = 3;
  376. }
  377. if (session()->get('sort_value', 0) == 3)
  378. {
  379. $sort_value = 0;
  380. }
  381. session()->put('sort_value', $sort_value);
  382. }
  383. session()->put('sort_order', $d);
  384. }
  385. }
  386. else
  387. {
  388. session()->put('sort_column', $f);
  389. session()->put('sort_order', $d);
  390. session()->put('sort_value', 0);
  391. $sort_value = 0;
  392. }
  393. }
  394. }
  395. //print $sort_value;
  396. $totals = [];
  397. $prices = [];
  398. $member_course_totals = $member_course->get();
  399. foreach($member_course_totals as $x)
  400. {
  401. $price = 0;
  402. $price = $x->price; // $x->course->price;
  403. $subPrice = $x->subscription_price; // $x->course->subscription_price;
  404. $records = \App\Models\Record::where('member_course_id', $x->id)->where('deleted', 0)->get();
  405. $prices = [];
  406. foreach ($records as $record)
  407. {
  408. foreach ($record->rows as $row)
  409. {
  410. if ($row->causal_id == $x->course->sub_causal_id) // || str_contains(strtolower($row->note), 'iscrizione'))
  411. //if (str_contains(strtolower($row->note), 'iscrizione'))
  412. {
  413. $subPrice = $row->amount;
  414. }
  415. if ($row->causal_id == $x->course->causal_id && !str_contains(strtolower($row->note), 'iscrizione'))
  416. {
  417. $tot = sizeof(json_decode($row->when));
  418. foreach(json_decode($row->when) as $m)
  419. {
  420. if (isset($prices[$m->month]))
  421. $prices[$m->month] += $row->amount / $tot;
  422. else
  423. $prices[$m->month] = $row->amount / $tot;
  424. }
  425. }
  426. }
  427. }
  428. for($i=1; $i<=12; $i++)
  429. {
  430. $cls = $this->getColor($x->months, $i, isset($prices[$i]) && $prices[$i] == $price);
  431. if ($cls != 'wgrey')
  432. {
  433. if (!isset($totals[$i]))
  434. {
  435. $totals[$i]['green'] = 0;
  436. $totals[$i]['orange'] = 0;
  437. $totals[$i]['yellow'] = 0;
  438. }
  439. if ($cls == 'yellow')
  440. {
  441. $totals[$i][$cls] += 1;
  442. }
  443. else
  444. {
  445. $p = isset($prices[$i]) ? $prices[$i] : $price;
  446. //if (isset($totals[$i][$cls]))
  447. $totals[$i][$cls] += $p;
  448. //else
  449. //$totals[$i][$cls] = $p;
  450. }
  451. }
  452. }
  453. $sub = $x->subscribed ? "Y" : "N";
  454. if (isset($totalIsc[$sub]))
  455. $totalIsc[$sub] += $subPrice;
  456. else
  457. $totalIsc[$sub] = $subPrice;
  458. $s = 0;
  459. if ($column != '')
  460. {
  461. $z = 0;
  462. switch ($column) {
  463. case 'column_3':
  464. $z = 9;
  465. break;
  466. case 'column_4':
  467. $z = 10;
  468. break;
  469. case 'column_5':
  470. $z = 11;
  471. break;
  472. case 'column_6':
  473. $z = 12;
  474. break;
  475. case 'column_7':
  476. $z = 1;
  477. break;
  478. case 'column_8':
  479. $z = 2;
  480. break;
  481. case 'column_9':
  482. $z = 3;
  483. break;
  484. case 'column_10':
  485. $z = 4;
  486. break;
  487. case 'column_11':
  488. $z = 5;
  489. break;
  490. case 'column_12':
  491. $z = 6;
  492. break;
  493. case 'column_13':
  494. $z = 7;
  495. break;
  496. case 'column_14':
  497. $z = 8;
  498. break;
  499. default:
  500. $z = 0;
  501. break;
  502. }
  503. $c = getColor($x->months, $z);
  504. if ($sort_value == 0)
  505. {
  506. switch ($c) {
  507. case 'wgrey':
  508. $s = 0;
  509. break;
  510. case 'orange':
  511. $s = 1;
  512. break;
  513. case 'green':
  514. $s = 2;
  515. break;
  516. case 'yellow':
  517. $s = 3;
  518. break;
  519. default:
  520. $s = 0;
  521. break;
  522. }
  523. }
  524. if ($sort_value == 1)
  525. {
  526. switch ($c) {
  527. case 'wgrey':
  528. $s = 3;
  529. break;
  530. case 'orange':
  531. $s = 0;
  532. break;
  533. case 'green':
  534. $s = 1;
  535. break;
  536. case 'yellow':
  537. $s = 2;
  538. break;
  539. default:
  540. $s = 0;
  541. break;
  542. }
  543. }
  544. if ($sort_value == 2)
  545. {
  546. switch ($c) {
  547. case 'wgrey':
  548. $s = 2;
  549. break;
  550. case 'orange':
  551. $s = 3;
  552. break;
  553. case 'green':
  554. $s = 0;
  555. break;
  556. case 'yellow':
  557. $s = 1;
  558. break;
  559. default:
  560. $s = 0;
  561. break;
  562. }
  563. }
  564. if ($sort_value == 3)
  565. {
  566. switch ($c) {
  567. case 'wgrey':
  568. $s = 1;
  569. break;
  570. case 'orange':
  571. $s = 2;
  572. break;
  573. case 'green':
  574. $s = 3;
  575. break;
  576. case 'yellow':
  577. $s = 0;
  578. break;
  579. default:
  580. $s = 0;
  581. break;
  582. }
  583. }
  584. }
  585. $datas[] = array(
  586. "column_19" => $x->course->name,
  587. "column_0" => $x->member->last_name,
  588. "column_1" => $x->member->first_name,
  589. "column_2" => $x->subscribed . "§" . formatPrice($subPrice),
  590. "column_3" => $this->getColor($x->months, 9, isset($prices[9]) && $prices[9] == $price) . "§" . formatPrice(isset($prices[9]) ? $prices[9] : $price) . "§" . (isset($prices[9]) && $prices[9] != $price ? 'X' : ''),
  591. "column_4" => $this->getColor($x->months, 10, isset($prices[10]) && $prices[10] == $price) . "§" . formatPrice(isset($prices[10]) ? $prices[10] : $price) . "§" . (isset($prices[10]) && $prices[10] != $price ? 'X' : ''),
  592. "column_5" => $this->getColor($x->months, 11, isset($prices[11]) && $prices[11] == $price) . "§" . formatPrice(isset($prices[11]) ? $prices[11] : $price) . "§" . (isset($prices[11]) && $prices[11] != $price ? 'X' : ''),
  593. "column_6" => $this->getColor($x->months, 12, isset($prices[12]) && $prices[12] == $price) . "§" . formatPrice(isset($prices[12]) ? $prices[12] : $price) . "§" . (isset($prices[12]) && $prices[12] != $price ? 'X' : ''),
  594. "column_7" => $this->getColor($x->months, 1, isset($prices[1]) && $prices[1] == $price) . "§" . formatPrice(isset($prices[1]) ? $prices[1] : $price) . "§" . (isset($prices[1]) && $prices[1] != $price ? 'X' : ''),
  595. "column_8" => $this->getColor($x->months, 2, isset($prices[2]) && $prices[2] == $price) . "§" . formatPrice(isset($prices[2]) ? $prices[2] : $price) . "§" . (isset($prices[2]) && $prices[2] != $price ? 'X' : ''),
  596. "column_9" => $this->getColor($x->months, 3, isset($prices[3]) && $prices[3] == $price) . "§" . formatPrice(isset($prices[3]) ? $prices[3] : $price) . "§" . (isset($prices[3]) && $prices[3] != $price ? 'X' : ''),
  597. "column_10" => $this->getColor($x->months, 4, isset($prices[4]) && $prices[4] == $price) . "§" . formatPrice(isset($prices[4]) ? $prices[4] : $price) . "§" . (isset($prices[4]) && $prices[4] != $price ? 'X' : ''),
  598. "column_11" => $this->getColor($x->months, 5, isset($prices[5]) && $prices[5] == $price) . "§" . formatPrice(isset($prices[5]) ? $prices[5] : $price) . "§" . (isset($prices[5]) && $prices[5] != $price ? 'X' : ''),
  599. "column_12" => $this->getColor($x->months, 6, isset($prices[6]) && $prices[6] == $price) . "§" . formatPrice(isset($prices[6]) ? $prices[6] : $price) . "§" . (isset($prices[6]) && $prices[6] != $price ? 'X' : ''),
  600. "column_13" => $this->getColor($x->months, 7, isset($prices[7]) && $prices[7] == $price) . "§" . formatPrice(isset($prices[7]) ? $prices[7] : $price) . "§" . (isset($prices[7]) && $prices[7] != $price ? 'X' : ''),
  601. "column_14" => $this->getColor($x->months, 8, isset($prices[8]) && $prices[8] == $price) . "§" . formatPrice(isset($prices[8]) ? $prices[8] : $price) . "§" . (isset($prices[8]) && $prices[8] != $price ? 'X' : ''),
  602. "column_15" => $x->course_id,
  603. "column_16" => $x->id,
  604. "column_17" => $x->member_id,
  605. "column_18" => $xxx++,
  606. "column_20" => $s
  607. );
  608. }
  609. $count = $member_course->count();
  610. $this->totSExcel = [];
  611. $this->totS = [];
  612. //$js = '';
  613. $xx = 4;
  614. $str = '';
  615. if ($count > 0)
  616. {
  617. $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>";
  618. $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>";
  619. $str .= "<a style='width:100%;float:right; text-align:right; display:block;' class=yellow><small>0</small></a><br>";
  620. $this->totSExcel[] = array('green' => (isset($totalIsc["Y"]) ? formatPrice($totalIsc["Y"]) : 0), 'orange' => (isset($totalIsc["N"]) ? formatPrice($totalIsc["N"]) : 0), 'yellow' => 0);
  621. }
  622. $this->totS[] = $str;
  623. $str = "";
  624. foreach($totals as $z => $t)
  625. {
  626. if ($z == 1) $xx = 5;
  627. if ($z == 2) $xx = 6;
  628. if ($z == 3) $xx = 7;
  629. if ($z == 4) $xx = 8;
  630. if ($z == 5) $xx = 9;
  631. if ($z == 6) $xx = 10;
  632. if ($z == 7) $xx = 11;
  633. if ($z == 8) $xx = 12;
  634. if ($z == 9) $xx = 1;
  635. if ($z == 10) $xx = 2;
  636. if ($z == 11) $xx = 3;
  637. if ($z == 12) $xx = 4;
  638. $str = '';
  639. $aaa = [];
  640. foreach($t as $x => $c)
  641. {
  642. $y = $x == 'yellow' ? $c : formatPrice($c);
  643. $str .= "<a style='width:100%;float:right; text-align:right; display:block;' class=" . $x . "><small>" . $y . "</small></a><br>";
  644. $aaa[$x] = $y;
  645. }
  646. $this->totSExcel[$xx] = $aaa;
  647. $this->totS[$xx] = $str;
  648. //$js .= $xx . "§" . $str . "_";
  649. $xx += 1;
  650. }
  651. for($e=sizeof($this->totS);$e<=12;$e++)
  652. {
  653. $this->totS[] = '';
  654. }
  655. if ($this->sort != '')
  656. {
  657. $s = $this->sort;
  658. if ($s == 1) $s = 21;
  659. if ($column != '')
  660. array_multisort(array_column($datas, 'column_20'), SORT_ASC, SORT_NATURAL|SORT_FLAG_CASE, $datas);
  661. else
  662. array_multisort(array_column($datas, 'column_' . ($s - 2)), $this->dir == "ASC" ? SORT_ASC : SORT_DESC, SORT_NATURAL|SORT_FLAG_CASE, $datas);
  663. }
  664. $xxx = 1;
  665. foreach($datas as $yyy => $d)
  666. {
  667. $datas[$yyy]["column_18"] = $xxx++;
  668. }
  669. $this->totalRecords = sizeof($datas);
  670. //$start = 0;
  671. $this->recordsNoPaginate = [];
  672. $this->recordsNoPaginate = $datas;
  673. //if (isset($_GET["start"]))
  674. $datas = array_slice($datas, $this->start, $this->pageLength);
  675. $this->pages = ceil($this->totalRecords / $this->pageLength);
  676. $this->records = [];
  677. $this->records = $datas;
  678. //$this->totS = $js;
  679. return view('livewire.course_list');
  680. }
  681. public function setPage($page)
  682. {
  683. $this->currentPage = $page;
  684. $this->start = $this->pageLength * ($page - 1);
  685. $this->emit('load-data-table');
  686. }
  687. public function setSort($sort)
  688. {
  689. $this->sort = $sort;
  690. if ($this->dir == '')
  691. $this->dir = 'ASC';
  692. else
  693. $this->dir = $this->dir == 'ASC' ? 'DESC' : 'ASC';
  694. $this->emit('load-data-table');
  695. }
  696. public function search()
  697. {
  698. $this->currentPage = 1;
  699. $this->start = 0;
  700. $this->hasFilter = true;
  701. $this->emit('load-data-table');
  702. }
  703. public function getColor($months, $m, $all)
  704. {
  705. $class = "wgrey";
  706. foreach(json_decode($months) as $mm)
  707. {
  708. if ($mm->m == $m)
  709. {
  710. if ($mm->status == "")
  711. {
  712. $class = "orange";
  713. }
  714. if ($mm->status == "1")
  715. {
  716. $class = "green";
  717. }
  718. if ($mm->status == "2")
  719. {
  720. $class = "yellow";
  721. }
  722. if (!$all)
  723. {
  724. $class = "orange";
  725. }
  726. }
  727. }
  728. return $class;
  729. }
  730. public function newPayment($course_id, $months, $member_id, $id, $subscription)
  731. {
  732. $newMonths = array();
  733. if ($months != '')
  734. {
  735. $mm = explode(",", $months);
  736. foreach($mm as $month)
  737. {
  738. if ($month < 5) $month += 12;
  739. if ($month >= 5) $month -= 4;
  740. $newMonths[] = $month;
  741. }
  742. }
  743. $c = \App\Models\Course::findOrFail($course_id);
  744. $m = \App\Models\MemberCourse::findOrFail($id);
  745. return redirect()->to('/in?new=1&memberId=' . $member_id . (sizeof($newMonths) > 0 ? '&causalId=' . $c->causal_id : '') . '&subCausalId=' . $c->sub_causal_id . '&createSubscription=' . ($subscription ? 1 : 0) . (sizeof($newMonths) > 0 ? '&months=' . implode("|", $newMonths) : '') . (sizeof($newMonths) > 0 ? '&price=' . $m->price : '') . '&subscription_price=' . $m->subscription_price . "&courseId=" . $id);
  746. }
  747. public function suspendPayment($course_id, $month, $member_id, $id, $subscription)
  748. {
  749. $monthMap = [
  750. 1 => 9, // September
  751. 2 => 10, // October
  752. 3 => 11, // November
  753. 4 => 12, // December
  754. 5 => 1, // January
  755. 6 => 2, // February
  756. 7 => 3, // March
  757. 8 => 4, // April
  758. 9 => 5, // May
  759. 10 => 6, // June
  760. 11 => 7, // July
  761. 12 => 8 // August
  762. ];
  763. $dbMonth = isset($monthMap[$month]) ? $monthMap[$month] : $month;
  764. $memberCourse = MemberCourse::where('id', $id)
  765. ->where('member_id', $member_id)
  766. ->where('course_id', $course_id)
  767. ->first();
  768. if (!$memberCourse) {
  769. return response()->json(['error' => 'Non Trovato'], 404);
  770. }
  771. $monthsData = json_decode($memberCourse->months, true);
  772. if (!is_array($monthsData)) {
  773. return response()->json(['error' => 'Invalid months data format'], 400);
  774. }
  775. $monthUpdated = false;
  776. foreach ($monthsData as &$monthData) {
  777. if ($monthData['m'] == $dbMonth) {
  778. $monthData['status'] = 2;
  779. $monthUpdated = true;
  780. }
  781. }
  782. if (!$monthUpdated) {
  783. return response()->json(['error' => 'Month not found in data'], 404);
  784. }
  785. $memberCourse->months = json_encode($monthsData);
  786. $memberCourse->save();
  787. session()->flash('success', 'Payment suspended successfully');
  788. return redirect()->to('/course_list');
  789. }
  790. public function resumePayment($course_id, $month, $member_id, $id, $subscription)
  791. {
  792. Log::info('resumePayment');
  793. Log::info($course_id);
  794. Log::info($month);
  795. Log::info($member_id);
  796. Log::info($id);
  797. $monthMap = [
  798. 1 => 9, // September
  799. 2 => 10, // October
  800. 3 => 11, // November
  801. 4 => 12, // December
  802. 5 => 1, // January
  803. 6 => 2, // February
  804. 7 => 3, // March
  805. 8 => 4, // April
  806. 9 => 5, // May
  807. 10 => 6, // June
  808. 11 => 7, // July
  809. 12 => 8 // August
  810. ];
  811. $dbMonth = isset($monthMap[$month]) ? $monthMap[$month] : $month;
  812. $memberCourse = MemberCourse::where('id', $id)
  813. ->where('member_id', $member_id)
  814. ->where('course_id', $course_id)
  815. ->first();
  816. if (!$memberCourse) {
  817. return response()->json(['error' => 'Non Trovato'], 404);
  818. }
  819. $monthsData = json_decode($memberCourse->months, true);
  820. Log::info('data mese',$monthsData);
  821. if (!is_array($monthsData)) {
  822. return response()->json(['error' => 'Invalid months data format'], 400);
  823. }
  824. $monthUpdated = false;
  825. foreach ($monthsData as &$monthData) {
  826. if ($monthData['m'] == $dbMonth) {
  827. $monthData['status'] = "";
  828. $monthUpdated = true;
  829. }
  830. }
  831. Log::info($monthUpdated);
  832. if (!$monthUpdated) {
  833. return response()->json(['error' => 'Month not found in data'], 404);
  834. }
  835. $memberCourse->months = json_encode($monthsData);
  836. $memberCourse->save();
  837. session()->flash('success', 'Payment resumed successfully');
  838. return redirect()->to('/course_list');
  839. }
  840. /*
  841. public function newPayment()
  842. {
  843. $c = \App\Models\Course::findOrFail($this->selectedCourseId);
  844. return redirect()->to('/in?new=1&memberId=' . $this->selectedMemberId . '&causalId=' . $c->causal_id . '&subCausalId=' . $c->sub_causal_id . '&createSubscription=0' . (sizeof($this->payMonths) > 0 ? '&months=' . implode("|", $this->payMonths) : "") . '&price=' . $c->price . '&subscription_price=' . $c->subscription_price . "&courseId=" . $this->selectedCourseId);
  845. }
  846. */
  847. public function newSubscription($course_id, $member_id, $id)
  848. {
  849. $c = \App\Models\Course::findOrFail($course_id);
  850. return redirect()->to('/in?new=1&memberId=' . $member_id . '&causalId=' . $c->causal_id . '&subCausalId=' . $c->sub_causal_id . '&createSubscription=1&price=0.00&subscription_price=' . $c->subscription_price . "&courseId=" . $id);
  851. }
  852. public function disableSearch()
  853. {
  854. $this->filterCourse = [];
  855. $this->filterLevel = [];
  856. $this->filterType = [];
  857. $this->filterDuration = [];
  858. $this->filterFrequency = [];
  859. $this->hasFilter = false;
  860. }
  861. public function export()
  862. {
  863. $letters = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
  864. $spreadsheet = new Spreadsheet();
  865. $activeWorksheet = $spreadsheet->getActiveSheet();
  866. //$activeWorksheet->setCellValue('A1', 'PrimaNota');
  867. $activeWorksheet->setCellValue('A1', "Corso");
  868. $activeWorksheet->setCellValue('B1', "Cognome");
  869. $activeWorksheet->setCellValue('C1', "Nome");
  870. $activeWorksheet->setCellValue('D1', "Iscrizione");
  871. $activeWorksheet->setCellValue('E1', "Settembre");
  872. $activeWorksheet->setCellValue('F1', "Ottobre");
  873. $activeWorksheet->setCellValue('G1', "Novembre");
  874. $activeWorksheet->setCellValue('H1', "Dicembre");
  875. $activeWorksheet->setCellValue('I1', "Gennaio");
  876. $activeWorksheet->setCellValue('J1', "Febbraio");
  877. $activeWorksheet->setCellValue('K1', "Marzo");
  878. $activeWorksheet->setCellValue('L1', "Aprile");
  879. $activeWorksheet->setCellValue('M1', "Maggio");
  880. $activeWorksheet->setCellValue('N1', "Giugno");
  881. $activeWorksheet->setCellValue('O1', "Luglio");
  882. $activeWorksheet->setCellValue('P1', "Agosto");
  883. $count = 2;
  884. foreach($this->recordsNoPaginate as $idx => $record)
  885. {
  886. $activeWorksheet->setCellValue('A' . $count, $record["column_19"]);
  887. $activeWorksheet->setCellValue('B' . $count, $record["column_0"]);
  888. $activeWorksheet->setCellValue('C' . $count, $record["column_1"]);
  889. list($color, $value) = explode("§", $record["column_2"]);
  890. $activeWorksheet->setCellValue('D' . $count, $value);
  891. $c = '#FFFFFF';
  892. if($color == 0)
  893. $c = 'ffa500';
  894. if($color == 1)
  895. $c = '00ff00';
  896. $activeWorksheet->getStyle('D' . $count . ':D' . $count)
  897. ->getFill()
  898. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  899. ->getStartColor()
  900. ->setARGB($c);
  901. for($ii=3; $ii<=14; $ii++)
  902. {
  903. list($color, $value) = explode("§", $record["column_" . $ii]);
  904. $c = 'FFFFFF';
  905. if($color == 'orange')
  906. $c = 'ffa500';
  907. if($color == 'green')
  908. $c = '00ff00';
  909. if($color == 'yellow')
  910. {
  911. $c = '5088bf';
  912. $activeWorksheet->getStyle($letters[$ii + 1] . $count . ':' . $letters[$ii + 1] . $count)
  913. ->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_WHITE);;
  914. }
  915. if($color == 'wgrey')
  916. $value = '';
  917. $activeWorksheet->setCellValue($letters[$ii + 1] . $count, $value);
  918. $activeWorksheet->getStyle($letters[$ii + 1] . $count . ':' . $letters[$ii + 1] . $count)
  919. ->getFill()
  920. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  921. ->getStartColor()
  922. ->setARGB($c);
  923. /*$activeWorksheet->cells($letters[$ii + 1] . $count . ':' . $letters[$ii + 1] . $count, function ($cells) use ($c)
  924. {
  925. $cells->setBackground($c);
  926. //$cells->setAlignment('center');
  927. });*/
  928. }
  929. $activeWorksheet->getStyle("A1:P1")->getFont()->setBold( true );
  930. $count++;
  931. }
  932. // Totali
  933. $activeWorksheet->setCellValue('A' . (1 + $count), '');
  934. $activeWorksheet->setCellValue('B' . (1 + $count), '');
  935. $activeWorksheet->setCellValue('C' . (1 + $count), '');
  936. for($x=0; $x<=sizeof($this->totSExcel); $x++)
  937. {
  938. if (isset($this->totSExcel[$x]))
  939. {
  940. $activeWorksheet->setCellValue($letters[$x + 3] . (1 + $count), isset($this->totSExcel[$x]['green']) ? $this->totSExcel[$x]['green'] : 0);
  941. $activeWorksheet->getStyle($letters[$x + 3] . (1 + $count) . ':' . $letters[$x + 3] . (1 + $count))
  942. ->getFill()
  943. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  944. ->getStartColor()
  945. ->setARGB('00ff00');
  946. $activeWorksheet->setCellValue($letters[$x + 3] . (2 + $count), isset($this->totSExcel[$x]['orange']) ? $this->totSExcel[$x]['orange'] : 0);
  947. $activeWorksheet->getStyle($letters[$x + 3] . (2 + $count) . ':' . $letters[$x + 3] . (2 + $count))
  948. ->getFill()
  949. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  950. ->getStartColor()
  951. ->setARGB('ffa500');
  952. $activeWorksheet->setCellValue($letters[$x + 3] . (3 + $count), isset($this->totSExcel[$x]['yellow']) ? $this->totSExcel[$x]['yellow'] : 0);
  953. $activeWorksheet->getStyle($letters[$x + 3] . (3 + $count) . ':' . $letters[$x + 3] . (3 + $count))
  954. ->getFill()
  955. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  956. ->getStartColor()
  957. ->setARGB('5088bf');
  958. $activeWorksheet->getStyle($letters[$x + 3] . (3 + $count) . ':' . $letters[$x + 3] . (3 + $count))
  959. ->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_WHITE);;
  960. }
  961. }
  962. $writer = new Xlsx($spreadsheet);
  963. $writer->save($path = storage_path('pagamento_corsi_' . date("YmdHis") . '.xlsx'));
  964. $this->emit('load-data-table');
  965. return response()->download($path)->deleteFileAfterSend();
  966. }
  967. }