'required', 'last_name' => 'required', ]; public function resetFields(){ $this->dataId = -1; $this->first_name = ''; $this->last_name = ''; $this->status = ''; $this->birth_city_id = null; $this->birth_province_id = null; $this->birth_nation_id = null; $this->birth_date = null; $this->gender = ''; $this->fiscal_code = ''; $this->address = ''; $this->zip_code = ''; $this->nation_id = null; $this->province_id = null; $this->city_id = null; $this->phone = ''; $this->email = ''; $this->enabled = true; } public function resetCardFields(){ $this->card_card_id = null; $this->card_number = ''; $this->card_date = null; $this->card_accept_date = null; $this->card_expire_date = null; $this->card_status = 0; } public function resetCertificateFields(){ $this->certificate_type = ''; $this->certificate_filename = ''; $this->certificate_expire_date = null; $this->certificate_status = 0; } public function resetCategoryFields(){ $this->category_category_id = null; } public function getCategories($records, $indentation) { foreach($records as $record) { $this->categories[] = array('id' => $record->id, 'name' => str_repeat(" -> ", $indentation) . $record->name); if(count($record->childs)) $this->getCategories($record->childs, $indentation + 1); } } public function mount() { $this->cards = \App\Models\Card::select('id', 'name')->get(); $this->categories = array(); $this->getCategories(\App\Models\Category::select('id', 'name')->where('parent_id', null)->get(), 0); $this->nations = \App\Models\Nation::select('id', 'name')->get(); $this->provinces = array(); $this->cities = array(); $this->birthNations = \App\Models\Nation::select('id', 'name')->get(); $this->birthProvinces = array(); $this->birthCities = array(); } public function loadProvinces() { $this->provinces = \App\Models\Province::where('nation_id', $this->nation_id)->get(); $this->cities = array(); } public function loadCities() { $this->cities = \App\Models\City::where('province_id', $this->province_id)->get(); } public function loadBirthProvinces() { $this->birthProvinces = \App\Models\Province::where('nation_id', $this->birth_nation_id)->get(); $this->birthCities = array(); } public function loadBirthCities() { $this->birthCities = \App\Models\City::where('province_id', $this->birth_province_id)->get(); } public function render() { $this->records = \App\Models\Member::select('id', 'first_name', 'last_name', 'status', 'enabled')->get(); $this->loadMemberCards(); $this->loadMemberCategories(); return view('livewire.member'); } public function loadMemberCards() { $this->member_cards = \App\Models\MemberCard::where('member_id', $this->dataId)->get(); // return view('livewire.member'); } public function loadMemberCategories() { $this->member_categories = \App\Models\MemberCategory::where('member_id', $this->dataId)->get(); // return view('livewire.member'); } public function loadMemberCertificates() { $this->member_certificates = \App\Models\MemberCertificate::where('member_id', $this->dataId)->get(); // return view('livewire.member'); } public function add() { $this->resetFields(); $this->add = true; $this->update = false; } public function store($close) { $this->validate(); try { $member = \App\Models\Member::create([ 'first_name' => $this->first_name, 'last_name' => $this->last_name, 'status' => $this->status, 'birth_city_id' => $this->birth_city_id, 'birth_province_id' => $this->birth_province_id, 'birth_nation_id' => $this->birth_nation_id, 'birth_date' => $this->birth_date, 'gender' => $this->gender, 'fiscal_code' => $this->fiscal_code, 'address' => $this->address, 'zip_code' => $this->zip_code, 'nation_id' => $this->nation_id, 'province_id' => $this->province_id, 'city_id' => $this->city_id, 'phone' => $this->phone, 'email' => $this->email, 'enabled' => $this->enabled ]); session()->flash('success, Tesserato creato'); if ($close) { $this->resetFields(); $this->add = false; } else { $this->edit($member->id); } } catch (\Exception $ex) { session()->flash('error','Errore in fase di salvataggio'); } } public function edit($id){ try { $member = \App\Models\Member::findOrFail($id); if( !$member) { session()->flash('error','Tesserato non trovato'); } else { $this->first_name = $member->first_name; $this->last_name = $member->last_name; $this->status = $member->status; $this->birth_city_id = $member->birth_city_id; $this->birth_province_id = $member->birth_province_id; $this->birth_nation_id = $member->birth_nation_id; $this->birth_date = $member->birth_date; $this->gender = $member->gender; $this->fiscal_code = $member->fiscal_code; $this->address = $member->address; $this->zip_code = $member->zip_code; $this->nation_id = $member->nation_id; $this->province_id = $member->province_id; $this->city_id = $member->city_id; $this->phone = $member->phone; $this->email = $member->email; $this->enabled = $member->enabled; $this->dataId = $member->id; $this->provinces = \App\Models\Province::where('nation_id', $this->nation_id)->get(); $this->cities = \App\Models\City::where('province_id', $this->province_id)->get(); $this->birthProvinces = \App\Models\Province::where('nation_id', $this->birth_nation_id)->get(); $this->birthCities = \App\Models\City::where('province_id', $this->birth_province_id)->get(); $this->update = true; $this->add = false; } } catch (\Exception $ex) { session()->flash('error','Errore'); } } public function update() { $this->validate(); try { \App\Models\Member::whereId($this->dataId)->update([ 'first_name' => $this->first_name, 'last_name' => $this->last_name, 'status' => $this->status, 'birth_city_id' => $this->birth_city_id, 'birth_province_id' => $this->birth_province_id, 'birth_nation_id' => $this->birth_nation_id, 'birth_date' => $this->birth_date, 'gender' => $this->gender, 'fiscal_code' => $this->fiscal_code, 'address' => $this->address, 'zip_code' => $this->zip_code, 'nation_id' => $this->nation_id, 'province_id' => $this->province_id, 'city_id' => $this->city_id, 'phone' => $this->phone, 'email' => $this->email, 'enabled' => $this->enabled ]); session()->flash('success','Tesserato aggiornato'); $this->resetFields(); $this->update = false; } catch (\Exception $ex) { session()->flash('success','Errore'); } } public function cancel() { $this->add = false; $this->update = false; $this->resetFields(); } public function delete($id) { try{ \App\Models\Member::find($id)->delete(); session()->flash('success',"Tesserato eliminato"); }catch(\Exception $e){ session()->flash('error',"Errore"); } } // Card public function addCard() { $this->resetCardFields(); $this->addCard = true; $this->updateCard = false; } public function storeCard() { $this->validate(['card_card_id' => 'required']); try { \App\Models\MemberCard::create([ 'member_id' => $this->dataId, 'card_id' => $this->card_card_id, 'number' => $this->card_number, 'date' => $this->card_date, 'accept_date' => $this->card_accept_date, 'expire_date' => $this->card_expire_date, 'status' => $this->card_status ]); session()->flash('success, Tesserato creato'); $this->resetCardFields(); $this->addCard = false; } catch (\Exception $ex) { session()->flash('error','Errore in fase di salvataggio'); } } public function editCard($id){ try { $memberCard = \App\Models\MemberCard::findOrFail($id); if( !$memberCard) { session()->flash('error','Tesserato non trovato'); } else { $this->card_card_id = $memberCard->card_id; $this->card_number = $memberCard->number; $this->card_date = $memberCard->date; $this->card_accept_date = $memberCard->accept_date; $this->card_expire_date = $memberCard->expire_date; $this->card_status = $memberCard->status; $this->cardDataId = $memberCard->id; $this->updateCard = true; $this->addCard = false; } } catch (\Exception $ex) { session()->flash('error','Errore'); } } public function updateCard() { //$this->validate(); try { \App\Models\MemberCard::whereId($this->dataId)->update([ 'member_id' => $this->dataId, 'card_id' => $this->card_card_id, 'number' => $this->card_number, 'date' => $this->card_date, 'accept_date' => $this->card_accept_date, 'expire_date' => $this->card_expire_date, 'status' => $this->card_status ]); session()->flash('success','Tesserato aggiornato'); $this->resetCardFields(); $this->updateCard = false; } catch (\Exception $ex) { session()->flash('success','Errore'); } } public function cancelCard() { $this->addCard = false; $this->updateCard = false; $this->resetCardFields(); } public function deleteCard($id) { try{ \App\Models\MemberCard::find($id)->delete(); session()->flash('success',"Tesserato eliminato"); }catch(\Exception $e){ session()->flash('error',"Errore"); } } // Certificates public function addCertificate() { $this->resetCertificateFields(); $this->addCertificate = true; $this->updateCertificate = false; } public function storeCertificate() { // $this->validate(); try { \App\Models\MemberCertificate::create([ 'member_id' => $this->dataId, 'type' => $this->certificate_type, 'filename' => $this->certificate_filename, 'expire_date' => $this->certificate_expire_date, 'status' => $this->certificate_status ]); session()->flash('success, Tesserato creato'); $this->resetCertificateFields(); $this->addCertificate = false; } catch (\Exception $ex) { session()->flash('error','Errore in fase di salvataggio'); } } public function editCertificate($id){ try { $memberCertificate = \App\Models\MemberCertificate::findOrFail($id); if( !$memberCertificate) { session()->flash('error','Tesserato non trovato'); } else { $this->certificate_type = $memberCertificate->type; $this->certificate_filename = $memberCertificate->filename; $this->certificate_expire_date = $memberCertificate->expire_date; $this->certificate_status = $memberCertificate->status; $this->cardCertificateId = $memberCertificate->id; $this->updateCertificate = true; $this->addCertificate = false; } } catch (\Exception $ex) { session()->flash('error','Errore'); } } public function updateCertificate() { //$this->validate(); try { \App\Models\MemberCertificate::whereId($this->dataId)->update([ 'member_id' => $this->dataId, 'type' => $this->certificate_type, 'filename' => $this->certificate_filename, 'expire_date' => $this->certificate_expire_date, 'status' => $this->certificate_status ]); session()->flash('success','Tesserato aggiornato'); $this->resetCertificateFields(); $this->updateCertificate = false; } catch (\Exception $ex) { session()->flash('success','Errore'); } } public function cancelCertificate() { $this->addCertificate = false; $this->updateCertificate = false; $this->resetCertificateFields(); } public function deleteCertificate($id) { try{ \App\Models\MemberCertificate::find($id)->delete(); session()->flash('success',"Tesserato eliminato"); }catch(\Exception $e){ session()->flash('error',"Errore"); } } // Gruppi di appartenenza public function storeCategory() { $this->validate(['category_category_id' => 'required']); try { \App\Models\MemberCategory::create([ 'member_id' => $this->dataId, 'category_id' => $this->category_category_id, 'date' => \Carbon\Carbon::now() ]); session()->flash('success, Associazione creato'); $this->resetCategoryFields(); $this->addCard = false; } catch (\Exception $ex) { session()->flash('error','Errore in fase di salvataggio'); } } public function deleteCategory($id) { try{ \App\Models\MemberCategory::find($id)->delete(); session()->flash('success',"Associazione eliminata"); }catch(\Exception $e){ session()->flash('error',"Errore"); } } }