| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934 |
- <?php
- namespace App\Http\Livewire;
- use App\Helpers\DateHelper;
- use App\Models\Anagrafica;
- use App\Models\ReportDataPedoni;
- use Illuminate\Support\Facades\Log;
- use Livewire\Component;
- class Istat extends Component
- {
- const LOCALIZZAZIONE_INCIDENTE_IN = 1;
- const LOCALIZZAZIONE_INCIDENTE_OUT = 2;
- const VISIBILITA_BUONA = 1;
- const VISIBILITA_LIMITATA = 2;
- const FONDO_STRADALE_SPECIFICA_OPTION_VALUE = 5;
- const PARTICOLARITA_STRADA_SPECIFICA_OPTION_VALUE = 5;
- const TRAFFICO_NORMALE = 1;
- const TRAFFICO_SCARSO = 2;
- const TRAFFICO_INTENSO = 3;
- const TIPO_STRADA_ALTRO_OPTION_VALUE = 8;
- const SEGNALETICA_ASSENTE = 1;
- const SEGNALETICA_VERTICALE = 2;
- const SEGNALETICA_ORIZZONTALE = 3;
- const SEGNALETICA_VERTICALE_ORIZZONTALE = 4;
- const SEGNALETICA_TEMPORANEA_CANTIERE = 5;
- const SEGNALETICA_VERTICALE_LIMITE_VELOCITA_OPTION_VALUE = 21;
- const SEGNALETICA_VERTICALE_PANNELLO_INTEGRATIVO_OPTION_VALUE = 22;
- const SEGNALETICA_VERTICALE_ALTRO_OPTION_VALUE = 23;
- const LINEA_CONTINUA = 1;
- const LINEA_TRATTEGGIATA = 2;
- const LINEA_TRATTEGGIATA_IN_CORRISPONDENZA = 3;
- const SEGNALETICA_ORIZZONTALE_LINEA_ASSENTE = 4;
- const MAX_PARTI_COINVOLTE = 10;
- const MAX_VEHICLES = 4;
- const MAX_PASSEGGERI = 4;
- const MAX_INFORTUNATI = 4;
- const MAX_PEDONI = 4;
- const MAX_TESTI_OCULARI = 4;
- const STATO_VEICOLO_MARCIA = 1;
- const STATO_VEICOLO_SOSTA = 2;
- const STATO_VEICOLO_NON_PRESENTE = 3;
- const POSIZIONE_STATICA_FINALE_DOCUMENTAZIONE_FOTOGRAFICA = 1;
- const POSIZIONE_STATICA_FINALE_RILIEVO_PLANIMETRICO = 2;
- const POSIZIONE_STATICA_FINALE_SCHIZZO_VISTA_NO_SCALA = 3;
- const POSIZIONE_STATICA_FINALE_GIA_RIMOSSO = 4;
- const DESTINAZIONE_VEICOLO_RIMOSSO_SENSI_ART_2028_CC = 1;
- const DESTINAZIONE_VEICOLO_SEQUESTRO_ART_354_CPP = 2;
- const DESTINAZIONE_VEICOLO_SEQUESTRO_ART_321_CPP = 3;
- const DESTINAZIONE_VEICOLO_SEQUESTRO = 4;
- const DESTINAZIONE_VEICOLO_SEQUESTRO_AMMINISTRATIVO = 5;
- const DESTINAZIONE_VEICOLO_FERMO_AMMINISTRATIVO = 6;
- const DESTINAZIONE_VEICOLO_RITIRATO_DAL_CONDUCENTE = 7;
- const STATO_PNEUMATICI_REGOLAMENTARE = 1;
- const STATO_PNEUMATICI_BUONO = 2;
- const STATO_PNEUMATICI_SUFFICIENTE = 3;
- const STATO_PNEUMATICI_INSUFFICIENTE = 4;
- const STATO_PNEUMATICI_NON_RILEVATO = 5;
- const STATO_PRESENTE = 1;
- const STATO_NON_PRESENTE = 2;
- const STATO_FUNZIONANTE = 1;
- const STATO_NON_FUNZIONANTE = 2;
- const STATO_NON_RILEVATO = 3;
- const YES = 1;
- const NO = 0;
- const SI_RISERVA = 3;
- const PATENTE_AL_SEGUITO_NON_PREVISTA = 3;
- const CONDUCENTE_GENERALIZZATO_LUOGO = 1;
- const CONDUCENTE_GENERALIZZATO_OSPEDALE = 2;
- const CONDUCENTE_GENERALIZZATO_ALTRO = 3;
- const INFORTUNATO_INCOLUME = 0;
- const INFORTUNATO_FERITO = 1;
- const INFORTUNATO_DECEDUTO_24H = 2;
- const INFORTUNATO_DECEDUTO_30GG = 3;
- const MARCIA_OPTION_N = 0;
- const MARCIA_OPTION_1 = 1;
- const MARCIA_OPTION_2 = 2;
- const MARCIA_OPTION_3 = 3;
- const MARCIA_OPTION_4 = 4;
- const MARCIA_OPTION_5 = 5;
- const MARCIA_OPTION_6 = 6;
- const MARCIA_OPTION_R = 7;
- const MARCIA_NON_RILEVATA = 8;
- const MARCIA_OPTION_AUTO = 9;
- const CRISTALLO_OPTION_INTERNO = 1;
- const CRISTALLO_OPTION_ESTERNO = 2;
- const CRISTALLO_OPTION_ALTRO = 3;
- const ESITO_NEGATIVO = 1;
- const ESITO_POSITIVO = 2;
- const DOCUMENTO_CI = 1;
- const DOCUMENTO_PAT = 2;
- const DOCUMENTO_PP = 3;
- const DOCUMENTO_ALTRO = 4;
- const PEDONE_PRESENTE_E_POSIZIONE = 1;
- const PEDONE_SOCCORSO_E_PRESSO = 2;
- const TESTE_PRESENTE_MOMENTO_RILIEVO = 1;
- const TESTE_NON_PRESENTE_MOMENTO_RILIEVO = 2;
- const TESTE_DICHIARA_LUOGO_INCIDENTE = 3;
- const TESTE_DICHIARA_PRONTO_SOCCORSO = 4;
- const TESTE_DICHIARA_PROSEGUO_TEMPO = 5;
- const PATENTE_CATEGORIA_AM = 'AM';
- const PATENTE_CATEGORIA_A1 = 'A1';
- const PATENTE_CATEGORIA_A1B = 'A1B';
- const PATENTE_CATEGORIA_A2 = 'A2';
- const PATENTE_CATEGORIA_A = 'A';
- const PATENTE_CATEGORIA_AB = 'AB';
- const PATENTE_CATEGORIA_B1 = 'B1';
- const PATENTE_CATEGORIA_B = 'B';
- const PATENTE_CATEGORIA_BE = 'BE';
- const PATENTE_CATEGORIA_C1 = 'C1';
- const PATENTE_CATEGORIA_C1E = 'C1E';
- const PATENTE_CATEGORIA_C = 'C';
- const PATENTE_CATEGORIA_CE = 'CE';
- const PATENTE_CATEGORIA_D1 = 'D1';
- const PATENTE_CATEGORIA_D1E = 'D1E';
- const PATENTE_CATEGORIA_DE = 'DE';
- const PATENTE_CATEGORIA_E = 'E';
- const PATENTE_ABILITAZIONI_CQC = 1;
- const PATENTE_ABILITAZIONI_CAP = 2;
- const PATENTE_ABILITAZIONI_CFC = 3;
- const DOCUMENTO_RILASCIATO_COMUNE = 1;
- const DOCUMENTO_RILASCIATO_MOTORIZZAZIONE_CIVILE = 2;
- const DOCUMENTO_RILASCIATO_ALTRO = 3;
- const DOCUMENTO_RILASCIATO_MCTC = 4;
- const DOCUMENTO_RILASCIATO_MIT = 5;
- const DOCUMENTO_RILASCIATO_UCO = 6;
- const DOCUMENTO_RILASCIATO_PREFETTURA = 7;
- const PATENTE_PRESCRIZIONI_OBBLIGO_LENTI = 1;
- const PATENTE_PRESCRIZIONI_APPARECCHIO_ACUSTICO = 2;
- const PATENTE_PRESCRIZIONI_LIMITAZIONI_ART_117 = 3;
- const PROTOCOLLO_NUM_LENGTH = 3;
- protected $paginationTheme = 'bootstrap';
- public $title = 'ISTAT';
- public $from, $to;
- public function mount()
- {
- // Initialize $from with today's date in Y-m-d format
- $this->from = now()->format('Y-m-d');
- }
- protected $rules = [
- 'from' => 'required',
- 'to' => 'required',
- ];
- protected $messages = [
- 'from.required' => 'La data inizio è obbligatoria',
- 'to.required' => 'La data fine è obbligatoria',
- ];
- public function resetFields()
- {
- $this->from = '';
- $this->to = '';
- }
- public function render()
- {
- return view('livewire.istat');
- }
- public function export()
- {
- Log::info('Export ISTAT');
- Log::info('From: ' . $this->from);
- Log::info('To: ' . $this->to);
- try {
- $items = \DB::table('fcf_reports_reports')
- ->leftjoin('fcf_users', 'fcf_reports_reports.created_by', '=', 'fcf_users.id')
- ->selectRaw('fcf_reports_reports.*, CONCAT(fcf_users.firstname," ",fcf_users.lastname) AS display_name')
- ->where(function ($query) {
- $query->where('fcf_reports_reports.incidente_con_feriti', '1')
- ->orWhere('fcf_reports_reports.incidente_mortale', '=', '1');
- })
- ->where('fcf_reports_reports.verificatosi_in_data', '>=', $this->from != '' ? $this->from : '2000-01-01')
- ->where('fcf_reports_reports.verificatosi_in_data', '<=', $this->to != '' ? $this->to : '2050-01-01')
- ->orderBy('fcf_reports_reports.verificatosi_in_data')
- ->get();
- $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);
- if ($town) {
- if ($town->istat_code) $town_code = $town->istat_code;
- $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 .= ' ';
- $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) {
- case 2:
- $loc = 2;
- break;
- case 3:
- $loc = 0;
- break;
- case 4:
- $loc = 3;
- break;
- default:
- $loc = 1;
- }
- } elseif ($item->localizzazione_incidente == Istat::LOCALIZZAZIONE_INCIDENTE_OUT) {
- switch ($item->nomenclatura_strada) {
- case 2:
- $loc = 5;
- break;
- case 3:
- $loc = 9;
- break;
- case 4:
- $loc = 6;
- break;
- default:
- $loc = 4;
- }
- } else $loc = ' ';
- $tipo_strada = ' ';
- $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)))
- $tipo_strada = 2;
- elseif (in_array(4, $types_saved))
- $tipo_strada = 3;
- }
- $pav = ' ';
- if ($item->pavimentazione)
- switch ($item->pavimentazione) {
- case 5:
- case 6:
- $pav = 3;
- break;
- default:
- if (!$item->condizione_strada || $item->condizione_strada == 1) $pav = 1;
- else $pav = 2;
- }
- $int = ' ';
- switch ($item->particolarita_strada) {
- case 1:
- $int = '07';
- break;
- case 2:
- $int = '01';
- break;
- case 3:
- $int = '08';
- break;
- case 4:
- $int = '02';
- break;
- case 5:
- $int = '10';
- break;
- case 6:
- case 10:
- $int = '09';
- break;
- case 7:
- $int = '06';
- break;
- case 12:
- $int = '04';
- break;
- }
- $fon = ' ';
- if ($item->fondo_stradale)
- switch ($item->fondo_stradale) {
- case 1:
- $fon = 1;
- break;
- case 2:
- $fon = 2;
- break;
- case 3:
- $fon = 4;
- break;
- case 4:
- $fon = 5;
- break;
- case 5:
- $fon = 3;
- break;
- }
- $seg = ' ';
- if ($item->segnaletica)
- switch ($item->segnaletica) {
- case Istat::SEGNALETICA_ASSENTE:
- $seg = 1;
- break;
- case Istat::SEGNALETICA_VERTICALE:
- $seg = 2;
- break;
- case Istat::SEGNALETICA_ORIZZONTALE:
- $seg = 3;
- break;
- case Istat::SEGNALETICA_VERTICALE_ORIZZONTALE:
- $seg = 4;
- break;
- case Istat::SEGNALETICA_TEMPORANEA_CANTIERE:
- $seg = 5;
- break;
- }
- $met = ' ';
- if ($item->condizioni_atmosferiche)
- switch ($item->condizioni_atmosferiche) {
- case 1:
- $met = 1;
- break;
- case 2:
- $met = 2;
- break;
- case 3:
- $met = 3;
- break;
- case 4:
- $met = 2;
- break;
- case 5:
- $met = 7;
- break;
- case 6:
- $met = 1;
- break;
- case 7:
- $met = 4;
- break;
- case 8:
- $met = 5;
- break;
- case 9:
- $met = 6;
- break;
- }
- $item_data .= str_pad($loc, 1, ' ', STR_PAD_RIGHT);
- $item_data .= str_pad($item->nomenclatura_strada_numero, 3, ' ', STR_PAD_RIGHT);
- $item_data .= str_repeat(' ', 3);
- $item_data .= str_repeat(' ', 2);
- $item_data .= str_pad($tipo_strada, 1, ' ', STR_PAD_RIGHT);
- $item_data .= str_pad($pav, 1, ' ', STR_PAD_RIGHT);
- $item_data .= str_pad($int, 2, ' ', STR_PAD_RIGHT);
- $item_data .= str_pad($fon, 1, ' ', STR_PAD_RIGHT);
- $item_data .= str_pad($seg, 1, ' ', STR_PAD_RIGHT);
- $item_data .= str_pad($met, 1, ' ', STR_PAD_RIGHT);
- $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)->pluck('tipo_urto_id')
- ->toArray();
- if ($types_saved) {
- if (in_array(1, $types_saved))
- $tipo_urto = '01';
- elseif (in_array(2, $types_saved))
- $tipo_urto = '02';
- elseif (in_array(3, $types_saved) || in_array(26, $types_saved) || in_array(27, $types_saved))
- $tipo_urto = '04';
- elseif (in_array(5, $types_saved))
- $tipo_urto = '03';
- elseif (in_array(7, $types_saved) || in_array(12, $types_saved))
- $tipo_urto = '05';
- elseif (in_array(4, $types_saved) || in_array(21, $types_saved))
- $tipo_urto = '07';
- elseif (in_array(8, $types_saved))
- $tipo_urto = '06';
- elseif (in_array(6, $types_saved) || in_array(19, $types_saved) || in_array(14, $types_saved))
- $tipo_urto = '08';
- elseif (in_array(9, $types_saved) || in_array(18, $types_saved))
- $tipo_urto = '10';
- 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(
- 'tipo' => '',
- 'circostanze' => '',
- 'veicolo' => '',
- 'conducente_passeggeri' => '',
- 'pedoni' => '',
- 'cittadinanza_conducente' => ''
- );
- $deceduti_24h = array();
- $deceduti_30gg = array();
- $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"]);
- $v_tipo = str_repeat(' ', 2);
- switch ($veicolo->tipo_id) {
- case 1:
- switch ($veicolo->destinazione_uso) {
- case \App\Models\Vehicle::DESTINAZIONE_USO_PRIVATO:
- $v_tipo = '01';
- break;
- case \App\Models\Vehicle::DESTINAZIONE_USO_PUBBLICO:
- $v_tipo = '03';
- break;
- case \App\Models\Vehicle::DESTINAZIONE_USO_SOCCORSO_POLIZIA:
- $v_tipo = '04';
- break;
- default:
- $v_tipo = '01';
- break;
- }
- break;
- case 17:
- $v_tipo = '02';
- break;
- case 7:
- if ($veicolo->destinazione_uso == \App\Models\Vehicle::DESTINAZIONE_USO_SERVIZO_PUBBLICO_DI_LINEA)
- $v_tipo = '05';
- else
- $v_tipo = '06';
- break;
- case 16:
- $v_tipo = '08';
- break;
- case 14:
- case 20:
- $v_tipo = '09';
- break;
- case 18:
- $v_tipo = '10';
- break;
- case 8:
- $v_tipo = '13';
- break;
- case 6:
- $v_tipo = '14';
- break;
- case 3:
- $v_tipo = '15';
- break;
- case 5:
- $v_tipo = '17';
- break;
- case 19:
- $v_tipo = '18';
- break;
- case 4:
- $v_tipo = '21';
- break;
- }
- $v_data['tipo'] .= $v_tipo;
- $v_data['veicolo'] .= $veicolo->foreign_country ?
- str_repeat(' ', 8) :
- str_pad(substr($veicolo->targa, 0, 8), 8, ' ', STR_PAD_RIGHT);
- $v_data['veicolo'] .= $veicolo->foreign_country ?
- 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);
- }
- if ($data_veicolo["alcool_test"] && $data_veicolo["alcool_test_esito"] == Istat::ESITO_POSITIVO)
- $stato_psico_fisico = '90';
- elseif ($data_veicolo["drug_test"] && $data_veicolo["drug_test_esito"] == Istat::ESITO_POSITIVO)
- $stato_psico_fisico = '94';
- $conducente = Anagrafica::findOrFail($data_veicolo["conducenti"]);
- $eta_conducente = '00';
- if ($conducente->birth_date != '' && $report->verificatosi_in_data != '') {
- $eta_conducente = 0;
- if ($eta_conducente > 100 || $eta_conducente < 0) $eta_conducente = 0;
- $eta_conducente = str_pad($eta_conducente, 2, '0', STR_PAD_LEFT);
- }
- $v_data['conducente_passeggeri'] .= $eta_conducente;
- $v_data['conducente_passeggeri'] .= $conducente->gender == 'F' ? '2' : '1';
- $details = array(
- 'firstname' => $conducente->firstname,
- 'lastname' => $conducente->lastname,
- 'ospedale' => $data_veicolo["infortunato_ospedale"],
- );
- switch ($data_veicolo["infortunato"]) {
- case Istat::INFORTUNATO_DECEDUTO_30GG:
- $infortunato = 4;
- $deceduti_30gg[] = $details;
- break;
- case Istat::INFORTUNATO_DECEDUTO_24H:
- $infortunato = 3;
- $deceduti_24h[] = $details;
- break;
- case Istat::INFORTUNATO_FERITO:
- $infortunato = 2;
- $feriti[] = $details;
- break;
- default:
- $infortunato = 1;
- break;
- }
- $v_data['conducente_passeggeri'] .= $infortunato;
- switch ($data_veicolo["conducente_patente_categoria"]) {
- case Istat::PATENTE_CATEGORIA_AM:
- $patente = '0';
- break;
- case Istat::PATENTE_CATEGORIA_A1:
- case Istat::PATENTE_CATEGORIA_A1B:
- case Istat::PATENTE_CATEGORIA_A2:
- case Istat::PATENTE_CATEGORIA_A:
- $patente = '1';
- break;
- case Istat::PATENTE_CATEGORIA_AB:
- case Istat::PATENTE_CATEGORIA_B1:
- case Istat::PATENTE_CATEGORIA_B:
- $patente = '2';
- break;
- case Istat::PATENTE_CATEGORIA_C1:
- case Istat::PATENTE_CATEGORIA_C:
- $patente = '3';
- break;
- case Istat::PATENTE_CATEGORIA_D1:
- $patente = '4';
- break;
- case Istat::PATENTE_CATEGORIA_BE:
- case Istat::PATENTE_CATEGORIA_C1E:
- case Istat::PATENTE_CATEGORIA_CE:
- case Istat::PATENTE_CATEGORIA_D1E:
- case Istat::PATENTE_CATEGORIA_DE:
- case Istat::PATENTE_CATEGORIA_E:
- $patente = '5';
- break;
- default:
- $patente = '9';
- break;
- }
- $v_data['conducente_passeggeri'] .= $patente;
- $patente_rilasciata_il = $data_veicolo["conducente_patente_rilasciata_il"];
- $v_data['conducente_passeggeri'] .= $patente_rilasciata_il ? date('y', strtotime($patente_rilasciata_il)) : str_repeat(' ', 2);
- $v_data['conducente_passeggeri'] .= str_repeat(' ', 4);
- $datiPasseggeri = \App\Models\ReportDataPasseggeri::where('progressive', $i)->where('report_id', $report->id)->get()->toArray();
- for ($j = 0; $j < 4; $j++) {
- $passeggero_data = str_repeat(' ', 4);
- if (isset($datiPasseggeri[$j])) {
- $passeggero = $datiPasseggeri[$j]["passeggero"];
- if ($passeggero) {
- $passeggero_infortunato = $datiPasseggeri[$j]["infortunato"];
- if ($passeggero_infortunato != Istat::INFORTUNATO_INCOLUME) {
- $passeggero_anagrafica = Anagrafica::findOrFail($passeggero);
- $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);
- 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);
- }
- $passeggero_data = sprintf(
- '%s%s%s',
- $passeggero_infortunato == Istat::INFORTUNATO_FERITO ? '2' : '1',
- $eta_passeggero,
- $passeggero_anagrafica->gender == 'F' ? '4' : '3'
- );
- $details = array(
- 'firstname' => $passeggero_anagrafica->firstname,
- 'lastname' => $passeggero_anagrafica->lastname,
- 'ospedale' => $datiPasseggeri[$j]["infortunato_ospedale"]
- );
- switch ($passeggero_infortunato) {
- case Istat::INFORTUNATO_DECEDUTO_30GG:
- $deceduti_30gg[] = $details;
- break;
- case Istat::INFORTUNATO_DECEDUTO_24H:
- $deceduti_24h[] = $details;
- break;
- case Istat::INFORTUNATO_FERITO:
- $feriti[] = $details;
- break;
- }
- }
- }
- }
- $v_data['conducente_passeggeri'] .= $passeggero_data;
- }
- $v_data['conducente_passeggeri'] .= str_repeat(' ', 8);
- $v_data['cittadinanza_conducente'] .= $conducente->nazionalita == 0 ? '1' : '2';
- $country_id = 106;
- if ($conducente->nazionalita != 0)
- $country_id = $conducente->nazione_straniera;
- if (!$country_id) $v_data['cittadinanza_conducente'] .= str_repeat(' ', 33);
- else {
- $country = \App\Models\LocationCountry::findOrFail($country_id);
- $v_data['cittadinanza_conducente'] .= str_pad($country->istat_code, 3, ' ', STR_PAD_RIGHT);
- $v_data['cittadinanza_conducente'] .= str_pad(preg_replace('[^A-Z ]', '', strtoupper($country->name)), 30, ' ', STR_PAD_RIGHT);
- }
- $countryDataLen = strlen($v_data['cittadinanza_conducente']);
- $expectedCountryDataLen = 34;
- if ($countryDataLen < $expectedCountryDataLen) {
- $v_data['cittadinanza_conducente'] .= str_repeat(' ', $expectedCountryDataLen - $countryDataLen);
- }
- } else {
- $v_data['tipo'] .= str_repeat(' ', 2);
- $v_data['veicolo'] .= str_repeat(' ', 18);
- $v_data['conducente_passeggeri'] .= str_repeat(' ', 35);
- $v_data['pedoni'] .= str_repeat(' ', 24);
- $v_data['cittadinanza_conducente'] .= str_repeat(' ', 34);
- }
- if ($i < 2) {
- $v_data['circostanze'] .= '00';
- $v_data['circostanze'] .= str_repeat(' ', 2);
- $v_data['circostanze'] .= $stato_psico_fisico;
- }
- }
- $p_data = '';
- $datiPedoni = ReportDataPedoni::where('report_id', $report->id)->get()->toArray();
- for ($j = 0; $j < 4; $j++) {
- $pedone_data = str_repeat(' ', 6);
- if (isset($datiPedoni[$j])) {
- $pedone = $datiPedoni[$j]["pedoni"];
- if ($pedone) {
- $pedone_infortunato = $infortunato;
- if ($pedone_infortunato != Istat::INFORTUNATO_INCOLUME) {
- $pedone_anagrafica = new Anagrafica([$pedone]);
- $eta_pedone = '00';
- if (DateHelper::filterDate($pedone_anagrafica->get('birth_date')) && DateHelper::filterDate($report->get('verificatosi_in_data'))) {
- $eta_pedone = DateHelper::calculateAge($pedone_anagrafica->get('birth_date'), $report->get('verificatosi_in_data'));
- if ($eta_pedone == 0) $eta_pedone = 1;
- elseif ($eta_pedone > 100 || $eta_pedone < 0) $eta_pedone = 0;
- $eta_pedone = str_pad($eta_pedone, 2, '0', STR_PAD_LEFT);
- }
- if ($pedone_infortunato == Istat::INFORTUNATO_FERITO)
- $pedone_data = sprintf(
- '%s%s%s',
- str_repeat(' ', 3),
- $pedone_anagrafica->get('gender') == 'F' ? '4' : '3',
- $eta_pedone
- );
- else
- $pedone_data = sprintf(
- '%s%s%s',
- $pedone_anagrafica->get('gender') == 'F' ? '2' : '1',
- $eta_pedone,
- str_repeat(' ', 3)
- );
- $details = array(
- 'firstname' => $pedone_anagrafica->get('firstname'),
- 'lastname' => $pedone_anagrafica->get('lastname'),
- 'ospedale' => ReportDataPedoni::where('report_id', $report->id)
- ->where('progressive', $j)
- ->value('infortunato_ospedale')
- );
- switch ($pedone_infortunato) {
- case Istat::INFORTUNATO_DECEDUTO_30GG:
- $deceduti_30gg[] = $details;
- break;
- case Istat::INFORTUNATO_DECEDUTO_24H:
- $deceduti_24h[] = $details;
- break;
- case Istat::INFORTUNATO_FERITO:
- $feriti[] = $details;
- break;
- }
- }
- }
- }
- $p_data .= $pedone_data;
- }
- $v_data['tipo'] = str_pad($v_data['tipo'], 6, ' ', STR_PAD_RIGHT);
- $item_data .= $v_data['tipo'];
- $item_data .= str_repeat(' ', 24);
- $v_data['circostanze'] = str_pad($v_data['circostanze'], 12, ' ', STR_PAD_RIGHT);
- $item_data .= $v_data['circostanze'];
- $v_data['veicolo'] = str_pad($v_data['veicolo'], 54, ' ', STR_PAD_RIGHT);
- $item_data .= $v_data['veicolo'];
- $v_data['conducente_passeggeri'] = str_pad($v_data['conducente_passeggeri'], 105, ' ', STR_PAD_RIGHT);
- $item_data .= $v_data['conducente_passeggeri'];
- $p_data = str_pad($p_data, 24, ' ', STR_PAD_RIGHT);
- $item_data .= $p_data;
- $item_data .= str_repeat(' ', 10);
- $item_data .= str_pad(count($deceduti_24h), 2, '0', STR_PAD_LEFT);
- $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);
- $sectionLength = strlen($item_data) - $sectionStart;
- $sectionCounts['Section 21-30'] = $sectionLength;
- } 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);
- $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'];
- $sectionLength = strlen($item_data) - $sectionStart;
- $sectionCounts['Section 31-40'] = $sectionLength;
- Log::info('Item Data 31-40 Length: ' . $sectionLength);
- $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());
- }
- $item_data = str_pad($item_data, 1939, ' ');
- $txt[] = $item_data;
- } catch (\Exception $e) {
- Log::error('Major error processing item ' . $item->id . ': ' . $e->getMessage());
- continue;
- }
- }
- $txt_data = implode("\r\n", $txt);
- return response()->streamDownload(function () use ($txt_data) {
- echo $txt_data;
- }, 'istat_' . $this->from . "_" . $this->to . '.txt');
- } catch (\Exception $e) {
- Log::error('Fatal error in export: ' . $e->getMessage());
- return response()->json(['error' => 'Export failed: ' . $e->getMessage()], 500);
- }
- }
- }
|