Presence.php 32 KB

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