Presence.php 17 KB

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