Supplier.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. }
  95. public function store()
  96. {
  97. $this->validate();
  98. $vatToCheck = trim($this->vat);
  99. $existingSupplier = \App\Models\Supplier::where('vat', $vatToCheck)->first();
  100. Log::info('VAT validation detailed debug', [
  101. 'vat_input' => $this->vat,
  102. 'vat_trimmed' => $vatToCheck,
  103. 'vat_empty_check' => empty($vatToCheck),
  104. 'existing_supplier_id' => $existingSupplier ? $existingSupplier->id : null,
  105. 'existing_supplier_name' => $existingSupplier ? $existingSupplier->name : null,
  106. 'existing_supplier_vat' => $existingSupplier ? $existingSupplier->vat : null,
  107. 'validation_rules' => $this->rules
  108. ]);
  109. if (!empty($vatToCheck)) {
  110. $duplicateCount = \App\Models\Supplier::where('vat', $vatToCheck)->count();
  111. if ($duplicateCount > 0) {
  112. Log::info('VAT duplicate found, adding error');
  113. $this->addError('vat', 'Già esiste un fornitore con questa Partita IVA');
  114. return;
  115. }
  116. }
  117. $fiscalCodeToCheck = trim($this->fiscal_code);
  118. if (!empty($fiscalCodeToCheck)) {
  119. $duplicateFiscalCount = \App\Models\Supplier::where('fiscal_code', $fiscalCodeToCheck)->count();
  120. if ($duplicateFiscalCount > 0) {
  121. Log::info('Fiscal code duplicate found, adding error');
  122. $this->addError('fiscal_code', 'Già esiste un fornitore con questo codice fiscale ');
  123. return;
  124. }
  125. }
  126. try {
  127. \App\Models\Supplier::create([
  128. 'name' => $this->name,
  129. 'fiscal_code' => $this->fiscal_code,
  130. 'vat' => $this->vat,
  131. 'address' => $this->address,
  132. 'zip_code' => $this->zip_code,
  133. 'nation_id' => $this->nation_id,
  134. 'province_id' => $this->province_id > 0 ? $this->province_id : null,
  135. 'city_id' => $this->city_id > 0 ? $this->city_id : null,
  136. 'referent' => $this->referent,
  137. 'website' => $this->website,
  138. 'phone' => $this->phone,
  139. 'email' => $this->email,
  140. 'referent_first_name' => $this->referent_first_name,
  141. 'referent_last_name' => $this->referent_last_name,
  142. 'referent_email' => $this->referent_email,
  143. 'referent_phone' => $this->referent_phone,
  144. 'referent_mobile' => $this->referent_mobile,
  145. 'enabled' => $this->enabled
  146. ]);
  147. session()->flash('success','Fornitore creato');
  148. $this->resetFields();
  149. $this->add = false;
  150. } catch (\Exception $ex) {
  151. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  152. }
  153. }
  154. public function edit($id){
  155. $this->emit('load-select');
  156. try {
  157. $supplier = \App\Models\Supplier::findOrFail($id);
  158. if( !$supplier) {
  159. session()->flash('error','Fornitore non trovato');
  160. } else {
  161. $this->name = $supplier->name;
  162. $this->fiscal_code = $supplier->fiscal_code;
  163. $this->vat = $supplier->vat;
  164. $this->address = $supplier->address;
  165. $this->zip_code = $supplier->zip_code;
  166. $this->nation_id = $supplier->nation_id;
  167. $this->province_id = $supplier->province_id;
  168. $this->city_id = $supplier->city_id;
  169. $this->referent = $supplier->referent;
  170. $this->website = $supplier->website;
  171. $this->phone = $supplier->phone;
  172. $this->email = $supplier->email;
  173. $this->referent_first_name = $supplier->referent_first_name;
  174. $this->referent_last_name = $supplier->referent_last_name;
  175. $this->referent_email = $supplier->referent_email;
  176. $this->referent_phone = $supplier->referent_phone;
  177. $this->referent_mobile = $supplier->referent_mobile;
  178. $this->enabled = $supplier->enabled;
  179. $this->dataId = $supplier->id;
  180. $this->update = true;
  181. $this->add = false;
  182. $this->checkIsItaly();
  183. $this->emit('load-provinces', $this->nation_id, 'provinceClass');
  184. $this->emit('load-cities', $this->province_id, 'cityClass');
  185. }
  186. } catch (\Exception $ex) {
  187. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  188. }
  189. }
  190. public function update()
  191. {
  192. $this->validate();
  193. try {
  194. \App\Models\Supplier::whereId($this->dataId)->update([
  195. 'name' => $this->name,
  196. 'fiscal_code' => $this->fiscal_code,
  197. 'vat' => $this->vat,
  198. 'address' => $this->address,
  199. 'zip_code' => $this->zip_code,
  200. 'nation_id' => $this->nation_id,
  201. 'province_id' => $this->province_id > 0 ? $this->province_id : null,
  202. 'city_id' => $this->city_id > 0 ? $this->city_id : null,
  203. 'referent' => $this->referent,
  204. 'website' => $this->website,
  205. 'phone' => $this->phone,
  206. 'email' => $this->email,
  207. 'referent_first_name' => $this->referent_first_name,
  208. 'referent_last_name' => $this->referent_last_name,
  209. 'referent_email' => $this->referent_email,
  210. 'referent_phone' => $this->referent_phone,
  211. 'referent_mobile' => $this->referent_mobile,
  212. 'enabled' => $this->enabled
  213. ]);
  214. session()->flash('success','Fornitore aggiornato');
  215. $this->resetFields();
  216. $this->update = false;
  217. } catch (\Exception $ex) {
  218. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  219. }
  220. }
  221. public function cancel()
  222. {
  223. $this->add = false;
  224. $this->update = false;
  225. $this->resetFields();
  226. }
  227. public function delete($id)
  228. {
  229. try{
  230. \App\Models\Supplier::find($id)->delete();
  231. session()->flash('success',"Fornitore eliminato");
  232. return redirect(request()->header('Referer'));
  233. }catch(\Exception $e){
  234. session()->flash('error','Errore (' . $e->getMessage() . ')');
  235. }
  236. }
  237. public function getNation($nation)
  238. {
  239. if ($nation > 0)
  240. {
  241. $ret = \App\Models\Nation::findOrFail($nation);
  242. return $ret->name;
  243. }
  244. return "";
  245. }
  246. public function getProvince($province)
  247. {
  248. if ($province > 0)
  249. {
  250. $ret = \App\Models\Province::findOrFail($province);
  251. return $ret->name;
  252. }
  253. return "";
  254. }
  255. public function getCity($city)
  256. {
  257. if ($city > 0)
  258. {
  259. $ret = \App\Models\City::findOrFail($city);
  260. return $ret->name;
  261. }
  262. return "";
  263. }
  264. }