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

fix aggiornamento conducente e veicoli fix switch da conducente a pedone

FabioFratini 1 год назад
Родитель
Сommit
9d1a889bb8
2 измененных файлов с 219 добавлено и 80 удалено
  1. 167 37
      app/Http/Livewire/Report.php
  2. 52 43
      resources/views/pdf/verbale.blade.php

+ 167 - 37
app/Http/Livewire/Report.php

@@ -9,7 +9,9 @@ use Livewire\WithPagination;
 use Livewire\WithFileUploads;
 
 use Barryvdh\DomPDF\Facade\Pdf;
-
+use App\Models\ReportDataPartiCoinvolte;
+use App\Models\ReportDataVeicoli;
+use App\Models\Vehicle;
 class Report extends Component
 {
 
@@ -1821,73 +1823,201 @@ class Report extends Component
         $this->allegatiDocumenti = \App\Models\ReportAllegatiGallery::where('report_id',  $this->dataId)->where('file_type', 1)->orderBy('name')->get();
     }
 
-    public function addParteCoinvolta($type = 'pedone')
-    {
-        $count = \App\Models\ReportDataPartiCoinvolte::where('report_id', $this->dataId)->count();
-        $progressive = $count == 0 ? 0 : $count;
-
-        \App\Models\ReportDataPartiCoinvolte::create([
-            'report_id' => $this->dataId,
-            'progressive' => $progressive,
-            'progressive_type' => $progressive,
-            'conducente_o_pedone' => $type === 'veicolo' ? 2 : 1
-        ]);
-
-        $this->parti_coinvolte = \App\Models\ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
-    }
 
     public function changeAnagType($type)
     {
         $this->anag_type = $type;
     }
 
+    private function recalculateProgressives()
+    {
+        $parti = ReportDataPartiCoinvolte::where('report_id', $this->dataId)
+            ->orderBy('progressive')
+            ->get();
+
+        foreach($parti as $index => $parte) {
+            if($parte->progressive != $index) {
+                $parte->progressive = $index;
+                $parte->save();
+
+                // Update corresponding vehicle data if exists
+                ReportDataVeicoli::where('report_id', $this->dataId)
+                    ->where('progressive', $parte->progressive)
+                    ->update(['progressive' => $index]);
+            }
+        }
+    }
+
     public function updateConducentePedoneParteCoinvolta($conducente_o_pedone, $progressive)
     {
-        \App\Models\ReportDataPartiCoinvolte::where('report_id', $this->dataId)->where('progressive', $progressive)->update([
-            'conducente_o_pedone' => $conducente_o_pedone
-        ]);
-        $this->parti_coinvolte = \App\Models\ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
+        // Update parti coinvolte
+        ReportDataPartiCoinvolte::where('report_id', $this->dataId)
+            ->where('progressive', $progressive)
+            ->update(['conducente_o_pedone' => $conducente_o_pedone]);
 
+        // If switching to conducente (2), create vehicle record
+        if ($conducente_o_pedone === 2) {
+            ReportDataVeicoli::updateOrCreate(
+                [
+                    'report_id' => $this->dataId,
+                    'progressive' => $progressive,
+                ],
+                [
+                    'conducente_uguale_proprietario' => false
+                ]
+            );
+        }
+        // If switching to pedone (1), remove vehicle record
+        else {
+            ReportDataVeicoli::where('report_id', $this->dataId)
+                ->where('progressive', $progressive)
+                ->delete();
+        }
+
+        $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
     }
 
     public function updateAnagraficaParteCoinvolta($anagrafica, $progressive)
     {
-        \App\Models\ReportDataPartiCoinvolte::where('report_id', $this->dataId)->where('progressive', $progressive)->update([
-            'anagrafica' => $anagrafica
-        ]);
-        $this->parti_coinvolte = \App\Models\ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
+        $parte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)
+            ->where('progressive', $progressive)
+            ->first();
+
+        if ($parte) {
+            $parte->update(['anagrafica' => $anagrafica]);
+
+            // Only update vehicle data if it's a conducente
+            if ($parte->conducente_o_pedone == 2) {
+                ReportDataVeicoli::updateOrCreate(
+                    [
+                        'report_id' => $this->dataId,
+                        'progressive' => $progressive,
+                    ],
+                    [
+                        'conducenti' => $anagrafica
+                    ]
+                );
+            }
+        }
 
