Presence.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 $court_id, $instructor_id, $motivation_id, $motivation_manual_id, $note, $manual;
  9. public $newMemberFirstName, $newMemberLastName, $newMemberEmail, $newMemberToComplete, $newMemberFiscalCode, $newMemberFiscalCodeExist, $newMemberMotivationId;
  10. public $userName, $userEmail;
  11. public $added = false;
  12. public $courts = [];
  13. public $instructors = [];
  14. public $motivations = [];
  15. public $motivations_add = [];
  16. public $members = [];
  17. public $newMembers = [];
  18. public $ids = [];
  19. public function mount()
  20. {
  21. $this->calendar = \App\Models\Calendar::findOrFail($_GET["calendarId"]);
  22. $this->court_id = $this->calendar->court_id;
  23. $this->instructor_id = $this->calendar->instructor_id;
  24. $this->motivation_manual_id = $this->calendar->motivation_manual_id;
  25. $this->manual = $this->calendar->manual;
  26. $this->note = $this->calendar->note;
  27. $this->courts = \App\Models\Court::select('*')->where('enabled', true)->get();
  28. $this->instructors = \App\Models\User::select('*')->where('level', 2)->where('enabled', true)->get();
  29. $this->motivations = \App\Models\Motivation::select('*')->where('enabled', true)->where('type', 'del')->get();
  30. $this->motivations_add = \App\Models\Motivation::select('*')->where('enabled', true)->where('type', 'add')->get();
  31. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])->orderBy('last_name')->orderBy('first_name')->get();
  32. }
  33. public function render()
  34. {
  35. $this->records = [];
  36. $presenceMembers = [];
  37. if (!$this->manual)
  38. {
  39. // Carco tutti gli iscritti a un corso padre in quel giorno con un range orario simile
  40. $days = ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'];
  41. $dow = date('w', strtotime($this->calendar->from));
  42. $d = $days[$dow];
  43. // Elenco corsi per tipologia in base al calendario
  44. $courses = \App\Models\Course::where('name', $this->calendar->name)->where('date_from', '<=', $this->calendar->from)->where('date_to', '>=', $this->calendar->to)->pluck('id')->toArray();
  45. // Elenco utenti iscritti al corso "padre"
  46. $members_courses = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")->whereIn('course_id', $courses)->pluck('member_id')->toArray();
  47. $members = \App\Models\Member::whereIn('id', $members_courses)->orderBy('last_name')->orderBy('first_name')->get();
  48. // $presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->pluck('member_id')->toArray();
  49. // $my_presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->pluck('member_id')->toArray();
  50. foreach($members as $member)
  51. {
  52. $presenceMembers[] = $member->id;
  53. $this->records[] = $this->getMember($member);
  54. }
  55. }
  56. // Aggiungo i membri iscritti
  57. $members_presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->whereNotIn('member_id', $presenceMembers)->pluck('member_id')->toArray();
  58. $members = \App\Models\Member::whereIn('id', $members_presences)->get();
  59. foreach($members as $member)
  60. {
  61. $this->records[] = $this->getMember($member);
  62. }
  63. foreach($this->newMembers as $m)
  64. {
  65. $member = \App\Models\Member::findOrFail($m);
  66. $this->records[] = $this->getMember($member);
  67. }
  68. /*$calendars = \App\Models\Calendar::get();
  69. foreach($calendars as $c)
  70. {
  71. $this->records[] = array('id' => $c->id, 'title' => $c->course->name, 'start' => $c->from, 'end' => $c->to);
  72. }*/
  73. return view('livewire.presence');
  74. }
  75. public function getMember($member)
  76. {
  77. $latestCert = \App\Models\MemberCertificate::where('member_id', $member->id)
  78. ->orderBy('expire_date', 'desc')
  79. ->first();
  80. $certificate = '';
  81. $y = "|";
  82. if ($latestCert)
  83. {
  84. $latest_date = $latestCert->expire_date;
  85. $status = '';
  86. if ($latest_date < date("Y-m-d")) {
  87. $status = "0"; // Expired
  88. } else if ($latest_date <= date("Y-m-d", strtotime("+1 month"))) {
  89. $status = "1"; // Expiring soon
  90. } else {
  91. $status = "2"; // Valid
  92. }
  93. $y = $status . "|" . date("d/m/Y", strtotime($latest_date));
  94. }
  95. $presence = false;
  96. $my_presence = false;
  97. $status = 0;
  98. $has_presence = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('member_id', $member->id)->first();
  99. if ($has_presence)
  100. {
  101. $presence = true;
  102. $my_presence = $has_presence->user_id == \Auth::user()->id;
  103. $status = $has_presence->status;
  104. }
  105. return array('id' => $member->id, 'first_name' => $member->first_name, 'last_name' => $member->last_name, 'certificate' => $y, 'presence' => $presence, 'my_presence' => $my_presence, 'status' => $status);
  106. }
  107. public function save($ids)
  108. {
  109. $this->calendar->court_id = $this->court_id;
  110. $this->calendar->instructor_id = $this->instructor_id;
  111. $this->calendar->note = $this->note;
  112. if ($this->motivation_id != "" && $this->motivation_id != null)
  113. $this->calendar->motivation_id = $this->motivation_id;
  114. if ($this->motivation_manual_id != "" && $this->motivation_manual_id != null)
  115. $this->calendar->motivation_manual_id = $this->motivation_manual_id;
  116. $this->calendar->save();
  117. \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->where('status', '<>', 99)->delete();
  118. foreach($ids as $id)
  119. {
  120. $p = new \App\Models\Presence();
  121. $p->member_id = $id;
  122. $p->calendar_id = $this->calendar->id;
  123. $p->user_id = \Auth::user()->id;
  124. $p->status = 0;
  125. $p->save();
  126. }
  127. return redirect()->to('/calendar');
  128. }
  129. public function cancel($ids, $motivation_id)
  130. {
  131. $presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->whereIn('member_id', $ids)->get();
  132. foreach($presences as $presence)
  133. {
  134. $presence->motivation_id = $motivation_id;
  135. $presence->status = 99;
  136. $presence->save();
  137. }
  138. return redirect()->to('/presences?calendarId=' . $this->calendar->id);
  139. }
  140. public function addMember($id)
  141. {
  142. $this->added = true;
  143. //if (!in_array($id, $this->newMembers))
  144. // $this->newMembers[] = $id;
  145. $this->member_id = $id;
  146. $this->emit('reload');
  147. }
  148. public function cancelCalendar()
  149. {
  150. $this->calendar->motivation_id = $this->motivation_id;
  151. $this->calendar->status = 99;
  152. $this->calendar->save();
  153. return redirect()->to('/calendar');
  154. }
  155. public function createMember()
  156. {
  157. if (!$this->added)
  158. {
  159. $this->newMemberFiscalCodeExist = false;
  160. $this->validate([
  161. // 'newMemberFiscalCode'=>'required|max:16',
  162. 'newMemberFirstName'=>'required',
  163. 'newMemberLastName'=>'required',
  164. //'newMemberEmail'=>'required',
  165. ]);
  166. // Check fiscal code exist
  167. $exist = false;
  168. if ($this->newMemberFiscalCode != '')
  169. {
  170. $check = \App\Models\Member::where('fiscal_code', $this->newMemberFiscalCode)->get();
  171. $exist = $check->count() > 0;
  172. }
  173. if (!$exist)
  174. {
  175. $member = \App\Models\Member::create([
  176. 'first_name' => strtoupper($this->newMemberFirstName),
  177. 'last_name' => strtoupper($this->newMemberLastName),
  178. 'email' => strtoupper($this->newMemberEmail),
  179. 'to_complete' => $this->newMemberToComplete,
  180. 'fiscal_code' => $this->newMemberFiscalCode,
  181. 'status' => true
  182. ]);
  183. if (!in_array($member->id, $this->newMembers))
  184. $this->newMembers[] = $member->id;
  185. $this->emit('reload');
  186. $this->emit('saved');
  187. }
  188. else
  189. {
  190. $this->newMemberFiscalCodeExist = true;
  191. }
  192. }
  193. else
  194. {
  195. if (!in_array($this->member_id, $this->newMembers))
  196. $this->newMembers[] = $this->member_id;
  197. $this->member_id = 0;
  198. $this->added = false;
  199. $this->emit('reload');
  200. $this->emit('saved');
  201. }
  202. }
  203. public function createInstructor()
  204. {
  205. $user = \App\Models\User::create([
  206. 'name' => $this->userName,
  207. 'email' => $this->userEmail,
  208. 'password' => '',
  209. 'level' => 2,
  210. 'enabled' => true
  211. ]);
  212. $this->instructor_id = $user->id;
  213. $this->instructors = \App\Models\User::select('*')->where('level', 2)->where('enabled', true)->get();
  214. $this->emit('saved');
  215. }
  216. }