| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- <?php
- namespace App\Http\Livewire;
- use Livewire\Component;
- class Member extends Component
- {
- public $records, $first_name, $last_name, $status, $birth_city_id, $birth_province_id, $birth_nation_id, $birth_date, $gender, $fiscal_code, $address, $zip_code, $nation_id, $province_id, $city_id, $phone, $email, $enabled, $dataId, $update = false, $add = false;
- public $cards = array();
- public $nations = array();
- public $provinces = array();
- public $cities = array();
- public $birthNations = array();
- public $birthProvinces = array();
- public $birthCities = array();
- // Card data
- public $member_cards = array(), $card_card_id, $card_number, $card_date, $card_accept_date, $card_expire_date, $card_status, $addCard, $updateCard, $cardDataId;
- // Categories data
- public $member_categories = array(), $category_category_id;
- // Certificates data
- public $member_certificates = array(), $certificate_type, $certificate_filename, $certificate_expire_date, $certificate_status, $addCertificate, $updateCertificate, $certificateDataId;
- protected $rules = [
- 'first_name' => '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 mount()
- {
- $this->cards = \App\Models\Card::select('id', 'name')->get();
- $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();
- 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");
- }
- }
- }
|