Просмотр исходного кода

aggiunta validazione campi email iserimento entrata con ricevuta

ferrari 3 месяцев назад
Родитель
Сommit
aecefd2e2a
3 измененных файлов с 137 добавлено и 21 удалено
  1. 96 21
      app/Http/Livewire/RecordIN.php
  2. 31 0
      app/Models/Azienda.php
  3. 10 0
      resources/views/livewire/records_in.blade.php

+ 96 - 21
app/Http/Livewire/RecordIN.php

@@ -123,7 +123,6 @@ class RecordIN extends Component
     public function boot()
     {
         app(TenantMiddleware::class)->setupTenantConnection();
-
     }
 
     public function updatedMemberId()
@@ -551,7 +550,42 @@ class RecordIN extends Component
 
         $this->validate($rules);
 
+        /* Validazione campi azienda per generazione ricevuta */
+        try {
+            if (!$this->financial_movement && !$this->commercial) {
+                $no_receipt_causal_azienda = false;
+                foreach ($this->rows as $row) {
+                    $no_receipt_causal_azienda = false;
+
+                    $cau = \App\Models\Causal::findOrFail($row["causal_id"]);
+                    if ($cau->no_receipt)
+                        $no_receipt_causal_azienda = true;
+                }
+
+                $azienda = \App\Models\Azienda::first();
+
+                $payment_method = \App\Models\PaymentMethod::findOrFail($this->payment_method_id);
+                if (!$payment_method->money) {
+                    if (!$no_receipt_causal_azienda) {
+                        if (!$azienda->isValid()) {
+                            session()->flash('error_ricevuta', implode("", array_map(function ($item) {
+                                return "<li>$item</li>";
+                            }, array_keys($azienda->validate()))));
+                            return false;
+                        }
+                    }
+                }
+            }
+        } catch (\Exception $ex) {
+            Log::error("Error in store method: " . $ex->getMessage());
+            Log::error("Stack trace: " . $ex->getTraceAsString());
+            session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
+        }
+        /* END - Validazione campi azienda per generazione ricevuta */
+
         try {
+
+
             $totalGross = 0;
             $totalSconto = 0;
             $totalNet = 0;
@@ -675,7 +709,7 @@ class RecordIN extends Component
     }
 
 
