Istat.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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 = '2023-11-01', $to;
  133. protected $rules = [
  134. 'from' => 'required',
  135. 'to' => 'required',
  136. ];
  137. protected $messages = [
  138. 'from.required' => 'La data inizio è obbligatoria',
  139. 'to.required' => 'La data fine è obbligatoria',
  140. ];
  141. public function resetFields()
  142. {
  143. $this->from = '';
  144. $this->to = '';
  145. }
  146. public function render()
  147. {
  148. return view('livewire.istat');
  149. }
  150. public function export()
  151. {
  152. Log::info('Export ISTAT');
  153. Log::info('From: ' . $this->from);
  154. Log::info('To: ' . $this->to);
  155. try {
  156. $items = \DB::table('fcf_reports_reports')
  157. ->leftjoin('fcf_users', 'fcf_reports_reports.created_by', '=', 'fcf_users.id')
  158. ->selectRaw('fcf_reports_reports.*, CONCAT(fcf_users.firstname," ",fcf_users.lastname) AS display_name')
  159. ->where(function ($query) {
  160. $query->where('fcf_reports_reports.incidente_con_feriti', '1')
  161. ->orWhere('fcf_reports_reports.incidente_mortale', '=', '1');
  162. })
  163. ->where('fcf_reports_reports.verificatosi_in_data', '>=', $this->from != '' ? $this->from : '2000-01-01')
  164. ->where('fcf_reports_reports.verificatosi_in_data', '<=', $this->to != '' ? $this->to : '2050-01-01')
  165. ->orderBy('fcf_reports_reports.verificatosi_in_data')
  166. ->get();
  167. $txt = array();
  168. Log::info('Items: ' . count($items));
  169. foreach ($items as $item) {
  170. try {
  171. Log::info('Item: ' . $item->id);
  172. $report = \App\Models\Report::findOrFail($item->id);
  173. $item_data = '';
  174. //1-10
  175. try {
  176. $town_code = $prov_code = '000';
  177. $town = \App\Models\LocationTown::findOrFail($item->localita_uno);
  178. if ($town) {
  179. if ($town->istat_code) $town_code = $town->istat_code;
  180. $prov = \App\Models\LocationProvince::where('code', $town->prov)->first();
  181. if ($prov && $prov->istat_code) $prov_code = $prov->istat_code;
  182. }
  183. $item_data .= date('y', strtotime($item->verificatosi_in_data));
  184. $item_data .= date('m', strtotime($item->verificatosi_in_data));
  185. $item_data .= str_pad($prov_code, 3, '0', STR_PAD_LEFT);
  186. $item_data .= str_pad($town_code, 3, '0', STR_PAD_LEFT);
  187. $item_data .= str_repeat(' ', 4);
  188. $item_data .= date('d', strtotime($item->verificatosi_in_data));
  189. $item_data .= str_repeat(' ', 2);
  190. $item_data .= '4';
  191. $item_data .= str_repeat(' ', 5);
  192. $item_data .= ' ';
  193. Log::info('Item Data 1 - 10: ' . $item_data);
  194. } catch (\Exception $e) {
  195. Log::error('Error in section 1-10 for item ' . $item->id . ': ' . $e->getMessage());
  196. $item_data = str_pad($item_data, 20, ' ');
  197. }
  198. //11-20
  199. try {
  200. if ($item->localizzazione_incidente == Istat::LOCALIZZAZIONE_INCIDENTE_IN) {
  201. switch ($item->nomenclatura_strada) {
  202. case 2:
  203. $loc = 2;
  204. break;
  205. case 3:
  206. $loc = 0;
  207. break;
  208. case 4:
  209. $loc = 3;
  210. break;
  211. default:
  212. $loc = 1;
  213. }
  214. } elseif ($item->localizzazione_incidente == Istat::LOCALIZZAZIONE_INCIDENTE_OUT) {
  215. switch ($item->nomenclatura_strada) {
  216. case 2:
  217. $loc = 5;
  218. break;
  219. case 3:
  220. $loc = 9;
  221. break;
  222. case 4:
  223. $loc = 6;
  224. break;
  225. default:
  226. $loc = 4;
  227. }
  228. } else $loc = ' ';
  229. $tipo_strada = ' ';
  230. $types_saved = \App\Models\ReportTipoStrada::where('report_id', $report->id)->select('tipo_strada_id')->get()->toArray();
  231. if ($types_saved) {
  232. if (in_array(3, $types_saved) && in_array(1, $types_saved))
  233. $tipo_strada = 1;
  234. elseif (in_array(3, $types_saved) && (in_array(11, $types_saved) || in_array(2, $types_saved)))
  235. $tipo_strada = 2;
  236. elseif (in_array(4, $types_saved))
  237. $tipo_strada = 3;
  238. }
  239. $pav = ' ';
  240. if ($item->pavimentazione)
  241. switch ($item->pavimentazione) {
  242. case 5:
  243. case 6:
  244. $pav = 3;
  245. break;
  246. default:
  247. if (!$item->condizione_strada || $item->condizione_strada == 1) $pav = 1;
  248. else $pav = 2;
  249. }
  250. $int = ' ';
  251. switch ($item->particolarita_strada) {
  252. case 1:
  253. $int = '07';
  254. break;
  255. case 2:
  256. $int = '01';
  257. break;
  258. case 3:
  259. $int = '08';
  260. break;
  261. case 4:
  262. $int = '02';
  263. break;
  264. case 5:
  265. $int = '10';
  266. break;
  267. case 6:
  268. case 10:
  269. $int = '09';
  270. break;
  271. case 7:
  272. $int = '06';
  273. break;
  274. case 12:
  275. $int = '04';
  276. break;
  277. }
  278. $fon = ' ';
  279. if ($item->fondo_stradale)
  280. switch ($item->fondo_stradale) {
  281. case 1:
  282. $fon = 1;
  283. break;
  284. case 2:
  285. $fon = 2;
  286. break;
  287. case 3:
  288. $fon = 4;
  289. break;
  290. case 4:
  291. $fon = 5;
  292. break;
  293. case 5:
  294. $fon = 3;
  295. break;
  296. }
  297. $seg = ' ';
  298. if ($item->segnaletica)
  299. switch ($item->segnaletica) {
  300. case Istat::SEGNALETICA_ASSENTE:
  301. $seg = 1;
  302. break;
  303. case Istat::SEGNALETICA_VERTICALE:
  304. $seg = 2;
  305. break;
  306. case Istat::SEGNALETICA_ORIZZONTALE:
  307. $seg = 3;
  308. break;
  309. case Istat::SEGNALETICA_VERTICALE_ORIZZONTALE:
  310. $seg = 4;
  311. break;
  312. case Istat::SEGNALETICA_TEMPORANEA_CANTIERE:
  313. $seg = 5;
  314. break;
  315. }
  316. $met = ' ';
  317. if ($item->condizioni_atmosferiche)
  318. switch ($item->condizioni_atmosferiche) {
  319. case 1:
  320. $met = 1;
  321. break;
  322. case 2:
  323. $met = 2;
  324. break;
  325. case 3:
  326. $met = 3;
  327. break;
  328. case 4:
  329. $met = 2;
  330. break;
  331. case 5:
  332. $met = 7;
  333. break;
  334. case 6:
  335. $met = 1;
  336. break;
  337. case 7:
  338. $met = 4;
  339. break;
  340. case 8:
  341. $met = 5;
  342. break;
  343. case 9:
  344. $met = 6;
  345. break;
  346. }
  347. $item_data .= $loc;
  348. $item_data .= str_pad($item->nomenclatura_strada_numero, 3, ' ', STR_PAD_RIGHT);
  349. $item_data .= str_repeat(' ', 3);
  350. $item_data .= str_repeat(' ', 2);
  351. $item_data .= $tipo_strada;
  352. $item_data .= $pav;
  353. $item_data .= $int;
  354. $item_data .= $fon;
  355. $item_data .= $seg;
  356. $item_data .= $met;
  357. Log::info('Item Data 11 - 20: ' . $item_data);
  358. } catch (\Exception $e) {
  359. Log::error('Error in section 11-20 for item ' . $item->id . ': ' . $e->getMessage());
  360. $item_data = str_pad($item_data, 20, ' ');
  361. }
  362. try {
  363. //21-30
  364. $tipo_urto = str_repeat(' ', 2);
  365. $types_saved = \App\Models\ReportTipoUrto::where('report_id', $report->id)->select('tipo_urto_id')->get()->toArray();
  366. if ($types_saved) {
  367. if (in_array(1, $types_saved))
  368. $tipo_urto = '01';
  369. elseif (in_array(2, $types_saved))
  370. $tipo_urto = '02';
  371. elseif (in_array(3, $types_saved) || in_array(26, $types_saved) || in_array(27, $types_saved))
  372. $tipo_urto = '04';
  373. elseif (in_array(5, $types_saved))
  374. $tipo_urto = '03';
  375. elseif (in_array(7, $types_saved) || in_array(12, $types_saved))
  376. $tipo_urto = '05';
  377. elseif (in_array(4, $types_saved) || in_array(21, $types_saved))
  378. $tipo_urto = '07';
  379. elseif (in_array(8, $types_saved))
  380. $tipo_urto = '06';
  381. elseif (in_array(6, $types_saved) || in_array(19, $types_saved) || in_array(14, $types_saved))
  382. $tipo_urto = '08';
  383. elseif (in_array(9, $types_saved) || in_array(18, $types_saved))
  384. $tipo_urto = '10';
  385. elseif (in_array(25, $types_saved) || in_array(22, $types_saved) || in_array(13, $types_saved))
  386. $tipo_urto = '12';
  387. }
  388. $item_data .= $tipo_urto;
  389. $v_data = array(
  390. 'tipo' => '',
  391. 'circostanze' => '',
  392. 'veicolo' => '',
  393. 'conducente_passeggeri' => '',
  394. 'pedoni' => '',
  395. 'cittadinanza_conducente' => ''
  396. );
  397. $deceduti_24h = array();
  398. $deceduti_30gg = array();
  399. $feriti = array();
  400. $data_veicoli = \App\Models\ReportDataVeicoli::where('report_id', $report->id)->get()->toArray();
  401. for ($i = 0; $i < 3; $i++) {
  402. $data_veicolo = isset($data_veicoli[$i]) ? $data_veicoli[$i] : false;
  403. $stato_psico_fisico = str_repeat(' ', 2);
  404. if ($data_veicolo) {
  405. if ($data_veicolo["veicoli"] > 0) {
  406. $veicolo = \App\Models\Vehicle::findOrFail($data_veicolo["veicoli"]); //(ArrayHelper::getValue($data_veicolo, 'veicoli', 0));
  407. $v_tipo = str_repeat(' ', 2);
  408. switch ($veicolo->tipo_id) {
  409. case 1:
  410. switch ($veicolo->destinazione_uso) {
  411. case \App\Models\Vehicle::DESTINAZIONE_USO_PRIVATO:
  412. $v_tipo = '01';
  413. break;
  414. case \App\Models\Vehicle::DESTINAZIONE_USO_PUBBLICO:
  415. $v_tipo = '03';
  416. break;
  417. case \App\Models\Vehicle::DESTINAZIONE_USO_SOCCORSO_POLIZIA:
  418. $v_tipo = '04';
  419. break;
  420. default:
  421. $v_tipo = '01';
  422. break;
  423. }
  424. break;
  425. case 17:
  426. $v_tipo = '02';
  427. break;
  428. case 7:
  429. if ($veicolo->destinazione_uso == \App\Models\Vehicle::DESTINAZIONE_USO_SERVIZO_PUBBLICO_DI_LINEA)
  430. $v_tipo = '05';
  431. else
  432. $v_tipo = '06';
  433. break;
  434. case 16:
  435. $v_tipo = '08';
  436. break;
  437. case 14:
  438. case 20:
  439. $v_tipo = '09';
  440. break;
  441. case 18:
  442. $v_tipo = '10';
  443. break;
  444. case 8:
  445. $v_tipo = '13';
  446. break;
  447. case 6:
  448. $v_tipo = '14';
  449. break;
  450. case 3:
  451. $v_tipo = '15';
  452. break;
  453. case 5:
  454. $v_tipo = '17';
  455. break;
  456. case 19:
  457. $v_tipo = '18';
  458. break;
  459. case 4:
  460. $v_tipo = '21';
  461. break;
  462. }
  463. $v_data['tipo'] .= $v_tipo;
  464. $v_data['veicolo'] .= $veicolo->foreign_country ?
  465. str_repeat(' ', 8) :
  466. str_pad(substr($veicolo->targa, 0, 8), 8, ' ', STR_PAD_RIGHT);
  467. $v_data['veicolo'] .= $veicolo->foreign_country ?
  468. str_pad($veicolo->getCartaCircolazioneRilasciataDaDiValueAttribute(), 3, ' ', STR_PAD_RIGHT) :
  469. str_repeat(' ', 3);
  470. $time = strtotime($veicolo->carta_circolazione_rilasciata_il);
  471. $v_data['veicolo'] .= str_pad($time && $time > 0 ? date('y', $time) : '', 2, ' ', STR_PAD_RIGHT);
  472. $v_data['veicolo'] .= str_repeat(' ', 5);
  473. }
  474. if ($data_veicolo["alcool_test"] && $data_veicolo["alcool_test_esito"] == Istat::ESITO_POSITIVO)
  475. $stato_psico_fisico = '90';
  476. elseif ($data_veicolo["drug_test"] && $data_veicolo["drug_test_esito"] == Istat::ESITO_POSITIVO)
  477. $stato_psico_fisico = '94';
  478. $conducente = Anagrafica::findOrFail($data_veicolo["conducenti"]);
  479. $eta_conducente = '00';
  480. if ($conducente->birth_date != '' && $report->verificatosi_in_data != '') {
  481. $eta_conducente = 0;
  482. if ($eta_conducente > 100 || $eta_conducente < 0) $eta_conducente = 0;
  483. $eta_conducente = str_pad($eta_conducente, 2, '0', STR_PAD_LEFT);
  484. }
  485. $v_data['conducente_passeggeri'] .= $eta_conducente;
  486. $v_data['conducente_passeggeri'] .= $conducente->gender == 'F' ? '2' : '1';
  487. $details = array(
  488. 'firstname' => $conducente->firstname,
  489. 'lastname' => $conducente->lastname,
  490. 'ospedale' => $data_veicolo["infortunato_ospedale"],
  491. );
  492. switch ($data_veicolo["infortunato"]) {
  493. case Istat::INFORTUNATO_DECEDUTO_30GG:
  494. $infortunato = 4;
  495. $deceduti_30gg[] = $details;
  496. break;
  497. case Istat::INFORTUNATO_DECEDUTO_24H:
  498. $infortunato = 3;
  499. $deceduti_24h[] = $details;
  500. break;
  501. case Istat::INFORTUNATO_FERITO:
  502. $infortunato = 2;
  503. $feriti[] = $details;
  504. break;
  505. default:
  506. $infortunato = 1;
  507. break;
  508. }
  509. $v_data['conducente_passeggeri'] .= $infortunato;
  510. switch ($data_veicolo["conducente_patente_categoria"]) {
  511. case Istat::PATENTE_CATEGORIA_AM:
  512. $patente = '0';
  513. break;
  514. case Istat::PATENTE_CATEGORIA_A1:
  515. case Istat::PATENTE_CATEGORIA_A1B:
  516. case Istat::PATENTE_CATEGORIA_A2:
  517. case Istat::PATENTE_CATEGORIA_A:
  518. $patente = '1';
  519. break;
  520. case Istat::PATENTE_CATEGORIA_AB:
  521. case Istat::PATENTE_CATEGORIA_B1:
  522. case Istat::PATENTE_CATEGORIA_B:
  523. $patente = '2';
  524. break;
  525. case Istat::PATENTE_CATEGORIA_C1:
  526. case Istat::PATENTE_CATEGORIA_C:
  527. $patente = '3';
  528. break;
  529. case Istat::PATENTE_CATEGORIA_D1:
  530. $patente = '4';
  531. break;
  532. case Istat::PATENTE_CATEGORIA_BE:
  533. case Istat::PATENTE_CATEGORIA_C1E:
  534. case Istat::PATENTE_CATEGORIA_CE:
  535. case Istat::PATENTE_CATEGORIA_D1E:
  536. case Istat::PATENTE_CATEGORIA_DE:
  537. case Istat::PATENTE_CATEGORIA_E:
  538. $patente = '5';
  539. break;
  540. default:
  541. $patente = '9';
  542. break;
  543. }
  544. $v_data['conducente_passeggeri'] .= $patente;
  545. $patente_rilasciata_il = $data_veicolo["conducente_patente_rilasciata_il"];
  546. $v_data['conducente_passeggeri'] .= $patente_rilasciata_il ? date('y', strtotime($patente_rilasciata_il)) : str_repeat(' ', 2);
  547. $v_data['conducente_passeggeri'] .= str_repeat(' ', 4);
  548. $datiPasseggeri = \App\Models\ReportDataPasseggeri::where('progressive', $i)->where('report_id', $report->id)->get()->toArray();
  549. for ($j = 0; $j < 4; $j++) {
  550. $passeggero_data = str_repeat(' ', 4);
  551. if (isset($datiPasseggeri[$j])) {
  552. $passeggero = $datiPasseggeri[$j]["passeggero"];
  553. if ($passeggero) {
  554. $passeggero_infortunato = $datiPasseggeri[$j]["infortunato"];
  555. if ($passeggero_infortunato != Istat::INFORTUNATO_INCOLUME) {
  556. $passeggero_anagrafica = Anagrafica::findOrFail($passeggero);
  557. $eta_passeggero = '00';
  558. if ($passeggero_anagrafica->birth_date && $report->verificatosi_in_data) {
  559. $eta_passeggero = 0; // * * *DateHelper::calculateAge($passeggero_anagrafica->birth_date,$report->verificatosi_in_data);
  560. if ($eta_passeggero == 0) $eta_passeggero = 1;
  561. elseif ($eta_passeggero > 100 || $eta_passeggero < 0) $eta_passeggero = 0;
  562. $eta_passeggero = str_pad($eta_passeggero, 2, '0', STR_PAD_LEFT);
  563. }
  564. $passeggero_data = sprintf(
  565. '%s%s%s',
  566. $passeggero_infortunato == Istat::INFORTUNATO_FERITO ? '2' : '1',
  567. $eta_passeggero,
  568. $passeggero_anagrafica->gender == 'F' ? '4' : '3'
  569. );
  570. $details = array(
  571. 'firstname' => $passeggero_anagrafica->firstname,
  572. 'lastname' => $passeggero_anagrafica->lastname,
  573. 'ospedale' => $datiPasseggeri[$j]["infortunato_ospedale"]
  574. );
  575. switch ($passeggero_infortunato) {
  576. case Istat::INFORTUNATO_DECEDUTO_30GG:
  577. $deceduti_30gg[] = $details;
  578. break;
  579. case Istat::INFORTUNATO_DECEDUTO_24H:
  580. $deceduti_24h[] = $details;
  581. break;
  582. case Istat::INFORTUNATO_FERITO:
  583. $feriti[] = $details;
  584. break;
  585. }
  586. }
  587. }
  588. }
  589. $v_data['conducente_passeggeri'] .= $passeggero_data;
  590. }
  591. $v_data['conducente_passeggeri'] .= str_repeat(' ', 8);
  592. $v_data['cittadinanza_conducente'] .= $conducente->nazionalita == 0 ? '1' : '2';
  593. $country_id = 106;
  594. if ($conducente->nazionalita != 0)
  595. $country_id = $conducente->nazione_straniera;
  596. if (!$country_id) $v_data['cittadinanza_conducente'] .= str_repeat(' ', 33);
  597. else {
  598. $country = \App\Models\LocationCountry::findOrFail($country_id);
  599. $v_data['cittadinanza_conducente'] .= str_pad($country->istat_code, 3, ' ', STR_PAD_RIGHT);
  600. $v_data['cittadinanza_conducente'] .= str_pad(preg_replace('[^A-Z ]', '', strtoupper($country->name)), 30, ' ', STR_PAD_RIGHT);
  601. }
  602. } else {
  603. $v_data['tipo'] .= str_repeat(' ', 2);
  604. $v_data['veicolo'] .= str_repeat(' ', 18);
  605. $v_data['conducente_passeggeri'] .= str_repeat(' ', 35);
  606. $v_data['pedoni'] .= str_repeat(' ', 24);
  607. $v_data['cittadinanza_conducente'] .= str_repeat(' ', 34);
  608. }
  609. if ($i < 2) {
  610. $v_data['circostanze'] .= '00';
  611. $v_data['circostanze'] .= str_repeat(' ', 2);
  612. $v_data['circostanze'] .= $stato_psico_fisico;
  613. }
  614. }
  615. $p_data = '';
  616. $datiPedoni = ReportDataPedoni::where('report_id', $report->id)->get()->toArray();
  617. for ($j = 0; $j < 4; $j++) {
  618. $pedone_data = str_repeat(' ', 6);
  619. if (isset($datiPedoni[$j])) {
  620. $pedone = $datiPedoni[$j]["pedoni"];
  621. if ($pedone) {
  622. $pedone_infortunato = $infortunato;
  623. if ($pedone_infortunato != Istat::INFORTUNATO_INCOLUME) {
  624. $pedone_anagrafica = new Anagrafica([$pedone]);
  625. $eta_pedone = '00';
  626. if (DateHelper::filterDate($pedone_anagrafica->get('birth_date')) && DateHelper::filterDate($report->get('verificatosi_in_data'))) {
  627. $eta_pedone = DateHelper::calculateAge($pedone_anagrafica->get('birth_date'), $report->get('verificatosi_in_data'));
  628. if ($eta_pedone == 0) $eta_pedone = 1;
  629. elseif ($eta_pedone > 100 || $eta_pedone < 0) $eta_pedone = 0;
  630. $eta_pedone = str_pad($eta_pedone, 2, '0', STR_PAD_LEFT);
  631. }
  632. if ($pedone_infortunato == Istat::INFORTUNATO_FERITO)
  633. $pedone_data = sprintf(
  634. '%s%s%s',
  635. str_repeat(' ', 3),
  636. $pedone_anagrafica->get('gender') == 'F' ? '4' : '3',
  637. $eta_pedone
  638. );
  639. else
  640. $pedone_data = sprintf(
  641. '%s%s%s',
  642. $pedone_anagrafica->get('gender') == 'F' ? '2' : '1',
  643. $eta_pedone,
  644. str_repeat(' ', 3)
  645. );
  646. $details = array(
  647. 'firstname' => $pedone_anagrafica->get('firstname'),
  648. 'lastname' => $pedone_anagrafica->get('lastname'),
  649. 'ospedale' => ReportDataPedoni::where('report_id', $report->id)
  650. ->where('progressive', $j)
  651. ->value('infortunato_ospedale')
  652. );
  653. switch ($pedone_infortunato) {
  654. case Istat::INFORTUNATO_DECEDUTO_30GG:
  655. $deceduti_30gg[] = $details;
  656. break;
  657. case Istat::INFORTUNATO_DECEDUTO_24H:
  658. $deceduti_24h[] = $details;
  659. break;
  660. case Istat::INFORTUNATO_FERITO:
  661. $feriti[] = $details;
  662. break;
  663. }
  664. }
  665. }
  666. }
  667. $p_data .= $pedone_data;
  668. }
  669. $item_data .= $v_data['tipo'];
  670. $item_data .= str_repeat(' ', 24);
  671. $item_data .= $v_data['circostanze'];
  672. $item_data .= $v_data['veicolo'];
  673. $item_data .= $v_data['conducente_passeggeri'];
  674. $item_data .= $p_data;
  675. $item_data .= str_repeat(' ', 10);
  676. $item_data .= str_pad(count($deceduti_24h), 2, '0', STR_PAD_LEFT);
  677. $item_data .= str_pad(count($deceduti_30gg), 2, '0', STR_PAD_LEFT);
  678. $item_data .= str_pad(count($feriti), 2, '0', STR_PAD_LEFT);
  679. $item_data .= str_repeat(' ', 9);
  680. } catch (\Exception $e) {
  681. Log::error('Error in section 21-30 for item ' . $item->id . ': ' . $e->getMessage());
  682. }
  683. $txt[] = $item_data;
  684. Log::info('Item Data: ' . $item_data);
  685. } catch (\Exception $e) {
  686. Log::error('Major error processing item ' . $item->id . ': ' . $e->getMessage());
  687. continue;
  688. }
  689. }
  690. Log::info('Fuori loop');
  691. $txt_data = implode("\n", $txt);
  692. Log::info('TXT Data: ' . $txt_data);
  693. return response()->streamDownload(function () use ($txt_data) {
  694. echo $txt_data;
  695. }, 'istat_' . $this->from . "_" . $this->to . '.txt');
  696. } catch (\Exception $e) {
  697. Log::error('Fatal error in export: ' . $e->getMessage());
  698. return response()->json(['error' => 'Export failed: ' . $e->getMessage()], 500);
  699. }
  700. }
  701. }