| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- <?php
- namespace App\Http\Livewire;
- use Livewire\Component;
- class Supplier extends Component
- {
- public $records, $name, $fiscal_code, $vat, $address, $zip_code,$nation_id,$province_id,$city_id,$referent,$website,$phone,$email,$enabled, $dataId, $update = false, $add = false;
- public $referent_first_name, $referent_last_name, $referent_email, $referent_phone, $referent_mobile;
- public $isItaly = true;
- public $searchTxt;
- public $search;
- protected $rules = [
- 'name' => '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,
- 'city_id' => $this->city_id,
- '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->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,
- 'city_id' => $this->city_id,
- '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");
- }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 "";
- }
- }
|