-public function edit($id)
+    public function edit($id)
     {
         if (!isset($_GET["from"]) && $this->fromPage == '')
             $this->fromPage = 'in';
@@ -691,7 +725,7 @@ public function edit($id)
             } else {
                 if ($record->member_id) {
                     $member = \App\Models\Member::find($record->member_id);
-                    if (!$member || $member->status =='archived') {
+                    if (!$member || $member->status == 'archived') {
                         $this->member_id = null;
                     } else {
                         $this->member_id = $record->member_id;
@@ -770,7 +804,7 @@ public function edit($id)
         $visibleCausals = \App\Models\Causal::select('id', 'name')
             ->where('parent_id', null)
             ->where('type', 'IN')
-            ->where(function($query) {
+            ->where(function ($query) {
                 $query->where('hidden', false)->orWhereNull('hidden');
             })
             ->get();
@@ -779,7 +813,7 @@ public function edit($id)
         $this->payments = \App\Models\PaymentMethod::select('id', 'name')
             ->where('enabled', true)
             ->whereIn('type', array('ALL', 'IN'))
-            ->where(function($query) {
+            ->where(function ($query) {
                 $query->where('hidden', false)->orWhereNull('hidden');
             })
             ->orderBy('name')
@@ -787,7 +821,7 @@ public function edit($id)
 
         if ($this->commercial) {
             $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])
-                ->where(function($query) {
+                ->where(function ($query) {
                     $query->where('status', '!=', 'archived');
                 })
                 ->orderBy('last_name')
@@ -795,10 +829,10 @@ public function edit($id)
                 ->get();
         } else {
             $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])
-                ->where(function($query) {
+                ->where(function ($query) {
                     $query->where('current_status', 2)->orWhere('current_status', 1);
                 })
-                ->where(function($query) {
+                ->where(function ($query) {
                     $query->where('status', '!=', 'archived');
                 })
                 ->orderBy('last_name')
@@ -822,6 +856,39 @@ public function edit($id)
 
         $this->validate($rules);
 
+        /* Validazione campi azienda per generazione ricevuta */
+        try {
+            if (!$this->financial_movement && !$this->commercial) {
+                $no_receipt_causal_azienda = false;
+                foreach ($this->rows as $row) {
+                    $no_receipt_causal_azienda = false;
+
+                    $cau = \App\Models\Causal::findOrFail($row["causal_id"]);
+                    if ($cau->no_receipt)
+                        $no_receipt_causal_azienda = true;
+                }
+
+                $azienda = \App\Models\Azienda::first();
+
+                $payment_method = \App\Models\PaymentMethod::findOrFail($this->payment_method_id);
+                if (!$payment_method->money) {
+                    if (!$no_receipt_causal_azienda) {
+                        if (!$azienda->isValid()) {
+                            session()->flash('error_ricevuta', implode("", array_map(function ($item) {
+                                return "<li>$item</li>";
+                            }, array_keys($azienda->validate()))));
+                            return false;
+                        }
+                    }
+                }
+            }
+        } catch (\Exception $ex) {
+            Log::error("Error in store method: " . $ex->getMessage());
+            Log::error("Stack trace: " . $ex->getTraceAsString());
+            session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
+        }
+        /* END - Validazione campi azienda per generazione ricevuta */
+
         try {
             $totalGross = 0;
             $totalSconto = 0;
@@ -890,18 +957,22 @@ public function edit($id)
 
             Log::info("Final amount saved to record: " . $totalNet);
 
+            $receipt_created = true;
             if (!$this->financial_movement) {
                 $payment_method = \App\Models\PaymentMethod::findOrFail($this->payment_method_id);
                 if (!$payment_method->money) {
                     if ($generate && !$no_receipt_causal)
-                        $this->createReceipt();
+                        $receipt_created = $this->createReceipt();
                 }
             }
-            session()->flash('success', 'Movimento aggiornato');
-            $this->resetFields();
-            $this->update = false;
-            $this->isDuplicate = false;
-            $this->emit('setEdit', false);
+
+            if ($receipt_created) {
+                session()->flash('success', 'Movimento aggiornato');
+                $this->resetFields();
+                $this->update = false;
+                $this->isDuplicate = false;
+                $this->emit('setEdit', false);
+            }
         } catch (\Exception $ex) {
             Log::error("Error in update method: " . $ex->getMessage());
             Log::error("Stack trace: " . $ex->getTraceAsString());
@@ -930,13 +1001,16 @@ public function edit($id)
 
             $this->dataId = $newRecord->id;
 
-            $this->createReceipt();
 
-            session()->flash('success', 'Movimento aggiornato');
-            $this->resetFields();
-            $this->update = false;
-            $this->isDuplicate = false;
-            $this->emit('setEdit', false);
+            $receipt_created = $this->createReceipt();
+
+            if ($receipt_created) {
+                session()->flash('success', 'Movimento aggiornato');
+                $this->resetFields();
+                $this->update = false;
+                $this->isDuplicate = false;
+                $this->emit('setEdit', false);
+            }
         } catch (\Exception $ex) {
             session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
         }
@@ -1064,7 +1138,6 @@ public function edit($id)
 
     public function createReceipt()
     {
-
         // Ulteriore controllo commerciale/non commerciale
         if (!$this->commercial) {
             $create = false;
@@ -1139,6 +1212,8 @@ public function edit($id)
                 $this->emit('showReceipt', $this->currentReceip->id);
             }
         }
+
+        return true;
     }
 
     public function removeReceipt()

+ 31 - 0
app/Models/Azienda.php

@@ -4,6 +4,7 @@ namespace App\Models;
 
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Support\Facades\Validator;
 
 class Azienda extends Model
 {
@@ -45,6 +46,36 @@ class Azienda extends Model
         'scadenza_pagamenti_uscita' => 'date',
     ];
 
+    /**
+     * Valida i campi richiesti prima della generazione del PDF.
+     *
+     * @return array|bool  true se valido, oppure array di errori se non valido
+     */
+    public function validate()
+    {
+        $rules = [
+            'ragione_sociale' => 'required|string|max:255',
+            'email' => 'required|email|max:255',
+            'pec' => 'required|email|max:255',
+            'cellulare' => 'required|string|max:20',
+        ];
+
+        $validator = Validator::make($this->attributesToArray(), $rules);
+
+        if ($validator->fails()) {
+            return $validator->errors()->toArray();
+        }
+
+        return true;
+    }
+
+    /**
+     * Restituisce true se l’azienda è valida (tutti i campi richiesti presenti).
+     */
+    public function isValid()
+    {
+        return $this->validate() === true;
+    }
 
     /**
      * Get the logo URL attribute.

+ 10 - 0
resources/views/livewire/records_in.blade.php

@@ -171,6 +171,16 @@
                 {{ session()->get('error') }}
             </div>
         @endif
+        @if (session()->has('error_ricevuta'))
+            <div class="alert alert-danger" role="alert">
+                <span>Errore durante la creazione della ricevuta.</span><br/>
+                <span>Per creare correttamente la ricevuta è necessario compilare i dati della <b><a href="/azienda" target="_blank" style="color: var(--color-blu)">Società</a></b> correttamente.</span><br/>
+                <span>Dati mancanti:</span>
+                <ul>
+                    {!! session()->get('error_ricevuta') !!}
+                </ul>
+            </div>
+        @endif
 
 
         <section id="accountingEntry" class="d-flex">