Supplier.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use Illuminate\Support\Facades\Auth;
  5. use Illuminate\Support\Facades\Log;
  6. class Supplier extends Component
  7. {
  8. 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;
  9. public $referent_first_name, $referent_last_name, $referent_email, $referent_phone, $referent_mobile;
  10. public $isItaly = true;
  11. public $showReset = false;
  12. public $searchTxt;
  13. public $search;
  14. protected $rules = [
  15. 'name' => 'required'
  16. ];
  17. protected $messages = [
  18. 'name.required' => 'Il nome è obbligatorio'
  19. ];
  20. public function resetFields(){
  21. $this->name = '';
  22. $this->fiscal_code = '';
  23. $this->vat = '';
  24. $this->address = '';
  25. $this->zip_code = '';
  26. $this->nation_id = null;
  27. $this->province_id = null;
  28. $this->city_id = null;
  29. $this->referent = '';
  30. $this->website = '';
  31. $this->phone = '';
  32. $this->email = '';
  33. $this->enabled = true;
  34. $this->referent_first_name = '';
  35. $this->referent_last_name = '';
  36. $this->referent_email = '';
  37. $this->referent_phone = '';
  38. $this->referent_mobile = '';
  39. $this->emit('load-data-table');
  40. }
  41. public $sortField ='name';
  42. public $sortAsc = true;
  43. public function sortBy($field)
  44. {
  45. if($this->sortField === $field)
  46. {
  47. $this->sortAsc = ! $this->sortAsc;
  48. } else {
  49. $this->sortAsc = true;
  50. }
  51. $this->sortField = $field;
  52. }
  53. public function mount()
  54. {
  55. if(Auth::user()->level != env('LEVEL_ADMIN', 0))
  56. return redirect()->to('/dashboard');
  57. if (isset($_GET["new"]))
  58. $this->add();
  59. }
  60. public function search()
  61. {
  62. if ($this->searchTxt != '')
  63. {
  64. $this->search = $this->searchTxt;
  65. $this->showReset = true;
  66. }
  67. }
  68. public function resetSearch()
  69. {
  70. $this->showReset = false;
  71. $this->searchTxt = '';
  72. $this->search = $this->searchTxt;
  73. }
  74. public function render()
  75. {
  76. $this->records = \App\Models\Supplier::with('nation')->get();
  77. return view('livewire.supplier');
  78. }
  79. public function hydrate()
  80. {
  81. $this->emit('load-select');
  82. }
  83. public function checkIsItaly()
  84. {
  85. $n = \App\Models\Nation::findOrFail($this->nation_id);
  86. $this->isItaly = $n->is_italy;
  87. }
  88. public function add()
  89. {
  90. $this->resetFields();
  91. $this->emit('load-select');
  92. $this->add = true;
  93. $this->update = false;
  94. $this->emit('setEdit', true);
  95. }
  96. public function store()
  97. {
  98. $this->validate();
  99. $vatToCheck = trim($this->vat);
  100. $existingSupplier = \App\Models\Supplier::where('vat', $vatToCheck)->first();
  101. Log::info('VAT validation detailed debug', [
  102. 'vat_input' => $this->vat,
  103. 'vat_trimmed' => $vatToCheck,
  104. 'vat_empty_check' => empty($vatToCheck),
  105. 'existing_supplier_id' => $existingSupplier ? $existingSupplier->id : null,
  106. 'existing_supplier_name' => $existingSupplier ? $existingSupplier->name : null,
  107. 'existing_supplier_vat' => $existingSupplier ? $existingSupplier->vat : null,
  108. 'validation_rules' => $this->rules
  109. ]);
  110. if (!empty($vatToCheck)) {
  111. $duplicateCount = \App\Models\Supplier::where('vat', $vatToCheck)->count();
  112. if ($duplicateCount > 0) {
  113. Log::info('VAT duplicate found, adding error');
  114. $this->addError('vat', 'Già esiste un fornitore con questa Partita IVA');
  115. return;
  116. }
  117. }
  118. $fiscalCodeToCheck = trim($this->fiscal_code);
  119. if (!empty($fiscalCodeToCheck)) {
  120. $duplicateFiscalCount = \App\Models\Supplier::where('fiscal_code', $fiscalCodeToCheck)->count();
  121. if ($duplicateFiscalCount > 0) {
  122. Log::info('Fiscal code duplicate found, adding error');
  123. $this->addError('fiscal_code', 'Già esiste un fornitore con questo codice fiscale ');
  124. return;
  125. }
  126. }
  127. try {
  128. \App\Models\Supplier::create([
  129. 'name' => $this->name,
  130. 'fiscal_code' => $this->fiscal_code,
  131. 'vat' => $this->vat,
  132. 'address' => $this->address,
  133. 'zip_code' => $this->zip_code,
  134. 'nation_id' => $this->nation_id,
  135. 'province_id' => $this->province_id > 0 ? $this->province_id : null,
  136. 'city_id' => $this->city_id > 0 ? $this->city_id : null,
  137. 'referent' => $this->referent,
  138. 'website' => $this->website,
  139. 'phone' => $this->phone,
  140. 'email' => $this->email,
  141. 'referent_first_name' => $this->referent_first_name,
  142. 'referent_last_name' => $this->referent_last_name,
  143. 'referent_email' => $this->referent_email,
  144. 'referent_phone' => $this->referent_phone,
  145. 'referent_mobile' => $this->referent_mobile,
  146. 'enabled' => $this->enabled
  147. ]);
  148. session()->flash('success','Fornitore creato');
  149. $this->resetFields();
  150. $this->add = false;
  151. $this->emit('setEdit', false);
  152. } catch (\Exception $ex) {
  153. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  154. }
  155. }
  156. public function edit($id){
  157. $this->emit('load-select');
  158. try {
  159. $supplier = \App\Models\Supplier::findOrFail($id);
  160. if( !$supplier) {
  161. session()->flash('error','Fornitore non trovato');
  162. } else {
  163. $this->name = $supplier->name;
  164. $this->fiscal_code = $supplier->fiscal_code;
  165. $this->vat = $supplier->vat;
  166. $this->address = $supplier->address;
  167. $this->zip_code = $supplier->zip_code;
  168. $this->nation_id = $supplier->nation_id;
  169. $this->province_id = $supplier->province_id;
  170. $this->city_id = $supplier->city_id;
  171. $this->referent = $supplier->referent;
  172. $this->website = $supplier->website;
  173. $this->phone = $supplier->phone;
  174. $this->email = $supplier->email;
  175. $this->referent_first_name = $supplier->referent_first_name;
  176. $this->referent_last_name = $supplier->referent_last_name;
  177. $this->referent_email = $supplier->referent_email;
  178. $this->referent_phone = $supplier->referent_phone;
  179. $this->referent_mobile = $supplier->referent_mobile;
  180. $this->enabled = $supplier->enabled;
  181. $this->dataId = $supplier->id;
  182. $this->update = true;
  183. $this->add = false;
  184. $this->checkIsItaly();
  185. $this->emit('load-provinces', $this->nation_id, 'provinceClass');
  186. $this->emit('load-cities', $this->province_id, 'cityClass');
  187. $this->emit('setEdit', true);
  188. }
  189. } catch (\Exception $ex) {
  190. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  191. }
  192. }
  193. public function update()
  194. {
  195. $this->validate();
  196. try {
  197. \App\Models\Supplier::whereId($this->dataId)->update([
  198. 'name' => $this->name,
  199. 'fiscal_code' => $this->fiscal_code,
  200. 'vat' => $this->vat,
  201. 'address' => $this->address,
  202. 'zip_code' => $this->zip_code,
  203. 'nation_id' => $this->nation_id,
  204. 'province_id' => $this->province_id > 0 ? $this->province_id : null,
  205. 'city_id' => $this->city_id > 0 ? $this->city_id : null,
  206. 'referent' => $this->referent,
  207. 'website' => $this->website,
  208. 'phone' => $this->phone,
  209. 'email' => $this->email,
  210. 'referent_first_name' => $this->referent_first_name,
  211. 'referent_last_name' => $this->referent_last_name,
  212. 'referent_email' => $this->referent_email,
  213. 'referent_phone' => $this->referent_phone,
  214. 'referent_mobile' => $this->referent_mobile,
  215. 'enabled' => $this->enabled
  216. ]);
  217. session()->flash('success','Fornitore aggiornato');
  218. $this->resetFields();
  219. $this->update = false;
  220. $this->emit('setEdit', false);
  221. } catch (\Exception $ex) {
  222. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  223. }
  224. }
  225. public function cancel()
  226. {
  227. $this->add = false;
  228. $this->update = false;
  229. $this->resetFields();
  230. $this->emit('setEdit', false);
  231. }
  232. public function delete($id)
  233. {
  234. try{
  235. \App\Models\Supplier::find($id)->delete();
  236. session()->flash('success',"Fornitore eliminato");
  237. return redirect(request()->header('Referer'));
  238. }catch(\Exception $e){
  239. session()->flash('error','Errore (' . $e->getMessage() . ')');
  240. }
  241. }
  242. public function getNation($nation)
  243. {
  244. if ($nation > 0)
  245. {
  246. $ret = \App\Models\Nation::findOrFail($nation);
  247. return $ret->name;
  248. }
  249. return "";
  250. }
  251. public function getProvince($province)
  252. {
  253. if ($province > 0)
  254. {
  255. $ret = \App\Models\Province::findOrFail($province);
  256. return $ret->name;
  257. }
  258. return "";
  259. }
  260. public function getCity($city)
  261. {
  262. if ($city > 0)
  263. {
  264. $ret = \App\Models\City::findOrFail($city);
  265. return $ret->name;
  266. }
  267. return "";
  268. }
  269. }