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.prefettura'); } public function export() { Log::info('Export file prefettura'); 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(); Log::info('Items: ' . count($items)); $spreadsheet = new Spreadsheet(); $activeWorksheet = $spreadsheet->getActiveSheet(); $activeWorksheet->setCellValue('A1', "IDIncidenti"); $activeWorksheet->setCellValue('B1', "Data"); $activeWorksheet->setCellValue('C1', "Ora"); $activeWorksheet->setCellValue('D1', "IDProv"); $activeWorksheet->setCellValue('E1', "IDCom"); $activeWorksheet->setCellValue('F1', "IDTipoIncidente"); $activeWorksheet->setCellValue('G1', "IDTipoStrada"); $activeWorksheet->setCellValue('H1', "CentroAbitato"); $activeWorksheet->setCellValue('I1', "IDPolizia"); $activeWorksheet->setCellValue('J1', "IDCaratteristiche"); $activeWorksheet->setCellValue('K1', "CantiereStradale"); $activeWorksheet->setCellValue('L1', "N_Autovettura"); $activeWorksheet->setCellValue('M1', "N_Autocarro fino 3,5t"); $activeWorksheet->setCellValue('N1', "N_Autocarro > 3,5t"); $activeWorksheet->setCellValue('O1', "N_Autotreno"); $activeWorksheet->setCellValue('P1', "N_Autoarticolato"); $activeWorksheet->setCellValue('Q1', "N_Autobus"); $activeWorksheet->setCellValue('R1', "N_Tram"); $activeWorksheet->setCellValue('S1', "N_Treno"); $activeWorksheet->setCellValue('T1', "N_Motociclo"); $activeWorksheet->setCellValue('U1', "N_Ciclomotore"); $activeWorksheet->setCellValue('V1', "N_Velocipede"); $activeWorksheet->setCellValue('W1', "N_Bicicletta a pedala assistita"); $activeWorksheet->setCellValue('X1', "N_Monopattini elettrici"); $activeWorksheet->setCellValue('Y1', "N_Altri dispositivi micromobilita'"); $activeWorksheet->setCellValue('Z1', "N_AltrIVeicoli"); $activeWorksheet->setCellValue('AA1', "Trasportanti merci pericolose"); $activeWorksheet->setCellValue('AB1', "N_Pedoni"); $activeWorksheet->setCellValue('AC1', "N_Deceduti"); $activeWorksheet->setCellValue('AD1', "N_Feriti"); $activeWorksheet->setCellValue('AE1', "Numero Nome Strada"); $activeWorksheet->setCellValue('AF1', "KM"); $activeWorksheet->setCellValue('AG1', "Metri"); $activeWorksheet->setCellValue('AH1', "Carreggiata"); $activeWorksheet->setCellValue('AI1', "Omissione"); $activeWorksheet->setCellValue('AJ1', "Contromano"); $activeWorksheet->setCellValue('AK1', "DettaglioPersone"); $activeWorksheet->setCellValue('AL1', "idPositivita"); $activeWorksheet->setCellValue('AM1', "ArtCds"); $activeWorksheet->setCellValue('AN1', "Coordinata X"); $activeWorksheet->setCellValue('AO1', "Coordinata Y"); $activeWorksheet->getStyle('A1:AO1')->getFont()->setBold(true); // Character counting variables $charCounts = []; $sectionCounts = []; $idx = 2; foreach ($items as $item) { try { Log::info('Item: ' . $item->id); $report = \App\Models\Report::findOrFail($item->id); //$activeWorksheet->setCellValue('A' . $idx, str_pad($report->protocollo_num, 3, '0', STR_PAD_LEFT) . "/" . date('Y', strtotime($item->verificatosi_in_data))); $activeWorksheet->setCellValue('A' . $idx, $item->id); $activeWorksheet->setCellValue('B' . $idx, date('d/m/Y', strtotime($item->verificatosi_in_data))); $activeWorksheet->setCellValue('C' . $idx, $item->verificatosi_in_data_ora . ":" . $item->verificatosi_in_data_minuti); $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; } $activeWorksheet->setCellValue('D' . $idx, $prov_code); $activeWorksheet->setCellValue('E' . $idx, $town_code); $activeWorksheet->setCellValue('F' . $idx, $report->tipo_incidente); $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; } $activeWorksheet->setCellValue('G' . $idx, $report->tipo_strada_prefettura); if ($report->localizzazione_incidente == Prefettura::LOCALIZZAZIONE_INCIDENTE_IN) { switch ($report->nomenclatura_strada) { case 2: $loc = 2; break; case 3: $loc = 0; break; case 4: $loc = 3; break; default: $loc = 1; } } elseif ($report->localizzazione_incidente == Prefettura::LOCALIZZAZIONE_INCIDENTE_OUT) { switch ($report->nomenclatura_strada) { case 2: $loc = 5; break; case 3: $loc = 9; break; case 4: $loc = 6; break; default: $loc = 4; } } else $loc = ' '; $activeWorksheet->setCellValue('H' . $idx, $loc); $int = ' '; switch ($report->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; } $activeWorksheet->setCellValue('I' . $idx, $report->autorita); $activeWorksheet->setCellValue('J' . $idx, $report->caratteristiche); $activeWorksheet->setCellValue('K' . $idx, "N"); $deceduti = 0; $feriti = 0; $pedoni = 0; $autovetture = 0; $autotreno = 0; $autoarticolato = 0; $autobus = 0; $tram = 0; $treno = 0; $motociclo = 0; $ciclomotore = 0; $velocipede = 0; $bicicletta = 0; $monopattino = 0; $altri_micro = 0; $altri = 0; $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; if ($data_veicolo) { if ($data_veicolo["veicoli"] > 0) { $veicolo = \App\Models\Vehicle::findOrFail($data_veicolo["veicoli"]); switch ($veicolo->tipo_id) { case \App\Models\Vehicle::AUTOVETTURA: $autovetture += 1; break; case \App\Models\Vehicle::CICLO_MOTORE: $ciclomotore += 1; break; case \App\Models\Vehicle::QUADRICICLO: $altri += 1; break; case \App\Models\Vehicle::MOTOCICLO: $motociclo += 1; break; case \App\Models\Vehicle::VELOCIPEDE: $velocipede += 1; break; case \App\Models\Vehicle::AUTOBUS: $autobus += 1; break; case \App\Models\Vehicle::TRATTORE_SEMIRIMORCHIO: $altri += 1; break; case \App\Models\Vehicle::AUTOTURISMO: $autobus += 1; break; case \App\Models\Vehicle::RIMORCHIO: $altri += 1; break; case \App\Models\Vehicle::AUTOCARRO: $altri += 1; break; case \App\Models\Vehicle::AUTOVETTURA_CON_RIMORCHIO: $autovetture += 1; break; case \App\Models\Vehicle::AUTOSNODATO_O_AUTOARTICOLATO: $autotreno += 1; break; case \App\Models\Vehicle::MOTOCARRO_O_MOTOFURGONE: $motociclo += 1; break; case \App\Models\Vehicle::AUTOTRENO_CON_RIMORCHIO: $autotreno += 1; break; } } switch ($data_veicolo["infortunato"]) { case Prefettura::INFORTUNATO_DECEDUTO_30GG: $deceduti += 1; break; case Prefettura::INFORTUNATO_DECEDUTO_24H: $deceduti += 1; break; case Prefettura::INFORTUNATO_FERITO: $feriti += 1; break; } } } $datiPedoni = ReportDataPedoni::where('report_id', $report->id)->get()->toArray(); for ($j = 0; $j < 4; $j++) { $pedone_data = str_repeat(' ', 6); if (isset($datiPedoni[$j])) { $pedoni += 1; } } $activeWorksheet->setCellValue('L' . $idx, $autovetture); $activeWorksheet->setCellValue('M' . $idx, "0"); $activeWorksheet->setCellValue('N' . $idx, "0"); $activeWorksheet->setCellValue('O' . $idx, $autotreno); $activeWorksheet->setCellValue('P' . $idx, $autoarticolato); $activeWorksheet->setCellValue('Q' . $idx, $autobus); $activeWorksheet->setCellValue('R' . $idx, $tram); $activeWorksheet->setCellValue('S' . $idx, $treno); $activeWorksheet->setCellValue('T' . $idx, $motociclo); $activeWorksheet->setCellValue('U' . $idx, $ciclomotore); $activeWorksheet->setCellValue('V' . $idx, $velocipede); $activeWorksheet->setCellValue('W' . $idx, $bicicletta); $activeWorksheet->setCellValue('X' . $idx, $monopattino); $activeWorksheet->setCellValue('Y' . $idx, $altri_micro); $activeWorksheet->setCellValue('Z' . $idx, $altri); $activeWorksheet->setCellValue('AA' . $idx, "???"); $activeWorksheet->setCellValue('AB' . $idx, $pedoni); $activeWorksheet->setCellValue('AC' . $idx, $deceduti); $activeWorksheet->setCellValue('AD' . $idx, $feriti); $stradario_due = \App\Models\Stradario::find($report->localita_due); $location_description = $stradario_due ? (($stradario_due['TOPONIMO'] ?? '') . ' ' . ($stradario_due['DESCRIZIONE'] ?? '')) : ''; $activeWorksheet->setCellValue('AE' . $idx, $location_description); $km = ''; $metri = ''; if ($report->prossimita_chilometro != '') { try { list($km, $metri) = explode("+", $report->prossimita_chilometro); } catch (\Exception $ex) { } try { list($km, $metri) = explode(".", $report->prossimita_chilometro); } catch (\Exception $ex) { } try { list($km, $metri) = explode(",", $report->prossimita_chilometro); } catch (\Exception $ex) { } } $activeWorksheet->setCellValue('AF' . $idx, $km); $activeWorksheet->setCellValue('AG' . $idx, $metri); $activeWorksheet->setCellValue('AH' . $idx, $report->carreggiata); $activeWorksheet->setCellValue('AI' . $idx, ""); $activeWorksheet->setCellValue('AJ' . $idx, ""); $activeWorksheet->setCellValue('AK' . $idx, ""); $activeWorksheet->setCellValue('AL' . $idx, $report->positivita); $activeWorksheet->setCellValue('AM' . $idx, $report->cds); $activeWorksheet->setCellValue('AN' . $idx, $item->latitude); $activeWorksheet->setCellValue('AO' . $idx, $item->longitude); $idx += 1; } catch (\Exception $e) { Log::error('Error for item ' . $item->id . ': ' . $e->getMessage()); } } $writer = new Xlsx($spreadsheet); $writer->save($path = storage_path('prefettura' . date("YmdHis") . '.xlsx')); return response()->download($path)->deleteFileAfterSend(); } catch (\Exception $e) { Log::error('Fatal error in export: ' . $e->getMessage()); return response()->json(['error' => 'Export failed: ' . $e->getMessage()], 500); } } }