CourseList.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 $courseId = 0;
  9. public $filterCourse = [];
  10. public $filterLevel = [];
  11. public $filterFrequency = [];
  12. public $filterType = [];
  13. public $filterDuration = [];
  14. public $course_durations = [];
  15. public $course_types = [];
  16. public $course_frequencies = [];
  17. public $course_levels = [];
  18. public $totals = [];
  19. public $totalIsc = [];
  20. public $months = array('Set', 'Ott', 'Nov', 'Dic', 'Gen', 'Feb', 'Mar', 'Apr', 'Mag', 'Giu', 'Lug', 'Ago');
  21. public function mount()
  22. {
  23. $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->get();
  24. $this->course_durations = \App\Models\CourseDuration::select('*')->where('enabled', true)->get();
  25. $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->get();
  26. $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->get();
  27. $this->courses = \App\Models\Course::orderBy('name')->groupBy('name')->pluck('name');
  28. //if (sizeof($this->courses) > 0)
  29. // $this->courseId = $this->courses[0]->id;
  30. }
  31. public function render()
  32. {
  33. // Carico tutti i corsi associati
  34. /*
  35. if ($this->courseId > 0)
  36. $member_course = \App\Models\MemberCourse::where('course_id', $this->courseId)->with('member'); //->get();
  37. else
  38. $member_course = \App\Models\MemberCourse::with('member'); //->get();
  39. */
  40. $member_course = \App\Models\MemberCourse::with('member');
  41. if (sizeof($this->filterCourse) > 0)
  42. {
  43. $course_ids = [];
  44. foreach($this->filterCourse as $c)
  45. {
  46. $all = \App\Models\Course::where('name', 'like', '%' . $c . "%")->get();
  47. foreach($all as $a)
  48. {
  49. $course_ids[] = $a->id;
  50. }
  51. //$course_ids = array_merge($course_ids, $a);
  52. }
  53. //$course_ids = \App\Models\Course::where('name', 'like', '%' . $this->filterCourse . "%")->pluck('id');
  54. $member_course = $member_course->whereIn('course_id', $course_ids);
  55. }
  56. if (sizeof($this->filterLevel) > 0)
  57. {
  58. $course_ids = \App\Models\Course::whereIn('course_level_id', $this->filterLevel)->pluck('id');
  59. $member_course = $member_course->whereIn('course_id', $course_ids);
  60. }
  61. if (sizeof($this->filterFrequency) > 0)
  62. {
  63. $course_ids = \App\Models\Course::whereIn('course_frequency_id', $this->filterFrequency)->pluck('id');
  64. $member_course = $member_course->whereIn('course_id', $course_ids);
  65. }
  66. if (sizeof($this->filterType) > 0)
  67. {
  68. $course_ids = \App\Models\Course::whereIn('course_type_id', $this->filterType)->pluck('id');
  69. $member_course = $member_course->whereIn('course_id', $course_ids);
  70. }
  71. if (sizeof($this->filterDuration) > 0)
  72. {
  73. $course_ids = \App\Models\Course::whereIn('course_duration_id', $this->filterDuration)->pluck('id');
  74. $member_course = $member_course->whereIn('course_id', $course_ids);
  75. }
  76. $member_course = $member_course->get();
  77. $this->records = array();
  78. foreach($member_course as $x)
  79. {
  80. $price = 0;
  81. $price = $x->course->price;
  82. $subPrice = $x->course->subscription_price;
  83. $records = \App\Models\Record::where('member_course_id', $x->id)->where('deleted', 0)->get();
  84. $prices = [];
  85. foreach ($records as $record)
  86. {
  87. foreach ($record->rows as $row)
  88. {
  89. //if ($row->causal_id == $x->course->sub_causal_id || str_contains(strtolower($row->note), 'iscrizione'))
  90. if (str_contains(strtolower($row->note), 'iscrizione'))
  91. {
  92. $subPrice = $row->amount;
  93. }
  94. if ($row->causal_id == $x->course->causal_id && !str_contains(strtolower($row->note), 'iscrizione'))
  95. {
  96. $tot = sizeof(json_decode($row->when));
  97. foreach(json_decode($row->when) as $m)
  98. {
  99. $prices[$m->month] = $row->amount / $tot;
  100. }
  101. }
  102. }
  103. }
  104. for($i=1; $i<=12; $i++)
  105. {
  106. $cls = $this->getColor($x->months, $i);
  107. if ($cls != 'grey')
  108. {
  109. if (!isset($this->totals[$i]))
  110. {
  111. $this->totals[$i]['green'] = 0;
  112. $this->totals[$i]['orange'] = 0;
  113. $this->totals[$i]['yellow'] = 0;
  114. }
  115. if ($cls == 'yellow')
  116. {
  117. $this->totals[$i][$cls] += 1;
  118. }
  119. else
  120. {
  121. $p = isset($prices[$i]) ? $prices[$i] : $price;
  122. $this->totals[$i][$cls] += $p;
  123. }
  124. }
  125. }
  126. $sub = $x->subscribed ? "Y" : "N";
  127. if (isset($this->totalIsc[$sub]))
  128. $this->totalIsc[$sub] += $subPrice;
  129. else
  130. $this->totalIsc[$sub] = $subPrice;
  131. $this->records[] = array(
  132. $x->member_id . "§" . $x->member->first_name . "§" . $x->member->last_name,
  133. $this->getColor($x->months, 9) . "§" . (isset($prices[9]) ? $prices[9] : $price),
  134. $this->getColor($x->months, 10) . "§" . (isset($prices[10]) ? $prices[10] : $price),
  135. $this->getColor($x->months, 11) . "§" . (isset($prices[11]) ? $prices[11] : $price),
  136. $this->getColor($x->months, 12) . "§" . (isset($prices[12]) ? $prices[12] : $price),
  137. $this->getColor($x->months, 1) . "§" . (isset($prices[1]) ? $prices[1] : $price),
  138. $this->getColor($x->months, 2) . "§" . (isset($prices[2]) ? $prices[2] : $price),
  139. $this->getColor($x->months, 3) . "§" . (isset($prices[3]) ? $prices[3] : $price),
  140. $this->getColor($x->months, 4) . "§" . (isset($prices[4]) ? $prices[4] : $price),
  141. $this->getColor($x->months, 5) . "§" . (isset($prices[5]) ? $prices[5] : $price),
  142. $this->getColor($x->months, 6) . "§" . (isset($prices[6]) ? $prices[6] : $price),
  143. $this->getColor($x->months, 7) . "§" . (isset($prices[7]) ? $prices[7] : $price),
  144. $this->getColor($x->months, 8) . "§" . (isset($prices[8]) ? $prices[8] : $price),
  145. $x->course_id,
  146. $x->id,
  147. $x->subscribed . "§" . $subPrice
  148. );
  149. }
  150. $this->emit('load-data-table');
  151. return view('livewire.course_list');
  152. }
  153. public function getColor($months, $m)
  154. {
  155. $class = "grey";
  156. foreach(json_decode($months) as $mm)
  157. {
  158. if ($mm->m == $m)
  159. {
  160. if ($mm->status == "")
  161. {
  162. $class = "orange";
  163. }
  164. if ($mm->status == "1")
  165. {
  166. $class = "green";
  167. }
  168. if ($mm->status == "2")
  169. {
  170. $class = "yellow";
  171. }
  172. }
  173. }
  174. return $class;
  175. }
  176. public function newPayment($course_id, $month, $member_id, $id)
  177. {
  178. if ($month < 5) $month += 12;
  179. if ($month >= 5) $month -= 4;
  180. $c = \App\Models\Course::findOrFail($course_id);
  181. return redirect()->to('/in?new=1&memberId=' . $member_id . '&causalId=' . $c->causal_id . '&subCausalId=' . $c->sub_causal_id . '&createSubscription=0&months=' . $month . '&price=' . $c->price . '&subscription_price=' . $c->subscription_price . "&courseId=" . $id);
  182. }
  183. public function newSubscription($course_id, $member_id, $id)
  184. {
  185. $c = \App\Models\Course::findOrFail($course_id);
  186. 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);
  187. }
  188. public function disableSearch()
  189. {
  190. $this->filterCourse = [];
  191. $this->filterLevel = [];
  192. $this->filterType = [];
  193. $this->filterDuration = [];
  194. $this->filterFrequency = [];
  195. }
  196. }