CourseList.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class CourseList extends Component
  5. {
  6. public $records = array();
  7. public $courses = array();
  8. public $hasFilter = false;
  9. public $courseId = 0;
  10. public $filterCourse = [];
  11. public $filterLevel = [];
  12. public $filterFrequency = [];
  13. public $filterType = [];
  14. public $filterDuration = [];
  15. public $course_durations = [];
  16. public $course_types = [];
  17. public $course_frequencies = [];
  18. public $course_levels = [];
  19. public $course_years = [];
  20. public $totals = [];
  21. public $totalIsc = [];
  22. public $aaa;
  23. public $months = array('Set', 'Ott', 'Nov', 'Dic', 'Gen', 'Feb', 'Mar', 'Apr', 'Mag', 'Giu', 'Lug', 'Ago');
  24. public function mount()
  25. {
  26. $this->selectedCourseId = 0;
  27. $this->selectedMemberId = 0;
  28. $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->get();
  29. $this->course_durations = \App\Models\CourseDuration::select('*')->where('enabled', true)->get();
  30. $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->get();
  31. $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->get();
  32. $this->course_years = \App\Models\Course::select('year')->where('year', '<>', '')->groupBy('year')->pluck('year');
  33. $this->courses = \App\Models\Course::orderBy('name')->groupBy('name')->pluck('name');
  34. //if (sizeof($this->courses) > 0)
  35. // $this->courseId = $this->courses[0]->id;
  36. }
  37. public function render()
  38. {
  39. // Carico tutti i corsi associati
  40. /*
  41. if ($this->courseId > 0)
  42. $member_course = \App\Models\MemberCourse::where('course_id', $this->courseId)->with('member'); //->get();
  43. else
  44. $member_course = \App\Models\MemberCourse::with('member'); //->get();
  45. */
  46. if ($this->hasFilter)
  47. {
  48. $member_course = \App\Models\MemberCourse::with('member');
  49. if (sizeof($this->filterCourse) > 0)
  50. {
  51. $course_ids = [];
  52. foreach($this->filterCourse as $c)
  53. {
  54. $all = \App\Models\Course::where('name', 'like', '%' . $c . "%")->get();
  55. foreach($all as $a)
  56. {
  57. $course_ids[] = $a->id;
  58. }
  59. //$course_ids = array_merge($course_ids, $a);
  60. }
  61. //$course_ids = \App\Models\Course::where('name', 'like', '%' . $this->filterCourse . "%")->pluck('id');
  62. $member_course = $member_course->whereIn('course_id', $course_ids);
  63. }
  64. if (sizeof($this->filterLevel) > 0)
  65. {
  66. $course_ids = \App\Models\Course::whereIn('course_level_id', $this->filterLevel)->pluck('id');
  67. $member_course = $member_course->whereIn('course_id', $course_ids);
  68. }
  69. if (sizeof($this->filterFrequency) > 0)
  70. {
  71. $course_ids = \App\Models\Course::whereIn('course_frequency_id', $this->filterFrequency)->pluck('id');
  72. $member_course = $member_course->whereIn('course_id', $course_ids);
  73. }
  74. if (sizeof($this->filterType) > 0)
  75. {
  76. $course_ids = \App\Models\Course::whereIn('course_type_id', $this->filterType)->pluck('id');
  77. $member_course = $member_course->whereIn('course_id', $course_ids);
  78. }
  79. if (sizeof($this->filterDuration) > 0)
  80. {
  81. $course_ids = \App\Models\Course::whereIn('course_duration_id', $this->filterDuration)->pluck('id');
  82. $member_course = $member_course->whereIn('course_id', $course_ids);
  83. }
  84. //$member_course = $member_course->where('course_id', 999999);
  85. $member_course = $member_course->get();
  86. }
  87. else
  88. {
  89. $member_course = \App\Models\MemberCourse::with('member')->get();
  90. }
  91. $this->records = [];
  92. $this->totals = [];
  93. $this->totalIsc = [];
  94. foreach($member_course as $x)
  95. {
  96. $price = 0;
  97. $price = $x->course->price;
  98. $subPrice = $x->course->subscription_price;
  99. $records = \App\Models\Record::where('member_course_id', $x->id)->where('deleted', 0)->get();
  100. $prices = [];
  101. foreach ($records as $record)
  102. {
  103. foreach ($record->rows as $row)
  104. {
  105. //if ($row->causal_id == $x->course->sub_causal_id || str_contains(strtolower($row->note), 'iscrizione'))
  106. if (str_contains(strtolower($row->note), 'iscrizione'))
  107. {
  108. $subPrice = $row->amount;
  109. }
  110. if ($row->causal_id == $x->course->causal_id && !str_contains(strtolower($row->note), 'iscrizione'))
  111. {
  112. $tot = sizeof(json_decode($row->when));
  113. foreach(json_decode($row->when) as $m)
  114. {
  115. $prices[$m->month] = $row->amount / $tot;
  116. }
  117. }
  118. }
  119. }
  120. for($i=1; $i<=12; $i++)
  121. {
  122. $cls = $this->getColor($x->months, $i);
  123. if ($cls != 'grey')
  124. {
  125. if (!isset($this->totals[$i]))
  126. {
  127. $this->totals[$i]['green'] = 0;
  128. $this->totals[$i]['orange'] = 0;
  129. $this->totals[$i]['yellow'] = 0;
  130. }
  131. if ($cls == 'yellow')
  132. {
  133. $this->totals[$i][$cls] += 1;
  134. }
  135. else
  136. {
  137. $p = isset($prices[$i]) ? $prices[$i] : $price;
  138. $this->totals[$i][$cls] += $p;
  139. }
  140. }
  141. }
  142. $sub = $x->subscribed ? "Y" : "N";
  143. if (isset($this->totalIsc[$sub]))
  144. $this->totalIsc[$sub] += $subPrice;
  145. else
  146. $this->totalIsc[$sub] = $subPrice;
  147. $this->records[] = array(
  148. $x->member_id . "§" . $x->member->first_name . "§" . $x->member->last_name,
  149. $this->getColor($x->months, 9) . "§" . (isset($prices[9]) ? $prices[9] : $price),
  150. $this->getColor($x->months, 10) . "§" . (isset($prices[10]) ? $prices[10] : $price),
  151. $this->getColor($x->months, 11) . "§" . (isset($prices[11]) ? $prices[11] : $price),
  152. $this->getColor($x->months, 12) . "§" . (isset($prices[12]) ? $prices[12] : $price),
  153. $this->getColor($x->months, 1) . "§" . (isset($prices[1]) ? $prices[1] : $price),
  154. $this->getColor($x->months, 2) . "§" . (isset($prices[2]) ? $prices[2] : $price),
  155. $this->getColor($x->months, 3) . "§" . (isset($prices[3]) ? $prices[3] : $price),
  156. $this->getColor($x->months, 4) . "§" . (isset($prices[4]) ? $prices[4] : $price),
  157. $this->getColor($x->months, 5) . "§" . (isset($prices[5]) ? $prices[5] : $price),
  158. $this->getColor($x->months, 6) . "§" . (isset($prices[6]) ? $prices[6] : $price),
  159. $this->getColor($x->months, 7) . "§" . (isset($prices[7]) ? $prices[7] : $price),
  160. $this->getColor($x->months, 8) . "§" . (isset($prices[8]) ? $prices[8] : $price),
  161. $x->course_id,
  162. $x->id,
  163. $x->subscribed . "§" . $subPrice
  164. );
  165. }
  166. $js = '';
  167. $xx = 2;
  168. $str = '';
  169. $str .= "<a class=green><small>" . (isset($this->totalIsc["Y"]) ? formatPrice($this->totalIsc["Y"]) : 0) . "</small></a><br>";
  170. $str .= "<a class=orange><small>" . (isset($this->totalIsc["N"]) ? formatPrice($this->totalIsc["N"]) : 0) . "</small></a><br>";
  171. $str .= "<a class=yellow><small>0</small></a><br>";
  172. $js .= "2§" . $str . "_";
  173. $str = "";
  174. foreach($this->totals as $z => $t)
  175. {
  176. if ($z == 1) $xx = 7;
  177. if ($z == 2) $xx = 8;
  178. if ($z == 3) $xx = 9;
  179. if ($z == 4) $xx = 10;
  180. if ($z == 5) $xx = 11;
  181. if ($z == 6) $xx = 12;
  182. if ($z == 7) $xx = 13;
  183. if ($z == 8) $xx = 14;
  184. if ($z == 9) $xx = 3;
  185. if ($z == 10) $xx = 4;
  186. if ($z == 11) $xx = 5;
  187. if ($z == 12) $xx = 6;
  188. $str = '';
  189. foreach($t as $x => $c)
  190. {
  191. $y = $x == 'yellow' ? $c : formatPrice($c);
  192. $str .= "<a class=" . $x . "><small>" . $y . "</small></a><br>";
  193. }
  194. $js .= $xx . "§" . $str . "_";
  195. $xx += 1;
  196. }
  197. //$this->aaa = $js;
  198. //$this->emit('setTotals', $js);
  199. $this->emit('load-data-table');
  200. return view('livewire.course_list');
  201. }
  202. public function search()
  203. {
  204. $this->hasFilter = true;
  205. }
  206. public function getColor($months, $m)
  207. {
  208. $class = "grey";
  209. foreach(json_decode($months) as $mm)
  210. {
  211. if ($mm->m == $m)
  212. {
  213. if ($mm->status == "")
  214. {
  215. $class = "orange";
  216. }
  217. if ($mm->status == "1")
  218. {
  219. $class = "green";
  220. }
  221. if ($mm->status == "2")
  222. {
  223. $class = "yellow";
  224. }
  225. }
  226. }
  227. return $class;
  228. }
  229. public function newPayment($course_id, $months, $member_id, $id, $subscription)
  230. {
  231. $newMonths = array();
  232. if ($months != '')
  233. {
  234. $mm = explode(",", $months);
  235. foreach($mm as $month)
  236. {
  237. if ($month < 5) $month += 12;
  238. if ($month >= 5) $month -= 4;
  239. $newMonths[] = $month;
  240. }
  241. }
  242. $c = \App\Models\Course::findOrFail($course_id);
  243. 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=' . $c->price : '') . '&subscription_price=' . $c->subscription_price . "&courseId=" . $id);
  244. }
  245. /*
  246. public function newPayment()
  247. {
  248. $c = \App\Models\Course::findOrFail($this->selectedCourseId);
  249. 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);
  250. }
  251. */
  252. public function newSubscription($course_id, $member_id, $id)
  253. {
  254. $c = \App\Models\Course::findOrFail($course_id);
  255. 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);
  256. }
  257. public function disableSearch()
  258. {
  259. $this->filterCourse = [];
  260. $this->filterLevel = [];
  261. $this->filterType = [];
  262. $this->filterDuration = [];
  263. $this->filterFrequency = [];
  264. $this->hasFilter = false;
  265. }
  266. }