| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
- namespace App\Livewire;
- use Livewire\Component;
- use Livewire\Attributes\Validate;
- class Customer extends Component
- {
- public $customers;
- #[Validate('required')]
- public $name;
- public $business_name;
- public $code;
- public $group_id;
- public $address;
- public $zip;
- public $city_id;
- public $country_id;
- public $fiscal_code;
- public $vat;
- public $sdi;
- public $ateco;
- public $channel;
- public $website = '';
- public $email = '';
- public $pec = '';
- public $phone = '';
- public $referent_first_name = '';
- public $referent_last_name = '';
- public $referent_email = '';
- public $referent_phone = '';
- public $enabled;
- public $current_customer = null;
- public $is_edit = false;
- public function resetFields(){
- $this->name = '';
- $this->business_name = '';
- $this->code = '';
- $this->group_id = null;
- $this->address = '';
- $this->zip = '';
- $this->city_id = null;
- $this->country_id = null;
- $this->fiscal_code = '';
- $this->vat = '';
- $this->sdi = '';
- $this->ateco = '';
- $this->channel = '';
- $this->website = '';
- $this->email = '';
- $this->pec = '';
- $this->phone = '';
- $this->referent_first_name = '';
- $this->referent_last_name = '';
- $this->referent_email = '';
- $this->referent_phone = '';
- $this->enabled = true;
- }
- public function render()
- {
- // Get Customers
- $this->customers = \App\Models\Customer::all();
- return view('livewire.customer');
- }
- public function add()
- {
- $this->resetFields();
- $this->is_edit = true;
- $this->current_customer = null;
- }
- public function edit($id)
- {
- $this->resetFields();
- $this->is_edit = true;
- $this->current_customer = \App\Models\Customer::findOrFail($id);
- $this->name = $this->current_customer->name;
- $this->business_name = $this->current_customer->business_name;
- $this->code = $this->current_customer->code;
- $this->group_id = $this->current_customer->group_id;
- $this->address = $this->current_customer->address;
- $this->zip = $this->current_customer->zip;
- $this->city_id = $this->current_customer->city_id;
- $this->country_id = $this->current_customer->country_id;
- $this->fiscal_code = $this->current_customer->fiscal_code;
- $this->vat = $this->current_customer->vat;
- $this->sdi = $this->current_customer->sdi;
- $this->ateco = $this->current_customer->ateco;
- $this->website = $this->current_customer->website;
- $this->email = $this->current_customer->email;
- $this->pec = $this->current_customer->pec;
- $this->phone = $this->current_customer->phone;
- $this->referent_first_name = $this->current_customer->referent_first_name;
- $this->referent_last_name = $this->current_customer->referent_last_name;
- $this->referent_email = $this->current_customer->referent_email;
- $this->referent_phone = $this->current_customer->referent_phone;
- $this->enabled = $this->current_customer->enabled;
- }
- public function save()
- {
- $this->validate();
- try
- {
- if ($this->current_customer == null)
- {
- \App\Models\Customer::create([
- 'name' => $this->name,
- 'business_name' => $this->business_name,
- 'code' => $this->code,
- 'group_id' => $this->group_id,
- 'address' => $this->address,
- 'zip' => $this->zip,
- 'city_id' => $this->city_id,
- 'country_id' => $this->country_id,
- 'fiscal_code' => $this->fiscal_code,
- 'vat' => $this->vat,
- 'sdi' => $this->sdi,
- 'ateco' => $this->ateco,
- 'website' => $this->website,
- 'email' => $this->email,
- 'pec' => $this->pec,
- 'phone' => $this->phone,
- 'referent_first_name' => $this->referent_first_name,
- 'referent_last_name' => $this->referent_last_name,
- 'referent_email' => $this->referent_email,
- 'referent_phone' => $this->referent_phone,
- 'enabled' => $this->enabled
- ]);
- }
- else
- {
- $this->current_company->update([
- 'name' => $this->name,
- 'business_name' => $this->business_name,
- 'code' => $this->code,
- 'group_id' => $this->group_id,
- 'address' => $this->address,
- 'zip' => $this->zip,
- 'city_id' => $this->city_id,
- 'country_id' => $this->country_id,
- 'fiscal_code' => $this->fiscal_code,
- 'vat' => $this->vat,
- 'sdi' => $this->sdi,
- 'ateco' => $this->ateco,
- 'website' => $this->website,
- 'email' => $this->email,
- 'pec' => $this->pec,
- 'phone' => $this->phone,
- 'referent_first_name' => $this->referent_first_name,
- 'referent_last_name' => $this->referent_last_name,
- 'referent_email' => $this->referent_email,
- 'referent_phone' => $this->referent_phone,
- 'enabled' => $this->enabled,
- ]);
- }
- session()->flash('success','Dati salvati con successo');
- $this->resetFields();
- $this->is_edit = false;
- } catch (\Exception $ex) {
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- public function cancel()
- {
- $this->resetFields();
- $this->is_edit = false;
- $this->current_company = null;
- }
- }
|