CourseMember.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class CourseMember extends Component
  5. {
  6. public $records = array();
  7. public $courses = [];
  8. public $course_frequencies = [];
  9. public $course_types = [];
  10. public $course_levels = [];
  11. public $course_durations = [];
  12. public $course_years = [];
  13. public $fromYear = "";
  14. public $toYear = "";
  15. public $fromFromYear = "";
  16. public $toToYear = "";
  17. public $filterCourse = [];
  18. public $filterLevel = [];
  19. public $filterFrequency = [];
  20. public $filterType = [];
  21. public $filterDuration = [];
  22. public $filterDays = [];
  23. public $filterHours = [];
  24. public $filterSubscription = "";
  25. public $filterStatus = [];
  26. public $filterYear = "";
  27. /*public $chkCertificateNormal = 0;
  28. public $chkCertificateAgonistico = 0;
  29. public $chkCertificateScaduti = 0;*/
  30. public $chkCertificateType = "";
  31. public $chkCertificateScadenza = 0;
  32. public $chkCard = [];
  33. public $filter = '';
  34. public $filterFromPrevious = '';
  35. public function mount()
  36. {
  37. if (isset($_GET["id"]))
  38. {
  39. $this->filterFromPrevious = $_GET["id"];
  40. }
  41. $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->get();
  42. $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->get();
  43. $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->get();
  44. $this->course_durations = \App\Models\CourseDuration::select('*')->where('enabled', true)->get();
  45. $this->course_years = \App\Models\Course::select('year')->where('year', '<>', '')->groupBy('year')->pluck('year');
  46. $this->courses = \App\Models\Course::orderBy('name')->groupBy('name')->pluck('name');
  47. if (date("m") >= env('FISCAL_YEAR_MONTH_FROM', 1))
  48. $this->filterYear = date("Y") . "-" . (date("Y") + 1);
  49. else
  50. $this->filterYear = (date("Y") - 1) . "-" . date("Y");
  51. }
  52. public function render()
  53. {
  54. // Carico tutti i corsi associati
  55. $this->filter = '';
  56. $datas = \App\Models\MemberCourse::with('member')
  57. ->select('member_courses.*')
  58. ->join('members', 'member_courses.member_id', '=', 'members.id')
  59. ->where(function($query) {
  60. $query->where('members.is_archived', false)
  61. ->orWhereNull('members.is_archived');
  62. })
  63. ->where(function($query) {
  64. $query->where('members.is_deleted', false)
  65. ->orWhereNull('members.is_deleted');
  66. });
  67. if (sizeof($this->filterCourse) > 0)
  68. {
  69. $course_ids = [];
  70. foreach($this->filterCourse as $c)
  71. {
  72. $all = \App\Models\Course::where('name', 'like', '%' . $c . "%")->get();
  73. foreach($all as $a)
  74. {
  75. $course_ids[] = $a->id;
  76. }
  77. }
  78. $datas = $datas->whereIn('course_id', $course_ids);
  79. }
  80. if (sizeof($this->filterLevel) > 0)
  81. {
  82. $course_ids = \App\Models\Course::whereIn('course_level_id', $this->filterLevel)->pluck('id');
  83. $datas = $datas->whereIn('course_id', $course_ids);
  84. }
  85. if (sizeof($this->filterFrequency) > 0)
  86. {
  87. $course_ids = \App\Models\Course::whereIn('course_frequency_id', $this->filterFrequency)->pluck('id');
  88. $datas = $datas->whereIn('course_id', $course_ids);
  89. }
  90. if (sizeof($this->filterType) > 0)
  91. {
  92. $course_ids = \App\Models\Course::whereIn('course_type_id', $this->filterType)->pluck('id');
  93. $datas = $datas->whereIn('course_id', $course_ids);
  94. }
  95. if (sizeof($this->filterDuration) > 0)
  96. {
  97. $course_ids = \App\Models\Course::whereIn('course_duration_id', $this->filterDuration)->pluck('id');
  98. $datas = $datas->whereIn('course_id', $course_ids);
  99. }
  100. if (sizeof($this->filterDays) > 0)
  101. {
  102. $m_ids = [];
  103. foreach($this->filterDays as $c)
  104. {
  105. $all = \App\Models\MemberCourse::where('when', 'like', "%" . $c . "%")->get();
  106. foreach($all as $a)
  107. {
  108. $m_ids[] = $a->member_id;
  109. }
  110. }
  111. $datas = $datas->whereIn('member_id', $m_ids);
  112. }
  113. if (sizeof($this->filterHours) > 0)
  114. {
  115. $m_ids = [];
  116. foreach($this->filterHours as $c)
  117. {
  118. $all = \App\Models\MemberCourse::where('when', 'like', '%"from":"' . $c . "%")->get();
  119. foreach($all as $a)
  120. {
  121. $m_ids[] = $a->member_id;
  122. }
  123. }
  124. $datas = $datas->whereIn('member_id', $m_ids);
  125. }
  126. /*if ($this->filterDays != "")
  127. {
  128. $members_ids = \App\Models\MemberCourse::where('when', 'like', "%" . $this->filterDays . "%")->pluck('member_id');
  129. $datas = $datas->whereIn('member_id', $members_ids);
  130. $this->filter .= $this->filter != '' ? ', ' : '';
  131. $this->filter .= "Giorni : " . $this->filterDays . " ";
  132. }
  133. if ($this->filterHours != "")
  134. {
  135. $members_ids = \App\Models\MemberCourse::where('when', 'like', '%"from":"' . $this->filterHours . "%")->pluck('member_id');
  136. $datas = $datas->whereIn('member_id', $members_ids);
  137. $this->filter .= $this->filter != '' ? ', ' : '';
  138. $this->filter .= "Ore : " . $this->filterHours . " ";
  139. }*/
  140. if ($this->filterSubscription != "")
  141. {
  142. $ids = \App\Models\MemberCourse::where('subscribed', $this->filterSubscription == 1 ? true : false)->pluck('id');
  143. $datas = $datas->whereIn('id', $ids);
  144. $this->filter .= $this->filter != '' ? ', ' : '';
  145. $this->filter .= "Pagata sottoscrizione : " . ($this->filterSubscription == 1 ? "SI" : "NO") . " ";
  146. }
  147. if ($this->chkCertificateType != "")
  148. {
  149. $types = \App\Models\MemberCertificate::where('type', $this->chkCertificateType)->pluck('member_id');
  150. $datas = $datas->whereIn('member_id', $types);
  151. }
  152. if ($this->chkCertificateScadenza > 0)
  153. {
  154. if ($this->chkCertificateScadenza == "1")
  155. $scad = \App\Models\MemberCertificate::where('expire_date', '<', date("Y-m-d"))->pluck('member_id');
  156. if ($this->chkCertificateScadenza == "2")
  157. $scad = \App\Models\MemberCertificate::whereBetween('expire_date', [date("Y-m-d"), date("Y-m-d", strtotime("+1 month"))])->pluck('member_id');
  158. $datas = $datas->whereIn('member_id', $scad);
  159. }
  160. if ($this->fromYear != "")
  161. {
  162. $m_ids = \App\Models\Member::where('birth_date', '<', date("Y-m-d", strtotime("-" . $this->fromYear . " year", time())))->pluck('id');
  163. $datas = $datas->whereIn('member_id', $m_ids);
  164. }
  165. if ($this->toYear != "")
  166. {
  167. $m_ids = \App\Models\Member::where('birth_date', '>', date("Y-m-d", strtotime("-" . $this->toYear . " year", time())))->pluck('id');
  168. $datas = $datas->whereIn('member_id', $m_ids);
  169. }
  170. if ($this->fromFromYear != "")
  171. {
  172. $m_ids = \App\Models\Member::whereYear('birth_date', '>=', $this->fromFromYear)->pluck('id');
  173. $datas = $datas->whereIn('member_id', $m_ids);
  174. }
  175. if ($this->toToYear != "")
  176. {
  177. $m_ids = \App\Models\Member::whereYear('birth_date', '<=', $this->toToYear)->pluck('id');
  178. $datas = $datas->whereIn('member_id', $m_ids);
  179. }
  180. /*if ($_GET["toYear"] != "")
  181. {
  182. $x = $x->where('birth_date', '>', date("Y-m-d", strtotime("-" . $_GET["toYear"] . " year", time())));
  183. }
  184. if ($_GET["fromYearYear"] != "")
  185. {
  186. $x = $x->whereYear('birth_date', '>=', $_GET["fromYearYear"]);
  187. }
  188. if ($_GET["toYearYear"] != "")
  189. {
  190. $x = $x->whereYear('birth_date', '<=', $_GET["toYearYear"]);
  191. }*/
  192. /*
  193. if ($this->chkCertificateNormal > 0)
  194. {
  195. $normal = \App\Models\MemberCertificate::where('type', 'N')->pluck('member_id');
  196. $datas = $datas->whereIn('member_id', $normal);
  197. $this->filter .= $this->filter != '' ? ', ' : '';
  198. $this->filter .= "Certificato normale : SI ";
  199. }
  200. if ($this->chkCertificateAgonistico > 0)
  201. {
  202. $agonistic = \App\Models\MemberCertificate::where('type', 'A')->pluck('member_id');
  203. $datas = $datas->whereIn('member_id', $agonistic);
  204. $this->filter .= $this->filter != '' ? ', ' : '';
  205. $this->filter .= "Certificato agonistico : SI ";
  206. }
  207. if ($this->chkCertificateScaduti > 0)
  208. {
  209. $scaduto = \App\Models\MemberCertificate::where('expire_date', '<', date("Y-m-d"))->pluck('member_id');
  210. $datas = $datas->whereIn('member_id', $scaduto);
  211. $this->filter .= $this->filter != '' ? ', ' : '';
  212. $this->filter .= "Certificato scaduto : SI ";
  213. }
  214. if ($this->chkCertificateScadenza > 0)
  215. {
  216. $scadenza = \App\Models\MemberCertificate::whereBetween('expire_date', [date("Y-m-d"), date("Y-m-d", strtotime("+1 month"))])->pluck('member_id');
  217. $datas = $datas->whereIn('member_id', $scadenza);
  218. $this->filter .= $this->filter != '' ? ', ' : '';
  219. $this->filter .= "Certificato in scadenza : SI ";
  220. }*/
  221. if (sizeof($this->chkCard) > 0)
  222. {
  223. $card_ids = \App\Models\MemberCard::whereIn('card_id', $this->chkCard)->pluck('member_id');
  224. $datas = $datas->whereIn('member_id', $card_ids);
  225. $this->filter .= $this->filter != '' ? ', ' : '';
  226. $this->filter .= "Tessera : ";
  227. foreach($this->chkCard as $card)
  228. {
  229. $this->filter .= \App\Models\Card::findOrFail($card)->name . " ";
  230. }
  231. }
  232. if ($this->filterYear != "")
  233. {
  234. $course_ids = \App\Models\Course::where('year', $this->filterYear)->pluck('id');
  235. $datas = $datas->whereIn('course_id', $course_ids);
  236. $this->filter .= $this->filter != '' ? ', ' : '';
  237. $this->filter .= "Anno : " . $this->filterYear . " ";
  238. }
  239. $aRet = [];
  240. if (sizeof($this->filterStatus) > 0)
  241. {
  242. foreach($this->filterStatus as $s)
  243. {
  244. foreach($datas->get() as $aaa)
  245. {
  246. $state = \App\Models\Member::findOrFail($aaa->member_id)->isActive();
  247. if ($state["status"] == $s)
  248. $aRet[] = $aaa;
  249. }
  250. }
  251. }
  252. else
  253. $aRet = $datas->get();
  254. $this->records = $aRet;
  255. $this->emit('load-data-table');
  256. return view('livewire.course_member');
  257. }
  258. public function disableSearch()
  259. {
  260. $this->filterCourse = [];
  261. $this->filterLevel = [];
  262. $this->filterType = [];
  263. $this->filterDuration = [];
  264. $this->filterFrequency = [];
  265. $this->filterDays = [];
  266. $this->filterHours = [];
  267. $this->filterSubscription = "";
  268. $this->filterStatus = [];
  269. $this->chkCertificateNormal = 0;
  270. $this->chkCertificateAgonistico = 0;
  271. $this->chkCertificateScaduti = 0;
  272. $this->chkCertificateScadenza = 0;
  273. $this->chkCard = [];
  274. $this->filterYear = "";
  275. $this->fromYear = "";
  276. $this->toYear = "";
  277. $this->fromFromYear = "";
  278. $this->toToYear = "";
  279. }
  280. }