Presence.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 . "%")->where('when', 'like', '%"from":"' . $h . '"%')->where('months', 'like', '%"m":' . $months . ',%')->whereIn('course_id', $courses)->pluck('member_id')->toArray();
  60. $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();
  61. if ($this->filter != '') {
  62. $filter = $this->filter;
  63. $members = \App\Models\Member::whereIn('id', $members_courses)->where(function ($query) use ($filter) {
  64. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $filter . "%'")
  65. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $filter . "%'");
  66. })->orderBy('last_name')->orderBy('first_name')->get();
  67. } else
  68. $members = \App\Models\Member::whereIn('id', $members_courses)->orderBy('last_name')->orderBy('first_name')->get();
  69. // $presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->pluck('member_id')->toArray();
  70. // $my_presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->pluck('member_id')->toArray();
  71. foreach ($members as $member) {
  72. $presenceMembers[] = $member->id;
  73. //$this->member_ids[] = $member->id;
  74. $this->records[] = $this->getMember($member);
  75. }
  76. }
  77. // Aggiungo i membri iscritti
  78. $members_presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->whereNotIn('member_id', $presenceMembers)->pluck('member_id')->toArray();
  79. if ($this->filter != '') {
  80. $filter = $this->filter;
  81. $members = \App\Models\Member::whereIn('id', $members_presences)->where(function ($query) use ($filter) {
  82. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $filter . "%'")
  83. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $filter . "%'");
  84. })->get();
  85. } else
  86. $members = \App\Models\Member::whereIn('id', $members_presences)->get();
  87. foreach ($members as $member) {
  88. //$this->member_ids[] = $member->id;
  89. $this->records[] = $this->getMember($member);
  90. }
  91. foreach ($this->newMembers as $m) {
  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. $latest_date = $latestCert->expire_date;
  119. $status = '';
  120. if ($latest_date < date("Y-m-d")) {
  121. $status = "0"; // Expired
  122. } else if ($latest_date <= date("Y-m-d", strtotime("+1 month"))) {
  123. $status = "1"; // Expiring soon
  124. } else {
  125. $status = "2"; // Valid
  126. }
  127. $y = $status . "|" . date("d/m/Y", strtotime($latest_date));
  128. }
  129. $presence = false;
  130. $my_presence = false;
  131. $motivation = '';
  132. $status = 0;
  133. $court = '';
  134. $instructor = '';
  135. $additional_instructor = '';
  136. $notes = '';
  137. $has_presence = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('member_id', $member->id)->first();
  138. if ($has_presence) {
  139. $presence = true;
  140. $my_presence = $has_presence->user_id == \Auth::user()->id;
  141. if ($has_presence->motivation_id > 0) {
  142. $motivation = \App\Models\Motivation::findOrFail($has_presence->motivation_id)->name;
  143. }
  144. $status = $has_presence->status;
  145. $instructor = \App\Models\User::findOrFail($has_presence->user_id)->name;
  146. if ($has_presence->court_id > 0) {
  147. $court = \App\Models\Court::findOrFail($has_presence->court_id)->name;
  148. }
  149. if ($has_presence->instructor_id > 0 && $has_presence->instructor_id !== $has_presence->user_id) {
  150. $additional_instructor = \App\Models\User::findOrFail($has_presence->instructor_id)->name;
  151. }
  152. if (!is_null($has_presence->notes)) {
  153. $notes = $has_presence->notes;
  154. }
  155. }
  156. if (in_array($member->id, $this->newMembers)) {
  157. $presence = true;
  158. $my_presence = true;
  159. }
  160. return array(
  161. 'id' => $member->id,
  162. 'first_name' => $member->first_name,
  163. 'last_name' => $member->last_name,
  164. 'certificate' => $y,
  165. 'presence' => $presence,
  166. 'my_presence' => $my_presence,
  167. 'status' => $status,
  168. 'motivation' => $motivation,
  169. 'court' => $court,
  170. 'instructor' => $instructor,
  171. 'additional_instructor' => $additional_instructor,
  172. 'notes' => $notes,
  173. );
  174. }
  175. public function save($ids)
  176. {
  177. $this->saveAndStay($ids);
  178. $last_date = explode(" ", $this->calendar->from)[0];
  179. return redirect()->to("/calendar?last_date={$last_date}");
  180. }
  181. public function saveAndStay($ids)
  182. {
  183. $this->calendar->court_id = $this->court_id;
  184. $this->calendar->instructor_id = $this->instructor_id;
  185. $this->calendar->note = $this->note;
  186. if ($this->motivation_id != "" && $this->motivation_id != null)
  187. $this->calendar->motivation_id = $this->motivation_id;
  188. if ($this->motivation_manual_id != "" && $this->motivation_manual_id != null)
  189. $this->calendar->motivation_manual_id = $this->motivation_manual_id;
  190. $this->calendar->save();
  191. // $x = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->where('status', '<>', 99)->first();
  192. // $mid = null;
  193. // if ($x) {
  194. // $mid = $x->motivation_id;
  195. // $x->delete();
  196. // }
  197. // Mappa degli ultimi motivation_id per ogni member_id dell'utente e calendario correnti
  198. $userId = \Auth::user()->id;
  199. $calendarId = $this->calendar->id;
  200. $lastEditData = \App\Models\Presence::query()
  201. ->select('member_id', 'motivation_id')
  202. ->where('calendar_id', $calendarId)
  203. ->where('user_id', $userId)
  204. ->where('status', '<>', 99)
  205. ->whereIn('id', function ($q) use ($calendarId, $userId) {
  206. $q->selectRaw('MAX(id)')
  207. ->from('presences')
  208. ->where('calendar_id', $calendarId)
  209. ->where('user_id', $userId)
  210. ->where('status', '<>', 99)
  211. ->groupBy('member_id');
  212. })
  213. ->get()
  214. ->keyBy('member_id') // -> [member_id => (obj con motivation_id)]
  215. ->map(fn($row) => $row->motivation_id)
  216. ->toArray();
  217. // Elimino tutti i dati correnti che devono essere sostituiti
  218. \App\Models\Presence::query()
  219. ->where('calendar_id', $calendarId)
  220. ->where('user_id', $userId)
  221. ->where('status', '<>', 99)
  222. ->delete();
  223. // Ricreo le presenze per ogni membro contenuto in $ids
  224. foreach ($ids as $id) {
  225. $p = new \App\Models\Presence();
  226. $p->member_id = $id;
  227. $p->calendar_id = $calendarId;
  228. // Se per quel membro esisteva un motivation_id, lo riuso, altrimenti lo lascio null
  229. $p->motivation_id = $lastEditData[$id] ?? null;
  230. $p->user_id = $userId;
  231. $p->status = 0;
  232. // Salvo eventuale court_id (se presente e maggiore di 0)
  233. if ($this->save_court_id > 0) {
  234. $p->court_id = $this->save_court_id;
  235. }
  236. // Salvo eventuale instructor_id (se presente e maggiore di 0)
  237. if ($this->save_instructor_id > 0) {
  238. $p->instructor_id = $this->save_instructor_id;
  239. }
  240. // Salvo eventuali note (se non vuote)
  241. if ($this->save_notes != '') {
  242. $p->notes = $this->save_notes;
  243. }
  244. $p->save();
  245. }
  246. $this->emit('setSaving');
  247. }
  248. public function cancel($ids, $motivation_id)
  249. {
  250. $presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->whereIn('member_id', $ids)->get();
  251. foreach ($presences as $presence) {
  252. $presence->motivation_id = $motivation_id;
  253. $presence->status = 99;
  254. $presence->save();
  255. }
  256. return redirect()->to('/presences?calendarId=' . $this->calendar->id);
  257. }
  258. public function addMember($ids)
  259. {
  260. $this->added = true;
  261. //if (!in_array($id, $this->newMembers))
  262. // $this->newMembers[] = $id;
  263. $this->member_ids = $ids;
  264. $this->emit('reload');
  265. }
  266. public function cancelCalendar()
  267. {
  268. $this->calendar->motivation_id = $this->motivation_id;
  269. $this->calendar->status = 99;
  270. $this->calendar->save();
  271. return redirect()->to('/calendar');
  272. }
  273. public function createMember()
  274. {
  275. if (!$this->added) {
  276. $this->newMemberFiscalCodeExist = false;
  277. $this->validate([
  278. "newMemberMotivationId" => 'required',
  279. ]);
  280. /*$this->validate([
  281. // 'newMemberFiscalCode'=>'required|max:16',
  282. 'newMemberFirstName'=>'required',
  283. 'newMemberLastName'=>'required',
  284. //'newMemberEmail'=>'required',
  285. ]);*/
  286. // Check fiscal code exist
  287. $exist = false;
  288. if ($this->newMemberFiscalCode != '') {
  289. $check = \App\Models\Member::where('fiscal_code', $this->newMemberFiscalCode)->get();
  290. $exist = $check->count() > 0;
  291. }
  292. if (!$exist) {
  293. $member = \App\Models\Member::create([
  294. 'first_name' => strtoupper($this->newMemberFirstName),
  295. 'last_name' => strtoupper($this->newMemberLastName),
  296. 'email' => strtoupper($this->newMemberEmail),
  297. 'to_complete' => true,
  298. 'fiscal_code' => $this->newMemberFiscalCode,
  299. 'status' => true
  300. ]);
  301. //if (!in_array($member->id, $this->newMembers))
  302. // $this->newMembers[] = $member->id;
  303. $p = new \App\Models\Presence();
  304. $p->member_id = $member->id;
  305. $p->calendar_id = $this->calendar->id;
  306. $p->motivation_id = $this->newMemberMotivationId;
  307. $p->user_id = \Auth::user()->id;
  308. $p->status = 0;
  309. $p->court_id = null;
  310. $p->instructor_id = null;
  311. $p->notes = null;
  312. $p->save();
  313. $this->emit('reload');
  314. $this->emit('saved');
  315. /*$this->newMemberFirstName = '';
  316. $this->newMemberLastName = '';
  317. $this->newMemberEmail = '';
  318. $this->newMemberFiscalCode = '';*/
  319. } else {
  320. $this->newMemberFiscalCodeExist = true;
  321. }
  322. } else {
  323. if ($this->member_ids != null) {
  324. $this->validate([
  325. "newMemberMotivationId" => 'required',
  326. ]);
  327. foreach ($this->member_ids as $m) {
  328. //if ($this->manual)
  329. //{
  330. //\App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->where('status', '<>', 99)->delete();
  331. //foreach($ids as $id)
  332. //{
  333. $p = new \App\Models\Presence();
  334. $p->member_id = $m;
  335. $p->calendar_id = $this->calendar->id;
  336. $p->motivation_id = $this->newMemberMotivationId;
  337. $p->user_id = \Auth::user()->id;
  338. $p->status = 0;
  339. $p->court_id = null;
  340. $p->instructor_id = null;
  341. $p->notes = null;
  342. $p->save();
  343. //}
  344. /*}
  345. else
  346. {
  347. if (!in_array($m, $this->newMembers))
  348. $this->newMembers[] = $m;
  349. }*/
  350. }
  351. }
  352. //$this->member_id = 0;
  353. $this->member_ids = [];
  354. $this->added = false;
  355. $this->emit('reload');
  356. $this->emit('saved');
  357. }
  358. }
  359. public function createInstructor()
  360. {
  361. $user = \App\Models\User::create([
  362. 'name' => $this->userName,
  363. 'email' => $this->userEmail,
  364. 'password' => '',
  365. 'level' => 2,
  366. 'enabled' => true
  367. ]);
  368. $this->instructor_id = $user->id;
  369. $this->instructors = \App\Models\User::select('*')->where('level', 2)->where('enabled', true)->orderBy('name', 'asc')->get();
  370. $this->emit('saved');
  371. }
  372. public function removeSingle($id)
  373. {
  374. \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('member_id', $id)->delete();
  375. $this->emit('reload');
  376. }
  377. public function revert($id)
  378. {
  379. $p = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('member_id', $id)->first();
  380. $p->motivation_id = null;
  381. $p->status = 0;
  382. $p->save();
  383. $this->emit('reload');
  384. }
  385. }