Presence.php 30 KB

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