Sfoglia il codice sorgente

check fornire p iva e cf

FabioFratini 7 mesi fa
parent
commit
868556567a
1 ha cambiato i file con 38 aggiunte e 7 eliminazioni
  1. 38 7
      app/Http/Livewire/Supplier.php

+ 38 - 7
app/Http/Livewire/Supplier.php

@@ -2,13 +2,14 @@
 
 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 $isItaly = true;
-
+    public $showReset = false;
     public $searchTxt;
     public $search;
 
@@ -60,7 +61,7 @@ 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"]))
@@ -85,9 +86,6 @@ 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();
         return view('livewire.supplier');
     }
@@ -114,6 +112,39 @@ class Supplier extends Component
     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', 'Già esiste un fornitore con questa Partita IVA');
+                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', 'Già esiste un fornitore con questo codice fiscale ');
+                return;
+            }
+        }
+
         try {
             \App\Models\Supplier::create([
                 'name' => $this->name,
@@ -226,7 +257,7 @@ class Supplier extends Component
             session()->flash('success',"Fornitore eliminato");
             return redirect(request()->header('Referer'));
         }catch(\Exception $e){
-            session()->flash('error','Errore (' . $ex->getMessage() . ')');
+            session()->flash('error','Errore (' . $e->getMessage() . ')');
         }
     }