Presence.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. $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. $notes = '';
  141. $has_presence = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('member_id', $member->id)->first();
  142. if ($has_presence) {
  143. $presence = true;
  144. $my_presence = $has_presence->user_id == \Auth::user()->id;
  145. if ($has_presence->motivation_id > 0) {
  146. $motivation = \App\Models\Motivation::findOrFail($has_presence->motivation_id)->name;
  147. }
  148. $status = $has_presence->status;
  149. if ($has_presence->court_id > 0) {
  150. $court = \App\Models\Court::findOrFail($has_presence->court_id)->name;
  151. }
  152. if ($has_presence->instructor_id > 0) {
  153. $instructor = \App\Models\User::findOrFail($has_presence->instructor_id)->name;
  154. }
  155. if (!is_null($has_presence->notes)) {
  156. $notes = $has_presence->notes;
  157. }
  158. }
  159. if (in_array($member->id, $this->newMembers)) {
  160. $presence = true;
  161. $my_presence = true;
  162. }
  163. return array(
  164. 'id' => $member->id,
  165. 'first_name' => $member->first_name,
  166. 'last_name' => $member->last_name,
  167. 'certificate' => $y,
  168. 'presence' => $presence,
  169. 'my_presence' => $my_presence,
  170. 'status' => $status,
  171. 'motivation' => $motivation,
  172. 'court' => $court,
  173. 'instructor' => $instructor,
  174. 'notes' => $notes,
  175. );
  176. }
  177. public function save($ids)
  178. {
  179. $this->saveAndStay($ids);
  180. $last_date = explode(" ", $this->calendar->from)[0];
  181. return redirect()->to("/calendar?last_date={$last_date}");
  182. }
  183. public function saveAndStay($ids)
  184. {
  185. $this->calendar->court_id = $this->court_id;
  186. $this->calendar->instructor_id = $this->instructor_id;
  187. $this->calendar->note = $this->note;
  188. if ($this->motivation_id != "" && $this->motivation_id != null)
  189. $this->calendar->motivation_id = $this->motivation_id;
  190. if ($this->motivation_manual_id != "" && $this->motivation_manual_id != null)
  191. $this->calendar->motivation_manual_id = $this->motivation_manual_id;
  192. $this->calendar->save();
  193. $x = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->where('status', '<>', 99)->first();
  194. $mid = null;
  195. if ($x) {
  196. $mid = $x->motivation_id;
  197. $x->delete();
  198. }
  199. foreach ($ids as $id) {
  200. $p = new \App\Models\Presence();
  201. $p->member_id = $id;
  202. $p->calendar_id = $this->calendar->id;
  203. $p->motivation_id = $mid;
  204. $p->user_id = \Auth::user()->id;
  205. $p->status = 0;
  206. if ($this->save_court_id > 0) {
  207. $p->court_id = $this->save_court_id;
  208. }
  209. if ($this->save_instructor_id > 0) {
  210. $p->instructor_id = $this->save_instructor_id;
  211. }
  212. if ($this->save_notes != '') {
  213. $p->notes = $this->save_notes;
  214. }
  215. $p->save();
  216. }
  217. $this->emit('setSaving');
  218. }
  219. public function cancel($ids, $motivation_id)
  220. {
  221. $presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->whereIn('member_id', $ids)->get();
  222. foreach ($presences as $presence) {
  223. $presence->motivation_id = $motivation_id;
  224. $presence->status = 99;
  225. $presence->save();
  226. }
  227. return redirect()->to('/presences?calendarId=' . $this->calendar->id);
  228. }
  229. public function addMember($ids)
  230. {
  231. $this->added = true;
  232. //if (!in_array($id, $this->newMembers))
  233. // $this->newMembers[] = $id;
  234. $this->member_ids = $ids;
  235. $this->emit('reload');
  236. }
  237. public function cancelCalendar()
  238. {
  239. $this->calendar->motivation_id = $this->motivation_id;
  240. $this->calendar->status = 99;
  241. $this->calendar->save();
  242. return redirect()->to('/calendar');
  243. }
  244. public function createMember()
  245. {
  246. if (!$this->added) {
  247. $this->newMemberFiscalCodeExist = false;
  248. $this->validate([
  249. "newMemberMotivationId" => 'required',
  250. ]);
  251. /*$this->validate([
  252. // 'newMemberFiscalCode'=>'required|max:16',
  253. 'newMemberFirstName'=>'required',
  254. 'newMemberLastName'=>'required',
  255. //'newMemberEmail'=>'required',
  256. ]);*/
  257. // Check fiscal code exist
  258. $exist = false;
  259. if ($this->newMemberFiscalCode != '') {
  260. $check = \App\Models\Member::where('fiscal_code', $this->newMemberFiscalCode)->get();
  261. $exist = $check->count() > 0;
  262. }
  263. if (!$exist) {
  264. $member = \App\Models\Member::create([
  265. 'first_name' => strtoupper($this->newMemberFirstName),
  266. 'last_name' => strtoupper($this->newMemberLastName),
  267. 'email' => strtoupper($this->newMemberEmail),
  268. 'to_complete' => true,
  269. 'fiscal_code' => $this->newMemberFiscalCode,
  270. 'status' => true
  271. ]);
  272. //if (!in_array($member->id, $this->newMembers))
  273. // $this->newMembers[] = $member->id;
  274. $p = new \App\Models\Presence();
  275. $p->member_id = $member->id;
  276. $p->calendar_id = $this->calendar->id;
  277. $p->motivation_id = $this->newMemberMotivationId;
  278. $p->user_id = \Auth::user()->id;
  279. $p->status = 0;
  280. $p->court_id = null;
  281. $p->instructor_id = null;
  282. $p->notes = null;
  283. $p->save();
  284. $this->emit('reload');
  285. $this->emit('saved');
  286. /*$this->newMemberFirstName = '';
  287. $this->newMemberLastName = '';
  288. $this->newMemberEmail = '';
  289. $this->newMemberFiscalCode = '';*/
  290. } else {
  291. $this->newMemberFiscalCodeExist = true;
  292. }
  293. } else {
  294. if ($this->member_ids != null) {
  295. $this->validate([
  296. "newMemberMotivationId" => 'required',
  297. ]);
  298. foreach ($this->member_ids as $m) {
  299. //if ($this->manual)
  300. //{
  301. //\App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->where('status', '<>', 99)->delete();
  302. //foreach($ids as $id)
  303. //{
  304. $p = new \App\Models\Presence();
  305. $p->member_id = $m;
  306. $p->calendar_id = $this->calendar->id;
  307. $p->motivation_id = $this->newMemberMotivationId;
  308. $p->user_id = \Auth::user()->id;
  309. $p->status = 0;
  310. $p->court_id = null;
  311. $p->instructor_id = null;
  312. $p->notes = null;
  313. $p->save();
  314. //}
  315. /*}
  316. else
  317. {
  318. if (!in_array($m, $this->newMembers))
  319. $this->newMembers[] = $m;
  320. }*/
  321. }
  322. }
  323. //$this->member_id = 0;
  324. $this->member_ids = [];
  325. $this->added = false;
  326. $this->emit('reload');
  327. $this->emit('saved');
  328. }
  329. }
  330. public function createInstructor()
  331. {
  332. $user = \App\Models\User::create([
  333. 'name' => $this->userName,
  334. 'email' => $this->userEmail,
  335. 'password' => '',
  336. 'level' => 2,
  337. 'enabled' => true
  338. ]);
  339. $this->instructor_id = $user->id;
  340. $this->instructors = \App\Models\User::select('*')->where('level', 2)->where('enabled', true)->get();
  341. $this->emit('saved');
  342. }
  343. public function removeSingle($id)
  344. {
  345. \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('member_id', $id)->delete();
  346. $this->emit('reload');
  347. }
  348. public function revert($id)
  349. {
  350. $p = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('member_id', $id)->first();
  351. $p->motivation_id = null;
  352. $p->status = 0;
  353. $p->save();
  354. $this->emit('reload');
  355. }
  356. }