Presence.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 $save_court_id, $save_instructor_id, $save_notes;
  11. public $newMemberFirstName, $newMemberLastName, $newMemberEmail, $newMemberToComplete, $newMemberFiscalCode, $newMemberFiscalCodeExist, $newMemberMotivationId;
  12. public $userName, $userEmail;
  13. public $added = false;
  14. public $filter = '';
  15. public $courts = [];
  16. public $instructors = [];
  17. public $motivations = [];
  18. public $motivations_add = [];
  19. public $members = [];
  20. public $newMembers = [];
  21. public $ids = [];
  22. public function mount()
  23. {
  24. setlocale(LC_ALL, 'it_IT');
  25. $this->calendar = \App\Models\Calendar::findOrFail($_GET["calendarId"]);
  26. $this->court_id = $this->calendar->court_id;
  27. $this->instructor_id = $this->calendar->instructor_id;
  28. $this->motivation_manual_id = $this->calendar->motivation_manual_id;
  29. $this->manual = $this->calendar->manual;
  30. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])->orderBy('last_name')->orderBy('first_name')->get();
  31. $this->note = $this->calendar->note;
  32. $this->courts = \App\Models\Court::select('*')->where('enabled', true)->get();
  33. $this->instructors = \App\Models\User::select('*')->where('level', 2)->where('enabled', true)->orderBy('name', 'asc')->get();
  34. $this->motivations = \App\Models\Motivation::select('*')->where('enabled', true)->where('type', 'del')->get();
  35. $this->motivations_add = \App\Models\Motivation::select('*')->where('enabled', true)->where('type', 'add')->get();
  36. $this->save_court_id = 0;
  37. $this->save_instructor_id = 0;
  38. $this->save_notes = '';
  39. }
  40. public function updatedNewMemberMotivationId()
  41. {
  42. $this->emit('reload');
  43. }
  44. public function render()
  45. {
  46. $this->records = [];
  47. setlocale(LC_ALL, 'it_IT');
  48. $presenceMembers = [];
  49. if (!$this->manual) {
  50. // Carco tutti gli iscritti a un corso padre in quel giorno con un range orario simile
  51. $days = ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'];
  52. $dow = date('w', strtotime($this->calendar->from));
  53. $d = $days[$dow];
  54. $h = date('H:i', strtotime($this->calendar->from));
  55. // Elenco corsi per tipologia in base al calendario
  56. $courses = \App\Models\Course::where('name', $this->calendar->name)->where('date_from', '<=', $this->calendar->from)->where('date_to', '>=', $this->calendar->to)->pluck('id')->toArray();
  57. $months = date("n", strtotime($this->calendar->from));
  58. // Elenco utenti iscritti al corso "padre"
  59. // $members_courses = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")
  60. // ->where('when', 'like', '%"from":"' . $h . '"%')
  61. // ->whereNot('months', 'like', '%"m":' . $months . ',"status":2%')
  62. // ->whereDate('date_from', '<=', $this->calendar->from)
  63. // ->whereDate('date_to', '>=', $this->calendar->from)
  64. // ->whereIn('course_id', $courses)
  65. // ->pluck('member_id')->toArray();
  66. // $members_courses = \App\Models\MemberCourse::whereRaw("JSON_CONTAINS(`when`, JSON_OBJECT('day', JSON_ARRAY(?), 'from', ?), '$')", [$d, $h])->where('months', 'like', '%"m":' . $months . ',%')->whereIn('course_id', $courses)->pluck('member_id')->toArray();
  67. $members_courses = \App\Models\MemberCourse::query()
  68. ->whereRaw("JSON_CONTAINS(`when`, JSON_OBJECT('day', JSON_ARRAY(?), 'from', ?), '$')", [$d, $h])
  69. ->whereRaw("JSON_CONTAINS(months, JSON_OBJECT('m', CAST(? AS UNSIGNED)), '$')", [$months])
  70. ->whereRaw("NOT JSON_CONTAINS(months, JSON_OBJECT('m', CAST(? AS UNSIGNED), 'status', 2), '$')", [$months])
  71. ->whereRaw("NOT JSON_CONTAINS(months, JSON_OBJECT('m', CAST(? AS UNSIGNED), 'status', '2'), '$')", [$months])
  72. ->whereIn('course_id', $courses)
  73. ->pluck('member_id')
  74. ->toArray();
  75. if ($this->filter != '') {
  76. $filter = $this->filter;
  77. $members = \App\Models\Member::whereIn('id', $members_courses)->where(function ($query) use ($filter) {
  78. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $filter . "%'")
  79. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $filter . "%'");
  80. })->orderBy('last_name')->orderBy('first_name')->get();
  81. } else
  82. $members = \App\Models\Member::whereIn('id', $members_courses)->orderBy('last_name')->orderBy('first_name')->get();
  83. // $presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->pluck('member_id')->toArray();
  84. // $my_presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->pluck('member_id')->toArray();
  85. foreach ($members as $member) {
  86. $presenceMembers[] = $member->id;
  87. //$this->member_ids[] = $member->id;
  88. $this->records[] = $this->getMember($member);
  89. }
  90. }
  91. // Aggiungo i membri iscritti
  92. $members_presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->whereNotIn('member_id', $presenceMembers)->pluck('member_id')->toArray();
  93. if ($this->filter != '') {
  94. $filter = $this->filter;
  95. $members = \App\Models\Member::whereIn('id', $members_presences)->where(function ($query) use ($filter) {
  96. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $filter . "%'")
  97. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $filter . "%'");
  98. })->get();
  99. } else
  100. $members = \App\Models\Member::whereIn('id', $members_presences)->get();
  101. foreach ($members as $member) {
  102. //$this->member_ids[] = $member->id;
  103. $this->records[] = $this->getMember($member);
  104. }
  105. foreach ($this->newMembers as $m) {
  106. $member = \App\Models\Member::findOrFail($m);
  107. //$this->member_ids[] = $member->id;
  108. $this->records[] = $this->getMember($member);
  109. }
  110. /*$calendars = \App\Models\Calendar::get();
  111. foreach($calendars as $c)
  112. {
  113. $this->records[] = array('id' => $c->id, 'title' => $c->course->name, 'start' => $c->from, 'end' => $c->to);
  114. }*/
  115. return view('livewire.presence');
  116. }
  117. public function getDateX()
  118. {
  119. setlocale(LC_ALL, 'it_IT');
  120. $days = ['Domenica', 'Lunedì', 'Martedì', 'Mercoledì', 'Giovedì', 'Venerdì', 'Sabato'];
  121. $months = ['Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno', 'Luglio', 'Agosto', 'Settembre', 'Ottobre', 'Novembre', 'Dicembre'];
  122. return $days[date('w', strtotime($this->calendar->from))] . " " . date("d", strtotime($this->calendar->from)) . " " . $months[date("n", strtotime($this->calendar->from)) - 1];
  123. }
  124. public function getMember($member)
  125. {
  126. $latestCert = \App\Models\MemberCertificate::where('member_id', $member->id)
  127. ->orderBy('expire_date', 'desc')
  128. ->first();
  129. $certificate = '';
  130. $y = "|";
  131. if ($latestCert) {
  132. $latest_date = $latestCert->expire_date;
  133. $status = '';
  134. if ($latest_date < date("Y-m-d")) {
  135. $status = "0"; // Expired
  136. } else if ($latest_date <= date("Y-m-d", strtotime("+1 month"))) {
  137. $status = "1"; // Expiring soon
  138. } else {
  139. $status = "2"; // Valid
  140. }
  141. $y = $status . "|" . date("d/m/Y", strtotime($latest_date));
  142. }
  143. $presence = false;
  144. $my_presence = false;
  145. $motivation = '';
  146. $status = 0;
  147. $court = '';
  148. $instructor = '';
  149. $additional_instructor = '';
  150. $notes = '';
  151. $has_presence = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('member_id', $member->id)->first();
  152. if ($has_presence) {
  153. $presence = true;
  154. $my_presence = $has_presence->user_id == \Auth::user()->id;
  155. if ($has_presence->motivation_id > 0) {
  156. $motivation = \App\Models\Motivation::findOrFail($has_presence->motivation_id)->name;
  157. }
  158. $status = $has_presence->status;
  159. $instructor = \App\Models\User::findOrFail($has_presence->user_id)->name;
  160. if ($has_presence->court_id > 0) {
  161. $court = \App\Models\Court::findOrFail($has_presence->court_id)->name;
  162. }
  163. if ($has_presence->instructor_id > 0 && $has_presence->instructor_id !== $has_presence->user_id) {
  164. $additional_instructor = \App\Models\User::findOrFail($has_presence->instructor_id)->name;
  165. }
  166. if (!is_null($has_presence->notes)) {
  167. $notes = $has_presence->notes;
  168. }
  169. }
  170. if (in_array($member->id, $this->newMembers)) {
  171. $presence = true;
  172. $my_presence = true;
  173. }
  174. return array(
  175. 'id' => $member->id,
  176. 'first_name' => $member->first_name,
  177. 'last_name' => $member->last_name,
  178. 'certificate' => $y,
  179. 'presence' => $presence,
  180. 'my_presence' => $my_presence,
  181. 'status' => $status,
  182. 'motivation' => $motivation,
  183. 'court' => $court,
  184. 'instructor' => $instructor,
  185. 'additional_instructor' => $additional_instructor,
  186. 'notes' => $notes,
  187. );
  188. }
  189. public function save($ids)
  190. {
  191. $this->saveAndStay($ids);
  192. $last_date = explode(" ", $this->calendar->from)[0];
  193. return redirect()->to("/calendar?last_date={$last_date}");
  194. }
  195. public function saveAndStay($ids)
  196. {
  197. $this->calendar->court_id = $this->court_id;
  198. $this->calendar->instructor_id = $this->instructor_id;
  199. $this->calendar->note = $this->note;
  200. if ($this->motivation_id != "" && $this->motivation_id != null)
  201. $this->calendar->motivation_id = $this->motivation_id;
  202. if ($this->motivation_manual_id != "" && $this->motivation_manual_id != null)
  203. $this->calendar->motivation_manual_id = $this->motivation_manual_id;
  204. $this->calendar->save();
  205. // $x = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->where('status', '<>', 99)->first();
  206. // $mid = null;
  207. // if ($x) {
  208. // $mid = $x->motivation_id;
  209. // $x->delete();
  210. // }
  211. // Mappa degli ultimi motivation_id per ogni member_id dell'utente e calendario correnti
  212. $userId = \Auth::user()->id;
  213. $calendarId = $this->calendar->id;
  214. $lastEditData = \App\Models\Presence::query()
  215. ->select('member_id', 'motivation_id')
  216. ->where('calendar_id', $calendarId)
  217. ->where('user_id', $userId)
  218. ->where('status', '<>', 99)
  219. ->whereIn('id', function ($q) use ($calendarId, $userId) {
  220. $q->selectRaw('MAX(id)')
  221. ->from('presences')
  222. ->where('calendar_id', $calendarId)
  223. ->where('user_id', $userId)
  224. ->where('status', '<>', 99)
  225. ->groupBy('member_id');
  226. })
  227. ->get()
  228. ->keyBy('member_id') // -> [member_id => (obj con motivation_id)]
  229. ->map(fn($row) => $row->motivation_id)
  230. ->toArray();
  231. // Elimino tutti i dati correnti che devono essere sostituiti
  232. \App\Models\Presence::query()
  233. ->where('calendar_id', $calendarId)
  234. ->where('user_id', $userId)
  235. ->where('status', '<>', 99)
  236. ->delete();
  237. // Ricreo le presenze per ogni membro contenuto in $ids
  238. foreach ($ids as $id) {
  239. $p = new \App\Models\Presence();
  240. $p->member_id = $id;
  241. $p->calendar_id = $calendarId;
  242. // Se per quel membro esisteva un motivation_id, lo riuso, altrimenti lo lascio null
  243. $p->motivation_id = $lastEditData[$id] ?? null;
  244. $p->user_id = $userId;
  245. $p->status = 0;
  246. // Salvo eventuale court_id (se presente e maggiore di 0)
  247. if ($this->save_court_id > 0) {
  248. $p->court_id = $this->save_court_id;
  249. }
  250. // Salvo eventuale instructor_id (se presente e maggiore di 0)
  251. if ($this->save_instructor_id > 0) {
  252. $p->instructor_id = $this->save_instructor_id;
  253. }
  254. // Salvo eventuali note (se non vuote)
  255. if ($this->save_notes != '') {
  256. $p->notes = $this->save_notes;
  257. }
  258. $p->save();
  259. }
  260. $this->emit('setSaving');
  261. }
  262. public function cancel($ids, $motivation_id)
  263. {
  264. $presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->whereIn('member_id', $ids)->get();
  265. foreach ($presences as $presence) {
  266. $presence->motivation_id = $motivation_id;
  267. $presence->status = 99;
  268. $presence->save();
  269. }
  270. return redirect()->to('/presences?calendarId=' . $this->calendar->id);
  271. }
  272. public function addMember($ids)
  273. {
  274. $this->added = true;
  275. //if (!in_array($id, $this->newMembers))
  276. // $this->newMembers[] = $id;
  277. $this->member_ids = $ids;
  278. $this->emit('reload');
  279. }
  280. public function cancelCalendar()
  281. {
  282. $this->calendar->motivation_id = $this->motivation_id;
  283. $this->calendar->status = 99;
  284. $this->calendar->save();
  285. return redirect()->to('/calendar');
  286. }
  287. public function createMember()
  288. {
  289. if (!$this->added) {
  290. $this->newMemberFiscalCodeExist = false;
  291. $this->validate([
  292. "newMemberMotivationId" => 'required',
  293. ]);
  294. /*$this->validate([
  295. // 'newMemberFiscalCode'=>'required|max:16',
  296. 'newMemberFirstName'=>'required',
  297. 'newMemberLastName'=>'required',
  298. //'newMemberEmail'=>'required',
  299. ]);*/
  300. // Check fiscal code exist
  301. $exist = false;
  302. if ($this->newMemberFiscalCode != '') {
  303. $check = \App\Models\Member::where('fiscal_code', $this->newMemberFiscalCode)->get();
  304. $exist = $check->count() > 0;
  305. }
  306. if (!$exist) {
  307. $member = \App\Models\Member::create([
  308. 'first_name' => strtoupper($this->newMemberFirstName),
  309. 'last_name' => strtoupper($this->newMemberLastName),
  310. 'email' => strtoupper($this->newMemberEmail),
  311. 'to_complete' => true,
  312. 'fiscal_code' => $this->newMemberFiscalCode,
  313. 'status' => true
  314. ]);
  315. //if (!in_array($member->id, $this->newMembers))
  316. // $this->newMembers[] = $member->id;
  317. $p = new \App\Models\Presence();
  318. $p->member_id = $member->id;
  319. $p->calendar_id = $this->calendar->id;
  320. $p->motivation_id = $this->newMemberMotivationId;
  321. $p->user_id = \Auth::user()->id;
  322. $p->status = 0;
  323. $p->court_id = null;
  324. $p->instructor_id = null;
  325. $p->notes = null;
  326. $p->save();
  327. $this->emit('reload');
  328. $this->emit('saved');
  329. /*$this->newMemberFirstName = '';
  330. $this->newMemberLastName = '';
  331. $this->newMemberEmail = '';
  332. $this->newMemberFiscalCode = '';*/
  333. } else {
  334. $this->newMemberFiscalCodeExist = true;
  335. }
  336. } else {
  337. if ($this->member_ids != null) {
  338. $this->validate([
  339. "newMemberMotivationId" => 'required',
  340. ]);
  341. foreach ($this->member_ids as $m) {
  342. //if ($this->manual)
  343. //{
  344. //\App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->where('status', '<>', 99)->delete();
  345. //foreach($ids as $id)
  346. //{
  347. $p = new \App\Models\Presence();
  348. $p->member_id = $m;
  349. $p->calendar_id = $this->calendar->id;
  350. $p->motivation_id = $this->newMemberMotivationId;
  351. $p->user_id = \Auth::user()->id;
  352. $p->status = 0;
  353. $p->court_id = null;
  354. $p->instructor_id = null;
  355. $p->notes = null;
  356. $p->save();
  357. //}
  358. /*}
  359. else
  360. {
  361. if (!in_array($m, $this->newMembers))
  362. $this->newMembers[] = $m;
  363. }*/
  364. }
  365. }
  366. //$this->member_id = 0;
  367. $this->member_ids = [];
  368. $this->added = false;
  369. $this->emit('reload');
  370. $this->emit('saved');
  371. }
  372. }
  373. public function createInstructor()
  374. {
  375. $user = \App\Models\User::create([
  376. 'name' => $this->userName,
  377. 'email' => $this->userEmail,
  378. 'password' => '',
  379. 'level' => 2,
  380. 'enabled' => true
  381. ]);
  382. $this->instructor_id = $user->id;
  383. $this->instructors = \App\Models\User::select('*')->where('level', 2)->where('enabled', true)->orderBy('name', 'asc')->get();
  384. $this->emit('saved');
  385. }
  386. public function removeSingle($id)
  387. {
  388. \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('member_id', $id)->delete();
  389. $this->emit('reload');
  390. }
  391. public function revert($id)
  392. {
  393. $p = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('member_id', $id)->first();
  394. $p->motivation_id = null;
  395. $p->status = 0;
  396. $p->save();
  397. $this->emit('reload');
  398. }
  399. }