CourseList.php 37 KB

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