CourseList.php 36 KB

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