CourseList.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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 ? 'Y' : ''),
  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 ? 'Y' : ''),
  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 ? 'Y' : ''),
  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 ? 'Y' : ''),
  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 ? 'Y' : ''),
  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 ? 'Y' : ''),
  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 ? 'Y' : ''),
  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 ? 'Y' : ''),
  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 ? 'Y' : ''),
  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 ? 'Y' : ''),
  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 ? 'Y' : ''),
  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 ? 'Y' : ''),
  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. // Tolto il 09/05
  551. /*if (!$all && $class == "green")
  552. {
  553. $class = "orange";
  554. }*/
  555. }
  556. }
  557. return $class;
  558. }
  559. public function newPayment($course_id, $months, $member_id, $id, $subscription)
  560. {
  561. $newMonths = array();
  562. if ($months != '')
  563. {
  564. $mm = explode(",", $months);
  565. foreach($mm as $month)
  566. {
  567. if ($month < 5) $month += 12;
  568. if ($month >= 5) $month -= 4;
  569. $newMonths[] = $month;
  570. }
  571. }
  572. $c = \App\Models\Course::findOrFail($course_id);
  573. $m = \App\Models\MemberCourse::findOrFail($id);
  574. $price = $m->price;
  575. $subscription_price = $m->subscription_price;
  576. $records = \App\Models\Record::where('member_course_id', $m->id)->where('deleted', 0)->get();
  577. foreach ($records as $record)
  578. {
  579. if (in_array($month, json_decode($record->months)))
  580. {
  581. foreach ($record->rows as $row)
  582. {
  583. if ($row->causal_id == $c->causal_id && !str_contains(strtolower($row->note), 'iscrizione'))
  584. {
  585. $tot = sizeof(json_decode($row->when));
  586. foreach(json_decode($row->when) as $m)
  587. {
  588. $price -= $row->amount / $tot;
  589. }
  590. }
  591. }
  592. }
  593. }
  594. 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);
  595. }
  596. public function suspendPayment($course_id, $month, $member_id, $id, $subscription)
  597. {
  598. $monthMap = [
  599. 1 => 9, // September
  600. 2 => 10, // October
  601. 3 => 11, // November
  602. 4 => 12, // December
  603. 5 => 1, // January
  604. 6 => 2, // February
  605. 7 => 3, // March
  606. 8 => 4, // April
  607. 9 => 5, // May
  608. 10 => 6, // June
  609. 11 => 7, // July
  610. 12 => 8 // August
  611. ];
  612. $dbMonth = isset($monthMap[$month]) ? $monthMap[$month] : $month;
  613. $memberCourse = MemberCourse::where('id', $id)
  614. ->where('member_id', $member_id)
  615. ->where('course_id', $course_id)
  616. ->first();
  617. if (!$memberCourse) {
  618. return response()->json(['error' => 'Non Trovato'], 404);
  619. }
  620. $monthsData = json_decode($memberCourse->months, true);
  621. if (!is_array($monthsData)) {
  622. return response()->json(['error' => 'Invalid months data format'], 400);
  623. }
  624. $monthUpdated = false;
  625. foreach ($monthsData as &$monthData) {
  626. if ($monthData['m'] == $dbMonth) {
  627. $monthData['status'] = 2;
  628. $monthUpdated = true;
  629. }
  630. }
  631. if (!$monthUpdated) {
  632. return response()->json(['error' => 'Month not found in data'], 404);
  633. }
  634. $memberCourse->months = json_encode($monthsData);
  635. $memberCourse->save();
  636. session()->flash('success', 'Payment suspended successfully');
  637. return redirect()->to('/course_list');
  638. }
  639. public function resumePayment($course_id, $month, $member_id, $id, $subscription)
  640. {
  641. Log::info('resumePayment');
  642. Log::info($course_id);
  643. Log::info($month);
  644. Log::info($member_id);
  645. Log::info($id);
  646. $monthMap = [
  647. 1 => 9, // September
  648. 2 => 10, // October
  649. 3 => 11, // November
  650. 4 => 12, // December
  651. 5 => 1, // January
  652. 6 => 2, // February
  653. 7 => 3, // March
  654. 8 => 4, // April
  655. 9 => 5, // May
  656. 10 => 6, // June
  657. 11 => 7, // July
  658. 12 => 8 // August
  659. ];
  660. $dbMonth = isset($monthMap[$month]) ? $monthMap[$month] : $month;
  661. $memberCourse = MemberCourse::where('id', $id)
  662. ->where('member_id', $member_id)
  663. ->where('course_id', $course_id)
  664. ->first();
  665. if (!$memberCourse) {
  666. return response()->json(['error' => 'Non Trovato'], 404);
  667. }
  668. $monthsData = json_decode($memberCourse->months, true);
  669. Log::info('data mese',$monthsData);
  670. if (!is_array($monthsData)) {
  671. return response()->json(['error' => 'Invalid months data format'], 400);
  672. }
  673. $monthUpdated = false;
  674. foreach ($monthsData as &$monthData) {
  675. if ($monthData['m'] == $dbMonth) {
  676. $monthData['status'] = "";
  677. $monthUpdated = true;
  678. }
  679. }
  680. Log::info($monthUpdated);
  681. if (!$monthUpdated) {
  682. return response()->json(['error' => 'Month not found in data'], 404);
  683. }
  684. $memberCourse->months = json_encode($monthsData);
  685. $memberCourse->save();
  686. session()->flash('success', 'Payment resumed successfully');
  687. return redirect()->to('/course_list');
  688. }
  689. /*
  690. public function newPayment()
  691. {
  692. $c = \App\Models\Course::findOrFail($this->selectedCourseId);
  693. 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);
  694. }
  695. */
  696. public function newSubscription($course_id, $member_id, $id)
  697. {
  698. $c = \App\Models\Course::findOrFail($course_id);
  699. 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);
  700. }
  701. public function disableSearch()
  702. {
  703. $this->filterCourse = [];
  704. $this->filterLevel = [];
  705. $this->filterType = [];
  706. $this->filterDuration = [];
  707. $this->filterFrequency = [];
  708. $this->hasFilter = false;
  709. }
  710. public function export()
  711. {
  712. $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');
  713. $spreadsheet = new Spreadsheet();
  714. $activeWorksheet = $spreadsheet->getActiveSheet();
  715. //$activeWorksheet->setCellValue('A1', 'PrimaNota');
  716. $activeWorksheet->setCellValue('A1', "Corso");
  717. $activeWorksheet->setCellValue('B1', "Cognome");
  718. $activeWorksheet->setCellValue('C1', "Nome");
  719. $activeWorksheet->setCellValue('D1', "Iscrizione");
  720. $activeWorksheet->setCellValue('E1', "Settembre");
  721. $activeWorksheet->setCellValue('F1', "Ottobre");
  722. $activeWorksheet->setCellValue('G1', "Novembre");
  723. $activeWorksheet->setCellValue('H1', "Dicembre");
  724. $activeWorksheet->setCellValue('I1', "Gennaio");
  725. $activeWorksheet->setCellValue('J1', "Febbraio");
  726. $activeWorksheet->setCellValue('K1', "Marzo");
  727. $activeWorksheet->setCellValue('L1', "Aprile");
  728. $activeWorksheet->setCellValue('M1', "Maggio");
  729. $activeWorksheet->setCellValue('N1', "Giugno");
  730. $activeWorksheet->setCellValue('O1', "Luglio");
  731. $activeWorksheet->setCellValue('P1', "Agosto");
  732. $count = 2;
  733. foreach($this->recordsNoPaginate as $idx => $record)
  734. {
  735. $activeWorksheet->setCellValue('A' . $count, $record["column_19"]);
  736. $activeWorksheet->setCellValue('B' . $count, $record["column_0"]);
  737. $activeWorksheet->setCellValue('C' . $count, $record["column_1"]);
  738. list($color, $value) = explode("§", $record["column_2"]);
  739. $activeWorksheet->setCellValue('D' . $count, $value);
  740. $c = '#FFFFFF';
  741. if($color == 0)
  742. $c = 'ffa500';
  743. if($color == 1)
  744. $c = '00ff00';
  745. $activeWorksheet->getStyle('D' . $count . ':D' . $count)
  746. ->getFill()
  747. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  748. ->getStartColor()
  749. ->setARGB($c);
  750. for($ii=3; $ii<=14; $ii++)
  751. {
  752. list($color, $value) = explode("§", $record["column_" . $ii]);
  753. $c = 'FFFFFF';
  754. if($color == 'orange')
  755. $c = 'ffa500';
  756. if($color == 'green')
  757. $c = '00ff00';
  758. if($color == 'yellow')
  759. {
  760. $c = '5088bf';
  761. $activeWorksheet->getStyle($letters[$ii + 1] . $count . ':' . $letters[$ii + 1] . $count)
  762. ->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_WHITE);;
  763. }
  764. if($color == 'wgrey')
  765. $value = '';
  766. $activeWorksheet->setCellValue($letters[$ii + 1] . $count, $value);
  767. $activeWorksheet->getStyle($letters[$ii + 1] . $count . ':' . $letters[$ii + 1] . $count)
  768. ->getFill()
  769. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  770. ->getStartColor()
  771. ->setARGB($c);
  772. /*$activeWorksheet->cells($letters[$ii + 1] . $count . ':' . $letters[$ii + 1] . $count, function ($cells) use ($c)
  773. {
  774. $cells->setBackground($c);
  775. //$cells->setAlignment('center');
  776. });*/
  777. }
  778. $activeWorksheet->getStyle("A1:P1")->getFont()->setBold( true );
  779. $count++;
  780. }
  781. // Totali
  782. $activeWorksheet->setCellValue('A' . (1 + $count), '');
  783. $activeWorksheet->setCellValue('B' . (1 + $count), '');
  784. $activeWorksheet->setCellValue('C' . (1 + $count), '');
  785. for($x=0; $x<=sizeof($this->totSExcel); $x++)
  786. {
  787. if (isset($this->totSExcel[$x]))
  788. {
  789. $activeWorksheet->setCellValue($letters[$x + 3] . (1 + $count), isset($this->totSExcel[$x]['green']) ? $this->totSExcel[$x]['green'] : 0);
  790. $activeWorksheet->getStyle($letters[$x + 3] . (1 + $count) . ':' . $letters[$x + 3] . (1 + $count))
  791. ->getFill()
  792. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  793. ->getStartColor()
  794. ->setARGB('00ff00');
  795. $activeWorksheet->setCellValue($letters[$x + 3] . (2 + $count), isset($this->totSExcel[$x]['orange']) ? $this->totSExcel[$x]['orange'] : 0);
  796. $activeWorksheet->getStyle($letters[$x + 3] . (2 + $count) . ':' . $letters[$x + 3] . (2 + $count))
  797. ->getFill()
  798. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  799. ->getStartColor()
  800. ->setARGB('ffa500');
  801. $activeWorksheet->setCellValue($letters[$x + 3] . (3 + $count), isset($this->totSExcel[$x]['yellow']) ? $this->totSExcel[$x]['yellow'] : 0);
  802. $activeWorksheet->getStyle($letters[$x + 3] . (3 + $count) . ':' . $letters[$x + 3] . (3 + $count))
  803. ->getFill()
  804. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  805. ->getStartColor()
  806. ->setARGB('5088bf');
  807. $activeWorksheet->getStyle($letters[$x + 3] . (3 + $count) . ':' . $letters[$x + 3] . (3 + $count))
  808. ->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_WHITE);;
  809. }
  810. }
  811. $writer = new Xlsx($spreadsheet);
  812. $writer->save($path = storage_path('pagamento_corsi_' . date("YmdHis") . '.xlsx'));
  813. $this->emit('load-data-table');
  814. return response()->download($path)->deleteFileAfterSend();
  815. }
  816. }