Presence.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Illuminate\Support\Facades\Validator;
  4. use Livewire\Component;
  5. class Presence extends Component
  6. {
  7. public $calendar;
  8. public $records;
  9. public $member_ids = [];
  10. public $court_id, $instructor_id, $motivation_id, $motivation_manual_id, $note, $manual;
  11. public $save_court_id, $save_instructor_id, $save_notes;
  12. public $newMemberFirstName, $newMemberLastName, $newMemberEmail, $newMemberToComplete, $newMemberFiscalCode, $newMemberFiscalCodeExist, $newMemberMotivationId;
  13. public $userName, $userEmail;
  14. public $added = false;
  15. public $filter = '';
  16. public $courts = [];
  17. public $instructors = [];
  18. public $motivations = [];
  19. public $motivations_add = [];
  20. public $insertUser = 'exist';
  21. public $course_limit;
  22. public $motivation_course_id = null;
  23. public $motivation_course_name = 0;
  24. public $motivation_course_level = 0;
  25. public $motivation_course_type = 0;
  26. public $motivation_course_frequency = 0;
  27. public $course_names = [];
  28. public $course_levels = [];
  29. public $course_types = [];
  30. public $course_frequencies = [];
  31. public $members = [];
  32. public $add_member_id = 0;
  33. public $add_member_courses = [];
  34. public $newMembers = [];
  35. public $ids = [];
  36. public function mount()
  37. {
  38. setlocale(LC_ALL, 'it_IT');
  39. $this->calendar = \App\Models\Calendar::findOrFail($_GET["calendarId"]);
  40. $this->court_id = $this->calendar->court_id;
  41. $this->instructor_id = $this->calendar->instructor_id;
  42. $this->motivation_manual_id = $this->calendar->motivation_manual_id;
  43. $this->manual = $this->calendar->manual;
  44. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])
  45. ->where(function ($query) {
  46. $query->where('is_archived', false)
  47. ->orWhereNull('is_archived');
  48. })
  49. ->where(function ($query) {
  50. $query->where('is_deleted', false)
  51. ->orWhereNull('is_deleted');
  52. })->orderBy('last_name')->orderBy('first_name')->get();
  53. $this->note = $this->calendar->note;
  54. $this->courts = \App\Models\Court::select('*')->where('enabled', true)->get();
  55. $this->instructors = \App\Models\User::select('*')->where('level', 2)->where('enabled', true)->orderBy('name', 'asc')->get();
  56. $this->motivations = \App\Models\Motivation::select('*')->where('enabled', true)->where('type', 'del')->get();
  57. $this->motivations_add = \App\Models\Motivation::select('*')->where('enabled', true)->where('type', 'add')->get();
  58. $this->save_court_id = 0;
  59. $this->save_instructor_id = 0;
  60. $this->save_notes = '';
  61. $this->insertUser = 'exist';
  62. $this->course_limit = now()->endOfDay();
  63. // $this->course_names = \App\Models\Course::whereDate('date_from', '<=', $this->course_limit)->whereDate('date_to', '>=', $this->course_limit)->where('active', true)->where('enabled', true)->orderBy('name')->groupBy('name')->pluck('name');
  64. $this->course_names = [];
  65. $this->course_levels = [];
  66. $this->course_types = [];
  67. $this->course_frequencies = [];
  68. }
  69. public function updatedAddMemberId()
  70. {
  71. if ($this->insertUser == 'exist') {
  72. $this->add_member_courses = \App\Models\MemberCourse::where('member_id', $this->add_member_id)->groupBy('course_id')->pluck('course_id');
  73. $this->course_names = \App\Models\Course::whereDate('date_from', '<=', $this->course_limit)->whereDate('date_to', '>=', $this->course_limit)->where('active', true)->where('enabled', true)->whereIn('id', $this->add_member_courses)->orderBy('name')->groupBy('name')->pluck('name');
  74. $this->course_levels = [];
  75. $this->course_types = [];
  76. $this->course_frequencies = [];
  77. $this->motivation_course_id = null;
  78. $this->motivation_course_name = 0;
  79. $this->motivation_course_level = 0;
  80. $this->motivation_course_type = 0;
  81. $this->motivation_course_frequency = 0;
  82. }
  83. }
  84. public function updatedInsertUser()
  85. {
  86. $this->add_member_id = 0;
  87. if ($this->insertUser == 'exist') {
  88. $this->course_names = [];
  89. } else {
  90. $this->course_names = \App\Models\Course::whereDate('date_from', '<=', $this->course_limit)->whereDate('date_to', '>=', $this->course_limit)->where('active', true)->where('enabled', true)->orderBy('name')->groupBy('name')->pluck('name');
  91. }
  92. $this->course_levels = [];
  93. $this->course_types = [];
  94. $this->course_frequencies = [];
  95. $this->motivation_course_id = null;
  96. $this->motivation_course_name = 0;
  97. $this->motivation_course_level = 0;
  98. $this->motivation_course_type = 0;
  99. $this->motivation_course_frequency = 0;
  100. $this->emit('reload');
  101. }
  102. function updatedMotivationCourseName()
  103. {
  104. if ($this->motivation_course_name > 0 && $this->motivation_course_name != '') {
  105. $this->motivation_course_id = null;
  106. $this->motivation_course_level = 0;
  107. $this->motivation_course_type = 0;
  108. $this->motivation_course_frequency = 0;
  109. $levels_ids = [];
  110. $levels_query = \App\Models\Course::whereDate('date_from', '<=', $this->course_limit)->whereDate('date_to', '>=', $this->course_limit)->where('active', true)->where('enabled', true)->where('name', $this->motivation_course_name);
  111. if ($this->insertUser == 'exist') {
  112. $levels_query->whereIn('id', $this->add_member_courses);
  113. }
  114. $levels = $levels_query->get();
  115. foreach ($levels as $l) {
  116. $levels_ids[] = $l->course_level_id;
  117. }
  118. $this->course_levels = \App\Models\CourseLevel::where('enabled', true)->whereIn('id', $levels_ids)->orderBy('name')->get();
  119. $this->course_types = [];
  120. $this->course_frequencies = [];
  121. } else {
  122. $this->course_levels = [];
  123. $this->course_types = [];
  124. $this->course_frequencies = [];
  125. }
  126. }
  127. function updatedMotivationCourseLevel()
  128. {
  129. if ($this->motivation_course_name > 0 && $this->motivation_course_name != '' && $this->motivation_course_level > 0 && $this->motivation_course_level != '') {
  130. $this->motivation_course_id = null;
  131. $this->motivation_course_type = 0;
  132. $this->motivation_course_frequency = 0;
  133. $type_ids = [];
  134. $types_query = \App\Models\Course::whereDate('date_from', '<=', $this->course_limit)->whereDate('date_to', '>=', $this->course_limit)->where('active', true)->where('enabled', true)->where('name', $this->motivation_course_name)->where('course_level_id', $this->motivation_course_level);
  135. if ($this->insertUser == 'exist') {
  136. $types_query->whereIn('id', $this->add_member_courses);
  137. }
  138. $types = $types_query->get();
  139. foreach ($types as $t) {
  140. $type_ids[] = $t->course_type_id;
  141. }
  142. $this->course_types = \App\Models\CourseType::where('enabled', true)->whereIn('id', $type_ids)->orderBy('name')->get();
  143. $this->course_frequencies = [];
  144. } else {
  145. $this->course_types = [];
  146. $this->course_frequencies = [];
  147. }
  148. }
  149. function updatedMotivationCourseType()
  150. {
  151. if ($this->motivation_course_name > 0 && $this->motivation_course_name != '' && $this->motivation_course_level > 0 && $this->motivation_course_level != '' && $this->motivation_course_type > 0 && $this->motivation_course_type != '') {
  152. $this->motivation_course_id = null;
  153. $this->motivation_course_frequency = 0;
  154. $frequency_ids = [];
  155. $frequencies_query = \App\Models\Course::whereDate('date_from', '<=', $this->course_limit)->whereDate('date_to', '>=', $this->course_limit)->where('active', true)->where('enabled', true)->where('name', $this->motivation_course_name)->where('course_level_id', $this->motivation_course_level)->where('course_type_id', $this->motivation_course_type);
  156. if ($this->insertUser == 'exist') {
  157. $frequencies_query->whereIn('id', $this->add_member_courses);
  158. }
  159. $frequencies = $frequencies_query->get();
  160. foreach ($frequencies as $f) {
  161. $frequency_ids[] = $f->course_frequency_id;
  162. }
  163. $this->course_frequencies = \App\Models\CourseFrequency::where('enabled', true)->whereIn('id', $frequency_ids)->orderBy('name')->get();
  164. } else {
  165. $this->course_frequencies = [];
  166. }
  167. }
  168. function updatedMotivationCourseFrequency()
  169. {
  170. if ($this->motivation_course_name > 0 && $this->motivation_course_name != '' && $this->motivation_course_level > 0 && $this->motivation_course_level != '' && $this->motivation_course_type > 0 && $this->motivation_course_type != '' && $this->motivation_course_frequency > 0 && $this->motivation_course_frequency != '') {
  171. $this->motivation_course_id = null;
  172. $course_query = \App\Models\Course::whereDate('date_from', '<=', $this->course_limit)->whereDate('date_to', '>=', $this->course_limit)->where('active', true)->where('enabled', true)->where('name', $this->motivation_course_name)->where('course_level_id', $this->motivation_course_level)->where('course_type_id', $this->motivation_course_type)->where('course_frequency_id', $this->motivation_course_frequency);
  173. if ($this->insertUser == 'exist') {
  174. $course_query->whereIn('id', $this->add_member_courses);
  175. }
  176. $course = $course_query->first();
  177. $this->motivation_course_id = $course->id;
  178. } else {
  179. $this->motivation_course_id = null;
  180. }
  181. }
  182. public function updatedNewMemberMotivationId()
  183. {
  184. $this->emit('reload');
  185. }
  186. public function render()
  187. {
  188. $this->records = [];
  189. setlocale(LC_ALL, 'it_IT');
  190. $presenceMembers = [];
  191. if (!$this->manual) {
  192. // Carco tutti gli iscritti a un corso padre in quel giorno con un range orario simile
  193. $days = ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'];
  194. $dow = date('w', strtotime($this->calendar->from));
  195. $d = $days[$dow];
  196. $h = date('H:i', strtotime($this->calendar->from));
  197. // Elenco corsi per tipologia in base al calendario
  198. $courses = \App\Models\Course::where('name', $this->calendar->name)->where('date_from', '<=', $this->calendar->from)->where('date_to', '>=', $this->calendar->to)->pluck('id')->toArray();
  199. $months = date("n", strtotime($this->calendar->from));
  200. // Elenco utenti iscritti al corso "padre"
  201. // $members_courses = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")
  202. // ->where('when', 'like', '%"from":"' . $h . '"%')
  203. // ->whereNot('months', 'like', '%"m":' . $months . ',"status":2%')
  204. // ->whereDate('date_from', '<=', $this->calendar->from)
  205. // ->whereDate('date_to', '>=', $this->calendar->from)
  206. // ->whereIn('course_id', $courses)
  207. // ->pluck('member_id')->toArray();
  208. // $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();
  209. $members_courses = \App\Models\MemberCourse::query()
  210. ->whereRaw("JSON_CONTAINS(`when`, JSON_OBJECT('day', JSON_ARRAY(?), 'from', ?), '$')", [$d, $h])
  211. ->whereRaw("JSON_CONTAINS(months, JSON_OBJECT('m', CAST(? AS UNSIGNED)), '$')", [$months])
  212. ->whereRaw("NOT JSON_CONTAINS(months, JSON_OBJECT('m', CAST(? AS UNSIGNED), 'status', 2), '$')", [$months])
  213. ->whereRaw("NOT JSON_CONTAINS(months, JSON_OBJECT('m', CAST(? AS UNSIGNED), 'status', '2'), '$')", [$months])
  214. ->whereIn('course_id', $courses)
  215. ->pluck('member_id')
  216. ->toArray();
  217. if ($this->filter != '') {
  218. $filter = $this->filter;
  219. $members = \App\Models\Member::whereIn('id', $members_courses)->where(function ($query) use ($filter) {
  220. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $filter . "%'")
  221. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $filter . "%'");
  222. })
  223. ->where(function ($query) {
  224. $query->where('is_archived', false)
  225. ->orWhereNull('is_archived');
  226. })
  227. ->where(function ($query) {
  228. $query->where('is_deleted', false)
  229. ->orWhereNull('is_deleted');
  230. })->orderBy('last_name')->orderBy('first_name')->get();
  231. } else
  232. $members = \App\Models\Member::whereIn('id', $members_courses)
  233. ->where(function ($query) {
  234. $query->where('is_archived', false)
  235. ->orWhereNull('is_archived');
  236. })
  237. ->where(function ($query) {
  238. $query->where('is_deleted', false)
  239. ->orWhereNull('is_deleted');
  240. })->orderBy('last_name')->orderBy('first_name')->get();
  241. // $presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->pluck('member_id')->toArray();
  242. // $my_presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->pluck('member_id')->toArray();
  243. foreach ($members as $member) {
  244. $presenceMembers[] = $member->id;
  245. //$this->member_ids[] = $member->id;
  246. $this->records[] = $this->getMember($member);
  247. }
  248. }
  249. // Aggiungo i membri iscritti
  250. $members_presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->whereNotIn('member_id', $presenceMembers)->pluck('member_id')->toArray();
  251. if ($this->filter != '') {
  252. $filter = $this->filter;
  253. $members = \App\Models\Member::whereIn('id', $members_presences)->where(function ($query) use ($filter) {
  254. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $filter . "%'")
  255. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $filter . "%'");
  256. })
  257. ->where(function ($query) {
  258. $query->where('is_archived', false)
  259. ->orWhereNull('is_archived');
  260. })
  261. ->where(function ($query) {
  262. $query->where('is_deleted', false)
  263. ->orWhereNull('is_deleted');
  264. })->get();
  265. } else
  266. $members = \App\Models\Member::whereIn('id', $members_presences)
  267. ->where(function ($query) {
  268. $query->where('is_archived', false)
  269. ->orWhereNull('is_archived');
  270. })
  271. ->where(function ($query) {
  272. $query->where('is_deleted', false)
  273. ->orWhereNull('is_deleted');
  274. })->get();
  275. foreach ($members as $member) {
  276. //$this->member_ids[] = $member->id;
  277. $this->records[] = $this->getMember($member);
  278. }
  279. foreach ($this->newMembers as $m) {
  280. $member = \App\Models\Member::findOrFail($m);
  281. //$this->member_ids[] = $member->id;
  282. $this->records[] = $this->getMember($member);
  283. }
  284. /*$calendars = \App\Models\Calendar::get();
  285. foreach($calendars as $c)
  286. {
  287. $this->records[] = array('id' => $c->id, 'title' => $c->course->name, 'start' => $c->from, 'end' => $c->to);
  288. }*/
  289. return view('livewire.presence');
  290. }
  291. public function getDateX()
  292. {
  293. setlocale(LC_ALL, 'it_IT');
  294. $days = ['Domenica', 'Lunedì', 'Martedì', 'Mercoledì', 'Giovedì', 'Venerdì', 'Sabato'];
  295. $months = ['Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno', 'Luglio', 'Agosto', 'Settembre', 'Ottobre', 'Novembre', 'Dicembre'];
  296. return $days[date('w', strtotime($this->calendar->from))] . " " . date("d", strtotime($this->calendar->from)) . " " . $months[date("n", strtotime($this->calendar->from)) - 1];
  297. }
  298. public function getMember($member)
  299. {
  300. $latestCert = \App\Models\MemberCertificate::where('member_id', $member->id)
  301. ->orderBy('expire_date', 'desc')
  302. ->first();
  303. $certificate = '';
  304. $y = "|";
  305. if ($latestCert) {
  306. $latest_date = $latestCert->expire_date;
  307. $status = '';
  308. if ($latest_date < date("Y-m-d")) {
  309. $status = "0"; // Expired
  310. } else if ($latest_date <= date("Y-m-d", strtotime("+1 month"))) {
  311. $status = "1"; // Expiring soon
  312. } else {
  313. $status = "2"; // Valid
  314. }
  315. $y = $status . "|" . date("d/m/Y", strtotime($latest_date));
  316. }
  317. $presence = false;
  318. $my_presence = false;
  319. $motivation = '';
  320. $status = 0;
  321. $court = '';
  322. $instructor = '';
  323. $additional_instructor = '';
  324. $notes = '';
  325. $has_presence = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('member_id', $member->id)->first();
  326. if ($has_presence) {
  327. $presence = true;
  328. $my_presence = $has_presence->user_id == \Auth::user()->id;
  329. if ($has_presence->motivation_id > 0) {
  330. $motivation = \App\Models\Motivation::findOrFail($has_presence->motivation_id)->name;
  331. }
  332. $status = $has_presence->status;
  333. $instructor = \App\Models\User::findOrFail($has_presence->user_id)->name;
  334. if ($has_presence->court_id > 0) {
  335. $court = \App\Models\Court::findOrFail($has_presence->court_id)->name;
  336. }
  337. if ($has_presence->instructor_id > 0 && $has_presence->instructor_id !== $has_presence->user_id) {
  338. $additional_instructor = \App\Models\User::findOrFail($has_presence->instructor_id)->name;
  339. }
  340. if (!is_null($has_presence->notes)) {
  341. $notes = $has_presence->notes;
  342. }
  343. }
  344. if (in_array($member->id, $this->newMembers)) {
  345. $presence = true;
  346. $my_presence = true;
  347. }
  348. return array(
  349. 'id' => $member->id,
  350. 'first_name' => $member->first_name,
  351. 'last_name' => $member->last_name,
  352. 'certificate' => $y,
  353. 'presence' => $presence,
  354. 'my_presence' => $my_presence,
  355. 'status' => $status,
  356. 'motivation' => $motivation,
  357. 'court' => $court,
  358. 'instructor' => $instructor,
  359. 'additional_instructor' => $additional_instructor,
  360. 'notes' => $notes,
  361. );
  362. }
  363. public function save($ids)
  364. {
  365. $this->saveAndStay($ids);
  366. $last_date = explode(" ", $this->calendar->from)[0];
  367. return redirect()->to("/calendar?last_date={$last_date}");
  368. }
  369. public function saveAndStay($ids)
  370. {
  371. $this->calendar->court_id = $this->court_id;
  372. $this->calendar->instructor_id = $this->instructor_id;
  373. $this->calendar->note = $this->note;
  374. if ($this->motivation_id != "" && $this->motivation_id != null)
  375. $this->calendar->motivation_id = $this->motivation_id;
  376. if ($this->motivation_manual_id != "" && $this->motivation_manual_id != null)
  377. $this->calendar->motivation_manual_id = $this->motivation_manual_id;
  378. $this->calendar->save();
  379. // $x = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->where('status', '<>', 99)->first();
  380. // $mid = null;
  381. // if ($x) {
  382. // $mid = $x->motivation_id;
  383. // $x->delete();
  384. // }
  385. // Mappa degli ultimi motivation_id per ogni member_id dell'utente e calendario correnti
  386. $userId = \Auth::user()->id;
  387. $calendarId = $this->calendar->id;
  388. $motivation_query = \App\Models\Presence::query()
  389. ->select('member_id', 'motivation_id', 'motivation_course_id')
  390. ->where('calendar_id', $calendarId)
  391. ->where('user_id', $userId)
  392. ->where('status', '<>', 99)
  393. ->whereIn('id', function ($q) use ($calendarId, $userId) {
  394. $q->selectRaw('MAX(id)')
  395. ->from('presences')
  396. ->where('calendar_id', $calendarId)
  397. ->where('user_id', $userId)
  398. ->where('status', '<>', 99)
  399. ->groupBy('member_id');
  400. })
  401. ->get()
  402. ->keyBy('member_id');
  403. $motivationMap = $motivation_query->map(fn($row) => $row->motivation_id)->toArray();
  404. $motivationCourseMap = $motivation_query->map(fn($row) => $row->motivation_course_id)->toArray();
  405. // Elimino tutti i dati correnti che devono essere sostituiti
  406. \App\Models\Presence::query()
  407. ->where('calendar_id', $calendarId)
  408. ->where('user_id', $userId)
  409. ->where('status', '<>', 99)
  410. ->delete();
  411. // Ricreo le presenze per ogni membro contenuto in $ids
  412. foreach ($ids as $id) {
  413. $p = new \App\Models\Presence();
  414. $p->member_id = $id;
  415. $p->calendar_id = $calendarId;
  416. // Se per quel membro esisteva un motivation_id, lo riuso, altrimenti lo lascio null
  417. $p->motivation_id = $motivationMap[$id] ?? null;
  418. $p->motivation_course_id = $motivationCourseMap[$id] ?? null;
  419. $p->user_id = $userId;
  420. $p->status = 0;
  421. // Salvo eventuale court_id (se presente e maggiore di 0)
  422. if ($this->save_court_id > 0) {
  423. $p->court_id = $this->save_court_id;
  424. }
  425. // Salvo eventuale instructor_id (se presente e maggiore di 0)
  426. if ($this->save_instructor_id > 0) {
  427. $p->instructor_id = $this->save_instructor_id;
  428. }
  429. // Salvo eventuali note (se non vuote)
  430. if ($this->save_notes != '') {
  431. $p->notes = $this->save_notes;
  432. }
  433. $p->save();
  434. }
  435. $this->emit('setSaving');
  436. }
  437. public function cancel($ids, $motivation_id)
  438. {
  439. $presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->whereIn('member_id', $ids)->get();
  440. foreach ($presences as $presence) {
  441. $presence->motivation_id = $motivation_id;
  442. $presence->status = 99;
  443. $presence->save();
  444. }
  445. return redirect()->to('/presences?calendarId=' . $this->calendar->id);
  446. }
  447. public function addMember($ids)
  448. {
  449. $this->added = true;
  450. //if (!in_array($id, $this->newMembers))
  451. // $this->newMembers[] = $id;
  452. if (!is_array($ids)) {
  453. $ids = [$ids];
  454. }
  455. $this->member_ids = $ids;
  456. $this->emit('reload');
  457. }
  458. public function cancelCalendar()
  459. {
  460. $this->calendar->motivation_id = $this->motivation_id;
  461. $this->calendar->status = 99;
  462. $this->calendar->save();
  463. return redirect()->to('/calendar');
  464. }
  465. public function createMember()
  466. {
  467. if (!$this->added) {
  468. $this->newMemberFiscalCodeExist = false;
  469. $this->validate([
  470. "newMemberMotivationId" => 'required',
  471. ]);
  472. /*$this->validate([
  473. // 'newMemberFiscalCode'=>'required|max:16',
  474. 'newMemberFirstName'=>'required',
  475. 'newMemberLastName'=>'required',
  476. //'newMemberEmail'=>'required',
  477. ]);*/
  478. // Check fiscal code exist
  479. $exist = false;
  480. if ($this->newMemberFiscalCode != '') {
  481. $check = \App\Models\Member::where('fiscal_code', $this->newMemberFiscalCode)->get();
  482. $exist = $check->count() > 0;
  483. }
  484. if (!$exist) {
  485. $member = \App\Models\Member::create([
  486. 'first_name' => strtoupper($this->newMemberFirstName),
  487. 'last_name' => strtoupper($this->newMemberLastName),
  488. 'email' => strtoupper($this->newMemberEmail),
  489. 'to_complete' => true,
  490. 'fiscal_code' => $this->newMemberFiscalCode,
  491. 'status' => true
  492. ]);
  493. //if (!in_array($member->id, $this->newMembers))
  494. // $this->newMembers[] = $member->id;
  495. $p = new \App\Models\Presence();
  496. $p->member_id = $member->id;
  497. $p->calendar_id = $this->calendar->id;
  498. $p->motivation_id = $this->newMemberMotivationId;
  499. $p->motivation_course_id = $this->motivation_course_id;
  500. $p->user_id = \Auth::user()->id;
  501. $p->status = 0;
  502. $p->court_id = null;
  503. $p->instructor_id = null;
  504. $p->notes = null;
  505. $p->save();
  506. $this->emit('reload');
  507. $this->emit('saved');
  508. /*$this->newMemberFirstName = '';
  509. $this->newMemberLastName = '';
  510. $this->newMemberEmail = '';
  511. $this->newMemberFiscalCode = '';*/
  512. } else {
  513. $this->newMemberFiscalCodeExist = true;
  514. }
  515. } else {
  516. if ($this->member_ids != null) {
  517. $this->validate([
  518. "newMemberMotivationId" => 'required',
  519. ]);
  520. $validator = Validator::make(
  521. [
  522. 'motivation_course_id' => $this->motivation_course_id,
  523. ],
  524. [
  525. 'motivation_course_id' => 'nullable|integer',
  526. ]
  527. );
  528. foreach ($this->member_ids as $m) {
  529. $validator->after(function ($validator) use ($m) {
  530. if (!$this->motivation_course_id) {
  531. return;
  532. }
  533. $exists = \App\Models\MemberCourse::where('member_id', $m)->where('course_id', $this->motivation_course_id)->exists();
  534. if (!$exists) {
  535. $validator->errors()->add(
  536. 'motivation_course_id',
  537. 'Il corso selezionato non è associato a questo utente.'
  538. );
  539. }
  540. });
  541. $validator->validate();
  542. //if ($this->manual)
  543. //{
  544. //\App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->where('status', '<>', 99)->delete();
  545. //foreach($ids as $id)
  546. //{
  547. $p = new \App\Models\Presence();
  548. $p->member_id = $m;
  549. $p->calendar_id = $this->calendar->id;
  550. $p->motivation_id = $this->newMemberMotivationId;
  551. $p->motivation_course_id = $this->motivation_course_id;
  552. $p->user_id = \Auth::user()->id;
  553. $p->status = 0;
  554. $p->court_id = null;
  555. $p->instructor_id = null;
  556. $p->notes = null;
  557. $p->save();
  558. //}
  559. /*}
  560. else
  561. {
  562. if (!in_array($m, $this->newMembers))
  563. $this->newMembers[] = $m;
  564. }*/
  565. }
  566. }
  567. //$this->member_id = 0;
  568. $this->member_ids = [];
  569. $this->added = false;
  570. $this->emit('reload');
  571. $this->emit('saved');
  572. }
  573. $this->resetCreationFields();
  574. }
  575. public function resetCreationFields()
  576. {
  577. $this->insertUser = 'new';
  578. $this->motivation_course_id = null;
  579. $this->motivation_course_name = null;
  580. $this->motivation_course_level = null;
  581. $this->motivation_course_type = null;
  582. $this->motivation_course_frequency = null;
  583. $this->newMemberMotivationId = null;
  584. $this->newMemberFirstName = null;
  585. $this->newMemberLastName = null;
  586. $this->newMemberEmail = null;
  587. $this->newMemberFiscalCode = null;
  588. $this->emit('resetCreationForm');
  589. }
  590. public function createInstructor()
  591. {
  592. $user = \App\Models\User::create([
  593. 'name' => $this->userName,
  594. 'email' => $this->userEmail,
  595. 'password' => '',
  596. 'level' => 2,
  597. 'enabled' => true
  598. ]);
  599. $this->instructor_id = $user->id;
  600. $this->instructors = \App\Models\User::select('*')->where('level', 2)->where('enabled', true)->orderBy('name', 'asc')->get();
  601. $this->emit('saved');
  602. }
  603. public function removeSingle($id)
  604. {
  605. \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('member_id', $id)->delete();
  606. $this->emit('reload');
  607. }
  608. public function revert($id)
  609. {
  610. $p = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('member_id', $id)->first();
  611. $p->motivation_id = null;
  612. $p->status = 0;
  613. $p->save();
  614. $this->emit('reload');
  615. }
  616. }