Presence.php 15 KB

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