| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?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 $nations = array();
- public $provinces = array();
- public $cities = array();
- 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;
- }
- public function mount()
- {
- if (isset($_GET["new"]))
- $this->add();
- $this->nations = \App\Models\Nation::select('id', 'name')->get();
- $this->provinces = array();
- $this->cities = array();
- }
- public function loadProvinces()
- {
- $this->provinces = \App\Models\Province::select('id', 'name')->where('nation_id', $this->nation_id)->get();
- $this->cities = array();\App\Models\City::select('id', 'name')->get();
- }
- public function loadCities()
- {
- $this->cities = \App\Models\City::select('id', 'name')->where('province_id', $this->province_id)->get();
- }
- public function render()
- {
- $this->records = \App\Models\Supplier::with('nation')->get();
- return view('livewire.supplier');
- }
- public function add()
- {
- $this->resetFields();
- $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,
- '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){
- 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->enabled = $supplier->enabled;
- $this->dataId = $supplier->id;
- $this->update = true;
- $this->add = false;
- }
- } 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,
- '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() . ')');
- }
- }
- }
|