Presence.php 28 KB

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