CourseList.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use App\Models\MemberCourse;
  5. use Illuminate\Support\Facades\Log;
  6. class CourseList extends Component
  7. {
  8. public $records = array();
  9. public $courses = array();
  10. public $hasFilter = false;
  11. public $courseId = 0;
  12. public $filterCourse = [];
  13. public $filterLevel = [];
  14. public $filterFrequency = [];
  15. public $filterType = [];
  16. public $filterDuration = [];
  17. public $course_durations = [];
  18. public $course_types = [];
  19. public $course_frequencies = [];
  20. public $course_levels = [];
  21. public $course_years = [];
  22. public $totals = [];
  23. public $totalIsc = [];
  24. public $aaa;
  25. public $months = array('Set', 'Ott', 'Nov', 'Dic', 'Gen', 'Feb', 'Mar', 'Apr', 'Mag', 'Giu', 'Lug', 'Ago');
  26. public function mount()
  27. {
  28. $this->selectedCourseId = 0;
  29. $this->selectedMemberId = 0;
  30. $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->get();
  31. $this->course_durations = \App\Models\CourseDuration::select('*')->where('enabled', true)->get();
  32. $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->get();
  33. $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->get();
  34. $this->course_years = \App\Models\Course::select('year')->where('year', '<>', '')->groupBy('year')->pluck('year');
  35. $this->courses = \App\Models\Course::orderBy('name')->groupBy('name')->pluck('name');
  36. //if (sizeof($this->courses) > 0)
  37. // $this->courseId = $this->courses[0]->id;
  38. }
  39. public function render()
  40. {
  41. // Carico tutti i corsi associati
  42. /*
  43. if ($this->courseId > 0)
  44. $member_course = \App\Models\MemberCourse::where('course_id', $this->courseId)->with('member'); //->get();
  45. else
  46. $member_course = \App\Models\MemberCourse::with('member'); //->get();
  47. */
  48. if ($this->hasFilter)
  49. {
  50. $member_course = \App\Models\MemberCourse::with('member');
  51. if (sizeof($this->filterCourse) > 0)
  52. {
  53. $course_ids = [];
  54. foreach($this->filterCourse as $c)
  55. {
  56. $all = \App\Models\Course::where('name', 'like', '%' . $c . "%")->get();
  57. foreach($all as $a)
  58. {
  59. $course_ids[] = $a->id;
  60. }
  61. //$course_ids = array_merge($course_ids, $a);
  62. }
  63. //$course_ids = \App\Models\Course::where('name', 'like', '%' . $this->filterCourse . "%")->pluck('id');
  64. $member_course = $member_course->whereIn('course_id', $course_ids);
  65. }
  66. if (sizeof($this->filterLevel) > 0)
  67. {
  68. $course_ids = \App\Models\Course::whereIn('course_level_id', $this->filterLevel)->pluck('id');
  69. $member_course = $member_course->whereIn('course_id', $course_ids);
  70. }
  71. if (sizeof($this->filterFrequency) > 0)
  72. {
  73. $course_ids = \App\Models\Course::whereIn('course_frequency_id', $this->filterFrequency)->pluck('id');
  74. $member_course = $member_course->whereIn('course_id', $course_ids);
  75. }
  76. if (sizeof($this->filterType) > 0)
  77. {
  78. $course_ids = \App\Models\Course::whereIn('course_type_id', $this->filterType)->pluck('id');
  79. $member_course = $member_course->whereIn('course_id', $course_ids);
  80. }
  81. if (sizeof($this->filterDuration) > 0)
  82. {
  83. $course_ids = \App\Models\Course::whereIn('course_duration_id', $this->filterDuration)->pluck('id');
  84. $member_course = $member_course->whereIn('course_id', $course_ids);
  85. }
  86. //$member_course = $member_course->where('course_id', 999999);
  87. $member_course = $member_course->get();
  88. }
  89. else
  90. {
  91. $member_course = \App\Models\MemberCourse::with('member')->get();
  92. }
  93. $this->records = [];
  94. $this->totals = [];
  95. $this->totalIsc = [];
  96. foreach($member_course as $x)
  97. {
  98. $price = 0;
  99. $price = $x->course->price;
  100. $subPrice = $x->course->subscription_price;
  101. $records = \App\Models\Record::where('member_course_id', $x->id)->where('deleted', 0)->get();
  102. $prices = [];
  103. foreach ($records as $record)
  104. {
  105. foreach ($record->rows as $row)
  106. {
  107. //if ($row->causal_id == $x->course->sub_causal_id || str_contains(strtolower($row->note), 'iscrizione'))
  108. if (str_contains(strtolower($row->note), 'iscrizione'))
  109. {
  110. $subPrice = $row->amount;
  111. }
  112. if ($row->causal_id == $x->course->causal_id && !str_contains(strtolower($row->note), 'iscrizione'))
  113. {
  114. $tot = sizeof(json_decode($row->when));
  115. foreach(json_decode($row->when) as $m)
  116. {
  117. $prices[$m->month] = $row->amount / $tot;
  118. }
  119. }
  120. }
  121. }
  122. for($i=1; $i<=12; $i++)
  123. {
  124. $cls = $this->getColor($x->months, $i);
  125. if ($cls != 'grey')
  126. {
  127. if (!isset($this->totals[$i]))
  128. {
  129. $this->totals[$i]['green'] = 0;
  130. $this->totals[$i]['orange'] = 0;
  131. $this->totals[$i]['yellow'] = 0;
  132. }
  133. if ($cls == 'yellow')
  134. {
  135. $this->totals[$i][$cls] += 1;
  136. }
  137. else
  138. {
  139. $p = isset($prices[$i]) ? $prices[$i] : $price;
  140. $this->totals[$i][$cls] += $p;
  141. }
  142. }
  143. }
  144. $sub = $x->subscribed ? "Y" : "N";
  145. if (isset($this->totalIsc[$sub]))
  146. $this->totalIsc[$sub] += $subPrice;
  147. else
  148. $this->totalIsc[$sub] = $subPrice;
  149. $this->records[] = array(
  150. $x->member_id . "§" . $x->member->first_name . "§" . $x->member->last_name,
  151. $this->getColor($x->months, 9) . "§" . (isset($prices[9]) ? $prices[9] : $price),
  152. $this->getColor($x->months, 10) . "§" . (isset($prices[10]) ? $prices[10] : $price),
  153. $this->getColor($x->months, 11) . "§" . (isset($prices[11]) ? $prices[11] : $price),
  154. $this->getColor($x->months, 12) . "§" . (isset($prices[12]) ? $prices[12] : $price),
  155. $this->getColor($x->months, 1) . "§" . (isset($prices[1]) ? $prices[1] : $price),
  156. $this->getColor($x->months, 2) . "§" . (isset($prices[2]) ? $prices[2] : $price),
  157. $this->getColor($x->months, 3) . "§" . (isset($prices[3]) ? $prices[3] : $price),
  158. $this->getColor($x->months, 4) . "§" . (isset($prices[4]) ? $prices[4] : $price),
  159. $this->getColor($x->months, 5) . "§" . (isset($prices[5]) ? $prices[5] : $price),
  160. $this->getColor($x->months, 6) . "§" . (isset($prices[6]) ? $prices[6] : $price),
  161. $this->getColor($x->months, 7) . "§" . (isset($prices[7]) ? $prices[7] : $price),
  162. $this->getColor($x->months, 8) . "§" . (isset($prices[8]) ? $prices[8] : $price),
  163. $x->course_id,
  164. $x->id,
  165. $x->subscribed . "§" . $subPrice
  166. );
  167. }
  168. $js = '';
  169. $xx = 2;
  170. $str = '';
  171. $str .= "<a class=green><small>" . (isset($this->totalIsc["Y"]) ? formatPrice($this->totalIsc["Y"]) : 0) . "</small></a><br>";
  172. $str .= "<a class=orange><small>" . (isset($this->totalIsc["N"]) ? formatPrice($this->totalIsc["N"]) : 0) . "</small></a><br>";
  173. $str .= "<a class=yellow><small>0</small></a><br>";
  174. $js .= "2§" . $str . "_";
  175. $str = "";
  176. foreach($this->totals as $z => $t)
  177. {
  178. if ($z == 1) $xx = 7;
  179. if ($z == 2) $xx = 8;
  180. if ($z == 3) $xx = 9;
  181. if ($z == 4) $xx = 10;
  182. if ($z == 5) $xx = 11;
  183. if ($z == 6) $xx = 12;
  184. if ($z == 7) $xx = 13;
  185. if ($z == 8) $xx = 14;
  186. if ($z == 9) $xx = 3;
  187. if ($z == 10) $xx = 4;
  188. if ($z == 11) $xx = 5;
  189. if ($z == 12) $xx = 6;
  190. $str = '';
  191. foreach($t as $x => $c)
  192. {
  193. $y = $x == 'yellow' ? $c : formatPrice($c);
  194. $str .= "<a class=" . $x . "><small>" . $y . "</small></a><br>";
  195. }
  196. $js .= $xx . "§" . $str . "_";
  197. $xx += 1;
  198. }
  199. //$this->aaa = $js;
  200. //$this->emit('setTotals', $js);
  201. $this->emit('load-data-table');
  202. return view('livewire.course_list');
  203. }
  204. public function search()
  205. {
  206. $this->hasFilter = true;
  207. }
  208. public function getColor($months, $m)
  209. {
  210. $class = "grey";
  211. foreach(json_decode($months) as $mm)
  212. {
  213. if ($mm->m == $m)
  214. {
  215. if ($mm->status == "")
  216. {
  217. $class = "orange";
  218. }
  219. if ($mm->status == "1")
  220. {
  221. $class = "green";
  222. }
  223. if ($mm->status == "2")
  224. {
  225. $class = "yellow";
  226. }
  227. }
  228. }
  229. return $class;
  230. }
  231. public function newPayment($course_id, $months, $member_id, $id, $subscription)
  232. {
  233. $newMonths = array();
  234. if ($months != '')
  235. {
  236. $mm = explode(",", $months);
  237. foreach($mm as $month)
  238. {
  239. if ($month < 5) $month += 12;
  240. if ($month >= 5) $month -= 4;
  241. $newMonths[] = $month;
  242. }
  243. }
  244. $c = \App\Models\Course::findOrFail($course_id);
  245. $m = MemberCourse::findOrFail($id);
  246. 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=' . $m->price : '') . '&subscription_price=' . $m->subscription_price . "&courseId=" . $id);
  247. }
  248. public function suspendPayment($course_id, $month, $member_id, $id, $subscription)
  249. {
  250. $monthMap = [
  251. 1 => 9, // September
  252. 2 => 10, // October
  253. 3 => 11, // November
  254. 4 => 12, // December
  255. 5 => 1, // January
  256. 6 => 2, // February
  257. 7 => 3, // March
  258. 8 => 4, // April
  259. 9 => 5, // May
  260. 10 => 6, // June
  261. 11 => 7, // July
  262. 12 => 8 // August
  263. ];
  264. $dbMonth = isset($monthMap[$month]) ? $monthMap[$month] : $month;
  265. $memberCourse = MemberCourse::where('id', $id)
  266. ->where('member_id', $member_id)
  267. ->where('course_id', $course_id)
  268. ->first();
  269. if (!$memberCourse) {
  270. return response()->json(['error' => 'Non Trovato'], 404);
  271. }
  272. $monthsData = json_decode($memberCourse->months, true);
  273. if (!is_array($monthsData)) {
  274. return response()->json(['error' => 'Invalid months data format'], 400);
  275. }
  276. $monthUpdated = false;
  277. foreach ($monthsData as &$monthData) {
  278. if ($monthData['m'] == $dbMonth) {
  279. $monthData['status'] = 2;
  280. $monthUpdated = true;
  281. }
  282. }
  283. if (!$monthUpdated) {
  284. return response()->json(['error' => 'Month not found in data'], 404);
  285. }
  286. $memberCourse->months = json_encode($monthsData);
  287. $memberCourse->save();
  288. session()->flash('success', 'Payment suspended successfully');
  289. return redirect()->to('/course_list');
  290. }
  291. public function resumePayment($course_id, $month, $member_id, $id, $subscription)
  292. {
  293. Log::info('resumePayment');
  294. log::info($course_id);
  295. log::info($month);
  296. log::info($member_id);
  297. log::info($id);
  298. $monthMap = [
  299. 1 => 9, // September
  300. 2 => 10, // October
  301. 3 => 11, // November
  302. 4 => 12, // December
  303. 5 => 1, // January
  304. 6 => 2, // February
  305. 7 => 3, // March
  306. 8 => 4, // April
  307. 9 => 5, // May
  308. 10 => 6, // June
  309. 11 => 7, // July
  310. 12 => 8 // August
  311. ];
  312. $dbMonth = isset($monthMap[$month]) ? $monthMap[$month] : $month;
  313. $memberCourse = MemberCourse::where('id', $id)
  314. ->where('member_id', $member_id)
  315. ->where('course_id', $course_id)
  316. ->first();
  317. if (!$memberCourse) {
  318. return response()->json(['error' => 'Non Trovato'], 404);
  319. }
  320. $monthsData = json_decode($memberCourse->months, true);
  321. Log::info('data mese',$monthsData);
  322. if (!is_array($monthsData)) {
  323. return response()->json(['error' => 'Invalid months data format'], 400);
  324. }
  325. $monthUpdated = false;
  326. foreach ($monthsData as &$monthData) {
  327. if ($monthData['m'] == $dbMonth) {
  328. $monthData['status'] = "";
  329. $monthUpdated = true;
  330. }
  331. }
  332. Log::info($monthUpdated);
  333. if (!$monthUpdated) {
  334. return response()->json(['error' => 'Month not found in data'], 404);
  335. }
  336. $memberCourse->months = json_encode($monthsData);
  337. $memberCourse->save();
  338. session()->flash('success', 'Payment resumed successfully');
  339. return redirect()->to('/course_list');
  340. }
  341. /*
  342. public function newPayment()
  343. {
  344. $c = \App\Models\Course::findOrFail($this->selectedCourseId);
  345. 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);
  346. }
  347. */
  348. public function newSubscription($course_id, $member_id, $id)
  349. {
  350. $c = \App\Models\Course::findOrFail($course_id);
  351. 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);
  352. }
  353. public function disableSearch()
  354. {
  355. $this->filterCourse = [];
  356. $this->filterLevel = [];
  357. $this->filterType = [];
  358. $this->filterDuration = [];
  359. $this->filterFrequency = [];
  360. $this->hasFilter = false;
  361. }
  362. }