CourseList.php 37 KB

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