FabioFratini 11 месяцев назад
Родитель
Сommit
5cce3b29c4
1 измененных файлов с 128 добавлено и 21 удалено
  1. 128 21
      app/Http/Livewire/Istat.php

+ 128 - 21
app/Http/Livewire/Istat.php

@@ -209,18 +209,24 @@ class Istat extends Component
 
             $txt = array();
 
+            $txt = array();
+
             Log::info('Items: ' . count($items));
 
+            // Character counting variables
+            $charCounts = [];
+            $sectionCounts = [];
             foreach ($items as $item) {
                 try {
                     Log::info('Item: ' . $item->id);
                     $report = \App\Models\Report::findOrFail($item->id);
 
                     $item_data = '';
-
+                    $sectionLength = 0;
                     //1-10
                     try {
 
+                        $sectionStart = strlen($item_data);
 
                         $town_code = $prov_code = '000';
                         $town = \App\Models\LocationTown::findOrFail($item->localita_uno);
@@ -229,25 +235,28 @@ class Istat extends Component
                             $prov = \App\Models\LocationProvince::where('code', $town->prov)->first();
                             if ($prov && $prov->istat_code) $prov_code = $prov->istat_code;
                         }
-
                         $item_data .= date('y', strtotime($item->verificatosi_in_data));
                         $item_data .= date('m', strtotime($item->verificatosi_in_data));
+
                         $item_data .= str_pad($prov_code, 3, '0', STR_PAD_LEFT);
                         $item_data .= str_pad($town_code, 3, '0', STR_PAD_LEFT);
+
                         $item_data .= str_repeat(' ', 4);
                         $item_data .= date('d', strtotime($item->verificatosi_in_data));
                         $item_data .= str_repeat(' ', 2);
                         $item_data .= '4';
                         $item_data .= str_repeat(' ', 5);
                         $item_data .= ' ';
-                        Log::info('Item Data 1 - 10: ' . $item_data);
+                        $sectionLength = strlen($item_data) - $sectionStart;
+                        $sectionCounts['Section 1-10'] = $sectionLength;
+                        Log::info('Item Data 1-10 Length: ' . $sectionLength);
                     } catch (\Exception $e) {
                         Log::error('Error in section 1-10 for item ' . $item->id . ': ' . $e->getMessage());
                         $item_data = str_pad($item_data, 20, ' ');
                     }
                     //11-20
                     try {
-
+                        $sectionStart = strlen($item_data);
 
                         if ($item->localizzazione_incidente == Istat::LOCALIZZAZIONE_INCIDENTE_IN) {
                             switch ($item->nomenclatura_strada) {
@@ -280,8 +289,11 @@ class Istat extends Component
                         } else $loc = ' ';
 
                         $tipo_strada = ' ';
-                        $types_saved = \App\Models\ReportTipoStrada::where('report_id', $report->id)->select('tipo_strada_id')->get()->toArray();
-                        if ($types_saved) {
+                        $types_saved = \App\Models\ReportTipoStrada::where('report_id', $report->id)
+                            ->pluck('tipo_strada_id')
+                            ->toArray();
+
+                        if (!empty($types_saved)) {
                             if (in_array(3, $types_saved) && in_array(1, $types_saved))
                                 $tipo_strada = 1;
                             elseif (in_array(3, $types_saved) && (in_array(11, $types_saved) || in_array(2, $types_saved)))
@@ -289,7 +301,6 @@ class Istat extends Component
                             elseif (in_array(4, $types_saved))
                                 $tipo_strada = 3;
                         }
-
                         $pav = ' ';
                         if ($item->pavimentazione)
                             switch ($item->pavimentazione) {
@@ -301,7 +312,6 @@ class Istat extends Component
                                     if (!$item->condizione_strada || $item->condizione_strada == 1) $pav = 1;
                                     else $pav = 2;
                             }
-
                         $int = ' ';
                         switch ($item->particolarita_strada) {
                             case 1:
@@ -330,7 +340,6 @@ class Istat extends Component
                                 $int = '04';
                                 break;
                         }
-
                         $fon = ' ';
                         if ($item->fondo_stradale)
                             switch ($item->fondo_stradale) {
@@ -371,7 +380,6 @@ class Istat extends Component
                                     break;
                             }
 
-
                         $met = ' ';
                         if ($item->condizioni_atmosferiche)
                             switch ($item->condizioni_atmosferiche) {
@@ -404,7 +412,6 @@ class Istat extends Component
                                     break;
                             }
 
-
                         $item_data .= $loc;
                         $item_data .= str_pad($item->nomenclatura_strada_numero, 3, ' ', STR_PAD_RIGHT);
                         $item_data .= str_repeat(' ', 3);
@@ -415,15 +422,20 @@ class Istat extends Component
                         $item_data .= $fon;
                         $item_data .= $seg;
                         $item_data .= $met;
-                        Log::info('Item Data 11 - 20: ' . $item_data);
+                        $sectionLength = strlen($item_data) - $sectionStart;
+                        $sectionCounts['Section 11-20'] = $sectionLength;
+                        Log::info('Item Data 11-20 Length: ' . $sectionLength);
                     } catch (\Exception $e) {
                         Log::error('Error in section 11-20 for item ' . $item->id . ': ' . $e->getMessage());
                         $item_data = str_pad($item_data, 20, ' ');
                     }
                     try {
                         //21-30
+                        $sectionStart = strlen($item_data);
+
                         $tipo_urto = str_repeat(' ', 2);
-                        $types_saved = \App\Models\ReportTipoUrto::where('report_id', $report->id)->select('tipo_urto_id')->get()->toArray();
+                        $types_saved = \App\Models\ReportTipoUrto::where('report_id', $report->id)->pluck('tipo_urto_id')
+                            ->toArray();
                         if ($types_saved) {
                             if (in_array(1, $types_saved))
                                 $tipo_urto = '01';
@@ -446,7 +458,6 @@ class Istat extends Component
                             elseif (in_array(25, $types_saved) || in_array(22, $types_saved) || in_array(13, $types_saved))
                                 $tipo_urto = '12';
                         }
-
                         $item_data .= $tipo_urto;
 
                         $v_data = array(
@@ -463,14 +474,13 @@ class Istat extends Component
                         $feriti = array();
 
                         $data_veicoli = \App\Models\ReportDataVeicoli::where('report_id', $report->id)->get()->toArray();
-
                         for ($i = 0; $i < 3; $i++) {
                             $data_veicolo = isset($data_veicoli[$i]) ? $data_veicoli[$i] : false;
                             $stato_psico_fisico = str_repeat(' ', 2);
 
                             if ($data_veicolo) {
                                 if ($data_veicolo["veicoli"] > 0) {
-                                    $veicolo = \App\Models\Vehicle::findOrFail($data_veicolo["veicoli"]); //(ArrayHelper::getValue($data_veicolo, 'veicoli', 0));
+                                    $veicolo = \App\Models\Vehicle::findOrFail($data_veicolo["veicoli"]);
                                     $v_tipo = str_repeat(' ', 2);
                                     switch ($veicolo->tipo_id) {
                                         case 1:
@@ -528,6 +538,7 @@ class Istat extends Component
                                             break;
                                     }
 
+
                                     $v_data['tipo'] .= $v_tipo;
 
                                     $v_data['veicolo'] .= $veicolo->foreign_country ?
@@ -538,6 +549,7 @@ class Istat extends Component
                                         str_pad($veicolo->getCartaCircolazioneRilasciataDaDiValueAttribute(), 3, ' ', STR_PAD_RIGHT) :
                                         str_repeat(' ', 3);
 
+
                                     $time = strtotime($veicolo->carta_circolazione_rilasciata_il);
                                     $v_data['veicolo'] .= str_pad($time && $time > 0 ? date('y', $time) : '', 2, ' ', STR_PAD_RIGHT);
                                     $v_data['veicolo'] .= str_repeat(' ', 5);
@@ -642,7 +654,8 @@ class Istat extends Component
 
                                                 $eta_passeggero = '00';
                                                 if ($passeggero_anagrafica->birth_date && $report->verificatosi_in_data) {
-                                                    $eta_passeggero = 0; // * * *DateHelper::calculateAge($passeggero_anagrafica->birth_date,$report->verificatosi_in_data);
+                                                    $eta_passeggero = 0;
+                                                    DateHelper::calculateAge($passeggero_anagrafica->birth_date, $report->verificatosi_in_data);
                                                     if ($eta_passeggero == 0) $eta_passeggero = 1;
                                                     elseif ($eta_passeggero > 100 || $eta_passeggero < 0) $eta_passeggero = 0;
                                                     $eta_passeggero = str_pad($eta_passeggero, 2, '0', STR_PAD_LEFT);
@@ -781,21 +794,115 @@ class Istat extends Component
                         $item_data .= str_pad(count($deceduti_30gg), 2, '0', STR_PAD_LEFT);
                         $item_data .= str_pad(count($feriti), 2, '0', STR_PAD_LEFT);
                         $item_data .= str_repeat(' ', 9);
+                        $totalLength = strlen($item_data);
+                        $charCounts[] = $totalLength;
+                        Log::info('Total item data length: ' . $totalLength);
                     } catch (\Exception $e) {
                         Log::error('Error in section 21-30 for item ' . $item->id . ': ' . $e->getMessage());
                     }
 
+                    try {
+                        //31-40
+                        $sectionStart = strlen($item_data);
+                        $stradario_due = \App\Models\Stradario::find($report->localita_due);
+                        $location_description = $stradario_due ?
+                            (($stradario_due['TOPONIMO'] ?? '') . ' ' . ($stradario_due['DESCRIZIONE'] ?? '')) :
+                            '';
+
+                        if ($report->intersezione_con) {
+                            $intersezione = \App\Models\Stradario::find($report->intersezione_con);
+
+                            if ($intersezione) {
+                                $intersezione_toponimo = $intersezione['TOPONIMO'];
+                                $intersezione_address = $intersezione['DESCRIZIONE'];
+
+                                $full_intersection = $intersezione_toponimo . ' ' . $intersezione_address;
+                                $location_description .= ' INTERSEZIONE CON ' . $full_intersection;
+                            }
+                        }
+
+                        if ($report->prossimita_civico) {
+                            $location_description .= ' PROSSIMITA CIVICO ' . $report->prossimita_civico;
+                        }
+
+                        if ($report->prossimita_chilometro) {
+                            $location_description .= ' PROSSIMITA CHILOMETRO ' . $report->prossimita_chilometro;
+                        }
+                        $item_data .= str_pad(substr(preg_replace('/[^A-Za-z0-9 ]/', '', strtoupper($location_description)), 0, 57), 57, ' ');
+                        $item_data .= str_repeat(' ', 100);
+
+                        $deceduti = array_merge($deceduti_24h, $deceduti_30gg);
+                        for ($i = 0; $i < 4; $i++) {
+                            $deceduto = isset($deceduti[$i]) ? $deceduti[$i] : null;
+                            $deceduto_data = str_repeat(' ', 60);
+                            if ($deceduto) {
+                                $deceduto_data = sprintf(
+                                    '%s%s',
+                                    str_pad(substr($deceduto['firstname'], 0, 30), 30, ' '),
+                                    str_pad(substr($deceduto['lastname'], 0, 30), 30, ' ')
+                                );
+                            }
+                            $item_data .= $deceduto_data;
+                        }
+
+                        for ($i = 0; $i < 8; $i++) {
+                            $ferito = isset($feriti[$i]) ? $feriti[$i] : null;
+                            $ferito_data = str_repeat(' ', 90);
+                            if ($ferito) {
+                                $ferito_data = sprintf(
+                                    '%s%s%s',
+                                    str_pad(substr($ferito['firstname'], 0, 30), 30, ' '),
+                                    str_pad(substr($ferito['lastname'], 0, 30), 30, ' '),
+                                    str_pad(substr($ferito['ospedale'], 0, 30), 30, ' ')
+                                );
+                            }
+                            $item_data .= $ferito_data;
+                        }
+
+                        $item_data .= str_repeat(' ', 10);
+                        $item_data .= str_repeat(' ', 102);
+                        $item_data .= str_repeat(' ', 8);
+
+                        $ora = $report->verificatosi_in_data_ora ?? '';
+                        $ora = $ora === '' ? '25' : str_pad(substr($ora, 0, 2), 2, '0', STR_PAD_LEFT);
+                        $item_data .= $ora;
+
+                        $minuti = (int) ($report->verificatosi_in_data_minuti ?? 0);
+                        if ($ora == '25') {
+                            $minuti = '  ';
+                        }
+                        $item_data .= str_pad(substr($minuti, 0, 2), 2, '0', STR_PAD_LEFT);
+
+                        $item_data .= str_repeat(' ', 30);
+                        $item_data .= str_repeat('0', 7);
+                        $item_data .= str_repeat(' ', 15);
+                        $item_data .= str_repeat(' ', 4);
+                        $item_data .= str_repeat(' ', 100);
+
+                        $town = \App\Models\LocationTown::find( $report->localita_uno);
+                        Log::info('Town: ' . json_encode($town));
+                        $town_title = $town ? $town['istat_code'] : '';
+
+                        $item_data .= str_pad(substr($town_title, 0, 40), 40, ' ');
+                        $item_data .= str_repeat(' ', 40);
+                        $item_data .= str_repeat(' ', 10);
+
+                        $item_data .= $v_data['cittadinanza_conducente'];
+
+                        $totalLength = strlen($item_data);
+                        $charCounts[] = $totalLength;
+                        Log::info('Total item data length: ' . $totalLength);
+                    } catch (\Exception $e) {
+                        Log::error('Error in section 31-40 for item ' . $item->id . ': ' . $e->getMessage());
+                    }
+
                     $txt[] = $item_data;
-                    Log::info('Item Data: ' . $item_data);
                 } catch (\Exception $e) {
                     Log::error('Major error processing item ' . $item->id . ': ' . $e->getMessage());
                     continue;
                 }
             }
-            Log::info('Fuori loop');
-
             $txt_data = implode("\n", $txt);
-            Log::info('TXT Data: ' . $txt_data);
             return response()->streamDownload(function () use ($txt_data) {
                 echo  $txt_data;
             }, 'istat_' . $this->from . "_" . $this->to . '.txt');