Przeglądaj źródła

check codice fiscale p iva esistente

FabioFratini 7 miesięcy temu
rodzic
commit
70153cd40e

+ 119 - 66
app/Http/Livewire/Supplier.php

@@ -1,26 +1,35 @@
 <?php
 
 namespace App\Http\Livewire;
+
 use Livewire\Component;
+use Illuminate\Support\Facades\Auth;
+use Illuminate\Support\Facades\Log;
 
 class Supplier extends Component
 {
-    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;
-    public $referent_first_name, $referent_last_name, $referent_email, $referent_phone, $referent_mobile;
+    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;
+    public $referent_first_name, $referent_last_name, $referent_email, $referent_phone, $referent_mobile, $showReset = false;
     public $isItaly = true;
 
     public $searchTxt;
     public $search;
 
     protected $rules = [
-        'name' => 'required'
+        'name' => 'required',
+        'vat' => 'nullable|unique:suppliers,vat',
+        'fiscal_code' => 'nullable|unique:suppliers,fiscal_code'
     ];
 
     protected $messages = [
-        'name.required' => 'Il nome è obbligatorio'
+        'name.required' => 'Il nome è obbligatorio',
+        'vat.unique' => 'Un fornitore con questa Partita IVA esiste già',
+        'fiscal_code.unique' => 'Un fornitore con questo Codice Fiscale esiste già'
     ];
 
-    public function resetFields(){
+
+    public function resetFields()
+    {
         $this->name = '';
         $this->fiscal_code = '';
         $this->vat = '';
@@ -42,13 +51,12 @@ class Supplier extends Component
         $this->emit('load-data-table');
     }
 
-    public $sortField ='name';
+    public $sortField = 'name';
     public $sortAsc = true;
 
     public function sortBy($field)
     {
-        if($this->sortField === $field)
-        {
+        if ($this->sortField === $field) {
             $this->sortAsc = ! $this->sortAsc;
         } else {
             $this->sortAsc = true;
@@ -60,18 +68,16 @@ class Supplier extends Component
     public function mount()
     {
 
-        if(\Auth::user()->level != env('LEVEL_ADMIN', 0))
+        if (Auth::user()->level != env('LEVEL_ADMIN', 0))
             return redirect()->to('/dashboard');
 
         if (isset($_GET["new"]))
             $this->add();
-
     }
 
     public function search()
     {
-        if ($this->searchTxt != '')
-        {
+        if ($this->searchTxt != '') {
             $this->search = $this->searchTxt;
             $this->showReset = true;
         }
@@ -85,10 +91,7 @@ class Supplier extends Component
 
     public function render()
     {
-        /*if ($this->search != '')
-            $this->records = \App\Models\Supplier::with('nation')->where('name', 'LIKE', '%' . $this->search . '%')->orderBy($this->sortField, $this->sortAsc ? 'asc' : 'desc')->get();
-        else*/
-            $this->records = \App\Models\Supplier::with('nation')->get();
+        $this->records = \App\Models\Supplier::with('nation')->get();
         return view('livewire.supplier');
     }
 
@@ -111,14 +114,52 @@ class Supplier extends Component
         $this->update = false;
     }
 
+    private function cleanEmptyFields()
+    {
+        $this->vat = trim($this->vat) === '' ? null : trim($this->vat);
+        $this->fiscal_code = trim($this->fiscal_code) === '' ? null : trim($this->fiscal_code);
+    }
     public function store()
     {
         $this->validate();
+
+        $vatToCheck = trim($this->vat);
+        $existingSupplier = \App\Models\Supplier::where('vat', $vatToCheck)->first();
+
+        Log::info('VAT validation detailed debug', [
+            'vat_input' => $this->vat,
+            'vat_trimmed' => $vatToCheck,
+            'vat_empty_check' => empty($vatToCheck),
+            'existing_supplier_id' => $existingSupplier ? $existingSupplier->id : null,
+            'existing_supplier_name' => $existingSupplier ? $existingSupplier->name : null,
+            'existing_supplier_vat' => $existingSupplier ? $existingSupplier->vat : null,
+            'validation_rules' => $this->rules
+        ]);
+
+        if (!empty($vatToCheck)) {
+            $duplicateCount = \App\Models\Supplier::where('vat', $vatToCheck)->count();
+            if ($duplicateCount > 0) {
+                Log::info('VAT duplicate found, adding error');
+                $this->addError('vat', 'Un fornitore con questa Partita IVA esiste già');
+                return;
+            }
+        }
+
+        $fiscalCodeToCheck = trim($this->fiscal_code);
+        if (!empty($fiscalCodeToCheck)) {
+            $duplicateFiscalCount = \App\Models\Supplier::where('fiscal_code', $fiscalCodeToCheck)->count();
+            if ($duplicateFiscalCount > 0) {
+                Log::info('Fiscal code duplicate found, adding error');
+                $this->addError('fiscal_code', 'Un fornitore con questo Codice Fiscale esiste già');
+                return;
+            }
+        }
+
         try {
             \App\Models\Supplier::create([
                 'name' => $this->name,
-                'fiscal_code' => $this->fiscal_code,
-                'vat' => $this->vat,
+                'fiscal_code' => !empty($fiscalCodeToCheck) ? $fiscalCodeToCheck : null,
+                'vat' => !empty($vatToCheck) ? $vatToCheck : null,
                 'address' => $this->address,
                 'zip_code' => $this->zip_code,
                 'nation_id' => $this->nation_id,
@@ -135,20 +176,21 @@ class Supplier extends Component
                 'referent_mobile' => $this->referent_mobile,
                 'enabled' => $this->enabled
             ]);
-            session()->flash('success','Fornitore creato');
+            session()->flash('success', 'Fornitore creato');
             $this->resetFields();
             $this->add = false;
         } catch (\Exception $ex) {
-            session()->flash('error','Errore (' . $ex->getMessage() . ')');
+            session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
         }
     }
 
-    public function edit($id){
+    public function edit($id)
+    {
         $this->emit('load-select');
         try {
             $supplier = \App\Models\Supplier::findOrFail($id);
-            if( !$supplier) {
-                session()->flash('error','Fornitore non trovato');
+            if (!$supplier) {
+                session()->flash('error', 'Fornitore non trovato');
             } else {
                 $this->name = $supplier->name;
                 $this->fiscal_code = $supplier->fiscal_code;
@@ -176,39 +218,7 @@ class Supplier extends Component
                 $this->emit('load-cities', $this->province_id, 'cityClass');
             }
         } catch (\Exception $ex) {
-            session()->flash('error','Errore (' . $ex->getMessage() . ')');
-        }
-    }
-
-    public function update()
-    {
-        $this->validate();
-        try {
-            \App\Models\Supplier::whereId($this->dataId)->update([
-                'name' => $this->name,
-                'fiscal_code' => $this->fiscal_code,
-                'vat' => $this->vat,
-                'address' => $this->address,
-                'zip_code' => $this->zip_code,
-                'nation_id' => $this->nation_id,
-                'province_id' => $this->province_id > 0 ? $this->province_id : null,
-                'city_id' => $this->city_id > 0 ? $this->city_id : null,
-                'referent' => $this->referent,
-                'website' => $this->website,
-                'phone' => $this->phone,
-                'email' => $this->email,
-                'referent_first_name' => $this->referent_first_name,
-                'referent_last_name' => $this->referent_last_name,
-                'referent_email' => $this->referent_email,
-                'referent_phone' => $this->referent_phone,
-                'referent_mobile' => $this->referent_mobile,
-                'enabled' => $this->enabled
-            ]);
-            session()->flash('success','Fornitore aggiornato');
-            $this->resetFields();
-            $this->update = false;
-        } catch (\Exception $ex) {
-            session()->flash('error','Errore (' . $ex->getMessage() . ')');
+            session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
         }
     }
 
@@ -221,19 +231,18 @@ class Supplier extends Component
 
     public function delete($id)
     {
-        try{
+        try {
             \App\Models\Supplier::find($id)->delete();
-            session()->flash('success',"Fornitore eliminato");
+            session()->flash('success', "Fornitore eliminato");
             return redirect(request()->header('Referer'));
-        }catch(\Exception $e){
-            session()->flash('error','Errore (' . $ex->getMessage() . ')');
+        } catch (\Exception $e) {
+            session()->flash('error', 'Errore (' . $e->getMessage() . ')');
         }
     }
 
     public function getNation($nation)
     {
-        if ($nation > 0)
-        {
+        if ($nation > 0) {
             $ret = \App\Models\Nation::findOrFail($nation);
             return $ret->name;
         }
@@ -242,8 +251,7 @@ class Supplier extends Component
 
     public function getProvince($province)
     {
-        if ($province > 0)
-        {
+        if ($province > 0) {
             $ret = \App\Models\Province::findOrFail($province);
             return $ret->name;
         }
@@ -252,11 +260,56 @@ class Supplier extends Component
 
     public function getCity($city)
     {
-        if ($city > 0)
-        {
+        if ($city > 0) {
             $ret = \App\Models\City::findOrFail($city);
             return $ret->name;
         }
         return "";
     }
+
+    protected function getRulesForUpdate()
+    {
+        return [
+            'name' => 'required',
+            'vat' => 'nullable|unique:suppliers,vat,' . $this->dataId,
+            'fiscal_code' => 'nullable|unique:suppliers,fiscal_code,' . $this->dataId
+        ];
+    }
+
+    // Updated update method
+    public function update()
+    {
+        // Clean empty fields first
+        $this->cleanEmptyFields();
+
+        $this->validate($this->getRulesForUpdate());
+
+        try {
+            \App\Models\Supplier::whereId($this->dataId)->update([
+                'name' => $this->name,
+                'fiscal_code' => $this->fiscal_code,
+                'vat' => $this->vat,
+                'address' => $this->address,
+                'zip_code' => $this->zip_code,
+                'nation_id' => $this->nation_id,
+                'province_id' => $this->province_id > 0 ? $this->province_id : null,
+                'city_id' => $this->city_id > 0 ? $this->city_id : null,
+                'referent' => $this->referent,
+                'website' => $this->website,
+                'phone' => $this->phone,
+                'email' => $this->email,
+                'referent_first_name' => $this->referent_first_name,
+                'referent_last_name' => $this->referent_last_name,
+                'referent_email' => $this->referent_email,
+                'referent_phone' => $this->referent_phone,
+                'referent_mobile' => $this->referent_mobile,
+                'enabled' => $this->enabled
+            ]);
+            session()->flash('success', 'Fornitore aggiornato');
+            $this->resetFields();
+            $this->update = false;
+        } catch (\Exception $ex) {
+            session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
+        }
+    }
 }

+ 3 - 2
app/Http/Livewire/Vat.php

@@ -3,6 +3,7 @@
 namespace App\Http\Livewire;
 
 use Livewire\Component;
+use Illuminate\Support\Facades\Auth;
 
 class Vat extends Component
 {
@@ -23,7 +24,7 @@ class Vat extends Component
 
     public function mount(){
 
-        if(\Auth::user()->level != env('LEVEL_ADMIN', 0))
+        if(Auth::user()->level != env('LEVEL_ADMIN', 0))
             return redirect()->to('/dashboard');
 
     }
@@ -125,7 +126,7 @@ class Vat extends Component
             session()->flash('success',"IVA eliminata");
             return redirect(request()->header('Referer'));
         }catch(\Exception $e){
-            session()->flash('error','Errore (' . $ex->getMessage() . ')');
+            session()->flash('error','Errore (' . $e->getMessage() . ')');
         }
     }
 }

+ 8 - 25
resources/views/livewire/supplier.blade.php

@@ -49,29 +49,6 @@
                     @endforeach
                 </tbody>
             </table>
-            <!--
-            <div class="paginator d-flex justify-content-center">
-                <nav aria-label="Page navigation example">
-                    <ul class="pagination">
-                        <li class="page-item">
-                        <a class="page-link" href="#" aria-label="Previous">
-                            <span aria-hidden="true"></span>
-                        </a>
-                        </li>
-                        <li class="page-item"><a class="page-link" href="#">1</a></li>
-                        <li class="page-item"><a class="page-link" href="#">2</a></li>
-                        <li class="page-item"><a class="page-link" href="#">3</a></li>
-                        <li class="page-item"><a class="page-link" href="#">3</a></li>
-                        <li class="page-item"><span class="more-page">...</span></li>
-                        <li class="page-item">
-                        <a class="page-link" href="#" aria-label="Next">
-                            <span aria-hidden="true"></span>
-                        </a>
-                        </li>
-                    </ul>
-                    </nav>
-            </div>
-            -->
         </section>
 
     @else
@@ -149,13 +126,19 @@
                             <div class="col">
                                 <div class="form--item">
                                     <label for="fiscal_code" class="form-label">Codice fiscale</label>
-                                    <input class="form-control" type="text" id="fiscal_code" placeholder="Codice fiscale" wire:model="fiscal_code" maxlength="16">
+                                    <input class="form-control @error('fiscal_code') is-invalid @enderror" type="text" id="fiscal_code" placeholder="Codice fiscale" wire:model="fiscal_code" maxlength="16">
+                                    @error('fiscal_code')
+                                        <div class="invalid-feedback">{{ $message }}</div>
+                                    @enderror
                                 </div>
                             </div>
                             <div class="col">
                                 <div class="form--item">
                                     <label for="vat" class="form-label">Partita iva</label>
-                                    <input class="form-control" type="text" id="vat" placeholder="Partita iva" wire:model="vat" maxlength="11">
+                                    <input class="form-control @error('vat') is-invalid @enderror" type="text" id="vat" placeholder="Partita iva" wire:model="vat">
+                                    @error('vat')
+                                        <div class="invalid-feedback">{{ $message }}</div>
+                                    @enderror
                                 </div>
                             </div>
                         </div>