CourseList.php 37 KB

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