CourseList.php 37 KB

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