Presence.php 30 KB

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