CourseList.php 37 KB

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