CourseList.php 35 KB

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