CourseList.php 37 KB

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