CourseMember.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. if (sizeof($this->filterCourse) > 0)
  58. {
  59. $course_ids = [];
  60. foreach($this->filterCourse as $c)
  61. {
  62. $all = \App\Models\Course::where('name', 'like', '%' . $c . "%")->get();
  63. foreach($all as $a)
  64. {
  65. $course_ids[] = $a->id;
  66. }
  67. }
  68. $datas = $datas->whereIn('course_id', $course_ids);
  69. }
  70. if (sizeof($this->filterLevel) > 0)
  71. {
  72. $course_ids = \App\Models\Course::whereIn('course_level_id', $this->filterLevel)->pluck('id');
  73. $datas = $datas->whereIn('course_id', $course_ids);
  74. }
  75. if (sizeof($this->filterFrequency) > 0)
  76. {
  77. $course_ids = \App\Models\Course::whereIn('course_frequency_id', $this->filterFrequency)->pluck('id');
  78. $datas = $datas->whereIn('course_id', $course_ids);
  79. }
  80. if (sizeof($this->filterType) > 0)
  81. {
  82. $course_ids = \App\Models\Course::whereIn('course_type_id', $this->filterType)->pluck('id');
  83. $datas = $datas->whereIn('course_id', $course_ids);
  84. }
  85. if (sizeof($this->filterDuration) > 0)
  86. {
  87. $course_ids = \App\Models\Course::whereIn('course_duration_id', $this->filterDuration)->pluck('id');
  88. $datas = $datas->whereIn('course_id', $course_ids);
  89. }
  90. if (sizeof($this->filterDays) > 0)
  91. {
  92. $m_ids = [];
  93. foreach($this->filterDays as $c)
  94. {
  95. $all = \App\Models\MemberCourse::where('when', 'like', "%" . $c . "%")->get();
  96. foreach($all as $a)
  97. {
  98. $m_ids[] = $a->member_id;
  99. }
  100. }
  101. $datas = $datas->whereIn('member_id', $m_ids);
  102. }
  103. if (sizeof($this->filterHours) > 0)
  104. {
  105. $m_ids = [];
  106. foreach($this->filterHours as $c)
  107. {
  108. $all = \App\Models\MemberCourse::where('when', 'like', '%"from":"' . $c . "%")->get();
  109. foreach($all as $a)
  110. {
  111. $m_ids[] = $a->member_id;
  112. }
  113. }
  114. $datas = $datas->whereIn('member_id', $m_ids);
  115. }
  116. /*if ($this->filterDays != "")
  117. {
  118. $members_ids = \App\Models\MemberCourse::where('when', 'like', "%" . $this->filterDays . "%")->pluck('member_id');
  119. $datas = $datas->whereIn('member_id', $members_ids);
  120. $this->filter .= $this->filter != '' ? ', ' : '';
  121. $this->filter .= "Giorni : " . $this->filterDays . " ";
  122. }
  123. if ($this->filterHours != "")
  124. {
  125. $members_ids = \App\Models\MemberCourse::where('when', 'like', '%"from":"' . $this->filterHours . "%")->pluck('member_id');
  126. $datas = $datas->whereIn('member_id', $members_ids);
  127. $this->filter .= $this->filter != '' ? ', ' : '';
  128. $this->filter .= "Ore : " . $this->filterHours . " ";
  129. }*/
  130. if ($this->filterSubscription != "")
  131. {
  132. $ids = \App\Models\MemberCourse::where('subscribed', $this->filterSubscription == 1 ? true : false)->pluck('id');
  133. $datas = $datas->whereIn('id', $ids);
  134. $this->filter .= $this->filter != '' ? ', ' : '';
  135. $this->filter .= "Pagata sottoscrizione : " . ($this->filterSubscription == 1 ? "SI" : "NO") . " ";
  136. }
  137. if ($this->chkCertificateType != "")
  138. {
  139. $types = \App\Models\MemberCertificate::where('type', $this->chkCertificateType)->pluck('member_id');
  140. $datas = $datas->whereIn('member_id', $types);
  141. }
  142. if ($this->chkCertificateScadenza > 0)
  143. {
  144. if ($this->chkCertificateScadenza == "1")
  145. $scad = \App\Models\MemberCertificate::where('expire_date', '<', date("Y-m-d"))->pluck('member_id');
  146. if ($this->chkCertificateScadenza == "2")
  147. $scad = \App\Models\MemberCertificate::whereBetween('expire_date', [date("Y-m-d"), date("Y-m-d", strtotime("+1 month"))])->pluck('member_id');
  148. $datas = $datas->whereIn('member_id', $scad);
  149. }
  150. if ($this->fromYear != "")
  151. {
  152. $m_ids = \App\Models\Member::where('birth_date', '<', date("Y-m-d", strtotime("-" . $this->fromYear . " year", time())))
  153. ->where(function($query) {
  154. $query->where('is_archived', false)
  155. ->orWhereNull('is_archived');
  156. })
  157. ->where(function($query) {
  158. $query->where('is_deleted', false)
  159. ->orWhereNull('is_deleted');
  160. })->pluck('id');
  161. $datas = $datas->whereIn('member_id', $m_ids);
  162. }
  163. if ($this->toYear != "")
  164. {
  165. $m_ids = \App\Models\Member::where('birth_date', '>', date("Y-m-d", strtotime("-" . $this->toYear . " year", time())))
  166. ->where(function($query) {
  167. $query->where('is_archived', false)
  168. ->orWhereNull('is_archived');
  169. })
  170. ->where(function($query) {
  171. $query->where('is_deleted', false)
  172. ->orWhereNull('is_deleted');
  173. })->pluck('id');
  174. $datas = $datas->whereIn('member_id', $m_ids);
  175. }
  176. if ($this->fromFromYear != "")
  177. {
  178. $m_ids = \App\Models\Member::whereYear('birth_date', '>=', $this->fromFromYear)
  179. ->where(function($query) {
  180. $query->where('is_archived', false)
  181. ->orWhereNull('is_archived');
  182. })
  183. ->where(function($query) {
  184. $query->where('is_deleted', false)
  185. ->orWhereNull('is_deleted');
  186. })->pluck('id');
  187. $datas = $datas->whereIn('member_id', $m_ids);
  188. }
  189. if ($this->toToYear != "")
  190. {
  191. $m_ids = \App\Models\Member::whereYear('birth_date', '<=', $this->toToYear)
  192. ->where(function($query) {
  193. $query->where('is_archived', false)
  194. ->orWhereNull('is_archived');
  195. })
  196. ->where(function($query) {
  197. $query->where('is_deleted', false)
  198. ->orWhereNull('is_deleted');
  199. })->pluck('id');
  200. $datas = $datas->whereIn('member_id', $m_ids);
  201. }
  202. /*if ($_GET["toYear"] != "")
  203. {
  204. $x = $x->where('birth_date', '>', date("Y-m-d", strtotime("-" . $_GET["toYear"] . " year", time())));
  205. }
  206. if ($_GET["fromYearYear"] != "")
  207. {
  208. $x = $x->whereYear('birth_date', '>=', $_GET["fromYearYear"]);
  209. }
  210. if ($_GET["toYearYear"] != "")
  211. {
  212. $x = $x->whereYear('birth_date', '<=', $_GET["toYearYear"]);
  213. }*/
  214. /*
  215. if ($this->chkCertificateNormal > 0)
  216. {
  217. $normal = \App\Models\MemberCertificate::where('type', 'N')->pluck('member_id');
  218. $datas = $datas->whereIn('member_id', $normal);
  219. $this->filter .= $this->filter != '' ? ', ' : '';
  220. $this->filter .= "Certificato normale : SI ";
  221. }
  222. if ($this->chkCertificateAgonistico > 0)
  223. {
  224. $agonistic = \App\Models\MemberCertificate::where('type', 'A')->pluck('member_id');
  225. $datas = $datas->whereIn('member_id', $agonistic);
  226. $this->filter .= $this->filter != '' ? ', ' : '';
  227. $this->filter .= "Certificato agonistico : SI ";
  228. }
  229. if ($this->chkCertificateScaduti > 0)
  230. {
  231. $scaduto = \App\Models\MemberCertificate::where('expire_date', '<', date("Y-m-d"))->pluck('member_id');
  232. $datas = $datas->whereIn('member_id', $scaduto);
  233. $this->filter .= $this->filter != '' ? ', ' : '';
  234. $this->filter .= "Certificato scaduto : SI ";
  235. }
  236. if ($this->chkCertificateScadenza > 0)
  237. {
  238. $scadenza = \App\Models\MemberCertificate::whereBetween('expire_date', [date("Y-m-d"), date("Y-m-d", strtotime("+1 month"))])->pluck('member_id');
  239. $datas = $datas->whereIn('member_id', $scadenza);
  240. $this->filter .= $this->filter != '' ? ', ' : '';
  241. $this->filter .= "Certificato in scadenza : SI ";
  242. }*/
  243. if (sizeof($this->chkCard) > 0)
  244. {
  245. $card_ids = \App\Models\MemberCard::whereIn('card_id', $this->chkCard)->pluck('member_id');
  246. $datas = $datas->whereIn('member_id', $card_ids);
  247. $this->filter .= $this->filter != '' ? ', ' : '';
  248. $this->filter .= "Tessera : ";
  249. foreach($this->chkCard as $card)
  250. {
  251. $this->filter .= \App\Models\Card::findOrFail($card)->name . " ";
  252. }
  253. }
  254. if ($this->filterYear != "")
  255. {
  256. $course_ids = \App\Models\Course::where('year', $this->filterYear)->pluck('id');
  257. $datas = $datas->whereIn('course_id', $course_ids);
  258. $this->filter .= $this->filter != '' ? ', ' : '';
  259. $this->filter .= "Anno : " . $this->filterYear . " ";
  260. }
  261. $aRet = [];
  262. if (sizeof($this->filterStatus) > 0)
  263. {
  264. foreach($this->filterStatus as $s)
  265. {
  266. foreach($datas->get() as $aaa)
  267. {
  268. $state = \App\Models\Member::findOrFail($aaa->member_id)->isActive();
  269. if ($state["status"] == $s)
  270. $aRet[] = $aaa;
  271. }
  272. }
  273. }
  274. else
  275. $aRet = $datas->get();
  276. $this->records = $aRet;
  277. $this->emit('load-data-table');
  278. return view('livewire.course_member');
  279. }
  280. public function disableSearch()
  281. {
  282. $this->filterCourse = [];
  283. $this->filterLevel = [];
  284. $this->filterType = [];
  285. $this->filterDuration = [];
  286. $this->filterFrequency = [];
  287. $this->filterDays = [];
  288. $this->filterHours = [];
  289. $this->filterSubscription = "";
  290. $this->filterStatus = [];
  291. $this->chkCertificateNormal = 0;
  292. $this->chkCertificateAgonistico = 0;
  293. $this->chkCertificateScaduti = 0;
  294. $this->chkCertificateScadenza = 0;
  295. $this->chkCard = [];
  296. $this->filterYear = "";
  297. $this->fromYear = "";
  298. $this->toYear = "";
  299. $this->fromFromYear = "";
  300. $this->toToYear = "";
  301. }
  302. }