'required' ]; protected $messages = [ 'name.required' => 'Il nome รจ obbligatorio' ]; public function resetFields(){ $this->name = ''; $this->fiscal_code = ''; $this->vat = ''; $this->address = ''; $this->zip_code = ''; $this->nation_id = null; $this->province_id = null; $this->city_id = null; $this->referent = ''; $this->website = ''; $this->phone = ''; $this->email = ''; $this->enabled = true; $this->referent_first_name = ''; $this->referent_last_name = ''; $this->referent_email = ''; $this->referent_phone = ''; $this->referent_mobile = ''; $this->emit('load-data-table'); } public $sortField ='name'; public $sortAsc = true; public function sortBy($field) { if($this->sortField === $field) { $this->sortAsc = ! $this->sortAsc; } else { $this->sortAsc = true; } $this->sortField = $field; } public function mount() { if(\Auth::user()->level != env('LEVEL_ADMIN', 0)) return redirect()->to('/dashboard'); if (isset($_GET["new"])) $this->add(); } 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 render() { /*if ($this->search != '') $this->records = \App\Models\Supplier::with('nation')->where('name', 'LIKE', '%' . $this->search . '%')->orderBy($this->sortField, $this->sortAsc ? 'asc' : 'desc')->get(); else*/ $this->records = \App\Models\Supplier::with('nation')->get(); return view('livewire.supplier'); } public function hydrate() { $this->emit('load-select'); } public function checkIsItaly() { $n = \App\Models\Nation::findOrFail($this->nation_id); $this->isItaly = $n->is_italy; } public function add() { $this->resetFields(); $this->emit('load-select'); $this->add = true; $this->update = false; } public function store() { $this->validate(); try { \App\Models\Supplier::create([ 'name' => $this->name, 'fiscal_code' => $this->fiscal_code, 'vat' => $this->vat, 'address' => $this->address, 'zip_code' => $this->zip_code, 'nation_id' => $this->nation_id, 'province_id' => $this->province_id > 0 ? $this->province_id : null, 'city_id' => $this->city_id > 0 ? $this->city_id : null, 'referent' => $this->referent, 'website' => $this->website, 'phone' => $this->phone, 'email' => $this->email, 'referent_first_name' => $this->referent_first_name, 'referent_last_name' => $this->referent_last_name, 'referent_email' => $this->referent_email, 'referent_phone' => $this->referent_phone, 'referent_mobile' => $this->referent_mobile, 'enabled' => $this->enabled ]); session()->flash('success','Fornitore creato'); $this->resetFields(); $this->add = false; } catch (\Exception $ex) { session()->flash('error','Errore (' . $ex->getMessage() . ')'); } } public function edit($id){ $this->emit('load-select'); try { $supplier = \App\Models\Supplier::findOrFail($id); if( !$supplier) { session()->flash('error','Fornitore non trovato'); } else { $this->name = $supplier->name; $this->fiscal_code = $supplier->fiscal_code; $this->vat = $supplier->vat; $this->address = $supplier->address; $this->zip_code = $supplier->zip_code; $this->nation_id = $supplier->nation_id; $this->province_id = $supplier->province_id; $this->city_id = $supplier->city_id; $this->referent = $supplier->referent; $this->website = $supplier->website; $this->phone = $supplier->phone; $this->email = $supplier->email; $this->referent_first_name = $supplier->referent_first_name; $this->referent_last_name = $supplier->referent_last_name; $this->referent_email = $supplier->referent_email; $this->referent_phone = $supplier->referent_phone; $this->referent_mobile = $supplier->referent_mobile; $this->enabled = $supplier->enabled; $this->dataId = $supplier->id; $this->update = true; $this->add = false; $this->checkIsItaly(); $this->emit('load-provinces', $this->nation_id, 'provinceClass'); $this->emit('load-cities', $this->province_id, 'cityClass'); } } catch (\Exception $ex) { session()->flash('error','Errore (' . $ex->getMessage() . ')'); } } public function update() { $this->validate(); try { \App\Models\Supplier::whereId($this->dataId)->update([ 'name' => $this->name, 'fiscal_code' => $this->fiscal_code, 'vat' => $this->vat, 'address' => $this->address, 'zip_code' => $this->zip_code, 'nation_id' => $this->nation_id, 'province_id' => $this->province_id > 0 ? $this->province_id : null, 'city_id' => $this->city_id > 0 ? $this->city_id : null, 'referent' => $this->referent, 'website' => $this->website, 'phone' => $this->phone, 'email' => $this->email, 'referent_first_name' => $this->referent_first_name, 'referent_last_name' => $this->referent_last_name, 'referent_email' => $this->referent_email, 'referent_phone' => $this->referent_phone, 'referent_mobile' => $this->referent_mobile, 'enabled' => $this->enabled ]); session()->flash('success','Fornitore aggiornato'); $this->resetFields(); $this->update = false; } catch (\Exception $ex) { session()->flash('error','Errore (' . $ex->getMessage() . ')'); } } public function cancel() { $this->add = false; $this->update = false; $this->resetFields(); } public function delete($id) { try{ \App\Models\Supplier::find($id)->delete(); session()->flash('success',"Fornitore eliminato"); return redirect(request()->header('Referer')); }catch(\Exception $e){ session()->flash('error','Errore (' . $ex->getMessage() . ')'); } } public function getNation($nation) { if ($nation > 0) { $ret = \App\Models\Nation::findOrFail($nation); return $ret->name; } return ""; } public function getProvince($province) { if ($province > 0) { $ret = \App\Models\Province::findOrFail($province); return $ret->name; } return ""; } public function getCity($city) { if ($city > 0) { $ret = \App\Models\City::findOrFail($city); return $ret->name; } return ""; } }