Presence.php 30 KB

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