Presence.php 14 KB

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