CourseList.php 34 KB

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