Presence.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class Presence extends Component
  5. {
  6. public $calendar;
  7. public $records;
  8. public $member_ids = [];
  9. public $court_id, $instructor_id, $motivation_id, $motivation_manual_id, $note, $manual;
  10. public $save_court_id, $save_instructor_id, $save_notes;
  11. public $newMemberFirstName, $newMemberLastName, $newMemberEmail, $newMemberToComplete, $newMemberFiscalCode, $newMemberFiscalCodeExist, $newMemberMotivationId;
  12. public $userName, $userEmail;
  13. public $added = false;
  14. public $filter = '';
  15. public $courts = [];
  16. public $instructors = [];
  17. public $motivations = [];
  18. public $motivations_add = [];
  19. public $insertUser = 'exist';
  20. public $course_limit;
  21. public $motivation_course_id = null;
  22. public $motivation_course_name = 0;
  23. public $motivation_course_level = 0;
  24. public $motivation_course_type = 0;
  25. public $motivation_course_frequency = 0;
  26. public $course_names = [];
  27. public $course_levels = [];
  28. public $course_types = [];
  29. public $course_frequencies = [];
  30. public $members = [];
  31. public $newMembers = [];
  32. public $ids = [];
  33. public function mount()
  34. {
  35. setlocale(LC_ALL, 'it_IT');
  36. $this->calendar = \App\Models\Calendar::findOrFail($_GET["calendarId"]);
  37. $this->court_id = $this->calendar->court_id;
  38. $this->instructor_id = $this->calendar->instructor_id;
  39. $this->motivation_manual_id = $this->calendar->motivation_manual_id;
  40. $this->manual = $this->calendar->manual;
  41. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])
  42. ->where(function($query) {
  43. $query->where('is_archived', false)
  44. ->orWhereNull('is_archived');
  45. })
  46. ->where(function($query) {
  47. $query->where('is_deleted', false)
  48. ->orWhereNull('is_deleted');
  49. })->orderBy('last_name')->orderBy('first_name')->get();
  50. $this->note = $this->calendar->note;
  51. $this->courts = \App\Models\Court::select('*')->where('enabled', true)->get();
  52. $this->instructors = \App\Models\User::select('*')->where('level', 2)->where('enabled', true)->orderBy('name', 'asc')->get();
  53. $this->motivations = \App\Models\Motivation::select('*')->where('enabled', true)->where('type', 'del')->get();
  54. $this->motivations_add = \App\Models\Motivation::select('*')->where('enabled', true)->where('type', 'add')->get();
  55. $this->save_court_id = 0;
  56. $this->save_instructor_id = 0;
  57. $this->save_notes = '';
  58. $this->insertUser = 'exist';
  59. $this->course_limit = now()->endOfDay();
  60. $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');
  61. $this->course_levels = [];
  62. $this->course_types = [];
  63. $this->course_frequencies = [];
  64. }
  65. function updatedMotivationCourseName() {
  66. if ($this->motivation_course_name > 0 && $this->motivation_course_name != '') {
  67. $this->motivation_course_id = null;
  68. $this->motivation_course_level = 0;
  69. $this->motivation_course_type = 0;
  70. $this->motivation_course_frequency = 0;
  71. $levels_ids = [];
  72. $levels = \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)->get();
  73. foreach ($levels as $l) {
  74. $levels_ids[] = $l->course_level_id;
  75. }
  76. $this->course_levels = \App\Models\CourseLevel::where('enabled', true)->whereIn('id', $levels_ids)->orderBy('name')->get();
  77. $this->course_types = [];
  78. $this->course_frequencies = [];
  79. } else {
  80. $this->course_levels = [];
  81. $this->course_types = [];
  82. $this->course_frequencies = [];
  83. }
  84. }
  85. function updatedMotivationCourseLevel() {
  86. if ($this->motivation_course_name > 0 && $this->motivation_course_name != '' && $this->motivation_course_level > 0 && $this->motivation_course_level != '') {
  87. $this->motivation_course_id = null;
  88. $this->motivation_course_type = 0;
  89. $this->motivation_course_frequency = 0;
  90. $type_ids = [];
  91. $types = \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)->get();
  92. foreach ($types as $t) {
  93. $type_ids[] = $t->course_type_id;
  94. }
  95. $this->course_types = \App\Models\CourseType::where('enabled', true)->whereIn('id', $type_ids)->orderBy('name')->get();
  96. $this->course_frequencies = [];
  97. } else {
  98. $this->course_types = [];
  99. $this->course_frequencies = [];
  100. }
  101. }
  102. function updatedMotivationCourseType() {
  103. 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 != '') {
  104. $this->motivation_course_id = null;
  105. $this->motivation_course_frequency = 0;
  106. $frequency_ids = [];
  107. $frequencies = \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)->get();
  108. foreach ($frequencies as $f) {
  109. $frequency_ids[] = $f->course_frequency_id;
  110. }
  111. $this->course_frequencies = \App\Models\CourseFrequency::where('enabled', true)->whereIn('id', $frequency_ids)->orderBy('name')->get();
  112. } else {
  113. $this->course_frequencies = [];
  114. }
  115. }
  116. function updatedMotivationCourseFrequency() {
  117. 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 != '') {
  118. $this->motivation_course_id = null;
  119. $course = \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)->first();
  120. $this->motivation_course_id = $course->id;
  121. } else {
  122. $this->motivation_course_id = null;
  123. }
  124. }
  125. public function updatedNewMemberMotivationId()
  126. {
  127. $this->emit('reload');
  128. }
  129. public function render()
  130. {
  131. $this->records = [];
  132. setlocale(LC_ALL, 'it_IT');
  133. $presenceMembers = [];
  134. if (!$this->manual) {
  135. // Carco tutti gli iscritti a un corso padre in quel giorno con un range orario simile
  136. $days = ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'];
  137. $dow = date('w', strtotime($this->calendar->from));
  138. $d = $days[$dow];
  139. $h = date('H:i', strtotime($this->calendar->from));
  140. // Elenco corsi per tipologia in base al calendario
  141. $courses = \App\Models\Course::where('name', $this->calendar->name)->where('date_from', '<=', $this->calendar->from)->where('date_to', '>=', $this->calendar->to)->pluck('id')->toArray();
  142. $months = date("n", strtotime($this->calendar->from));
  143. // Elenco utenti iscritti al corso "padre"
  144. // $members_courses = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")
  145. // ->where('when', 'like', '%"from":"' . $h . '"%')
  146. // ->whereNot('months', 'like', '%"m":' . $months . ',"status":2%')
  147. // ->whereDate('date_from', '<=', $this->calendar->from)
  148. // ->whereDate('date_to', '>=', $this->calendar->from)
  149. // ->whereIn('course_id', $courses)
  150. // ->pluck('member_id')->toArray();
  151. // $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();
  152. $members_courses = \App\Models\MemberCourse::query()
  153. ->whereRaw("JSON_CONTAINS(`when`, JSON_OBJECT('day', JSON_ARRAY(?), 'from', ?), '$')", [$d, $h])
  154. ->whereRaw("JSON_CONTAINS(months, JSON_OBJECT('m', CAST(? AS UNSIGNED)), '$')", [$months])
  155. ->whereRaw("NOT JSON_CONTAINS(months, JSON_OBJECT('m', CAST(? AS UNSIGNED), 'status', 2), '$')", [$months])
  156. ->whereRaw("NOT JSON_CONTAINS(months, JSON_OBJECT('m', CAST(? AS UNSIGNED), 'status', '2'), '$')", [$months])
  157. ->whereIn('course_id', $courses)
  158. ->pluck('member_id')
  159. ->toArray();
  160. if ($this->filter != '') {
  161. $filter = $this->filter;
  162. $members = \App\Models\Member::whereIn('id', $members_courses)->where(function ($query) use ($filter) {
  163. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $filter . "%'")
  164. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $filter . "%'");
  165. })
  166. ->where(function($query) {
  167. $query->where('is_archived', false)
  168. ->orWhereNull('is_archived');
  169. })
  170. ->where(function($query) {
  171. $query->where('is_deleted', false)
  172. ->orWhereNull('is_deleted');
  173. })->orderBy('last_name')->orderBy('first_name')->get();
  174. } else
  175. $members = \App\Models\Member::whereIn('id', $members_courses)
  176. ->where(function($query) {
  177. $query->where('is_archived', false)
  178. ->orWhereNull('is_archived');
  179. })
  180. ->where(function($query) {
  181. $query->where('is_deleted', false)
  182. ->orWhereNull('is_deleted');
  183. })->orderBy('last_name')->orderBy('first_name')->get();
  184. // $presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->pluck('member_id')->toArray();
  185. // $my_presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->pluck('member_id')->toArray();
  186. foreach ($members as $member) {
  187. $presenceMembers[] = $member->id;
  188. //$this->member_ids[] = $member->id;
  189. $this->records[] = $this->getMember($member);
  190. }
  191. }
  192. // Aggiungo i membri iscritti
  193. $members_presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->whereNotIn('member_id', $presenceMembers)->pluck('member_id')->toArray();
  194. if ($this->filter != '') {
  195. $filter = $this->filter;
  196. $members = \App\Models\Member::whereIn('id', $members_presences)->where(function ($query) use ($filter) {
  197. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $filter . "%'")
  198. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $filter . "%'");
  199. })
  200. ->where(function($query) {
  201. $query->where('is_archived', false)
  202. ->orWhereNull('is_archived');
  203. })
  204. ->where(function($query) {
  205. $query->where('is_deleted', false)
  206. ->orWhereNull('is_deleted');
  207. })->get();
  208. } else
  209. $members = \App\Models\Member::whereIn('id', $members_presences)
  210. ->where(function($query) {
  211. $query->where('is_archived', false)
  212. ->orWhereNull('is_archived');
  213. })
  214. ->where(function($query) {
  215. $query->where('is_deleted', false)
  216. ->orWhereNull('is_deleted');
  217. })->get();
  218. foreach ($members as $member) {
  219. //$this->member_ids[] = $member->id;
  220. $this->records[] = $this->getMember($member);
  221. }
  222. foreach ($this->newMembers as $m) {
  223. $member = \App\Models\Member::findOrFail($m);
  224. //$this->member_ids[] = $member->id;
  225. $this->records[] = $this->getMember($member);
  226. }
  227. /*$calendars = \App\Models\Calendar::get();
  228. foreach($calendars as $c)
  229. {
  230. $this->records[] = array('id' => $c->id, 'title' => $c->course->name, 'start' => $c->from, 'end' => $c->to);
  231. }*/
  232. return view('livewire.presence');
  233. }
  234. public function getDateX()
  235. {
  236. setlocale(LC_ALL, 'it_IT');
  237. $days = ['Domenica', 'Lunedì', 'Martedì', 'Mercoledì', 'Giovedì', 'Venerdì', 'Sabato'];
  238. $months = ['Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno', 'Luglio', 'Agosto', 'Settembre', 'Ottobre', 'Novembre', 'Dicembre'];
  239. return $days[date('w', strtotime($this->calendar->from))] . " " . date("d", strtotime($this->calendar->from)) . " " . $months[date("n", strtotime($this->calendar->from)) - 1];
  240. }
  241. public function getMember($member)
  242. {
  243. $latestCert = \App\Models\MemberCertificate::where('member_id', $member->id)
  244. ->orderBy('expire_date', 'desc')
  245. ->first();
  246. $certificate = '';
  247. $y = "|";
  248. if ($latestCert) {
  249. $latest_date = $latestCert->expire_date;
  250. $status = '';
  251. if ($latest_date < date("Y-m-d")) {
  252. $status = "0"; // Expired
  253. } else if ($latest_date <= date("Y-m-d", strtotime("+1 month"))) {
  254. $status = "1"; // Expiring soon
  255. } else {
  256. $status = "2"; // Valid
  257. }
  258. $y = $status . "|" . date("d/m/Y", strtotime($latest_date));
  259. }
  260. $presence = false;
  261. $my_presence = false;
  262. $motivation = '';
  263. $status = 0;
  264. $court = '';
  265. $instructor = '';
  266. $additional_instructor = '';
  267. $notes = '';
  268. $has_presence = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('member_id', $member->id)->first();
  269. if ($has_presence) {
  270. $presence = true;
  271. $my_presence = $has_presence->user_id == \Auth::user()->id;
  272. if ($has_presence->motivation_id > 0) {
  273. $motivation = \App\Models\Motivation::findOrFail($has_presence->motivation_id)->name;
  274. }
  275. $status = $has_presence->status;
  276. $instructor = \App\Models\User::findOrFail($has_presence->user_id)->name;
  277. if ($has_presence->court_id > 0) {
  278. $court = \App\Models\Court::findOrFail($has_presence->court_id)->name;
  279. }
  280. if ($has_presence->instructor_id > 0 && $has_presence->instructor_id !== $has_presence->user_id) {
  281. $additional_instructor = \App\Models\User::findOrFail($has_presence->instructor_id)->name;
  282. }
  283. if (!is_null($has_presence->notes)) {
  284. $notes = $has_presence->notes;
  285. }
  286. }
  287. if (in_array($member->id, $this->newMembers)) {
  288. $presence = true;
  289. $my_presence = true;
  290. }
  291. return array(
  292. 'id' => $member->id,
  293. 'first_name' => $member->first_name,
  294. 'last_name' => $member->last_name,
  295. 'certificate' => $y,
  296. 'presence' => $presence,
  297. 'my_presence' => $my_presence,
  298. 'status' => $status,
  299. 'motivation' => $motivation,
  300. 'court' => $court,
  301. 'instructor' => $instructor,
  302. 'additional_instructor' => $additional_instructor,
  303. 'notes' => $notes,
  304. );
  305. }
  306. public function save($ids)
  307. {
  308. $this->saveAndStay($ids);
  309. $last_date = explode(" ", $this->calendar->from)[0];
  310. return redirect()->to("/calendar?last_date={$last_date}");
  311. }
  312. public function saveAndStay($ids)
  313. {
  314. $this->calendar->court_id = $this->court_id;
  315. $this->calendar->instructor_id = $this->instructor_id;
  316. $this->calendar->note = $this->note;
  317. if ($this->motivation_id != "" && $this->motivation_id != null)
  318. $this->calendar->motivation_id = $this->motivation_id;
  319. if ($this->motivation_manual_id != "" && $this->motivation_manual_id != null)
  320. $this->calendar->motivation_manual_id = $this->motivation_manual_id;
  321. $this->calendar->save();
  322. // $x = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->where('status', '<>', 99)->first();
  323. // $mid = null;
  324. // if ($x) {
  325. // $mid = $x->motivation_id;
  326. // $x->delete();
  327. // }
  328. // Mappa degli ultimi motivation_id per ogni member_id dell'utente e calendario correnti
  329. $userId = \Auth::user()->id;
  330. $calendarId = $this->calendar->id;
  331. $motivation_query = \App\Models\Presence::query()
  332. ->select('member_id', 'motivation_id', 'motivation_course_id')
  333. ->where('calendar_id', $calendarId)
  334. ->where('user_id', $userId)
  335. ->where('status', '<>', 99)
  336. ->whereIn('id', function ($q) use ($calendarId, $userId) {
  337. $q->selectRaw('MAX(id)')
  338. ->from('presences')
  339. ->where('calendar_id', $calendarId)
  340. ->where('user_id', $userId)
  341. ->where('status', '<>', 99)
  342. ->groupBy('member_id');
  343. })
  344. ->get()
  345. ->keyBy('member_id');
  346. $motivationMap = $motivation_query->map(fn($row) => $row->motivation_id)->toArray();
  347. $motivationCourseMap = $motivation_query->map(fn($row) => $row->motivation_course_id)->toArray();
  348. // Elimino tutti i dati correnti che devono essere sostituiti
  349. \App\Models\Presence::query()
  350. ->where('calendar_id', $calendarId)
  351. ->where('user_id', $userId)
  352. ->where('status', '<>', 99)
  353. ->delete();
  354. // Ricreo le presenze per ogni membro contenuto in $ids
  355. foreach ($ids as $id) {
  356. $p = new \App\Models\Presence();
  357. $p->member_id = $id;
  358. $p->calendar_id = $calendarId;
  359. // Se per quel membro esisteva un motivation_id, lo riuso, altrimenti lo lascio null
  360. $p->motivation_id = $motivationMap[$id] ?? null;
  361. $p->motivation_course_id = $motivationCourseMap[$id] ?? null;
  362. $p->user_id = $userId;
  363. $p->status = 0;
  364. // Salvo eventuale court_id (se presente e maggiore di 0)
  365. if ($this->save_court_id > 0) {
  366. $p->court_id = $this->save_court_id;
  367. }
  368. // Salvo eventuale instructor_id (se presente e maggiore di 0)
  369. if ($this->save_instructor_id > 0) {
  370. $p->instructor_id = $this->save_instructor_id;
  371. }
  372. // Salvo eventuali note (se non vuote)
  373. if ($this->save_notes != '') {
  374. $p->notes = $this->save_notes;
  375. }
  376. $p->save();
  377. }
  378. $this->emit('setSaving');
  379. }
  380. public function cancel($ids, $motivation_id)
  381. {
  382. $presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->whereIn('member_id', $ids)->get();
  383. foreach ($presences as $presence) {
  384. $presence->motivation_id = $motivation_id;
  385. $presence->status = 99;
  386. $presence->save();
  387. }
  388. return redirect()->to('/presences?calendarId=' . $this->calendar->id);
  389. }
  390. public function addMember($ids)
  391. {
  392. $this->added = true;
  393. //if (!in_array($id, $this->newMembers))
  394. // $this->newMembers[] = $id;
  395. $this->member_ids = $ids;
  396. $this->emit('reload');
  397. }
  398. public function cancelCalendar()
  399. {
  400. $this->calendar->motivation_id = $this->motivation_id;
  401. $this->calendar->status = 99;
  402. $this->calendar->save();
  403. return redirect()->to('/calendar');
  404. }
  405. public function createMember()
  406. {
  407. if (!$this->added) {
  408. $this->newMemberFiscalCodeExist = false;
  409. $this->validate([
  410. "newMemberMotivationId" => 'required',
  411. ]);
  412. /*$this->validate([
  413. // 'newMemberFiscalCode'=>'required|max:16',
  414. 'newMemberFirstName'=>'required',
  415. 'newMemberLastName'=>'required',
  416. //'newMemberEmail'=>'required',
  417. ]);*/
  418. // Check fiscal code exist
  419. $exist = false;
  420. if ($this->newMemberFiscalCode != '') {
  421. $check = \App\Models\Member::where('fiscal_code', $this->newMemberFiscalCode)->get();
  422. $exist = $check->count() > 0;
  423. }
  424. if (!$exist) {
  425. $member = \App\Models\Member::create([
  426. 'first_name' => strtoupper($this->newMemberFirstName),
  427. 'last_name' => strtoupper($this->newMemberLastName),
  428. 'email' => strtoupper($this->newMemberEmail),
  429. 'to_complete' => true,
  430. 'fiscal_code' => $this->newMemberFiscalCode,
  431. 'status' => true
  432. ]);
  433. //if (!in_array($member->id, $this->newMembers))
  434. // $this->newMembers[] = $member->id;
  435. $p = new \App\Models\Presence();
  436. $p->member_id = $member->id;
  437. $p->calendar_id = $this->calendar->id;
  438. $p->motivation_id = $this->newMemberMotivationId;
  439. $p->motivation_course_id = $this->motivation_course_id;
  440. $p->user_id = \Auth::user()->id;
  441. $p->status = 0;
  442. $p->court_id = null;
  443. $p->instructor_id = null;
  444. $p->notes = null;
  445. $p->save();
  446. $this->emit('reload');
  447. $this->emit('saved');
  448. /*$this->newMemberFirstName = '';
  449. $this->newMemberLastName = '';
  450. $this->newMemberEmail = '';
  451. $this->newMemberFiscalCode = '';*/
  452. } else {
  453. $this->newMemberFiscalCodeExist = true;
  454. }
  455. } else {
  456. if ($this->member_ids != null) {
  457. $this->validate([
  458. "newMemberMotivationId" => 'required',
  459. ]);
  460. foreach ($this->member_ids as $m) {
  461. //if ($this->manual)
  462. //{
  463. //\App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->where('status', '<>', 99)->delete();
  464. //foreach($ids as $id)
  465. //{
  466. $p = new \App\Models\Presence();
  467. $p->member_id = $m;
  468. $p->calendar_id = $this->calendar->id;
  469. $p->motivation_id = $this->newMemberMotivationId;
  470. $p->motivation_course_id = $this->motivation_course_id;
  471. $p->user_id = \Auth::user()->id;
  472. $p->status = 0;
  473. $p->court_id = null;
  474. $p->instructor_id = null;
  475. $p->notes = null;
  476. $p->save();
  477. //}
  478. /*}
  479. else
  480. {
  481. if (!in_array($m, $this->newMembers))
  482. $this->newMembers[] = $m;
  483. }*/
  484. }
  485. }
  486. //$this->member_id = 0;
  487. $this->member_ids = [];
  488. $this->added = false;
  489. $this->emit('reload');
  490. $this->emit('saved');
  491. }
  492. $this->resetCreationFields();
  493. }
  494. public function resetCreationFields() {
  495. $this->insertUser = 'new';
  496. $this->motivation_course_id = null;
  497. $this->motivation_course_name = null;
  498. $this->motivation_course_level = null;
  499. $this->motivation_course_type = null;
  500. $this->motivation_course_frequency = null;
  501. $this->newMemberMotivationId = null;
  502. $this->newMemberFirstName = null;
  503. $this->newMemberLastName = null;
  504. $this->newMemberEmail = null;
  505. $this->newMemberFiscalCode = null;
  506. $this->emit('resetCreationForm');
  507. }
  508. public function createInstructor()
  509. {
  510. $user = \App\Models\User::create([
  511. 'name' => $this->userName,
  512. 'email' => $this->userEmail,
  513. 'password' => '',
  514. 'level' => 2,
  515. 'enabled' => true
  516. ]);
  517. $this->instructor_id = $user->id;
  518. $this->instructors = \App\Models\User::select('*')->where('level', 2)->where('enabled', true)->orderBy('name', 'asc')->get();
  519. $this->emit('saved');
  520. }
  521. public function removeSingle($id)
  522. {
  523. \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('member_id', $id)->delete();
  524. $this->emit('reload');
  525. }
  526. public function revert($id)
  527. {
  528. $p = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('member_id', $id)->first();
  529. $p->motivation_id = null;
  530. $p->status = 0;
  531. $p->save();
  532. $this->emit('reload');
  533. }
  534. }