Presence.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class Presence extends Component
  5. {
  6. public $calendar;
  7. public $records;
  8. public $member_ids = [];
  9. public $court_id, $instructor_id, $motivation_id, $motivation_manual_id, $note, $manual;
  10. public $newMemberFirstName, $newMemberLastName, $newMemberEmail, $newMemberToComplete, $newMemberFiscalCode, $newMemberFiscalCodeExist, $newMemberMotivationId;
  11. public $userName, $userEmail;
  12. public $added = false;
  13. public $filter = '';
  14. public $courts = [];
  15. public $instructors = [];
  16. public $motivations = [];
  17. public $motivations_add = [];
  18. public $members = [];
  19. public $newMembers = [];
  20. public $ids = [];
  21. public function mount()
  22. {
  23. setlocale(LC_ALL, 'it_IT');
  24. $this->calendar = \App\Models\Calendar::findOrFail($_GET["calendarId"]);
  25. $this->court_id = $this->calendar->court_id;
  26. $this->instructor_id = $this->calendar->instructor_id;
  27. $this->motivation_manual_id = $this->calendar->motivation_manual_id;
  28. $this->manual = $this->calendar->manual;
  29. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])->orderBy('last_name')->orderBy('first_name')->get();
  30. $this->note = $this->calendar->note;
  31. $this->courts = \App\Models\Court::select('*')->where('enabled', true)->get();
  32. $this->instructors = \App\Models\User::select('*')->where('level', 2)->where('enabled', true)->get();
  33. $this->motivations = \App\Models\Motivation::select('*')->where('enabled', true)->where('type', 'del')->get();
  34. $this->motivations_add = \App\Models\Motivation::select('*')->where('enabled', true)->where('type', 'add')->get();
  35. }
  36. public function updatedNewMemberMotivationId() {
  37. $this->emit('reload');
  38. }
  39. public function render()
  40. {
  41. $this->records = [];
  42. setlocale(LC_ALL, 'it_IT');
  43. $presenceMembers = [];
  44. if (!$this->manual)
  45. {
  46. // Carco tutti gli iscritti a un corso padre in quel giorno con un range orario simile
  47. $days = ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'];
  48. $dow = date('w', strtotime($this->calendar->from));
  49. $d = $days[$dow];
  50. // Elenco corsi per tipologia in base al calendario
  51. $courses = \App\Models\Course::where('name', $this->calendar->name)->where('date_from', '<=', $this->calendar->from)->where('date_to', '>=', $this->calendar->to)->pluck('id')->toArray();
  52. // Elenco utenti iscritti al corso "padre"
  53. $members_courses = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")->whereIn('course_id', $courses)->pluck('member_id')->toArray();
  54. if ($this->filter != '')
  55. {
  56. $filter = $this->filter;
  57. $members = \App\Models\Member::whereIn('id', $members_courses)->where(function ($query) use ($filter) {
  58. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $filter . "%'")
  59. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $filter . "%'");
  60. })->orderBy('last_name')->orderBy('first_name')->get();
  61. }
  62. else
  63. $members = \App\Models\Member::whereIn('id', $members_courses)->orderBy('last_name')->orderBy('first_name')->get();
  64. // $presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->pluck('member_id')->toArray();
  65. // $my_presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->pluck('member_id')->toArray();
  66. foreach($members as $member)
  67. {
  68. $presenceMembers[] = $member->id;
  69. //$this->member_ids[] = $member->id;
  70. $this->records[] = $this->getMember($member);
  71. }
  72. }
  73. // Aggiungo i membri iscritti
  74. $members_presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->whereNotIn('member_id', $presenceMembers)->pluck('member_id')->toArray();
  75. if ($this->filter != '')
  76. {
  77. $filter = $this->filter;
  78. $members = \App\Models\Member::whereIn('id', $members_presences)->where(function ($query) use ($filter) {
  79. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $filter . "%'")
  80. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $filter . "%'");
  81. })->get();
  82. }
  83. else
  84. $members = \App\Models\Member::whereIn('id', $members_presences)->get();
  85. foreach($members as $member)
  86. {
  87. //$this->member_ids[] = $member->id;
  88. $this->records[] = $this->getMember($member);
  89. }
  90. foreach($this->newMembers as $m)
  91. {
  92. $member = \App\Models\Member::findOrFail($m);
  93. //$this->member_ids[] = $member->id;
  94. $this->records[] = $this->getMember($member);
  95. }
  96. /*$calendars = \App\Models\Calendar::get();
  97. foreach($calendars as $c)
  98. {
  99. $this->records[] = array('id' => $c->id, 'title' => $c->course->name, 'start' => $c->from, 'end' => $c->to);
  100. }*/
  101. return view('livewire.presence');
  102. }
  103. public function getDateX()
  104. {
  105. setlocale(LC_ALL, 'it_IT');
  106. $days = ['Domenica', 'Lunedì', 'Martedì', 'Mercoledì', 'Giovedì', 'Venerdì', 'Sabato'];
  107. $months = ['Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno', 'Luglio', 'Agosto', 'Settembre', 'Ottobre', 'Novembre', 'Dicembre'];
  108. return $days[date('w', strtotime($this->calendar->from))] . " " . date("d", strtotime($this->calendar->from)) . " " . $months[date("n", strtotime($this->calendar->from)) - 1];
  109. }
  110. public function getMember($member)
  111. {
  112. $latestCert = \App\Models\MemberCertificate::where('member_id', $member->id)
  113. ->orderBy('expire_date', 'desc')
  114. ->first();
  115. $certificate = '';
  116. $y = "|";
  117. if ($latestCert)
  118. {
  119. $latest_date = $latestCert->expire_date;
  120. $status = '';
  121. if ($latest_date < date("Y-m-d")) {
  122. $status = "0"; // Expired
  123. } else if ($latest_date <= date("Y-m-d", strtotime("+1 month"))) {
  124. $status = "1"; // Expiring soon
  125. } else {
  126. $status = "2"; // Valid
  127. }
  128. $y = $status . "|" . date("d/m/Y", strtotime($latest_date));
  129. }
  130. $presence = false;
  131. $my_presence = false;
  132. $status = 0;
  133. $has_presence = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('member_id', $member->id)->first();
  134. if ($has_presence)
  135. {
  136. $presence = true;
  137. $my_presence = $has_presence->user_id == \Auth::user()->id;
  138. $status = $has_presence->status;
  139. }
  140. return array('id' => $member->id, 'first_name' => $member->first_name, 'last_name' => $member->last_name, 'certificate' => $y, 'presence' => $presence, 'my_presence' => $my_presence, 'status' => $status);
  141. }
  142. public function save($ids)
  143. {
  144. $this->calendar->court_id = $this->court_id;
  145. $this->calendar->instructor_id = $this->instructor_id;
  146. $this->calendar->note = $this->note;
  147. if ($this->motivation_id != "" && $this->motivation_id != null)
  148. $this->calendar->motivation_id = $this->motivation_id;
  149. if ($this->motivation_manual_id != "" && $this->motivation_manual_id != null)
  150. $this->calendar->motivation_manual_id = $this->motivation_manual_id;
  151. $this->calendar->save();
  152. \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->where('status', '<>', 99)->delete();
  153. foreach($ids as $id)
  154. {
  155. $p = new \App\Models\Presence();
  156. $p->member_id = $id;
  157. $p->calendar_id = $this->calendar->id;
  158. $p->user_id = \Auth::user()->id;
  159. $p->status = 0;
  160. $p->save();
  161. }
  162. $this->emit('setSaving');
  163. return redirect()->to('/calendar');
  164. }
  165. public function cancel($ids, $motivation_id)
  166. {
  167. $presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->whereIn('member_id', $ids)->get();
  168. foreach($presences as $presence)
  169. {
  170. $presence->motivation_id = $motivation_id;
  171. $presence->status = 99;
  172. $presence->save();
  173. }
  174. return redirect()->to('/presences?calendarId=' . $this->calendar->id);
  175. }
  176. public function addMember($ids)
  177. {
  178. $this->added = true;
  179. //if (!in_array($id, $this->newMembers))
  180. // $this->newMembers[] = $id;
  181. $this->member_ids = $ids;
  182. $this->emit('reload');
  183. }
  184. public function cancelCalendar()
  185. {
  186. $this->calendar->motivation_id = $this->motivation_id;
  187. $this->calendar->status = 99;
  188. $this->calendar->save();
  189. return redirect()->to('/calendar');
  190. }
  191. public function createMember()
  192. {
  193. if (!$this->added)
  194. {
  195. $this->newMemberFiscalCodeExist = false;
  196. $this->validate([
  197. // 'newMemberFiscalCode'=>'required|max:16',
  198. 'newMemberFirstName'=>'required',
  199. 'newMemberLastName'=>'required',
  200. //'newMemberEmail'=>'required',
  201. ]);
  202. // Check fiscal code exist
  203. $exist = false;
  204. if ($this->newMemberFiscalCode != '')
  205. {
  206. $check = \App\Models\Member::where('fiscal_code', $this->newMemberFiscalCode)->get();
  207. $exist = $check->count() > 0;
  208. }
  209. if (!$exist)
  210. {
  211. $member = \App\Models\Member::create([
  212. 'first_name' => strtoupper($this->newMemberFirstName),
  213. 'last_name' => strtoupper($this->newMemberLastName),
  214. 'email' => strtoupper($this->newMemberEmail),
  215. 'to_complete' => $this->newMemberToComplete,
  216. 'fiscal_code' => $this->newMemberFiscalCode,
  217. 'status' => true
  218. ]);
  219. if (!in_array($member->id, $this->newMembers))
  220. $this->newMembers[] = $member->id;
  221. $this->emit('reload');
  222. $this->emit('saved');
  223. }
  224. else
  225. {
  226. $this->newMemberFiscalCodeExist = true;
  227. }
  228. }
  229. else
  230. {
  231. foreach($this->member_ids as $m)
  232. {
  233. if ($this->manual)
  234. {
  235. //\App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->where('status', '<>', 99)->delete();
  236. //foreach($ids as $id)
  237. //{
  238. $p = new \App\Models\Presence();
  239. $p->member_id = $m;
  240. $p->calendar_id = $this->calendar->id;
  241. $p->user_id = \Auth::user()->id;
  242. $p->status = 0;
  243. $p->save();
  244. //}
  245. }
  246. else
  247. {
  248. if (!in_array($m, $this->newMembers))
  249. $this->newMembers[] = $m;
  250. }
  251. }
  252. //$this->member_id = 0;
  253. $this->member_ids = [];
  254. $this->added = false;
  255. //$this->emit('reload');
  256. $this->emit('saved');
  257. }
  258. }
  259. public function createInstructor()
  260. {
  261. $user = \App\Models\User::create([
  262. 'name' => $this->userName,
  263. 'email' => $this->userEmail,
  264. 'password' => '',
  265. 'level' => 2,
  266. 'enabled' => true
  267. ]);
  268. $this->instructor_id = $user->id;
  269. $this->instructors = \App\Models\User::select('*')->where('level', 2)->where('enabled', true)->get();
  270. $this->emit('saved');
  271. }
  272. public function removeSingle($id)
  273. {
  274. \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('member_id', $id)->delete();
  275. $this->emit('reload');
  276. }
  277. }