Supplier.php 12 KB

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