CourseList.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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. }
  268. $sub = $x->subscribed ? "Y" : "N";
  269. if (isset($totalIsc[$sub]))
  270. $totalIsc[$sub] += $subPrice;
  271. else
  272. $totalIsc[$sub] = $subPrice;
  273. $s = 0;
  274. if ($column != '')
  275. {
  276. $z = 0;
  277. switch ($column) {
  278. case 'column_3':
  279. $z = 9;
  280. break;
  281. case 'column_4':
  282. $z = 10;
  283. break;
  284. case 'column_5':
  285. $z = 11;
  286. break;
  287. case 'column_6':
  288. $z = 12;
  289. break;
  290. case 'column_7':
  291. $z = 1;
  292. break;
  293. case 'column_8':
  294. $z = 2;
  295. break;
  296. case 'column_9':
  297. $z = 3;
  298. break;
  299. case 'column_10':
  300. $z = 4;
  301. break;
  302. case 'column_11':
  303. $z = 5;
  304. break;
  305. case 'column_12':
  306. $z = 6;
  307. break;
  308. case 'column_13':
  309. $z = 7;
  310. break;
  311. case 'column_14':
  312. $z = 8;
  313. break;
  314. default:
  315. $z = 0;
  316. break;
  317. }
  318. $c = getColor($x->months, $z);
  319. if ($sort_value == 0)
  320. {
  321. switch ($c) {
  322. case 'wgrey':
  323. $s = 0;
  324. break;
  325. case 'orange':
  326. $s = 1;
  327. break;
  328. case 'green':
  329. $s = 2;
  330. break;
  331. case 'yellow':
  332. $s = 3;
  333. break;
  334. default:
  335. $s = 0;
  336. break;
  337. }
  338. }
  339. if ($sort_value == 1)
  340. {
  341. switch ($c) {
  342. case 'wgrey':
  343. $s = 3;
  344. break;
  345. case 'orange':
  346. $s = 0;
  347. break;
  348. case 'green':
  349. $s = 1;
  350. break;
  351. case 'yellow':
  352. $s = 2;
  353. break;
  354. default:
  355. $s = 0;
  356. break;
  357. }
  358. }
  359. if ($sort_value == 2)
  360. {
  361. switch ($c) {
  362. case 'wgrey':
  363. $s = 2;
  364. break;
  365. case 'orange':
  366. $s = 3;
  367. break;
  368. case 'green':
  369. $s = 0;
  370. break;
  371. case 'yellow':
  372. $s = 1;
  373. break;
  374. default:
  375. $s = 0;
  376. break;
  377. }
  378. }
  379. if ($sort_value == 3)
  380. {
  381. switch ($c) {
  382. case 'wgrey':
  383. $s = 1;
  384. break;
  385. case 'orange':
  386. $s = 2;
  387. break;
  388. case 'green':
  389. $s = 3;
  390. break;
  391. case 'yellow':
  392. $s = 0;
  393. break;
  394. default:
  395. $s = 0;
  396. break;
  397. }
  398. }
  399. }
  400. $datas[] = array(
  401. "column_19" => $x->course->name,
  402. "column_0" => $x->member->last_name,
  403. "column_1" => $x->member->first_name,
  404. "column_2" => $x->subscribed . "§" . formatPrice($subPrice),
  405. "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' : ''),
  406. "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' : ''),
  407. "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' : ''),
  408. "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' : ''),
  409. "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' : ''),
  410. "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' : ''),
  411. "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' : ''),
  412. "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' : ''),
  413. "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' : ''),
  414. "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' : ''),
  415. "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' : ''),
  416. "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' : ''),
  417. "column_15" => $x->course_id,
  418. "column_16" => $x->id,
  419. "column_17" => $x->member_id,
  420. "column_18" => $xxx++,
  421. "column_20" => $s
  422. );
  423. }
  424. $count = $member_course->count();
  425. $this->totSExcel = [];
  426. $this->totS = [];
  427. //$js = '';
  428. $xx = 4;
  429. $str = '';
  430. if ($count > 0)
  431. {
  432. $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>";
  433. $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>";
  434. $str .= "<a style='width:100%;float:right; text-align:right; display:block;' class=yellow><small>0</small></a><br>";
  435. $this->totSExcel[] = array('green' => (isset($totalIsc["Y"]) ? formatPrice($totalIsc["Y"]) : 0), 'orange' => (isset($totalIsc["N"]) ? formatPrice($totalIsc["N"]) : 0), 'yellow' => 0);
  436. }
  437. $this->totS[] = $str;
  438. $str = "";
  439. foreach($totals as $z => $t)
  440. {
  441. if ($z == 1) $xx = 5;
  442. if ($z == 2) $xx = 6;
  443. if ($z == 3) $xx = 7;
  444. if ($z == 4) $xx = 8;
  445. if ($z == 5) $xx = 9;
  446. if ($z == 6) $xx = 10;
  447. if ($z == 7) $xx = 11;
  448. if ($z == 8) $xx = 12;
  449. if ($z == 9) $xx = 1;
  450. if ($z == 10) $xx = 2;
  451. if ($z == 11) $xx = 3;
  452. if ($z == 12) $xx = 4;
  453. $str = '';
  454. $aaa = [];
  455. foreach($t as $x => $c)
  456. {
  457. $y = $x == 'yellow' ? $c : formatPrice($c);
  458. $str .= "<a style='width:100%;float:right; text-align:right; display:block;' class=" . $x . "><small>" . $y . "</small></a><br>";
  459. $aaa[$x] = $y;
  460. }
  461. $this->totSExcel[$xx] = $aaa;
  462. $this->totS[$xx] = $str;
  463. //$js .= $xx . "§" . $str . "_";
  464. $xx += 1;
  465. }
  466. for($e=sizeof($this->totS);$e<=12;$e++)
  467. {
  468. $this->totS[] = '';
  469. }
  470. if ($this->sort != '')
  471. {
  472. $s = $this->sort;
  473. if ($s == 1) $s = 21;
  474. if ($column != '')
  475. array_multisort(array_column($datas, 'column_20'), SORT_ASC, SORT_NATURAL|SORT_FLAG_CASE, $datas);
  476. else
  477. array_multisort(array_column($datas, 'column_' . ($s - 2)), $this->dir == "ASC" ? SORT_ASC : SORT_DESC, SORT_NATURAL|SORT_FLAG_CASE, $datas);
  478. }
  479. else
  480. {
  481. array_multisort(array_column($datas, 'column_0'), SORT_ASC, SORT_NATURAL|SORT_FLAG_CASE, $datas);
  482. }
  483. $xxx = 1;
  484. foreach($datas as $yyy => $d)
  485. {
  486. $datas[$yyy]["column_18"] = $xxx++;
  487. }
  488. $this->totalRecords = sizeof($datas);
  489. //$start = 0;
  490. $this->recordsNoPaginate = [];
  491. $this->recordsNoPaginate = $datas;
  492. //if (isset($_GET["start"]))
  493. $datas = array_slice($datas, $this->start, $this->pageLength);
  494. $this->pages = ceil($this->totalRecords / $this->pageLength);
  495. $this->records = [];
  496. $this->records = $datas;
  497. //$this->totS = $js;
  498. return view('livewire.course_list');
  499. }
  500. public function setPage($page)
  501. {
  502. $this->currentPage = $page;
  503. $this->start = $this->pageLength * ($page - 1);
  504. $this->emit('load-data-table');
  505. }
  506. public function setSort($sort)
  507. {
  508. $this->sort = $sort;
  509. if ($this->dir == '')
  510. $this->dir = 'ASC';
  511. else
  512. $this->dir = $this->dir == 'ASC' ? 'DESC' : 'ASC';
  513. $this->emit('load-data-table');
  514. }
  515. public function search()
  516. {
  517. $this->currentPage = 1;
  518. $this->start = 0;
  519. $this->hasFilter = true;
  520. $this->emit('load-data-table');
  521. }
  522. public function getColor($months, $m, $all)
  523. {
  524. $class = "wgrey";
  525. foreach(json_decode($months) as $mm)
  526. {
  527. if ($mm->m == $m)
  528. {
  529. if ($mm->status == "")
  530. {
  531. $class = "orange";
  532. }
  533. if ($mm->status == "1")
  534. {
  535. $class = "green";
  536. }
  537. if ($mm->status == "2")
  538. {
  539. $class = "yellow";
  540. }
  541. if (!$all && $class == "green")
  542. {
  543. $class = "orange";
  544. }
  545. }
  546. }
  547. return $class;
  548. }
  549. public function newPayment($course_id, $months, $member_id, $id, $subscription)
  550. {
  551. $newMonths = array();
  552. if ($months != '')
  553. {
  554. $mm = explode(",", $months);
  555. foreach($mm as $month)
  556. {
  557. if ($month < 5) $month += 12;
  558. if ($month >= 5) $month -= 4;
  559. $newMonths[] = $month;
  560. }
  561. }
  562. $c = \App\Models\Course::findOrFail($course_id);
  563. $m = \App\Models\MemberCourse::findOrFail($id);
  564. $price = $m->price;
  565. $subscription_price = $m->subscription_price;
  566. $records = \App\Models\Record::where('member_course_id', $m->id)->where('deleted', 0)->get();
  567. foreach ($records as $record)
  568. {
  569. if (in_array($month, json_decode($record->months)))
  570. {
  571. foreach ($record->rows as $row)
  572. {
  573. if ($row->causal_id == $c->causal_id && !str_contains(strtolower($row->note), 'iscrizione'))
  574. {
  575. $tot = sizeof(json_decode($row->when));
  576. foreach(json_decode($row->when) as $m)
  577. {
  578. $price -= $row->amount / $tot;
  579. }
  580. }
  581. }
  582. }
  583. }
  584. 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);
  585. }
  586. public function suspendPayment($course_id, $month, $member_id, $id, $subscription)
  587. {
  588. $monthMap = [
  589. 1 => 9, // September
  590. 2 => 10, // October
  591. 3 => 11, // November
  592. 4 => 12, // December
  593. 5 => 1, // January
  594. 6 => 2, // February
  595. 7 => 3, // March
  596. 8 => 4, // April
  597. 9 => 5, // May
  598. 10 => 6, // June
  599. 11 => 7, // July
  600. 12 => 8 // August
  601. ];
  602. $dbMonth = isset($monthMap[$month]) ? $monthMap[$month] : $month;
  603. $memberCourse = MemberCourse::where('id', $id)
  604. ->where('member_id', $member_id)
  605. ->where('course_id', $course_id)
  606. ->first();
  607. if (!$memberCourse) {
  608. return response()->json(['error' => 'Non Trovato'], 404);
  609. }
  610. $monthsData = json_decode($memberCourse->months, true);
  611. if (!is_array($monthsData)) {
  612. return response()->json(['error' => 'Invalid months data format'], 400);
  613. }
  614. $monthUpdated = false;
  615. foreach ($monthsData as &$monthData) {
  616. if ($monthData['m'] == $dbMonth) {
  617. $monthData['status'] = 2;
  618. $monthUpdated = true;
  619. }
  620. }
  621. if (!$monthUpdated) {
  622. return response()->json(['error' => 'Month not found in data'], 404);
  623. }
  624. $memberCourse->months = json_encode($monthsData);
  625. $memberCourse->save();
  626. session()->flash('success', 'Payment suspended successfully');
  627. return redirect()->to('/course_list');
  628. }
  629. public function resumePayment($course_id, $month, $member_id, $id, $subscription)
  630. {
  631. Log::info('resumePayment');
  632. Log::info($course_id);
  633. Log::info($month);
  634. Log::info($member_id);
  635. Log::info($id);
  636. $monthMap = [
  637. 1 => 9, // September
  638. 2 => 10, // October
  639. 3 => 11, // November
  640. 4 => 12, // December
  641. 5 => 1, // January
  642. 6 => 2, // February
  643. 7 => 3, // March
  644. 8 => 4, // April
  645. 9 => 5, // May
  646. 10 => 6, // June
  647. 11 => 7, // July
  648. 12 => 8 // August
  649. ];
  650. $dbMonth = isset($monthMap[$month]) ? $monthMap[$month] : $month;
  651. $memberCourse = MemberCourse::where('id', $id)
  652. ->where('member_id', $member_id)
  653. ->where('course_id', $course_id)
  654. ->first();
  655. if (!$memberCourse) {
  656. return response()->json(['error' => 'Non Trovato'], 404);
  657. }
  658. $monthsData = json_decode($memberCourse->months, true);
  659. Log::info('data mese',$monthsData);
  660. if (!is_array($monthsData)) {
  661. return response()->json(['error' => 'Invalid months data format'], 400);
  662. }
  663. $monthUpdated = false;
  664. foreach ($monthsData as &$monthData) {
  665. if ($monthData['m'] == $dbMonth) {
  666. $monthData['status'] = "";
  667. $monthUpdated = true;
  668. }
  669. }
  670. Log::info($monthUpdated);
  671. if (!$monthUpdated) {
  672. return response()->json(['error' => 'Month not found in data'], 404);
  673. }
  674. $memberCourse->months = json_encode($monthsData);
  675. $memberCourse->save();
  676. session()->flash('success', 'Payment resumed successfully');
  677. return redirect()->to('/course_list');
  678. }
  679. /*
  680. public function newPayment()
  681. {
  682. $c = \App\Models\Course::findOrFail($this->selectedCourseId);
  683. 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);
  684. }
  685. */
  686. public function newSubscription($course_id, $member_id, $id)
  687. {
  688. $c = \App\Models\Course::findOrFail($course_id);
  689. 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);
  690. }
  691. public function disableSearch()
  692. {
  693. $this->filterCourse = [];
  694. $this->filterLevel = [];
  695. $this->filterType = [];
  696. $this->filterDuration = [];
  697. $this->filterFrequency = [];
  698. $this->hasFilter = false;
  699. }
  700. public function export()
  701. {
  702. $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');
  703. $spreadsheet = new Spreadsheet();
  704. $activeWorksheet = $spreadsheet->getActiveSheet();
  705. //$activeWorksheet->setCellValue('A1', 'PrimaNota');
  706. $activeWorksheet->setCellValue('A1', "Corso");
  707. $activeWorksheet->setCellValue('B1', "Cognome");
  708. $activeWorksheet->setCellValue('C1', "Nome");
  709. $activeWorksheet->setCellValue('D1', "Iscrizione");
  710. $activeWorksheet->setCellValue('E1', "Settembre");
  711. $activeWorksheet->setCellValue('F1', "Ottobre");
  712. $activeWorksheet->setCellValue('G1', "Novembre");
  713. $activeWorksheet->setCellValue('H1', "Dicembre");
  714. $activeWorksheet->setCellValue('I1', "Gennaio");
  715. $activeWorksheet->setCellValue('J1', "Febbraio");
  716. $activeWorksheet->setCellValue('K1', "Marzo");
  717. $activeWorksheet->setCellValue('L1', "Aprile");
  718. $activeWorksheet->setCellValue('M1', "Maggio");
  719. $activeWorksheet->setCellValue('N1', "Giugno");
  720. $activeWorksheet->setCellValue('O1', "Luglio");
  721. $activeWorksheet->setCellValue('P1', "Agosto");
  722. $count = 2;
  723. foreach($this->recordsNoPaginate as $idx => $record)
  724. {
  725. $activeWorksheet->setCellValue('A' . $count, $record["column_19"]);
  726. $activeWorksheet->setCellValue('B' . $count, $record["column_0"]);
  727. $activeWorksheet->setCellValue('C' . $count, $record["column_1"]);
  728. list($color, $value) = explode("§", $record["column_2"]);
  729. $activeWorksheet->setCellValue('D' . $count, $value);
  730. $c = '#FFFFFF';
  731. if($color == 0)
  732. $c = 'ffa500';
  733. if($color == 1)
  734. $c = '00ff00';
  735. $activeWorksheet->getStyle('D' . $count . ':D' . $count)
  736. ->getFill()
  737. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  738. ->getStartColor()
  739. ->setARGB($c);
  740. for($ii=3; $ii<=14; $ii++)
  741. {
  742. list($color, $value) = explode("§", $record["column_" . $ii]);
  743. $c = 'FFFFFF';
  744. if($color == 'orange')
  745. $c = 'ffa500';
  746. if($color == 'green')
  747. $c = '00ff00';
  748. if($color == 'yellow')
  749. {
  750. $c = '5088bf';
  751. $activeWorksheet->getStyle($letters[$ii + 1] . $count . ':' . $letters[$ii + 1] . $count)
  752. ->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_WHITE);;
  753. }
  754. if($color == 'wgrey')
  755. $value = '';
  756. $activeWorksheet->setCellValue($letters[$ii + 1] . $count, $value);
  757. $activeWorksheet->getStyle($letters[$ii + 1] . $count . ':' . $letters[$ii + 1] . $count)
  758. ->getFill()
  759. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  760. ->getStartColor()
  761. ->setARGB($c);
  762. /*$activeWorksheet->cells($letters[$ii + 1] . $count . ':' . $letters[$ii + 1] . $count, function ($cells) use ($c)
  763. {
  764. $cells->setBackground($c);
  765. //$cells->setAlignment('center');
  766. });*/
  767. }
  768. $activeWorksheet->getStyle("A1:P1")->getFont()->setBold( true );
  769. $count++;
  770. }
  771. // Totali
  772. $activeWorksheet->setCellValue('A' . (1 + $count), '');
  773. $activeWorksheet->setCellValue('B' . (1 + $count), '');
  774. $activeWorksheet->setCellValue('C' . (1 + $count), '');
  775. for($x=0; $x<=sizeof($this->totSExcel); $x++)
  776. {
  777. if (isset($this->totSExcel[$x]))
  778. {
  779. $activeWorksheet->setCellValue($letters[$x + 3] . (1 + $count), isset($this->totSExcel[$x]['green']) ? $this->totSExcel[$x]['green'] : 0);
  780. $activeWorksheet->getStyle($letters[$x + 3] . (1 + $count) . ':' . $letters[$x + 3] . (1 + $count))
  781. ->getFill()
  782. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  783. ->getStartColor()
  784. ->setARGB('00ff00');
  785. $activeWorksheet->setCellValue($letters[$x + 3] . (2 + $count), isset($this->totSExcel[$x]['orange']) ? $this->totSExcel[$x]['orange'] : 0);
  786. $activeWorksheet->getStyle($letters[$x + 3] . (2 + $count) . ':' . $letters[$x + 3] . (2 + $count))
  787. ->getFill()
  788. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  789. ->getStartColor()
  790. ->setARGB('ffa500');
  791. $activeWorksheet->setCellValue($letters[$x + 3] . (3 + $count), isset($this->totSExcel[$x]['yellow']) ? $this->totSExcel[$x]['yellow'] : 0);
  792. $activeWorksheet->getStyle($letters[$x + 3] . (3 + $count) . ':' . $letters[$x + 3] . (3 + $count))
  793. ->getFill()
  794. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  795. ->getStartColor()
  796. ->setARGB('5088bf');
  797. $activeWorksheet->getStyle($letters[$x + 3] . (3 + $count) . ':' . $letters[$x + 3] . (3 + $count))
  798. ->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_WHITE);;
  799. }
  800. }
  801. $writer = new Xlsx($spreadsheet);
  802. $writer->save($path = storage_path('pagamento_corsi_' . date("YmdHis") . '.xlsx'));
  803. $this->emit('load-data-table');
  804. return response()->download($path)->deleteFileAfterSend();
  805. }
  806. }