| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792 |
- <?php
- namespace App\Http\Livewire;
- use Livewire\Component;
- use Livewire\Attributes\Url;
- use Livewire\WithFileUploads;
- use DateTime;
- class Member extends Component
- {
- use WithFileUploads;
- public $records, $first_name, $last_name, $image, $image_old, $status, $birth_place, $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 $father_name, $mother_name, $father_email, $mother_email, $father_phone, $mother_phone, $father_fiscal_code, $mother_fiscal_code;
- public $selectId = 0;
- public $causalId = 0;
- public $under18 = false;
- public $multipleIds = [];
- public $multipleAction = '';
- public $isItaly = true;
- public $isBirthItaly = true;
- public $showDetail = false;
- public $currentMember;
- public $age = '';
- public $cards = array();
- public $categories = array();
- public $nations = array();
- public $provinces = array();
- public $cities = array();
- public $searchTxt;
- public $search;
- public $showReset = false;
- public $advanced = false;
- public $birthNations = array();
- public $birthProvinces = array();
- public $birthCities = array();
- public $active;
- // Card data
- public $member_cards = array(), $card_card_id, $card_number, $card_date, $card_accept_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_old, $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_province_id = 178;
- $this->birth_nation_id = 1;
- $this->birth_date = null;
- $this->birth_place = '';
- $this->father_name = '';
- $this->mother_name = '';
- $this->father_email = '';
- $this->mother_email = '';
- $this->father_phone = '';
- $this->mother_phone = '';
- $this->father_fiscal_code = '';
- $this->mother_fiscal_code = '';
- $this->gender = '';
- $this->fiscal_code = '';
- $this->address = '';
- $this->zip_code = '';
- //$this->nation_id = null;
- //$this->province_id = null;
- $this->nation_id = 1;
- $this->province_id = 178;
- $this->city_id = null;
- $this->phone = '';
- $this->email = '';
- $this->enabled = true;
- $this->under18 = false;
- $this->image = null;
- }
- public function resetCardFields(){
- $this->card_card_id = null;
- $this->card_number = '';
- $this->card_date = null;
- $this->card_accept_date = null;
- $this->card_status = 0;
- }
- public function resetCertificateFields(){
- $this->certificate_type = '';
- $this->certificate_filename = '';
- $this->certificate_filename_old = '';
- $this->certificate_expire_date = null;
- $this->certificate_status = 0;
- }
- public function executeMultipleAction(){
- if ($this->multipleAction == 'delete')
- $this->multipleDelete();
- }
- public function updatedImage()
- {
- $this->validate([
- 'image' => 'image|max:1024',
- ]);
- $this->image_old = '';
- }
- 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);
- $this->categories[] = array('id' => $record->id, 'name' => $record->getTree());
- if(count($record->childs))
- $this->getCategories($record->childs, $indentation + 1);
- }
- }
- public function updatedBirthDate()
- {
- $date1 = new DateTime($this->birth_date);
- $date2 = new DateTime("now");
- $interval = $date1->diff($date2);
- $this->age = $interval->y . " anni";
- $this->under18 = $interval->y < 18;
- }
- public function mount()
- {
- if (isset($_GET["new"]))
- $this->add();
- $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')->orderBy('name')->get();
- $this->provinces = \App\Models\Province::where('nation_id', 1)->orderBy('name')->get();
- $this->cities = \App\Models\City::where('province_id', 178)->orderBy('name')->orderBy('name')->get();
- $c = \App\Models\Causal::where('type', 'IN')->where('money', true)->first();
- if ($c)
- $this->causalId = $c->id;
- $this->birthNations = \App\Models\Nation::select('id', 'name')->orderBy('name')->get();
- //$this->birthProvinces = array();
- //$this->birthCities = array();
- $this->birthProvinces = \App\Models\Province::where('nation_id', 1)->orderBy('name')->get();
- $this->birthCities = \App\Models\City::where('province_id', 178)->orderBy('name')->orderBy('name')->get();
- }
- public function updated()
- {
- $this->emit('load-select');
- }
- public function hydrate()
- {
- $this->emit('load-select');
- }
- public function loadProvinces()
- {
- $n = \App\Models\Nation::findOrFail($this->nation_id);
- $this->isItaly = $n->is_italy;
- $this->provinces = \App\Models\Province::where('nation_id', $this->nation_id)->orderBy('name')->get();
- $this->cities = array();
- $this->selectId++;
- }
- public function loadCities()
- {
- $this->cities = \App\Models\City::where('province_id', $this->province_id)->orderBy('name')->orderBy('name')->get();
- $this->selectId++;
- }
- public function loadBirthProvinces()
- {
- $n = \App\Models\Nation::findOrFail($this->birth_nation_id);
- $this->isBirthItaly = $n->is_italy;
- $this->birthProvinces = \App\Models\Province::where('nation_id', $this->birth_nation_id)->orderBy('name')->get();
- $this->birthCities = array();
- $this->selectId++;
- }
- public function loadBirthCities()
- {
- $this->birthCities = \App\Models\City::where('province_id', $this->birth_province_id)->get();
- $this->selectId++;
- }
- public function search()
- {
- if ($this->searchTxt != '')
- {
- $this->search = $this->searchTxt;
- $this->showReset = true;
- }
- }
- public function resetSearch()
- {
- $this->showReset = false;
- $this->searchTxt = '';
- $this->search = $this->searchTxt;
- }
- public function advancedSearch()
- {
- $this->avanced = true;
- }
- public function render()
- {
- if (!$this->advanced)
- {
- if ($this->search != '')
- $this->records = \App\Models\Member::where('first_name', 'LIKE', '%' . $this->search . '%')->orWhere('last_name', 'LIKE', '%' . $this->search . '%')->orWhere('email', 'LIKE', '%' . $this->search . '%')->get();
- else
- $this->records = \App\Models\Member::get();
- }
- else
- {
- $this->records = \App\Models\Member::where('id', '>', 0);
- $this->records = $this->records->get();
- }
- //$this->records = $this->records->get();
- $this->loadMemberCards();
- $this->loadMemberCategories();
- $this->loadMemberCertificates();
- 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 showDetail($id)
- {
- $this->currentMember = \App\Models\Member::findOrFail($id);
- $this->showDetail = true;
- }
- public function add()
- {
- $this->emit('load-select');
- $this->showDetail = false;
- $this->resetFields();
- $this->add = true;
- $this->update = false;
- }
- public function store($close)
- {
- $this->emit('load-select');
- $this->validate();
- try {
- $name = '';
- if ($this->image)
- {
- $name = md5($this->image . microtime()).'.'.$this->image->extension();
- $this->image->storeAs('public', $name);
- }
- $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,
- 'birth_place' => $this->birth_place,
- 'father_name' => $this->father_name,
- 'mother_name' => $this->mother_name,
- 'father_email' => $this->father_email,
- 'mother_email' => $this->mother_email,
- 'father_phone' => $this->father_phone,
- 'mother_phone' => $this->mother_phone,
- 'father_fiscal_code' => $this->father_fiscal_code,
- 'mother_fiscal_code' => $this->mother_fiscal_code,
- '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,
- 'image' => $name,
- 'enabled' => $this->enabled
- ]);
- session()->flash('success, Tesserato creato');
- $this->resetFields();
- if ($close)
- {
- $this->add = false;
- }
- else
- {
- $this->edit($member->id);
- }
- } catch (\Exception $ex) {
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- public function duplicate($id){
- $member = \App\Models\Member::findOrFail($id);
- $newMember = $member->replicate();
- $newMember->save();
- $this->edit($newMember->id);
- }
- public function edit($id){
- $this->showDetail = false;
- $this->emit('load-select');
- 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->birth_place = $member->birth_place;
- $this->father_name = $member->father_name;
- $this->mother_name = $member->mother_name;
- $this->father_email = $member->father_email;
- $this->mother_email = $member->mother_email;
- $this->father_phone = $member->father_phone;
- $this->mother_phone = $member->mother_phone;
- $this->father_fiscal_code = $member->father_fiscal_code;
- $this->mother_fiscal_code = $member->mother_fiscal_code;
- $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->image_old = $member->image;
- $this->enabled = $member->enabled;
- $this->dataId = $member->id;
- $this->active = $member->isActive();
- $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 (' . $ex->getMessage() . ')');
- }
- }
- public function update($close)
- {
- $this->emit('load-select');
- $this->validate();
- try {
- $name = '';
- if ($this->image)
- {
- $name = md5($this->image . microtime()).'.'.$this->image->extension();
- $this->image->storeAs('public', $name);
- }
- \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,
- 'birth_place' => $this->birth_place,
- 'father_name' => $this->father_name,
- 'mother_name' => $this->mother_name,
- 'father_email' => $this->father_email,
- 'mother_email' => $this->mother_email,
- 'father_phone' => $this->father_phone,
- 'mother_phone' => $this->mother_phone,
- 'father_fiscal_code' => $this->father_fiscal_code,
- 'mother_fiscal_code' => $this->mother_fiscal_code,
- '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,
- 'image' => $name != '' ? $name : $this->image_old,
- 'phone' => $this->phone,
- 'email' => $this->email,
- 'enabled' => $this->enabled
- ]);
- session()->flash('success','Tesserato aggiornato');
- if ($close)
- {
- $this->resetFields();
- $this->update = false;
- }
- } catch (\Exception $ex) {
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- public function cancel()
- {
- $this->add = false;
- $this->update = false;
- $this->showDetail = 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 (' . $ex->getMessage() . ')');
- }
- }
- public function multipleDelete()
- {
- try{
- foreach($this->multipleIds as $id)
- {
- \App\Models\Member::find($id)->delete();
- }
- }catch(\Exception $e){
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- $this->multipleAction = '';
- }
- // Card
- public function addCard()
- {
- $this->resetCardFields();
- $this->addCard = true;
- $this->updateCard = false;
- }
- public function storeCard()
- {
- $this->validate(['card_card_id' => 'required']);
- try {
- // in base alla card selezionata calcolo la scadenza
- $expire_date = null;
- if ($this->card_date != '')
- {
- $card = \App\Models\Card::findOrFail($this->card_card_id);
- if ($card->next_day_expire > 0 && $card->next_month_expire > 0)
- {
- $m = strlen($card->next_month_expire) == 1 ? ('0' . $card->next_month_expire) : $card->next_month_expire;
- $next_exp = date("Y-" . $m . "-" . $card->next_day_expire);
- if ($next_exp > $this->card_date)
- {
- $expire_date = $next_exp;
- }
- else
- $expire_date = date("Y-" . $card->next_month_expire . "-" . $card->next_day_expire, strtotime(' + 1 years'));
- }
- }
- \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' => $expire_date,
- 'status' => $this->card_status
- ]);
- session()->flash('success, Tesserato creato');
- $this->resetCardFields();
- $this->addCard = false;
- } catch (\Exception $ex) {
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- 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_status = $memberCard->status;
- $this->cardDataId = $memberCard->id;
- $this->updateCard = true;
- $this->addCard = false;
- }
- } catch (\Exception $ex) {
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- public function updateCard()
- {
- //$this->validate();
- $this->validate(['card_card_id' => 'required']);
- try {
- // in base alla card selezionata calcolo la scadenza
- $expire_date = null;
- if ($this->card_date != '')
- {
- $card = \App\Models\Card::findOrFail($this->card_card_id);
- if ($card->next_day_expire > 0 && $card->next_month_expire > 0)
- {
- $m = strlen($card->next_month_expire) == 1 ? ('0' . $card->next_month_expire) : $card->next_month_expire;
- $next_exp = date("Y-" . $m . "-" . $card->next_day_expire);
- if ($next_exp > $this->card_date)
- {
- $expire_date = $next_exp;
- }
- else
- $expire_date = date("Y-" . $card->next_month_expire . "-" . $card->next_day_expire, strtotime(' + 1 years'));
- }
- }
- \App\Models\MemberCard::whereId($this->cardDataId)->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' => $expire_date,
- 'status' => $this->card_status
- ]);
- session()->flash('success','Tesserato aggiornato');
- $this->resetCardFields();
- $this->updateCard = false;
- } catch (\Exception $ex) {
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- 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 (' . $ex->getMessage() . ')');
- }
- }
- // Certificates
- public function addCertificate()
- {
- $this->resetCertificateFields();
- $this->addCertificate = true;
- $this->updateCertificate = false;
- }
- public function storeCertificate()
- {
- $this->validate(['certificate_expire_date' => 'required']);
- // $this->validate();
- try {
- $name = '';
- try{
- if ($this->certificate_filename)
- {
- $name = md5($this->certificate_filename . microtime()).'.'.$this->certificate_filename->extension();
- $this->certificate_filename->storeAs('public', $name);
- }
- } catch (\Exception $ex) {
- //session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- if ($this->dataId > -1)
- {
- \App\Models\MemberCertificate::create([
- 'member_id' => $this->dataId,
- 'type' => $this->certificate_type,
- 'filename' => $name,
- 'expire_date' => $this->certificate_expire_date,
- 'status' => $this->certificate_status
- ]);
- }
- /*else
- {
- $this->certificateTmp = new \App\Models\MemberCertificate();
- $this->certificateTmp->type = $this->certificate_type;
- $this->certificateTmp->filename = $name;
- $this->certificateTmp->expire_date = $this->certificate_expire_date;
- $this->certificateTmp->status = $this->certificate_status;
- $this->certificateTmp->status = $this->certificate_status;
- // s $this->member_certificates[] = $certificateTmp;
- }*/
- session()->flash('success, Tesserato creato');
- $this->resetCertificateFields();
- $this->addCertificate = false;
- } catch (\Exception $ex) {
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- 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_old = $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 (' . $ex->getMessage() . ')');
- }
- }
- public function updateCertificate()
- {
- $this->validate(['certificate_expire_date' => 'required']);
- try {
- $name = '';
- try{
- if ($this->certificate_filename)
- {
- $name = md5($this->certificate_filename . microtime()).'.'.$this->certificate_filename->extension();
- $this->certificate_filename->storeAs('public', $name);
- }
- } catch (\Exception $ex) {
- //session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- \App\Models\MemberCertificate::whereId($this->cardCertificateId)->update([
- 'member_id' => $this->dataId,
- 'type' => $this->certificate_type,
- 'filename' => $name != '' ? $name : $this->certificate_filename_old,
- '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('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- 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 (' . $ex->getMessage() . ')');
- }
- }
- // 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 (' . $ex->getMessage() . ')');
- }
- }
- public function deleteCategory($id)
- {
- try{
- \App\Models\MemberCategory::find($id)->delete();
- session()->flash('success',"Associazione eliminata");
- }catch(\Exception $e){
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- }
|