CourseList.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use App\Models\MemberCourse;
  5. use Illuminate\Support\Facades\Log;
  6. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  7. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  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 = 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. }