Presence.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. $h = date('H:i', strtotime($this->calendar->from));
  51. // Elenco corsi per tipologia in base al calendario
  52. $courses = \App\Models\Course::where('name', $this->calendar->name)->where('date_from', '<=', $this->calendar->from)->where('date_to', '>=', $this->calendar->to)->pluck('id')->toArray();
  53. // Elenco utenti iscritti al corso "padre"
  54. $members_courses = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")->where('when', 'like', '%"from":"' . $h . '"%')->whereIn('course_id', $courses)->pluck('member_id')->toArray();
  55. if ($this->filter != '')
  56. {
  57. $filter = $this->filter;
  58. $members = \App\Models\Member::whereIn('id', $members_courses)->where(function ($query) use ($filter) {
  59. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $filter . "%'")
  60. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $filter . "%'");
  61. })->orderBy('last_name')->orderBy('first_name')->get();
  62. }
  63. else
  64. $members = \App\Models\Member::whereIn('id', $members_courses)->orderBy('last_name')->orderBy('first_name')->get();
  65. // $presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->pluck('member_id')->toArray();
  66. // $my_presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->pluck('member_id')->toArray();
  67. foreach($members as $member)
  68. {
  69. $presenceMembers[] = $member->id;
  70. //$this->member_ids[] = $member->id;
  71. $this->records[] = $this->getMember($member);
  72. }
  73. }
  74. // Aggiungo i membri iscritti
  75. $members_presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->whereNotIn('member_id', $presenceMembers)->pluck('member_id')->toArray();
  76. if ($this->filter != '')
  77. {
  78. $filter = $this->filter;
  79. $members = \App\Models\Member::whereIn('id', $members_presences)->where(function ($query) use ($filter) {
  80. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $filter . "%'")
  81. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $filter . "%'");
  82. })->get();
  83. }
  84. else
  85. $members = \App\Models\Member::whereIn('id', $members_presences)->get();
  86. foreach($members as $member)
  87. {
  88. //$this->member_ids[] = $member->id;
  89. $this->records[] = $this->getMember($member);
  90. }
  91. foreach($this->newMembers as $m)
  92. {
  93. $member = \App\Models\Member::findOrFail($m);
  94. //$this->member_ids[] = $member->id;
  95. $this->records[] = $this->getMember($member);
  96. }
  97. /*$calendars = \App\Models\Calendar::get();
  98. foreach($calendars as $c)
  99. {
  100. $this->records[] = array('id' => $c->id, 'title' => $c->course->name, 'start' => $c->from, 'end' => $c->to);
  101. }*/
  102. return view('livewire.presence');
  103. }
  104. public function getDateX()
  105. {
  106. setlocale(LC_ALL, 'it_IT');
  107. $days = ['Domenica', 'Lunedì', 'Martedì', 'Mercoledì', 'Giovedì', 'Venerdì', 'Sabato'];
  108. $months = ['Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno', 'Luglio', 'Agosto', 'Settembre', 'Ottobre', 'Novembre', 'Dicembre'];
  109. return $days[date('w', strtotime($this->calendar->from))] . " " . date("d", strtotime($this->calendar->from)) . " " . $months[date("n", strtotime($this->calendar->from)) - 1];
  110. }
  111. public function getMember($member)
  112. {
  113. $latestCert = \App\Models\MemberCertificate::where('member_id', $member->id)
  114. ->orderBy('expire_date', 'desc')
  115. ->first();
  116. $certificate = '';
  117. $y = "|";
  118. if ($latestCert)
  119. {
  120. $latest_date = $latestCert->expire_date;
  121. $status = '';
  122. if ($latest_date < date("Y-m-d")) {
  123. $status = "0"; // Expired
  124. } else if ($latest_date <= date("Y-m-d", strtotime("+1 month"))) {
  125. $status = "1"; // Expiring soon
  126. } else {
  127. $status = "2"; // Valid
  128. }
  129. $y = $status . "|" . date("d/m/Y", strtotime($latest_date));
  130. }
  131. $presence = false;
  132. $my_presence = false;
  133. $status = 0;
  134. $has_presence = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('member_id', $member->id)->first();
  135. if ($has_presence)
  136. {
  137. $presence = true;
  138. $my_presence = $has_presence->user_id == \Auth::user()->id;
  139. $status = $has_presence->status;
  140. }
  141. if (in_array($member->id, $this->newMembers))
  142. {
  143. $presence = true;
  144. $my_presence = true;
  145. }
  146. 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);
  147. }
  148. public function save($ids)
  149. {
  150. $this->calendar->court_id = $this->court_id;
  151. $this->calendar->instructor_id = $this->instructor_id;
  152. $this->calendar->note = $this->note;
  153. if ($this->motivation_id != "" && $this->motivation_id != null)
  154. $this->calendar->motivation_id = $this->motivation_id;
  155. if ($this->motivation_manual_id != "" && $this->motivation_manual_id != null)
  156. $this->calendar->motivation_manual_id = $this->motivation_manual_id;
  157. $this->calendar->save();
  158. $x = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->where('status', '<>', 99)->first();
  159. $mid = null;
  160. if ($x)
  161. {
  162. $mid = $x->motivation_id;
  163. $x->delete();
  164. }
  165. foreach($ids as $id)
  166. {
  167. $p = new \App\Models\Presence();
  168. $p->member_id = $id;
  169. $p->calendar_id = $this->calendar->id;
  170. $p->motivation_id = $mid;
  171. $p->user_id = \Auth::user()->id;
  172. $p->status = 0;
  173. $p->save();
  174. }
  175. $this->emit('setSaving');
  176. return redirect()->to('/calendar');
  177. }
  178. public function cancel($ids, $motivation_id)
  179. {
  180. $presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->whereIn('member_id', $ids)->get();
  181. foreach($presences as $presence)
  182. {
  183. $presence->motivation_id = $motivation_id;
  184. $presence->status = 99;
  185. $presence->save();
  186. }
  187. return redirect()->to('/presences?calendarId=' . $this->calendar->id);
  188. }
  189. public function addMember($ids)
  190. {
  191. $this->added = true;
  192. //if (!in_array($id, $this->newMembers))
  193. // $this->newMembers[] = $id;
  194. $this->member_ids = $ids;
  195. $this->emit('reload');
  196. }
  197. public function cancelCalendar()
  198. {
  199. $this->calendar->motivation_id = $this->motivation_id;
  200. $this->calendar->status = 99;
  201. $this->calendar->save();
  202. return redirect()->to('/calendar');
  203. }
  204. public function createMember()
  205. {
  206. if (!$this->added)
  207. {
  208. $this->newMemberFiscalCodeExist = false;
  209. $this->validate([
  210. "newMemberMotivationId" => 'required',
  211. ]);
  212. /*$this->validate([
  213. // 'newMemberFiscalCode'=>'required|max:16',
  214. 'newMemberFirstName'=>'required',
  215. 'newMemberLastName'=>'required',
  216. //'newMemberEmail'=>'required',
  217. ]);*/
  218. // Check fiscal code exist
  219. $exist = false;
  220. if ($this->newMemberFiscalCode != '')
  221. {
  222. $check = \App\Models\Member::where('fiscal_code', $this->newMemberFiscalCode)->get();
  223. $exist = $check->count() > 0;
  224. }
  225. if (!$exist)
  226. {
  227. $member = \App\Models\Member::create([
  228. 'first_name' => strtoupper($this->newMemberFirstName),
  229. 'last_name' => strtoupper($this->newMemberLastName),
  230. 'email' => strtoupper($this->newMemberEmail),
  231. 'to_complete' => true,
  232. 'fiscal_code' => $this->newMemberFiscalCode,
  233. 'status' => true
  234. ]);
  235. //if (!in_array($member->id, $this->newMembers))
  236. // $this->newMembers[] = $member->id;
  237. $p = new \App\Models\Presence();
  238. $p->member_id = $member->id;
  239. $p->calendar_id = $this->calendar->id;
  240. $p->motivation_id = $this->newMemberMotivationId;
  241. $p->user_id = \Auth::user()->id;
  242. $p->status = 0;
  243. $p->save();
  244. $this->emit('reload');
  245. $this->emit('saved');
  246. /*$this->newMemberFirstName = '';
  247. $this->newMemberLastName = '';
  248. $this->newMemberEmail = '';
  249. $this->newMemberFiscalCode = '';*/
  250. }
  251. else
  252. {
  253. $this->newMemberFiscalCodeExist = true;
  254. }
  255. }
  256. else
  257. {
  258. if ($this->member_ids != null)
  259. {
  260. $this->validate([
  261. "newMemberMotivationId" => 'required',
  262. ]);
  263. foreach($this->member_ids as $m)
  264. {
  265. if ($this->manual)
  266. {
  267. //\App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->where('status', '<>', 99)->delete();
  268. //foreach($ids as $id)
  269. //{
  270. $p = new \App\Models\Presence();
  271. $p->member_id = $m;
  272. $p->calendar_id = $this->calendar->id;
  273. $p->motivation_id = $this->newMemberMotivationId;
  274. $p->user_id = \Auth::user()->id;
  275. $p->status = 0;
  276. $p->save();
  277. //}
  278. }
  279. else
  280. {
  281. if (!in_array($m, $this->newMembers))
  282. $this->newMembers[] = $m;
  283. }
  284. }
  285. }
  286. //$this->member_id = 0;
  287. $this->member_ids = [];
  288. $this->added = false;
  289. $this->emit('reload');
  290. $this->emit('saved');
  291. }
  292. }
  293. public function createInstructor()
  294. {
  295. $user = \App\Models\User::create([
  296. 'name' => $this->userName,
  297. 'email' => $this->userEmail,
  298. 'password' => '',
  299. 'level' => 2,
  300. 'enabled' => true
  301. ]);
  302. $this->instructor_id = $user->id;
  303. $this->instructors = \App\Models\User::select('*')->where('level', 2)->where('enabled', true)->get();
  304. $this->emit('saved');
  305. }
  306. public function removeSingle($id)
  307. {
  308. \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('member_id', $id)->delete();
  309. $this->emit('reload');
  310. }
  311. public function revert($id)
  312. {
  313. $p = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('member_id', $id)->first();
  314. $p->status = 0;
  315. $p->save();
  316. $this->emit('reload');
  317. }
  318. }