Supplier.php 11 KB

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