CourseList.php 11 KB

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