Istat.php 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. <?php
  2. namespace App\Http\Livewire;
  3. use App\Helpers\DateHelper;
  4. use App\Models\Anagrafica;
  5. use App\Models\ReportDataPedoni;
  6. use Illuminate\Support\Facades\Log;
  7. use Livewire\Component;
  8. class Istat extends Component
  9. {
  10. const LOCALIZZAZIONE_INCIDENTE_IN = 1;
  11. const LOCALIZZAZIONE_INCIDENTE_OUT = 2;
  12. const VISIBILITA_BUONA = 1;
  13. const VISIBILITA_LIMITATA = 2;
  14. const FONDO_STRADALE_SPECIFICA_OPTION_VALUE = 5;
  15. const PARTICOLARITA_STRADA_SPECIFICA_OPTION_VALUE = 5;
  16. const TRAFFICO_NORMALE = 1;
  17. const TRAFFICO_SCARSO = 2;
  18. const TRAFFICO_INTENSO = 3;
  19. const TIPO_STRADA_ALTRO_OPTION_VALUE = 8;
  20. const SEGNALETICA_ASSENTE = 1;
  21. const SEGNALETICA_VERTICALE = 2;
  22. const SEGNALETICA_ORIZZONTALE = 3;
  23. const SEGNALETICA_VERTICALE_ORIZZONTALE = 4;
  24. const SEGNALETICA_TEMPORANEA_CANTIERE = 5;
  25. const SEGNALETICA_VERTICALE_LIMITE_VELOCITA_OPTION_VALUE = 21;
  26. const SEGNALETICA_VERTICALE_PANNELLO_INTEGRATIVO_OPTION_VALUE = 22;
  27. const SEGNALETICA_VERTICALE_ALTRO_OPTION_VALUE = 23;
  28. const LINEA_CONTINUA = 1;
  29. const LINEA_TRATTEGGIATA = 2;
  30. const LINEA_TRATTEGGIATA_IN_CORRISPONDENZA = 3;
  31. const SEGNALETICA_ORIZZONTALE_LINEA_ASSENTE = 4;
  32. const MAX_PARTI_COINVOLTE = 10;
  33. const MAX_VEHICLES = 4;
  34. const MAX_PASSEGGERI = 4;
  35. const MAX_INFORTUNATI = 4;
  36. const MAX_PEDONI = 4;
  37. const MAX_TESTI_OCULARI = 4;
  38. const STATO_VEICOLO_MARCIA = 1;
  39. const STATO_VEICOLO_SOSTA = 2;
  40. const STATO_VEICOLO_NON_PRESENTE = 3;
  41. const POSIZIONE_STATICA_FINALE_DOCUMENTAZIONE_FOTOGRAFICA = 1;
  42. const POSIZIONE_STATICA_FINALE_RILIEVO_PLANIMETRICO = 2;
  43. const POSIZIONE_STATICA_FINALE_SCHIZZO_VISTA_NO_SCALA = 3;
  44. const POSIZIONE_STATICA_FINALE_GIA_RIMOSSO = 4;
  45. const DESTINAZIONE_VEICOLO_RIMOSSO_SENSI_ART_2028_CC = 1;
  46. const DESTINAZIONE_VEICOLO_SEQUESTRO_ART_354_CPP = 2;
  47. const DESTINAZIONE_VEICOLO_SEQUESTRO_ART_321_CPP = 3;
  48. const DESTINAZIONE_VEICOLO_SEQUESTRO = 4;
  49. const DESTINAZIONE_VEICOLO_SEQUESTRO_AMMINISTRATIVO = 5;
  50. const DESTINAZIONE_VEICOLO_FERMO_AMMINISTRATIVO = 6;
  51. const DESTINAZIONE_VEICOLO_RITIRATO_DAL_CONDUCENTE = 7;
  52. const STATO_PNEUMATICI_REGOLAMENTARE = 1;
  53. const STATO_PNEUMATICI_BUONO = 2;
  54. const STATO_PNEUMATICI_SUFFICIENTE = 3;
  55. const STATO_PNEUMATICI_INSUFFICIENTE = 4;
  56. const STATO_PNEUMATICI_NON_RILEVATO = 5;
  57. const STATO_PRESENTE = 1;
  58. const STATO_NON_PRESENTE = 2;
  59. const STATO_FUNZIONANTE = 1;
  60. const STATO_NON_FUNZIONANTE = 2;
  61. const STATO_NON_RILEVATO = 3;
  62. const YES = 1;
  63. const NO = 0;
  64. const SI_RISERVA = 3;
  65. const PATENTE_AL_SEGUITO_NON_PREVISTA = 3;
  66. const CONDUCENTE_GENERALIZZATO_LUOGO = 1;
  67. const CONDUCENTE_GENERALIZZATO_OSPEDALE = 2;
  68. const CONDUCENTE_GENERALIZZATO_ALTRO = 3;
  69. const INFORTUNATO_INCOLUME = 0;
  70. const INFORTUNATO_FERITO = 1;
  71. const INFORTUNATO_DECEDUTO_24H = 2;
  72. const INFORTUNATO_DECEDUTO_30GG = 3;
  73. const MARCIA_OPTION_N = 0;
  74. const MARCIA_OPTION_1 = 1;
  75. const MARCIA_OPTION_2 = 2;
  76. const MARCIA_OPTION_3 = 3;
  77. const MARCIA_OPTION_4 = 4;
  78. const MARCIA_OPTION_5 = 5;
  79. const MARCIA_OPTION_6 = 6;
  80. const MARCIA_OPTION_R = 7;
  81. const MARCIA_NON_RILEVATA = 8;
  82. const MARCIA_OPTION_AUTO = 9;
  83. const CRISTALLO_OPTION_INTERNO = 1;
  84. const CRISTALLO_OPTION_ESTERNO = 2;
  85. const CRISTALLO_OPTION_ALTRO = 3;
  86. const ESITO_NEGATIVO = 1;
  87. const ESITO_POSITIVO = 2;
  88. const DOCUMENTO_CI = 1;
  89. const DOCUMENTO_PAT = 2;
  90. const DOCUMENTO_PP = 3;
  91. const DOCUMENTO_ALTRO = 4;
  92. const PEDONE_PRESENTE_E_POSIZIONE = 1;
  93. const PEDONE_SOCCORSO_E_PRESSO = 2;
  94. const TESTE_PRESENTE_MOMENTO_RILIEVO = 1;
  95. const TESTE_NON_PRESENTE_MOMENTO_RILIEVO = 2;
  96. const TESTE_DICHIARA_LUOGO_INCIDENTE = 3;
  97. const TESTE_DICHIARA_PRONTO_SOCCORSO = 4;
  98. const TESTE_DICHIARA_PROSEGUO_TEMPO = 5;
  99. const PATENTE_CATEGORIA_AM = 'AM';
  100. const PATENTE_CATEGORIA_A1 = 'A1';
  101. const PATENTE_CATEGORIA_A1B = 'A1B';
  102. const PATENTE_CATEGORIA_A2 = 'A2';
  103. const PATENTE_CATEGORIA_A = 'A';
  104. const PATENTE_CATEGORIA_AB = 'AB';
  105. const PATENTE_CATEGORIA_B1 = 'B1';
  106. const PATENTE_CATEGORIA_B = 'B';
  107. const PATENTE_CATEGORIA_BE = 'BE';
  108. const PATENTE_CATEGORIA_C1 = 'C1';
  109. const PATENTE_CATEGORIA_C1E = 'C1E';
  110. const PATENTE_CATEGORIA_C = 'C';
  111. const PATENTE_CATEGORIA_CE = 'CE';
  112. const PATENTE_CATEGORIA_D1 = 'D1';
  113. const PATENTE_CATEGORIA_D1E = 'D1E';
  114. const PATENTE_CATEGORIA_DE = 'DE';
  115. const PATENTE_CATEGORIA_E = 'E';
  116. const PATENTE_ABILITAZIONI_CQC = 1;
  117. const PATENTE_ABILITAZIONI_CAP = 2;
  118. const PATENTE_ABILITAZIONI_CFC = 3;
  119. const DOCUMENTO_RILASCIATO_COMUNE = 1;
  120. const DOCUMENTO_RILASCIATO_MOTORIZZAZIONE_CIVILE = 2;
  121. const DOCUMENTO_RILASCIATO_ALTRO = 3;
  122. const DOCUMENTO_RILASCIATO_MCTC = 4;
  123. const DOCUMENTO_RILASCIATO_MIT = 5;
  124. const DOCUMENTO_RILASCIATO_UCO = 6;
  125. const DOCUMENTO_RILASCIATO_PREFETTURA = 7;
  126. const PATENTE_PRESCRIZIONI_OBBLIGO_LENTI = 1;
  127. const PATENTE_PRESCRIZIONI_APPARECCHIO_ACUSTICO = 2;
  128. const PATENTE_PRESCRIZIONI_LIMITAZIONI_ART_117 = 3;
  129. const PROTOCOLLO_NUM_LENGTH = 3;
  130. protected $paginationTheme = 'bootstrap';
  131. public $title = 'ISTAT';
  132. public $from, $to;
  133. public function mount()
  134. {
  135. // Initialize $from with today's date in Y-m-d format
  136. $this->from = now()->format('Y-m-d');
  137. }
  138. protected $rules = [
  139. 'from' => 'required',
  140. 'to' => 'required',
  141. ];
  142. protected $messages = [
  143. 'from.required' => 'La data inizio è obbligatoria',
  144. 'to.required' => 'La data fine è obbligatoria',
  145. ];
  146. public function resetFields()
  147. {
  148. $this->from = '';
  149. $this->to = '';
  150. }
  151. public function render()
  152. {
  153. return view('livewire.istat');
  154. }
  155. public function export()
  156. {
  157. Log::info('Export ISTAT');
  158. Log::info('From: ' . $this->from);
  159. Log::info('To: ' . $this->to);
  160. try {
  161. $items = \DB::table('fcf_reports_reports')
  162. ->leftjoin('fcf_users', 'fcf_reports_reports.created_by', '=', 'fcf_users.id')
  163. ->selectRaw('fcf_reports_reports.*, CONCAT(fcf_users.firstname," ",fcf_users.lastname) AS display_name')
  164. ->where(function ($query) {
  165. $query->where('fcf_reports_reports.incidente_con_feriti', '1')
  166. ->orWhere('fcf_reports_reports.incidente_mortale', '=', '1');
  167. })
  168. ->where('fcf_reports_reports.verificatosi_in_data', '>=', $this->from != '' ? $this->from : '2000-01-01')
  169. ->where('fcf_reports_reports.verificatosi_in_data', '<=', $this->to != '' ? $this->to : '2050-01-01')
  170. ->orderBy('fcf_reports_reports.verificatosi_in_data')
  171. ->get();
  172. $txt = array();
  173. $txt = array();
  174. Log::info('Items: ' . count($items));
  175. // Character counting variables
  176. $charCounts = [];
  177. $sectionCounts = [];
  178. foreach ($items as $item) {
  179. try {
  180. Log::info('Item: ' . $item->id);
  181. $report = \App\Models\Report::findOrFail($item->id);
  182. $item_data = '';
  183. $sectionLength = 0;
  184. //1-10
  185. try {
  186. $sectionStart = strlen($item_data);
  187. $town_code = $prov_code = '000';
  188. $town = \App\Models\LocationTown::findOrFail($item->localita_uno);
  189. if ($town) {
  190. if ($town->istat_code) $town_code = $town->istat_code;
  191. $prov = \App\Models\LocationProvince::where('code', $town->prov)->first();
  192. if ($prov && $prov->istat_code) $prov_code = $prov->istat_code;
  193. }
  194. $item_data .= date('y', strtotime($item->verificatosi_in_data));
  195. $item_data .= date('m', strtotime($item->verificatosi_in_data));
  196. $item_data .= str_pad($prov_code, 3, '0', STR_PAD_LEFT);
  197. $item_data .= str_pad($town_code, 3, '0', STR_PAD_LEFT);
  198. $item_data .= str_repeat(' ', 4);
  199. $item_data .= date('d', strtotime($item->verificatosi_in_data));
  200. $item_data .= str_repeat(' ', 2);
  201. $item_data .= '4';
  202. $item_data .= str_repeat(' ', 5);
  203. $item_data .= ' ';
  204. $sectionLength = strlen($item_data) - $sectionStart;
  205. $sectionCounts['Section 1-10'] = $sectionLength;
  206. Log::info('Item Data 1-10 Length: ' . $sectionLength);
  207. } catch (\Exception $e) {
  208. Log::error('Error in section 1-10 for item ' . $item->id . ': ' . $e->getMessage());
  209. $item_data = str_pad($item_data, 20, ' ');
  210. }
  211. //11-20
  212. try {
  213. $sectionStart = strlen($item_data);
  214. if ($item->localizzazione_incidente == Istat::LOCALIZZAZIONE_INCIDENTE_IN) {
  215. switch ($item->nomenclatura_strada) {
  216. case 2:
  217. $loc = 2;
  218. break;
  219. case 3:
  220. $loc = 0;
  221. break;
  222. case 4:
  223. $loc = 3;
  224. break;
  225. default:
  226. $loc = 1;
  227. }
  228. } elseif ($item->localizzazione_incidente == Istat::LOCALIZZAZIONE_INCIDENTE_OUT) {
  229. switch ($item->nomenclatura_strada) {
  230. case 2:
  231. $loc = 5;
  232. break;
  233. case 3:
  234. $loc = 9;
  235. break;
  236. case 4:
  237. $loc = 6;
  238. break;
  239. default:
  240. $loc = 4;
  241. }
  242. } else $loc = ' ';
  243. $tipo_strada = ' ';
  244. $types_saved = \App\Models\ReportTipoStrada::where('report_id', $report->id)
  245. ->pluck('tipo_strada_id')
  246. ->toArray();
  247. if (!empty($types_saved)) {
  248. if (in_array(3, $types_saved) && in_array(1, $types_saved))
  249. $tipo_strada = 1;
  250. elseif (in_array(3, $types_saved) && (in_array(11, $types_saved) || in_array(2, $types_saved)))
  251. $tipo_strada = 2;
  252. elseif (in_array(4, $types_saved))
  253. $tipo_strada = 3;
  254. }
  255. $pav = ' ';
  256. if ($item->pavimentazione)
  257. switch ($item->pavimentazione) {
  258. case 5:
  259. case 6:
  260. $pav = 3;
  261. break;
  262. default:
  263. if (!$item->condizione_strada || $item->condizione_strada == 1) $pav = 1;
  264. else $pav = 2;
  265. }
  266. $int = ' ';
  267. switch ($item->particolarita_strada) {
  268. case 1:
  269. $int = '07';
  270. break;
  271. case 2:
  272. $int = '01';
  273. break;
  274. case 3:
  275. $int = '08';
  276. break;
  277. case 4:
  278. $int = '02';
  279. break;
  280. case 5:
  281. $int = '10';
  282. break;
  283. case 6:
  284. case 10:
  285. $int = '09';
  286. break;
  287. case 7:
  288. $int = '06';
  289. break;
  290. case 12:
  291. $int = '04';
  292. break;
  293. }
  294. $fon = ' ';
  295. if ($item->fondo_stradale)
  296. switch ($item->fondo_stradale) {
  297. case 1:
  298. $fon = 1;
  299. break;
  300. case 2:
  301. $fon = 2;
  302. break;
  303. case 3:
  304. $fon = 4;
  305. break;
  306. case 4:
  307. $fon = 5;
  308. break;
  309. case 5:
  310. $fon = 3;
  311. break;
  312. }
  313. $seg = ' ';
  314. if ($item->segnaletica)
  315. switch ($item->segnaletica) {
  316. case Istat::SEGNALETICA_ASSENTE:
  317. $seg = 1;
  318. break;
  319. case Istat::SEGNALETICA_VERTICALE:
  320. $seg = 2;
  321. break;
  322. case Istat::SEGNALETICA_ORIZZONTALE:
  323. $seg = 3;
  324. break;
  325. case Istat::SEGNALETICA_VERTICALE_ORIZZONTALE:
  326. $seg = 4;
  327. break;
  328. case Istat::SEGNALETICA_TEMPORANEA_CANTIERE:
  329. $seg = 5;
  330. break;
  331. }
  332. $met = ' ';
  333. if ($item->condizioni_atmosferiche)
  334. switch ($item->condizioni_atmosferiche) {
  335. case 1:
  336. $met = 1;
  337. break;
  338. case 2:
  339. $met = 2;
  340. break;
  341. case 3:
  342. $met = 3;
  343. break;
  344. case 4:
  345. $met = 2;
  346. break;
  347. case 5:
  348. $met = 7;
  349. break;
  350. case 6:
  351. $met = 1;
  352. break;
  353. case 7:
  354. $met = 4;
  355. break;
  356. case 8:
  357. $met = 5;
  358. break;
  359. case 9:
  360. $met = 6;
  361. break;
  362. }
  363. $item_data .= str_pad($loc, 1, ' ', STR_PAD_RIGHT);
  364. $item_data .= str_pad($item->nomenclatura_strada_numero, 3, ' ', STR_PAD_RIGHT);
  365. $item_data .= str_repeat(' ', 3);
  366. $item_data .= str_repeat(' ', 2);
  367. $item_data .= str_pad($tipo_strada, 1, ' ', STR_PAD_RIGHT);
  368. $item_data .= str_pad($pav, 1, ' ', STR_PAD_RIGHT);
  369. $item_data .= str_pad($int, 2, ' ', STR_PAD_RIGHT);
  370. $item_data .= str_pad($fon, 1, ' ', STR_PAD_RIGHT);
  371. $item_data .= str_pad($seg, 1, ' ', STR_PAD_RIGHT);
  372. $item_data .= str_pad($met, 1, ' ', STR_PAD_RIGHT);
  373. $sectionLength = strlen($item_data) - $sectionStart;
  374. $sectionCounts['Section 11-20'] = $sectionLength;
  375. Log::info('Item Data 11-20 Length: ' . $sectionLength);
  376. } catch (\Exception $e) {
  377. Log::error('Error in section 11-20 for item ' . $item->id . ': ' . $e->getMessage());
  378. $item_data = str_pad($item_data, 20, ' ');
  379. }
  380. try {
  381. //21-30
  382. $sectionStart = strlen($item_data);
  383. $tipo_urto = str_repeat(' ', 2);
  384. $types_saved = \App\Models\ReportTipoUrto::where('report_id', $report->id)->pluck('tipo_urto_id')
  385. ->toArray();
  386. if ($types_saved) {
  387. if (in_array(1, $types_saved))
  388. $tipo_urto = '01';
  389. elseif (in_array(2, $types_saved))
  390. $tipo_urto = '02';
  391. elseif (in_array(3, $types_saved) || in_array(26, $types_saved) || in_array(27, $types_saved))
  392. $tipo_urto = '04';
  393. elseif (in_array(5, $types_saved))
  394. $tipo_urto = '03';
  395. elseif (in_array(7, $types_saved) || in_array(12, $types_saved))
  396. $tipo_urto = '05';
  397. elseif (in_array(4, $types_saved) || in_array(21, $types_saved))
  398. $tipo_urto = '07';
  399. elseif (in_array(8, $types_saved))
  400. $tipo_urto = '06';
  401. elseif (in_array(6, $types_saved) || in_array(19, $types_saved) || in_array(14, $types_saved))
  402. $tipo_urto = '08';
  403. elseif (in_array(9, $types_saved) || in_array(18, $types_saved))
  404. $tipo_urto = '10';
  405. elseif (in_array(25, $types_saved) || in_array(22, $types_saved) || in_array(13, $types_saved))
  406. $tipo_urto = '12';
  407. }
  408. $item_data .= $tipo_urto;
  409. $v_data = array(
  410. 'tipo' => '',
  411. 'circostanze' => '',
  412. 'veicolo' => '',
  413. 'conducente_passeggeri' => '',
  414. 'pedoni' => '',
  415. 'cittadinanza_conducente' => ''
  416. );
  417. $deceduti_24h = array();
  418. $deceduti_30gg = array();
  419. $feriti = array();
  420. $data_veicoli = \App\Models\ReportDataVeicoli::where('report_id', $report->id)->get()->toArray();
  421. for ($i = 0; $i < 3; $i++) {
  422. $data_veicolo = isset($data_veicoli[$i]) ? $data_veicoli[$i] : false;
  423. $stato_psico_fisico = str_repeat(' ', 2);
  424. if ($data_veicolo) {
  425. if ($data_veicolo["veicoli"] > 0) {
  426. $veicolo = \App\Models\Vehicle::findOrFail($data_veicolo["veicoli"]);
  427. $v_tipo = str_repeat(' ', 2);
  428. switch ($veicolo->tipo_id) {
  429. case 1:
  430. switch ($veicolo->destinazione_uso) {
  431. case \App\Models\Vehicle::DESTINAZIONE_USO_PRIVATO:
  432. $v_tipo = '01';
  433. break;
  434. case \App\Models\Vehicle::DESTINAZIONE_USO_PUBBLICO:
  435. $v_tipo = '03';
  436. break;
  437. case \App\Models\Vehicle::DESTINAZIONE_USO_SOCCORSO_POLIZIA:
  438. $v_tipo = '04';
  439. break;
  440. default:
  441. $v_tipo = '01';
  442. break;
  443. }
  444. break;
  445. case 17:
  446. $v_tipo = '02';
  447. break;
  448. case 7:
  449. if ($veicolo->destinazione_uso == \App\Models\Vehicle::DESTINAZIONE_USO_SERVIZO_PUBBLICO_DI_LINEA)
  450. $v_tipo = '05';
  451. else
  452. $v_tipo = '06';
  453. break;
  454. case 16:
  455. $v_tipo = '08';
  456. break;
  457. case 14:
  458. case 20:
  459. $v_tipo = '09';
  460. break;
  461. case 18:
  462. $v_tipo = '10';
  463. break;
  464. case 8:
  465. $v_tipo = '13';
  466. break;
  467. case 6:
  468. $v_tipo = '14';
  469. break;
  470. case 3:
  471. $v_tipo = '15';
  472. break;
  473. case 5:
  474. $v_tipo = '17';
  475. break;
  476. case 19:
  477. $v_tipo = '18';
  478. break;
  479. case 4:
  480. $v_tipo = '21';
  481. break;
  482. }
  483. $v_data['tipo'] .= $v_tipo;
  484. $v_data['veicolo'] .= $veicolo->foreign_country ?
  485. str_repeat(' ', 7) :
  486. str_pad(substr($veicolo->targa, 0, 8), 8, ' ', STR_PAD_RIGHT);
  487. $v_data['veicolo'] .= $veicolo->foreign_country ?
  488. str_pad($veicolo->getCartaCircolazioneRilasciataDaDiValueAttribute(), 3, ' ', STR_PAD_RIGHT) :
  489. str_repeat(' ', 3);
  490. $time = strtotime($veicolo->carta_circolazione_rilasciata_il);
  491. $v_data['veicolo'] .= str_pad($time && $time > 0 ? date('y', $time) : '', 2, ' ', STR_PAD_RIGHT);
  492. $v_data['veicolo'] .= str_repeat(' ', 5);
  493. }
  494. if ($data_veicolo["alcool_test"] && $data_veicolo["alcool_test_esito"] == Istat::ESITO_POSITIVO)
  495. $stato_psico_fisico = '90';
  496. elseif ($data_veicolo["drug_test"] && $data_veicolo["drug_test_esito"] == Istat::ESITO_POSITIVO)
  497. $stato_psico_fisico = '94';
  498. try
  499. {
  500. $conducente = Anagrafica::findOrFail($data_veicolo["conducenti"]);
  501. $eta_conducente = '00';
  502. if ($conducente->birth_date != '' && $report->verificatosi_in_data != '') {
  503. $eta_conducente = 0;
  504. if ($eta_conducente > 100 || $eta_conducente < 0) $eta_conducente = 0;
  505. $eta_conducente = str_pad($eta_conducente, 2, '0', STR_PAD_LEFT);
  506. }
  507. $v_data['conducente_passeggeri'] .= $eta_conducente;
  508. $v_data['conducente_passeggeri'] .= $conducente->gender == 'F' ? '2' : '1';
  509. $details = array(
  510. 'firstname' => $conducente->firstname,
  511. 'lastname' => $conducente->lastname,
  512. 'ospedale' => $data_veicolo["infortunato_ospedale"],
  513. );
  514. } catch (\Exception $e) {
  515. $v_data['conducente_passeggeri'] .= '00';
  516. $v_data['conducente_passeggeri'] .= ' ';
  517. $details = array(
  518. 'firstname' => '',
  519. 'lastname' => '',
  520. 'ospedale' => '0',
  521. );
  522. Log::error('non trovato conducente ' . $data_veicolo["conducenti"] . ': ' . $report->id . " -- " . $e->getMessage());
  523. }
  524. switch ($data_veicolo["infortunato"]) {
  525. case Istat::INFORTUNATO_DECEDUTO_30GG:
  526. $infortunato = 4;
  527. $deceduti_30gg[] = $details;
  528. break;
  529. case Istat::INFORTUNATO_DECEDUTO_24H:
  530. $infortunato = 3;
  531. $deceduti_24h[] = $details;
  532. break;
  533. case Istat::INFORTUNATO_FERITO:
  534. $infortunato = 2;
  535. $feriti[] = $details;
  536. break;
  537. default:
  538. $infortunato = 1;
  539. break;
  540. }
  541. $v_data['conducente_passeggeri'] .= $infortunato;
  542. switch ($data_veicolo["conducente_patente_categoria"]) {
  543. case Istat::PATENTE_CATEGORIA_AM:
  544. $patente = '0';
  545. break;
  546. case Istat::PATENTE_CATEGORIA_A1:
  547. case Istat::PATENTE_CATEGORIA_A1B:
  548. case Istat::PATENTE_CATEGORIA_A2:
  549. case Istat::PATENTE_CATEGORIA_A:
  550. $patente = '1';
  551. break;
  552. case Istat::PATENTE_CATEGORIA_AB:
  553. case Istat::PATENTE_CATEGORIA_B1:
  554. case Istat::PATENTE_CATEGORIA_B:
  555. $patente = '2';
  556. break;
  557. case Istat::PATENTE_CATEGORIA_C1:
  558. case Istat::PATENTE_CATEGORIA_C:
  559. $patente = '3';
  560. break;
  561. case Istat::PATENTE_CATEGORIA_D1:
  562. $patente = '4';
  563. break;
  564. case Istat::PATENTE_CATEGORIA_BE:
  565. case Istat::PATENTE_CATEGORIA_C1E:
  566. case Istat::PATENTE_CATEGORIA_CE:
  567. case Istat::PATENTE_CATEGORIA_D1E:
  568. case Istat::PATENTE_CATEGORIA_DE:
  569. case Istat::PATENTE_CATEGORIA_E:
  570. $patente = '5';
  571. break;
  572. default:
  573. $patente = '9';
  574. break;
  575. }
  576. Log::error('QUA 1');
  577. $v_data['conducente_passeggeri'] .= $patente;
  578. $patente_rilasciata_il = $data_veicolo["conducente_patente_rilasciata_il"];
  579. $v_data['conducente_passeggeri'] .= $patente_rilasciata_il ? date('y', strtotime($patente_rilasciata_il)) : str_repeat(' ', 2);
  580. $v_data['conducente_passeggeri'] .= str_repeat(' ', 4);
  581. $datiPasseggeri = \App\Models\ReportDataPasseggeri::where('progressive', $i)->where('report_id', $report->id)->get()->toArray();
  582. for ($j = 0; $j < 4; $j++) {
  583. $passeggero_data = str_repeat(' ', 4);
  584. if (isset($datiPasseggeri[$j])) {
  585. $passeggero = $datiPasseggeri[$j]["passeggero"];
  586. if ($passeggero) {
  587. $passeggero_infortunato = $datiPasseggeri[$j]["infortunato"];
  588. if ($passeggero_infortunato != Istat::INFORTUNATO_INCOLUME) {
  589. try{
  590. $passeggero_anagrafica = Anagrafica::findOrFail($passeggero);
  591. $eta_passeggero = '00';
  592. if ($passeggero_anagrafica->birth_date && $report->verificatosi_in_data) {
  593. $eta_passeggero = 0;
  594. DateHelper::calculateAge($passeggero_anagrafica->birth_date, $report->verificatosi_in_data);
  595. if ($eta_passeggero == 0) $eta_passeggero = 1;
  596. elseif ($eta_passeggero > 100 || $eta_passeggero < 0) $eta_passeggero = 0;
  597. $eta_passeggero = str_pad($eta_passeggero, 2, '0', STR_PAD_LEFT);
  598. }
  599. $passeggero_data = sprintf(
  600. '%s%s%s',
  601. $passeggero_infortunato == Istat::INFORTUNATO_FERITO ? '2' : '1',
  602. $eta_passeggero,
  603. $passeggero_anagrafica->gender == 'F' ? '4' : '3'
  604. );
  605. $details = array(
  606. 'firstname' => $passeggero_anagrafica->firstname,
  607. 'lastname' => $passeggero_anagrafica->lastname,
  608. 'ospedale' => $datiPasseggeri[$j]["infortunato_ospedale"]
  609. );
  610. switch ($passeggero_infortunato) {
  611. case Istat::INFORTUNATO_DECEDUTO_30GG:
  612. $deceduti_30gg[] = $details;
  613. break;
  614. case Istat::INFORTUNATO_DECEDUTO_24H:
  615. $deceduti_24h[] = $details;
  616. break;
  617. case Istat::INFORTUNATO_FERITO:
  618. $feriti[] = $details;
  619. break;
  620. }
  621. } catch (\Exception $e) {
  622. Log::error('non trovato passeggero ' . $passeggero . ': ' . $e->getMessage());
  623. }
  624. }
  625. }
  626. }
  627. $v_data['conducente_passeggeri'] .= $passeggero_data;
  628. }
  629. $v_data['conducente_passeggeri'] .= str_repeat(' ', 8);
  630. Log::error('QUA 2');
  631. $v_data['cittadinanza_conducente'] .= @$conducente->nazionalita == 0 ? '1' : '2';
  632. $country_id = 106;
  633. if (@$conducente->nazionalita != 0)
  634. $country_id = @$conducente->nazione_straniera;
  635. if (!$country_id) $v_data['cittadinanza_conducente'] .= str_repeat(' ', 33);
  636. else {
  637. $country = \App\Models\LocationCountry::findOrFail($country_id);
  638. $v_data['cittadinanza_conducente'] .= str_pad($country->istat_code, 3, ' ', STR_PAD_RIGHT);
  639. $v_data['cittadinanza_conducente'] .= str_pad(preg_replace('[^A-Z ]', '', strtoupper($country->name)), 30, ' ', STR_PAD_RIGHT);
  640. }
  641. $countryDataLen = strlen($v_data['cittadinanza_conducente']);
  642. $expectedCountryDataLen = 34;
  643. if ($countryDataLen < $expectedCountryDataLen) {
  644. $v_data['cittadinanza_conducente'] .= str_repeat(' ', $expectedCountryDataLen - $countryDataLen);
  645. }
  646. } else {
  647. $v_data['tipo'] .= str_repeat(' ', 2);
  648. $v_data['veicolo'] .= str_repeat(' ', 18);
  649. $v_data['conducente_passeggeri'] .= str_repeat(' ', 35);
  650. $v_data['pedoni'] .= str_repeat(' ', 24);
  651. $v_data['cittadinanza_conducente'] .= str_repeat(' ', 34);
  652. }
  653. if ($i < 2) {
  654. $v_data['circostanze'] .= '00';
  655. $v_data['circostanze'] .= str_repeat(' ', 2);
  656. $v_data['circostanze'] .= $stato_psico_fisico;
  657. }
  658. }
  659. $p_data = '';
  660. Log::error('QUA 3');
  661. $datiPedoni = ReportDataPedoni::where('report_id', $report->id)->get()->toArray();
  662. for ($j = 0; $j < 4; $j++) {
  663. $pedone_data = str_repeat(' ', 6);
  664. if (isset($datiPedoni[$j])) {
  665. $pedone = $datiPedoni[$j]["pedoni"];
  666. if ($pedone) {
  667. $pedone_infortunato = $infortunato;
  668. if ($pedone_infortunato != Istat::INFORTUNATO_INCOLUME) {
  669. Log::error('QUA 4');
  670. try{
  671. $pedone_anagrafica = new Anagrafica([$pedone]);
  672. $eta_pedone = '00';
  673. if (DateHelper::filterDate($pedone_anagrafica->get('birth_date')) && DateHelper::filterDate($report->get('verificatosi_in_data'))) {
  674. $eta_pedone = DateHelper::calculateAge($pedone_anagrafica->get('birth_date'), $report->get('verificatosi_in_data'));
  675. if ($eta_pedone == 0) $eta_pedone = 1;
  676. elseif ($eta_pedone > 100 || $eta_pedone < 0) $eta_pedone = 0;
  677. $eta_pedone = str_pad($eta_pedone, 2, '0', STR_PAD_LEFT);
  678. }
  679. if ($pedone_infortunato == Istat::INFORTUNATO_FERITO)
  680. $pedone_data = sprintf(
  681. '%s%s%s',
  682. str_repeat(' ', 3),
  683. $pedone_anagrafica->get('gender') == 'F' ? '4' : '3',
  684. $eta_pedone
  685. );
  686. else
  687. $pedone_data = sprintf(
  688. '%s%s%s',
  689. $pedone_anagrafica->get('gender') == 'F' ? '2' : '1',
  690. $eta_pedone,
  691. str_repeat(' ', 3)
  692. );
  693. $details = array(
  694. 'firstname' => $pedone_anagrafica->get('firstname'),
  695. 'lastname' => $pedone_anagrafica->get('lastname'),
  696. 'ospedale' => ReportDataPedoni::where('report_id', $report->id)
  697. ->where('progressive', $j)
  698. ->value('infortunato_ospedale')
  699. );
  700. switch ($pedone_infortunato) {
  701. case Istat::INFORTUNATO_DECEDUTO_30GG:
  702. $deceduti_30gg[] = $details;
  703. break;
  704. case Istat::INFORTUNATO_DECEDUTO_24H:
  705. $deceduti_24h[] = $details;
  706. break;
  707. case Istat::INFORTUNATO_FERITO:
  708. $feriti[] = $details;
  709. break;
  710. }
  711. } catch (\Exception $e) {
  712. Log::error('non trovato pedone ' . $pedone . ': ' . $e->getMessage());
  713. }
  714. }
  715. }
  716. }
  717. $p_data .= $pedone_data;
  718. }
  719. $v_data['tipo'] = str_pad($v_data['tipo'], 6, ' ', STR_PAD_RIGHT);
  720. $item_data .= $v_data['tipo'];
  721. $item_data .= str_repeat(' ', 24);
  722. $v_data['circostanze'] = str_pad($v_data['circostanze'], 12, ' ', STR_PAD_RIGHT);
  723. $item_data .= $v_data['circostanze'];
  724. $v_data['veicolo'] = str_pad($v_data['veicolo'], 54, ' ', STR_PAD_RIGHT);
  725. $item_data .= $v_data['veicolo'];
  726. $v_data['conducente_passeggeri'] = str_pad($v_data['conducente_passeggeri'], 105, ' ', STR_PAD_RIGHT);
  727. $item_data .= $v_data['conducente_passeggeri'];
  728. $p_data = str_pad($p_data, 24, ' ', STR_PAD_RIGHT);
  729. $item_data .= $p_data;
  730. $item_data .= str_repeat(' ', 10);
  731. $item_data .= str_pad(count($deceduti_24h), 2, '0', STR_PAD_LEFT);
  732. $item_data .= str_pad(count($deceduti_30gg), 2, '0', STR_PAD_LEFT);
  733. $item_data .= str_pad(count($feriti), 2, '0', STR_PAD_LEFT);
  734. $item_data .= str_repeat(' ', 9);
  735. $sectionLength = strlen($item_data) - $sectionStart;
  736. $sectionCounts['Section 21-30'] = $sectionLength;
  737. } catch (\Exception $e) {
  738. Log::error('Error in section 21-30 for item ' . $item->id . ': ' . $e->getMessage());
  739. $item_data .= str_repeat(' ', 250);
  740. $sectionLength = strlen($item_data) - $sectionStart;
  741. $sectionCounts['Section 21-30'] = $sectionLength;
  742. }
  743. try {
  744. //31-40
  745. $sectionStart = strlen($item_data);
  746. $stradario_due = \App\Models\Stradario::find($report->localita_due);
  747. $location_description = $stradario_due ?
  748. (($stradario_due['TOPONIMO'] ?? '') . ' ' . ($stradario_due['DESCRIZIONE'] ?? '')) :
  749. '';
  750. if ($report->intersezione_con) {
  751. $intersezione = \App\Models\Stradario::find($report->intersezione_con);
  752. if ($intersezione) {
  753. $intersezione_toponimo = $intersezione['TOPONIMO'];
  754. $intersezione_address = $intersezione['DESCRIZIONE'];
  755. $full_intersection = $intersezione_toponimo . ' ' . $intersezione_address;
  756. $location_description .= ' INTERSEZIONE CON ' . $full_intersection;
  757. }
  758. }
  759. if ($report->prossimita_civico) {
  760. $location_description .= ' PROSSIMITA CIVICO ' . $report->prossimita_civico;
  761. }
  762. if ($report->prossimita_chilometro) {
  763. $location_description .= ' PROSSIMITA CHILOMETRO ' . $report->prossimita_chilometro;
  764. }
  765. $item_data .= str_pad(substr(preg_replace('/[^A-Za-z0-9 ]/', '', strtoupper($location_description)), 0, 57), 57, ' ');
  766. $item_data .= str_repeat(' ', 100);
  767. $deceduti = array_merge($deceduti_24h, $deceduti_30gg);
  768. for ($i = 0; $i < 4; $i++) {
  769. $deceduto = isset($deceduti[$i]) ? $deceduti[$i] : null;
  770. $deceduto_data = str_repeat(' ', 60);
  771. if ($deceduto) {
  772. $deceduto_data = sprintf(
  773. '%s%s',
  774. str_pad(substr($deceduto['firstname'], 0, 30), 30, ' '),
  775. str_pad(substr($deceduto['lastname'], 0, 30), 30, ' ')
  776. );
  777. }
  778. $item_data .= $deceduto_data;
  779. }
  780. for ($i = 0; $i < 8; $i++) {
  781. $ferito = isset($feriti[$i]) ? $feriti[$i] : null;
  782. $ferito_data = str_repeat(' ', 90);
  783. if ($ferito) {
  784. $ferito_data = sprintf(
  785. '%s%s%s',
  786. str_pad(substr($ferito['firstname'], 0, 30), 30, ' '),
  787. str_pad(substr($ferito['lastname'], 0, 30), 30, ' '),
  788. str_pad(substr($ferito['ospedale'], 0, 30), 30, ' ')
  789. );
  790. }
  791. $item_data .= $ferito_data;
  792. }
  793. $item_data .= str_repeat(' ', 10);
  794. $item_data .= str_repeat(' ', 2);
  795. // Coordinate
  796. //$item_data .= str_repeat(' ', 100);
  797. $item_data .= str_pad($item->longitude, 50, ' ', STR_PAD_LEFT);
  798. $item_data .= str_pad($item->latitude, 50, ' ', STR_PAD_LEFT);
  799. $item_data .= str_repeat(' ', 8);
  800. $ora = $report->verificatosi_in_data_ora ?? '';
  801. $ora = $ora === '' ? '25' : str_pad(substr($ora, 0, 2), 2, '0', STR_PAD_LEFT);
  802. $item_data .= $ora;
  803. $minuti = (int) ($report->verificatosi_in_data_minuti ?? 0);
  804. if ($ora == '25') {
  805. $minuti = ' ';
  806. }
  807. $item_data .= str_pad(substr($minuti, 0, 2), 2, '0', STR_PAD_LEFT);
  808. $item_data .= str_repeat(' ', 30);
  809. $item_data .= str_repeat('0', 7);
  810. $item_data .= str_repeat(' ', 15);
  811. $item_data .= str_repeat(' ', 4);
  812. $item_data .= str_repeat(' ', 100);
  813. $town = \App\Models\LocationTown::find( $report->localita_uno);
  814. $town_title = $town ? $town['istat_code'] : '';
  815. $item_data .= str_pad(substr($town_title, 0, 40), 40, ' ');
  816. $item_data .= str_repeat(' ', 40);
  817. $item_data .= str_repeat(' ', 10);
  818. $item_data .= $v_data['cittadinanza_conducente'];
  819. $sectionLength = strlen($item_data) - $sectionStart;
  820. $sectionCounts['Section 31-40'] = $sectionLength;
  821. Log::info('Item Data 31-40 Length: ' . $sectionLength);
  822. $totalLength = strlen($item_data);
  823. $charCounts[] = $totalLength;
  824. Log::info('Total item data length: ' . $totalLength);
  825. } catch (\Exception $e) {
  826. Log::error('Error in section 31-40 for item ' . $item->id . ': ' . $e->getMessage());
  827. }
  828. $item_data = str_pad($item_data, 1939, ' ');
  829. // Coordinate
  830. $txt[] = $item_data;
  831. } catch (\Exception $e) {
  832. Log::error('Major error processing item ' . $item->id . ': ' . $e->getMessage());
  833. continue;
  834. }
  835. }
  836. $txt_data = implode("\r\n", $txt);
  837. return response()->streamDownload(function () use ($txt_data) {
  838. echo $txt_data;
  839. }, 'istat_' . $this->from . "_" . $this->to . '.txt');
  840. } catch (\Exception $e) {
  841. Log::error('Fatal error in export: ' . $e->getMessage());
  842. return response()->json(['error' => 'Export failed: ' . $e->getMessage()], 500);
  843. }
  844. }
  845. }