+        $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
     }
 
     public function removeAnagraficaParteCoinvolta($progressive)
     {
-        \App\Models\ReportDataPartiCoinvolte::where('report_id', $this->dataId)->where('progressive', $progressive)->update([
-            'anagrafica' => 0
-        ]);
-        $this->parti_coinvolte = \App\Models\ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
+        $parte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)
+            ->where('progressive', $progressive)
+            ->first();
+
+        if ($parte) {
+            $parte->update(['anagrafica' => 0]);
+
+            // Only update vehicle data if it's a conducente
+            if ($parte->conducente_o_pedone == 2) {
+                ReportDataVeicoli::where('report_id', $this->dataId)
+                    ->where('progressive', $progressive)
+                    ->update([
+                        'conducenti' => null,
+                        'conducente_uguale_proprietario' => null
+                    ]);
+            }
+        }
+
+        $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
     }
 
     public function updateVeicoloParteCoinvolta($veicolo, $progressive)
     {
-        \App\Models\ReportDataPartiCoinvolte::where('report_id', $this->dataId)->where('progressive', $progressive)->update([
-            'veicolo' => $veicolo
-        ]);
-        $this->parti_coinvolte = \App\Models\ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
+        $parte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)
+            ->where('progressive', $progressive)
+            ->first();
+
+        if ($parte && $parte->conducente_o_pedone == 2) {
+            $parte->update(['veicolo' => $veicolo]);
+
+            $vehicleInfo = Vehicle::find($veicolo);
+            if ($vehicleInfo) {
+                ReportDataVeicoli::updateOrCreate(
+                    [
+                        'report_id' => $this->dataId,
+                        'progressive' => $progressive,
+                    ],
+                    [
+                        'veicoli' => $veicolo,
+                    ]
+                );
+            }
+        }
 
+        $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
     }
 
     public function removeVeicoloParteCoinvolta($progressive)
     {
-        \App\Models\ReportDataPartiCoinvolte::where('report_id', $this->dataId)->where('progressive', $progressive)->update([
-            'veicolo' => 0
-        ]);
-        $this->parti_coinvolte = \App\Models\ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
+        $parte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)
+            ->where('progressive', $progressive)
+            ->first();
+
+        if ($parte) {
+            $parte->update(['veicolo' => 0]);
+
+            if ($parte->conducente_o_pedone == 2) {
+                ReportDataVeicoli::where('report_id', $this->dataId)
+                    ->where('progressive', $progressive)
+                    ->update([
+                        'veicoli' => null,
+                        'conducenti' => null,
+                        'conducente_uguale_proprietario' => null
+                    ]);
+            }
+        }
+
+        $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
     }
 
     public function removeParteCoinvolta($progressive)
     {
-        \App\Models\ReportDataPartiCoinvolte::where('report_id', $this->dataId)->where('progressive', $progressive)->delete();
-        $this->parti_coinvolte = \App\Models\ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
+        $parte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)
+            ->where('progressive', $progressive)
+            ->first();
+
+        if ($parte) {
+            // Delete the parti coinvolte record
+            $parte->delete();
+
+            // Delete corresponding vehicle data if exists
+            ReportDataVeicoli::where('report_id', $this->dataId)
+                ->where('progressive', $progressive)
+                ->delete();
+
+            // Recalculate progressives
+            $this->recalculateProgressives();
+        }
+
+        $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
+    }
+
+    public function addParteCoinvolta($type = 'pedone')
+    {
+        $count = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->count();
+        $progressive = $count == 0 ? 0 : $count;
+
+        ReportDataPartiCoinvolte::create([
+            'report_id' => $this->dataId,
+            'progressive' => $progressive,
+            'progressive_type' => $progressive,
+            'conducente_o_pedone' => $type === 'veicolo' ? 2 : 1
+        ]);
+
+        // Only create vehicle record if it's a veicolo type
+        if ($type === 'veicolo') {
+            ReportDataVeicoli::create([
+                'report_id' => $this->dataId,
+                'progressive' => $progressive
+            ]);
+        }
+
+        $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
     }
 
     public function editAnagraficaParte($progressive)

+ 52 - 43
resources/views/pdf/verbale.blade.php

@@ -1,6 +1,7 @@
 @php
     use App\Helpers\PoliziaUtils;
     use App\Helpers\DateHelper;
+
 @endphp
 <!DOCTYPE html>
 <html>
