CourseList.php 7.5 KB

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