Presence.php 32 KB

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