@@ -160,7 +161,7 @@
     <div class="page-break"></div>
 
     @if (
-            $data['record']->accertatore_1 > 0 ||
+        $data['record']->accertatore_1 > 0 ||
             $data['record']->accertatore_2 > 0 ||
             $data['record']->accertatore_3 > 0 ||
             $data['record']->accertatore_4 > 0 ||
@@ -1332,7 +1333,12 @@
             $datiCascoOmologazione = $data['datiCascoOmologazione'][$parte->progressive];
             $datiCasco = $data['datiCasco'][$parte->progressive];
             $datiCintureSicurezza = $data['datiCintureSicurezza'][$parte->progressive];
-
+            $vehicleNumber = 0;
+            foreach($data['parti_coinvolte'] as $prog => $parteCoinvolta) {
+                if($prog < $parte->progressive && $parteCoinvolta->conducente_o_pedone == 2) {
+                    $vehicleNumber++;
+                }
+            }
         @endphp
 
         @if ($datiVeicolo)
@@ -1355,7 +1361,7 @@
                         <table class="">
                             <tr class="tr-title">
                                 <td align="center" class="no-border">VEICOLO
-                                    {{ PoliziaUtils::numberToLetter(@$parte->progressive) }}</td>
+                                    {{ PoliziaUtils::numberToLetter(@$vehicleNumber) }}</td>
                             </tr>
                         </table>
                     </td>
@@ -1521,7 +1527,7 @@
 
                                 <tr>
                                     <td width="15%" class="veicolo-title-cell">NATO A</td>
-                                    @if ($datiVeicolo->proprietario->nazionalita == 1)
+                                    @if (optional($datiVeicolo->proprietario)->nazionalita == 1)
                                         <td width="25%" class="veicolo-content-cell">
                                             {{ @$datiVeicolo->proprietario->birth_town_id_value }}</td>
                                     @else
@@ -1533,7 +1539,7 @@
                                         {{ DateHelper::formatDate(@$datiVeicolo->proprietario->birth_date) }}</td>
                                 </tr>
 
-                                @if (!$datiVeicolo->proprietario->residenza_town_id)
+                                @if (!@$datiVeicolo->proprietario->residenza_town_id)
                                     <tr>
                                         <td width="15%" class="veicolo-title-cell">RESIDENTE A</td>
                                         <td width="25%" class="veicolo-content-cell">
@@ -1544,10 +1550,10 @@
                                     </tr>
                                 @endif
 
-                                @if (!$datiVeicolo->proprietario->foreign_country)
+                                @if (!@$datiVeicolo->proprietario->foreign_country)
                                     @php
                                         $rilDa = '';
-                                        switch ($datiVeicolo->proprietario->documento_rilasciato_da) {
+                                        switch (@$datiVeicolo->proprietario->documento_rilasciato_da) {
                                             case 1:
                                                 $rilDa = 'Comune';
                                                 break;
@@ -1590,7 +1596,7 @@
                                     </tr>
                                 @endif
 
-                                @if ($datiVeicolo->proprietario->mobile != '')
+                                @if (@$datiVeicolo->proprietario->mobile != '')
                                     <tr>
                                         <td width="15%" class="veicolo-title-cell">TELEFONO</td>
                                         <td width="25%" class="veicolo-content-cell">
@@ -1606,7 +1612,7 @@
             <table class="table-content" width="100%">
                 <tr>
                     <td class="bold" style="font-size: 20px; padding-bottom: 10px;">
-                        {{ $datiVeicolo->conducente_uguale_proprietario == 0 ? 'CONDUCENTE' : 'CONDUCENTE E PROPRIETARIO' }}
+                        {{ @$datiVeicolo->conducente_uguale_proprietario == 0 ? 'CONDUCENTE' : 'CONDUCENTE E PROPRIETARIO' }}
                     </td>
                 </tr>
 
@@ -1615,58 +1621,60 @@
                         <table width="100%">
                             <tr>
                                 <td class="veicolo-title-cell" width="20%">COGNOME:</td>
-                                <td class="veicolo-content-cell">{{ $datiVeicolo->conducente->lastname ?? '' }}</td>
+                                <td class="veicolo-content-cell">{{ @$datiVeicolo->conducente->lastname ?? '' }}</td>
                             </tr>
                             <tr>
                                 <td class="veicolo-title-cell">NOME:</td>
-                                <td class="veicolo-content-cell">{{ $datiVeicolo->conducente->firstname ?? '' }}</td>
+                                <td class="veicolo-content-cell">{{ @$datiVeicolo->conducente->firstname ?? '' }}
+                                </td>
                             </tr>
                             <tr>
                                 <td class="veicolo-title-cell">RAGIONE SOCIALE:</td>
-                                <td class="veicolo-content-cell">{{ $datiVeicolo->proprietario->rag_soc ?? '' }}</td>
+                                <td class="veicolo-content-cell">{{ @$datiVeicolo->proprietario->rag_soc ?? '' }}
+                                </td>
                             </tr>
                             <tr>
                                 <td class="veicolo-title-cell">NATO A:</td>
                                 <td class="veicolo-content-cell">
-                                    @if ($datiVeicolo->conducente->nazionalita == 1)
-                                        {{ $datiVeicolo->conducente->birth_town_id_value ?? '' }}
+                                    @if (@$datiVeicolo->conducente->nazionalita == 1)
+                                        {{ @$datiVeicolo->conducente->birth_town_id_value ?? '' }}
                                     @else
-                                        {{ $datiVeicolo->conducente->localita_straniera ?? '' }}
+                                        {{ @$datiVeicolo->conducente->localita_straniera ?? '' }}
                                     @endif
                                 </td>
                             </tr>
                             <tr>
                                 <td class="veicolo-title-cell">NATO IL:</td>
                                 <td class="veicolo-content-cell">
-                                    {{ DateHelper::formatDate($datiVeicolo->conducente->birth_date ?? null) }}</td>
+                                    {{ DateHelper::formatDate(@$datiVeicolo->conducente->birth_date ?? null) }}</td>
                             </tr>
 
-                            @if (!$datiVeicolo->conducente->residenza_town_id)
+                            @if (!@$datiVeicolo->conducente->residenza_town_id)
                                 <tr>
                                     <td class="veicolo-title-cell">RESIDENTE A:</td>
                                     <td class="veicolo-content-cell">
-                                        {{ $datiVeicolo->conducente->residenza_town_id_value ?? '' }}</td>
+                                        {{ @$datiVeicolo->conducente->residenza_town_id_value ?? '' }}</td>
                                 </tr>
                                 <tr>
                                     <td class="veicolo-title-cell">RESIDENTE IN:</td>
                                     <td class="veicolo-content-cell">
-                                        {{ $datiVeicolo->conducente->residenza_address ?? '' }}</td>
+                                        {{ @$datiVeicolo->conducente->residenza_address ?? '' }}</td>
                                 </tr>
                             @endif
 
                             <tr>
                                 <td class="veicolo-title-cell">TELEFONO:</td>
-                                <td class="veicolo-content-cell">{{ $datiVeicolo->conducente->mobile ?? '' }}</td>
+                                <td class="veicolo-content-cell">{{ @$datiVeicolo->conducente->mobile ?? '' }}</td>
                             </tr>
                             <tr>
                                 <td class="veicolo-title-cell">INDIRIZZO SEDE LEGALE:</td>
                                 <td class="veicolo-content-cell">
-                                    {{ $datiVeicolo->proprietario->sede_legale_address ?? '' }}</td>
+                                    {{ @$datiVeicolo->proprietario->sede_legale_address ?? '' }}</td>
                             </tr>
                             <tr>
                                 <td class="veicolo-title-cell">COMUNE SEDE LEGALE:</td>
                                 <td class="veicolo-content-cell">
-                                    {{ $datiVeicolo->proprietario->sede_legale_town_id_value->title ?? '' }}</td>
+                                    {{ @$datiVeicolo->proprietario->sede_legale_town_id_value->title ?? '' }}</td>
                             </tr>
                             <tr>
                                 <td class="veicolo-title-cell">CONDUCENTE INFORTUNATO:</td>
@@ -1679,7 +1687,7 @@
                                             3 => 'DECEDUTO ENTRO 30 GG.',
                                         ];
                                     @endphp
-                                    {{ $infortunatoStatus[$datiVeicolo->infortunato] ?? '' }}
+                                    {{ $infortunatoStatus[@$datiVeicolo->infortunato] ?? '' }}
                                 </td>
                             </tr>
                             <tr>
@@ -2949,28 +2957,29 @@
     @endif
 
     @if (isset($data['allegati']) && count($data['allegati']) > 0)
-    <table width="100%">
-        <tr>
-            <td width="100%" valign="top" align="center">
-                <h2>ALLEGATI</h2>
-            </td>
-        </tr>
-    </table>
+        <table width="100%">
+            <tr>
+                <td width="100%" valign="top" align="center">
+                    <h2>ALLEGATI</h2>
+                </td>
+            </tr>
+        </table>
 
-    @foreach ($data['allegati'] as $allegato)
-        @if ($allegato->is_visible && $allegato->files != '')
-            @foreach (explode('|', $allegato->files) as $file)
-                @php
-                    $filePath = storage_path('app/public/' . $file);
-                @endphp
+        @foreach ($data['allegati'] as $allegato)
+            @if ($allegato->is_visible && $allegato->files != '')
+                @foreach (explode('|', $allegato->files) as $file)
+                    @php
+                        $filePath = storage_path('app/public/' . $file);
+                    @endphp
 
-                @if (file_exists($filePath) && in_array(pathinfo($filePath, PATHINFO_EXTENSION), ['jpg', 'jpeg', 'png', 'gif']))
-                    <img src="{{ $filePath }}" alt="{{ $file }}" style="max-width: 100%; height: auto; margin-bottom: 10px;"><br>
-                @endif
-            @endforeach
-        @endif
-    @endforeach
-@endif
+                    @if (file_exists($filePath) && in_array(pathinfo($filePath, PATHINFO_EXTENSION), ['jpg', 'jpeg', 'png', 'gif']))
+                        <img src="{{ $filePath }}" alt="{{ $file }}"
+                            style="max-width: 100%; height: auto; margin-bottom: 10px;"><br>
+                    @endif
+                @endforeach
+            @endif
+        @endforeach
+    @endif
 
 
     <table width="100%">