| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- <?php
- namespace App\Http\Livewire;
- use Livewire\Component;
- class Presence extends Component
- {
- public $calendar;
- public $records;
- public $member_ids = [];
- public $court_id, $instructor_id, $motivation_id, $motivation_manual_id, $note, $manual;
- public $newMemberFirstName, $newMemberLastName, $newMemberEmail, $newMemberToComplete, $newMemberFiscalCode, $newMemberFiscalCodeExist, $newMemberMotivationId;
- public $userName, $userEmail;
- public $added = false;
- public $filter = '';
- public $courts = [];
- public $instructors = [];
- public $motivations = [];
- public $motivations_add = [];
- public $members = [];
- public $newMembers = [];
- public $ids = [];
- public function mount()
- {
- setlocale(LC_ALL, 'it_IT');
- $this->calendar = \App\Models\Calendar::findOrFail($_GET["calendarId"]);
- $this->court_id = $this->calendar->court_id;
- $this->instructor_id = $this->calendar->instructor_id;
- $this->motivation_manual_id = $this->calendar->motivation_manual_id;
- $this->manual = $this->calendar->manual;
- $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])->orderBy('last_name')->orderBy('first_name')->get();
- $this->note = $this->calendar->note;
- $this->courts = \App\Models\Court::select('*')->where('enabled', true)->get();
- $this->instructors = \App\Models\User::select('*')->where('level', 2)->where('enabled', true)->get();
- $this->motivations = \App\Models\Motivation::select('*')->where('enabled', true)->where('type', 'del')->get();
- $this->motivations_add = \App\Models\Motivation::select('*')->where('enabled', true)->where('type', 'add')->get();
-
- }
- public function updatedNewMemberMotivationId() {
- $this->emit('reload');
- }
- public function render()
- {
-
- $this->records = [];
- setlocale(LC_ALL, 'it_IT');
- $presenceMembers = [];
- if (!$this->manual)
- {
-
- // Carco tutti gli iscritti a un corso padre in quel giorno con un range orario simile
- $days = ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'];
- $dow = date('w', strtotime($this->calendar->from));
- $d = $days[$dow];
- // Elenco corsi per tipologia in base al calendario
- $courses = \App\Models\Course::where('name', $this->calendar->name)->where('date_from', '<=', $this->calendar->from)->where('date_to', '>=', $this->calendar->to)->pluck('id')->toArray();
- // Elenco utenti iscritti al corso "padre"
- $members_courses = \App\Models\MemberCourse::where('when', 'like', "%" . $d . "%")->whereIn('course_id', $courses)->pluck('member_id')->toArray();
- if ($this->filter != '')
- {
- $filter = $this->filter;
- $members = \App\Models\Member::whereIn('id', $members_courses)->where(function ($query) use ($filter) {
- $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $filter . "%'")
- ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $filter . "%'");
- })->orderBy('last_name')->orderBy('first_name')->get();
- }
- else
- $members = \App\Models\Member::whereIn('id', $members_courses)->orderBy('last_name')->orderBy('first_name')->get();
- // $presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->pluck('member_id')->toArray();
- // $my_presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->pluck('member_id')->toArray();
- foreach($members as $member)
- {
- $presenceMembers[] = $member->id;
- //$this->member_ids[] = $member->id;
- $this->records[] = $this->getMember($member);
- }
- }
- // Aggiungo i membri iscritti
- $members_presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->whereNotIn('member_id', $presenceMembers)->pluck('member_id')->toArray();
- if ($this->filter != '')
- {
- $filter = $this->filter;
- $members = \App\Models\Member::whereIn('id', $members_presences)->where(function ($query) use ($filter) {
- $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $filter . "%'")
- ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $filter . "%'");
- })->get();
- }
- else
- $members = \App\Models\Member::whereIn('id', $members_presences)->get();
- foreach($members as $member)
- {
- //$this->member_ids[] = $member->id;
- $this->records[] = $this->getMember($member);
- }
- foreach($this->newMembers as $m)
- {
- $member = \App\Models\Member::findOrFail($m);
- //$this->member_ids[] = $member->id;
- $this->records[] = $this->getMember($member);
- }
- /*$calendars = \App\Models\Calendar::get();
- foreach($calendars as $c)
- {
- $this->records[] = array('id' => $c->id, 'title' => $c->course->name, 'start' => $c->from, 'end' => $c->to);
- }*/
- return view('livewire.presence');
- }
- public function getDateX()
- {
- setlocale(LC_ALL, 'it_IT');
- $days = ['Domenica', 'Lunedì', 'Martedì', 'Mercoledì', 'Giovedì', 'Venerdì', 'Sabato'];
- $months = ['Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno', 'Luglio', 'Agosto', 'Settembre', 'Ottobre', 'Novembre', 'Dicembre'];
- return $days[date('w', strtotime($this->calendar->from))] . " " . date("d", strtotime($this->calendar->from)) . " " . $months[date("n", strtotime($this->calendar->from)) - 1];
- }
- public function getMember($member)
- {
- $latestCert = \App\Models\MemberCertificate::where('member_id', $member->id)
- ->orderBy('expire_date', 'desc')
- ->first();
- $certificate = '';
- $y = "|";
- if ($latestCert)
- {
- $latest_date = $latestCert->expire_date;
- $status = '';
- if ($latest_date < date("Y-m-d")) {
- $status = "0"; // Expired
- } else if ($latest_date <= date("Y-m-d", strtotime("+1 month"))) {
- $status = "1"; // Expiring soon
- } else {
- $status = "2"; // Valid
- }
- $y = $status . "|" . date("d/m/Y", strtotime($latest_date));
- }
- $presence = false;
- $my_presence = false;
- $status = 0;
- $has_presence = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('member_id', $member->id)->first();
- if ($has_presence)
- {
- $presence = true;
- $my_presence = $has_presence->user_id == \Auth::user()->id;
- $status = $has_presence->status;
- }
- if (in_array($member->id, $this->newMembers))
- {
- $presence = true;
- $my_presence = true;
- }
- return array('id' => $member->id, 'first_name' => $member->first_name, 'last_name' => $member->last_name, 'certificate' => $y, 'presence' => $presence, 'my_presence' => $my_presence, 'status' => $status);
- }
- public function save($ids)
- {
- $this->calendar->court_id = $this->court_id;
- $this->calendar->instructor_id = $this->instructor_id;
- $this->calendar->note = $this->note;
- if ($this->motivation_id != "" && $this->motivation_id != null)
- $this->calendar->motivation_id = $this->motivation_id;
- if ($this->motivation_manual_id != "" && $this->motivation_manual_id != null)
- $this->calendar->motivation_manual_id = $this->motivation_manual_id;
- $this->calendar->save();
- \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->where('status', '<>', 99)->delete();
- foreach($ids as $id)
- {
- $p = new \App\Models\Presence();
- $p->member_id = $id;
- $p->calendar_id = $this->calendar->id;
- $p->user_id = \Auth::user()->id;
- $p->status = 0;
- $p->save();
- }
- $this->emit('setSaving');
- return redirect()->to('/calendar');
- }
- public function cancel($ids, $motivation_id)
- {
- $presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->whereIn('member_id', $ids)->get();
- foreach($presences as $presence)
- {
- $presence->motivation_id = $motivation_id;
- $presence->status = 99;
- $presence->save();
- }
- return redirect()->to('/presences?calendarId=' . $this->calendar->id);
- }
- public function addMember($ids)
- {
- $this->added = true;
- //if (!in_array($id, $this->newMembers))
- // $this->newMembers[] = $id;
-
- $this->member_ids = $ids;
- $this->emit('reload');
- }
- public function cancelCalendar()
- {
- $this->calendar->motivation_id = $this->motivation_id;
- $this->calendar->status = 99;
- $this->calendar->save();
- return redirect()->to('/calendar');
- }
- public function createMember()
- {
- if (!$this->added)
- {
- $this->newMemberFiscalCodeExist = false;
- /*$this->validate([
- // 'newMemberFiscalCode'=>'required|max:16',
- 'newMemberFirstName'=>'required',
- 'newMemberLastName'=>'required',
- //'newMemberEmail'=>'required',
- ]);*/
- // Check fiscal code exist
- $exist = false;
- if ($this->newMemberFiscalCode != '')
- {
- $check = \App\Models\Member::where('fiscal_code', $this->newMemberFiscalCode)->get();
- $exist = $check->count() > 0;
- }
- if (!$exist)
- {
- $member = \App\Models\Member::create([
- 'first_name' => strtoupper($this->newMemberFirstName),
- 'last_name' => strtoupper($this->newMemberLastName),
- 'email' => strtoupper($this->newMemberEmail),
- 'to_complete' => true,
- 'fiscal_code' => $this->newMemberFiscalCode,
- 'status' => true
- ]);
- if (!in_array($member->id, $this->newMembers))
- $this->newMembers[] = $member->id;
- $this->emit('reload');
- $this->emit('saved');
- /*$this->newMemberFirstName = '';
- $this->newMemberLastName = '';
- $this->newMemberEmail = '';
- $this->newMemberFiscalCode = '';*/
- }
- else
- {
- $this->newMemberFiscalCodeExist = true;
- }
- }
- else
- {
- if ($this->member_ids != null)
- {
- foreach($this->member_ids as $m)
- {
- if ($this->manual)
- {
- //\App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->where('status', '<>', 99)->delete();
- //foreach($ids as $id)
- //{
- $p = new \App\Models\Presence();
- $p->member_id = $m;
- $p->calendar_id = $this->calendar->id;
- $p->motivation_id = $this->newMemberMotivationId;
- $p->user_id = \Auth::user()->id;
- $p->status = 0;
- $p->save();
- //}
- }
- else
- {
- if (!in_array($m, $this->newMembers))
- $this->newMembers[] = $m;
- }
- }
- }
- //$this->member_id = 0;
- $this->member_ids = [];
- $this->added = false;
-
- $this->emit('reload');
- $this->emit('saved');
- }
- }
- public function createInstructor()
- {
-
- $user = \App\Models\User::create([
- 'name' => $this->userName,
- 'email' => $this->userEmail,
- 'password' => '',
- 'level' => 2,
- 'enabled' => true
- ]);
- $this->instructor_id = $user->id;
- $this->instructors = \App\Models\User::select('*')->where('level', 2)->where('enabled', true)->get();
- $this->emit('saved');
- }
- public function removeSingle($id)
- {
- \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('member_id', $id)->delete();
- $this->emit('reload');
- }
- public function revert($id)
- {
- $p = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('member_id', $id)->first();
- $p->status = 0;
- $p->save();
- $this->emit('reload');
- }
- }
|