Customer.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace App\Livewire;
  3. use Livewire\Component;
  4. use Livewire\Attributes\Validate;
  5. class Customer extends Component
  6. {
  7. public $customers;
  8. #[Validate('required')]
  9. public $name;
  10. public $business_name;
  11. public $code;
  12. public $group_id;
  13. public $address;
  14. public $zip;
  15. public $city_id;
  16. public $country_id;
  17. public $fiscal_code;
  18. public $vat;
  19. public $sdi;
  20. public $ateco;
  21. public $channel;
  22. public $website = '';
  23. public $email = '';
  24. public $pec = '';
  25. public $phone = '';
  26. public $referent_first_name = '';
  27. public $referent_last_name = '';
  28. public $referent_email = '';
  29. public $referent_phone = '';
  30. public $enabled;
  31. public $current_customer = null;
  32. public $is_edit = false;
  33. public function resetFields(){
  34. $this->name = '';
  35. $this->business_name = '';
  36. $this->code = '';
  37. $this->group_id = null;
  38. $this->address = '';
  39. $this->zip = '';
  40. $this->city_id = null;
  41. $this->country_id = null;
  42. $this->fiscal_code = '';
  43. $this->vat = '';
  44. $this->sdi = '';
  45. $this->ateco = '';
  46. $this->channel = '';
  47. $this->website = '';
  48. $this->email = '';
  49. $this->pec = '';
  50. $this->phone = '';
  51. $this->referent_first_name = '';
  52. $this->referent_last_name = '';
  53. $this->referent_email = '';
  54. $this->referent_phone = '';
  55. $this->enabled = true;
  56. }
  57. public function render()
  58. {
  59. // Get Customers
  60. $this->customers = \App\Models\Customer::all();
  61. return view('livewire.customer');
  62. }
  63. public function add()
  64. {
  65. $this->resetFields();
  66. $this->is_edit = true;
  67. $this->current_customer = null;
  68. }
  69. public function edit($id)
  70. {
  71. $this->resetFields();
  72. $this->is_edit = true;
  73. $this->current_customer = \App\Models\Customer::findOrFail($id);
  74. $this->name = $this->current_customer->name;
  75. $this->business_name = $this->current_customer->business_name;
  76. $this->code = $this->current_customer->code;
  77. $this->group_id = $this->current_customer->group_id;
  78. $this->address = $this->current_customer->address;
  79. $this->zip = $this->current_customer->zip;
  80. $this->city_id = $this->current_customer->city_id;
  81. $this->country_id = $this->current_customer->country_id;
  82. $this->fiscal_code = $this->current_customer->fiscal_code;
  83. $this->vat = $this->current_customer->vat;
  84. $this->sdi = $this->current_customer->sdi;
  85. $this->ateco = $this->current_customer->ateco;
  86. $this->website = $this->current_customer->website;
  87. $this->email = $this->current_customer->email;
  88. $this->pec = $this->current_customer->pec;
  89. $this->phone = $this->current_customer->phone;
  90. $this->referent_first_name = $this->current_customer->referent_first_name;
  91. $this->referent_last_name = $this->current_customer->referent_last_name;
  92. $this->referent_email = $this->current_customer->referent_email;
  93. $this->referent_phone = $this->current_customer->referent_phone;
  94. $this->enabled = $this->current_customer->enabled;
  95. }
  96. public function save()
  97. {
  98. $this->validate();
  99. try
  100. {
  101. if ($this->current_customer == null)
  102. {
  103. \App\Models\Customer::create([
  104. 'name' => $this->name,
  105. 'business_name' => $this->business_name,
  106. 'code' => $this->code,
  107. 'group_id' => $this->group_id,
  108. 'address' => $this->address,
  109. 'zip' => $this->zip,
  110. 'city_id' => $this->city_id,
  111. 'country_id' => $this->country_id,
  112. 'fiscal_code' => $this->fiscal_code,
  113. 'vat' => $this->vat,
  114. 'sdi' => $this->sdi,
  115. 'ateco' => $this->ateco,
  116. 'website' => $this->website,
  117. 'email' => $this->email,
  118. 'pec' => $this->pec,
  119. 'phone' => $this->phone,
  120. 'referent_first_name' => $this->referent_first_name,
  121. 'referent_last_name' => $this->referent_last_name,
  122. 'referent_email' => $this->referent_email,
  123. 'referent_phone' => $this->referent_phone,
  124. 'enabled' => $this->enabled
  125. ]);
  126. }
  127. else
  128. {
  129. $this->current_company->update([
  130. 'name' => $this->name,
  131. 'business_name' => $this->business_name,
  132. 'code' => $this->code,
  133. 'group_id' => $this->group_id,
  134. 'address' => $this->address,
  135. 'zip' => $this->zip,
  136. 'city_id' => $this->city_id,
  137. 'country_id' => $this->country_id,
  138. 'fiscal_code' => $this->fiscal_code,
  139. 'vat' => $this->vat,
  140. 'sdi' => $this->sdi,
  141. 'ateco' => $this->ateco,
  142. 'website' => $this->website,
  143. 'email' => $this->email,
  144. 'pec' => $this->pec,
  145. 'phone' => $this->phone,
  146. 'referent_first_name' => $this->referent_first_name,
  147. 'referent_last_name' => $this->referent_last_name,
  148. 'referent_email' => $this->referent_email,
  149. 'referent_phone' => $this->referent_phone,
  150. 'enabled' => $this->enabled,
  151. ]);
  152. }
  153. session()->flash('success','Dati salvati con successo');
  154. $this->resetFields();
  155. $this->is_edit = false;
  156. } catch (\Exception $ex) {
  157. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  158. }
  159. }
  160. public function cancel()
  161. {
  162. $this->resetFields();
  163. $this->is_edit = false;
  164. $this->current_company = null;
  165. }
  166. }