Presence.php 20 KB

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