Istat.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. <?php
  2. namespace App\Http\Livewire;
  3. use App\Helpers\DateHelper;
  4. use App\Models\Anagrafica;
  5. use App\Models\ReportDataPedoni;
  6. use Livewire\Component;
  7. class Istat extends Component
  8. {
  9. const LOCALIZZAZIONE_INCIDENTE_IN = 1;
  10. const LOCALIZZAZIONE_INCIDENTE_OUT = 2;
  11. const VISIBILITA_BUONA = 1;
  12. const VISIBILITA_LIMITATA = 2;
  13. const FONDO_STRADALE_SPECIFICA_OPTION_VALUE = 5;
  14. const PARTICOLARITA_STRADA_SPECIFICA_OPTION_VALUE = 5;
  15. const TRAFFICO_NORMALE = 1;
  16. const TRAFFICO_SCARSO = 2;
  17. const TRAFFICO_INTENSO = 3;
  18. const TIPO_STRADA_ALTRO_OPTION_VALUE = 8;
  19. const SEGNALETICA_ASSENTE = 1;
  20. const SEGNALETICA_VERTICALE = 2;
  21. const SEGNALETICA_ORIZZONTALE = 3;
  22. const SEGNALETICA_VERTICALE_ORIZZONTALE = 4;
  23. const SEGNALETICA_TEMPORANEA_CANTIERE = 5;
  24. const SEGNALETICA_VERTICALE_LIMITE_VELOCITA_OPTION_VALUE = 21;
  25. const SEGNALETICA_VERTICALE_PANNELLO_INTEGRATIVO_OPTION_VALUE = 22;
  26. const SEGNALETICA_VERTICALE_ALTRO_OPTION_VALUE = 23;
  27. const LINEA_CONTINUA = 1;
  28. const LINEA_TRATTEGGIATA = 2;
  29. const LINEA_TRATTEGGIATA_IN_CORRISPONDENZA = 3;
  30. const SEGNALETICA_ORIZZONTALE_LINEA_ASSENTE = 4;
  31. const MAX_PARTI_COINVOLTE = 10;
  32. const MAX_VEHICLES = 4;
  33. const MAX_PASSEGGERI = 4;
  34. const MAX_INFORTUNATI = 4;
  35. const MAX_PEDONI = 4;
  36. const MAX_TESTI_OCULARI = 4;
  37. const STATO_VEICOLO_MARCIA = 1;
  38. const STATO_VEICOLO_SOSTA = 2;
  39. const STATO_VEICOLO_NON_PRESENTE = 3;
  40. const POSIZIONE_STATICA_FINALE_DOCUMENTAZIONE_FOTOGRAFICA = 1;
  41. const POSIZIONE_STATICA_FINALE_RILIEVO_PLANIMETRICO = 2;
  42. const POSIZIONE_STATICA_FINALE_SCHIZZO_VISTA_NO_SCALA = 3;
  43. const POSIZIONE_STATICA_FINALE_GIA_RIMOSSO = 4;
  44. const DESTINAZIONE_VEICOLO_RIMOSSO_SENSI_ART_2028_CC = 1;
  45. const DESTINAZIONE_VEICOLO_SEQUESTRO_ART_354_CPP = 2;
  46. const DESTINAZIONE_VEICOLO_SEQUESTRO_ART_321_CPP = 3;
  47. const DESTINAZIONE_VEICOLO_SEQUESTRO = 4;
  48. const DESTINAZIONE_VEICOLO_SEQUESTRO_AMMINISTRATIVO = 5;
  49. const DESTINAZIONE_VEICOLO_FERMO_AMMINISTRATIVO = 6;
  50. const DESTINAZIONE_VEICOLO_RITIRATO_DAL_CONDUCENTE = 7;
  51. const STATO_PNEUMATICI_REGOLAMENTARE = 1;
  52. const STATO_PNEUMATICI_BUONO = 2;
  53. const STATO_PNEUMATICI_SUFFICIENTE = 3;
  54. const STATO_PNEUMATICI_INSUFFICIENTE = 4;
  55. const STATO_PNEUMATICI_NON_RILEVATO = 5;
  56. const STATO_PRESENTE = 1;
  57. const STATO_NON_PRESENTE = 2;
  58. const STATO_FUNZIONANTE = 1;
  59. const STATO_NON_FUNZIONANTE = 2;
  60. const STATO_NON_RILEVATO = 3;
  61. const YES = 1;
  62. const NO = 0;
  63. const SI_RISERVA = 3;
  64. const PATENTE_AL_SEGUITO_NON_PREVISTA = 3;
  65. const CONDUCENTE_GENERALIZZATO_LUOGO = 1;
  66. const CONDUCENTE_GENERALIZZATO_OSPEDALE = 2;
  67. const CONDUCENTE_GENERALIZZATO_ALTRO = 3;
  68. const INFORTUNATO_INCOLUME = 0;
  69. const INFORTUNATO_FERITO = 1;
  70. const INFORTUNATO_DECEDUTO_24H = 2;
  71. const INFORTUNATO_DECEDUTO_30GG = 3;
  72. const MARCIA_OPTION_N = 0;
  73. const MARCIA_OPTION_1 = 1;
  74. const MARCIA_OPTION_2 = 2;
  75. const MARCIA_OPTION_3 = 3;
  76. const MARCIA_OPTION_4 = 4;
  77. const MARCIA_OPTION_5 = 5;
  78. const MARCIA_OPTION_6 = 6;
  79. const MARCIA_OPTION_R = 7;
  80. const MARCIA_NON_RILEVATA = 8;
  81. const MARCIA_OPTION_AUTO = 9;
  82. const CRISTALLO_OPTION_INTERNO = 1;
  83. const CRISTALLO_OPTION_ESTERNO = 2;
  84. const CRISTALLO_OPTION_ALTRO = 3;
  85. const ESITO_NEGATIVO = 1;
  86. const ESITO_POSITIVO = 2;
  87. const DOCUMENTO_CI = 1;
  88. const DOCUMENTO_PAT = 2;
  89. const DOCUMENTO_PP = 3;
  90. const DOCUMENTO_ALTRO = 4;
  91. const PEDONE_PRESENTE_E_POSIZIONE = 1;
  92. const PEDONE_SOCCORSO_E_PRESSO = 2;
  93. const TESTE_PRESENTE_MOMENTO_RILIEVO = 1;
  94. const TESTE_NON_PRESENTE_MOMENTO_RILIEVO = 2;
  95. const TESTE_DICHIARA_LUOGO_INCIDENTE = 3;
  96. const TESTE_DICHIARA_PRONTO_SOCCORSO = 4;
  97. const TESTE_DICHIARA_PROSEGUO_TEMPO = 5;
  98. const PATENTE_CATEGORIA_AM = 'AM';
  99. const PATENTE_CATEGORIA_A1 = 'A1';
  100. const PATENTE_CATEGORIA_A1B = 'A1B';
  101. const PATENTE_CATEGORIA_A2 = 'A2';
  102. const PATENTE_CATEGORIA_A = 'A';
  103. const PATENTE_CATEGORIA_AB = 'AB';
  104. const PATENTE_CATEGORIA_B1 = 'B1';
  105. const PATENTE_CATEGORIA_B = 'B';
  106. const PATENTE_CATEGORIA_BE = 'BE';
  107. const PATENTE_CATEGORIA_C1 = 'C1';
  108. const PATENTE_CATEGORIA_C1E = 'C1E';
  109. const PATENTE_CATEGORIA_C = 'C';
  110. const PATENTE_CATEGORIA_CE = 'CE';
  111. const PATENTE_CATEGORIA_D1 = 'D1';
  112. const PATENTE_CATEGORIA_D1E = 'D1E';
  113. const PATENTE_CATEGORIA_DE = 'DE';
  114. const PATENTE_CATEGORIA_E = 'E';
  115. const PATENTE_ABILITAZIONI_CQC = 1;
  116. const PATENTE_ABILITAZIONI_CAP = 2;
  117. const PATENTE_ABILITAZIONI_CFC = 3;
  118. const DOCUMENTO_RILASCIATO_COMUNE = 1;
  119. const DOCUMENTO_RILASCIATO_MOTORIZZAZIONE_CIVILE = 2;
  120. const DOCUMENTO_RILASCIATO_ALTRO = 3;
  121. const DOCUMENTO_RILASCIATO_MCTC = 4;
  122. const DOCUMENTO_RILASCIATO_MIT = 5;
  123. const DOCUMENTO_RILASCIATO_UCO = 6;
  124. const DOCUMENTO_RILASCIATO_PREFETTURA = 7;
  125. const PATENTE_PRESCRIZIONI_OBBLIGO_LENTI = 1;
  126. const PATENTE_PRESCRIZIONI_APPARECCHIO_ACUSTICO = 2;
  127. const PATENTE_PRESCRIZIONI_LIMITAZIONI_ART_117 = 3;
  128. const PROTOCOLLO_NUM_LENGTH = 3;
  129. protected $paginationTheme = 'bootstrap';
  130. public $title = 'ISTAT';
  131. public $from = '2023-11-01', $to;
  132. protected $rules = [
  133. 'from' => 'required',
  134. 'to' => 'required',
  135. ];
  136. protected $messages = [
  137. 'from.required' => 'La data inizio è obbligatoria',
  138. 'to.required' => 'La data fine è obbligatoria',
  139. ];
  140. public function resetFields(){
  141. $this->from = '';
  142. $this->to = '';
  143. }
  144. public function render()
  145. {
  146. return view('livewire.istat');
  147. }
  148. public function export()
  149. {
  150. $items = \DB::table('fcf_reports_reports')
  151. ->leftjoin('fcf_users','fcf_reports_reports.created_by','=','fcf_users.id')
  152. ->selectRaw('fcf_reports_reports.*, CONCAT(fcf_users.firstname," ",fcf_users.lastname) AS display_name')
  153. ->where(function ($query) {
  154. $query->where('fcf_reports_reports.incidente_con_feriti', '1')
  155. ->orWhere('fcf_reports_reports.incidente_mortale', '=', '1');
  156. })
  157. ->where('fcf_reports_reports.verificatosi_in_data', '>=', $this->from != '' ? $this->from : '2000-01-01')
  158. ->where('fcf_reports_reports.verificatosi_in_data', '<=', $this->to != '' ? $this->to : '2050-01-01')
  159. /*->where(function ($query) {
  160. $query->where('fcf_reports_reports.verificatosi_in_data', '>=', $this->from)
  161. ->where('fcf_reports_reports.verificatosi_in_data', '<=', $this->to);
  162. })*/
  163. ->orderBy('fcf_reports_reports.verificatosi_in_data')
  164. ->get();
  165. $txt = array();
  166. foreach($items as $item)
  167. {
  168. $report = \App\Models\Report::findOrFail($item->id);
  169. $item_data = '';
  170. //1-10
  171. $town_code = $prov_code = '000';
  172. $town = \App\Models\LocationTown::findOrFail($item->localita_uno);
  173. if($town)
  174. {
  175. if($town->istat_code) $town_code = $town->istat_code;
  176. $prov = \App\Models\LocationProvince::where('code', $town->prov)->first();
  177. if($prov && $prov->istat_code) $prov_code = $prov->istat_code;
  178. }
  179. $item_data .= date('y', strtotime($item->verificatosi_in_data));
  180. $item_data .= date('m', strtotime($item->verificatosi_in_data));
  181. $item_data .= str_pad($prov_code,3,'0',STR_PAD_LEFT);
  182. $item_data .= str_pad($town_code,3,'0',STR_PAD_LEFT);
  183. $item_data .= str_repeat(' ',4);
  184. $item_data .= date('d', strtotime($item->verificatosi_in_data));
  185. $item_data .= str_repeat(' ',2);
  186. $item_data .= '4';
  187. $item_data .= str_repeat(' ',5);
  188. $item_data .= ' ';
  189. //11-20
  190. if($item->localizzazione_incidente == Istat::LOCALIZZAZIONE_INCIDENTE_IN)
  191. {
  192. switch($item->nomenclatura_strada) {
  193. case 2: $loc = 2; break;
  194. case 3: $loc = 0; break;
  195. case 4: $loc = 3; break;
  196. default: $loc = 1;
  197. }
  198. }
  199. elseif($item->localizzazione_incidente == Istat::LOCALIZZAZIONE_INCIDENTE_OUT)
  200. {
  201. switch($item->nomenclatura_strada) {
  202. case 2: $loc = 5; break;
  203. case 3: $loc = 9; break;
  204. case 4: $loc = 6; break;
  205. default: $loc = 4;
  206. }
  207. }
  208. else $loc = ' ';
  209. $tipo_strada = ' ';
  210. $types_saved = \App\Models\ReportTipoStrada::where('report_id', $report->id)->select('tipo_strada_id')->get()->toArray();
  211. // $types_saved = $report->getReportTipoStrada();
  212. if($types_saved)
  213. {
  214. if(in_array(3,$types_saved) && in_array(1,$types_saved))
  215. $tipo_strada = 1;
  216. elseif(in_array(3,$types_saved) && (in_array(11,$types_saved) || in_array(2,$types_saved)))
  217. $tipo_strada = 2;
  218. elseif(in_array(4,$types_saved))
  219. $tipo_strada = 3;
  220. }
  221. $pav = ' ';
  222. if($item->pavimentazione)
  223. switch($item->pavimentazione)
  224. {
  225. case 5:
  226. case 6:
  227. $pav = 3; break;
  228. default:
  229. if(!$item->condizione_strada || $item->condizione_strada == 1) $pav = 1;
  230. else $pav = 2;
  231. }
  232. $int = ' ';
  233. switch($item->particolarita_strada)
  234. {
  235. case 1: $int = '07'; break;
  236. case 2: $int = '01'; break;
  237. case 3: $int = '08'; break;
  238. case 4: $int = '02'; break;
  239. case 5: $int = '10'; break;
  240. case 6:
  241. case 10:$int = '09'; break;
  242. case 7: $int = '06'; break;
  243. case 12:$int = '04'; break;
  244. }
  245. $fon = ' ';
  246. if($item->fondo_stradale)
  247. switch($item->fondo_stradale)
  248. {
  249. case 1: $fon = 1; break;
  250. case 2: $fon = 2; break;
  251. case 3: $fon = 4; break;
  252. case 4: $fon = 5; break;
  253. case 5: $fon = 3; break;
  254. }
  255. $seg = ' ';
  256. if($item->segnaletica)
  257. switch($item->segnaletica)
  258. {
  259. case Istat::SEGNALETICA_ASSENTE: $seg = 1; break;
  260. case Istat::SEGNALETICA_VERTICALE: $seg = 2; break;
  261. case Istat::SEGNALETICA_ORIZZONTALE: $seg = 3; break;
  262. case Istat::SEGNALETICA_VERTICALE_ORIZZONTALE: $seg = 4; break;
  263. case Istat::SEGNALETICA_TEMPORANEA_CANTIERE: $seg = 5; break;
  264. }
  265. $met = ' ';
  266. if($item->condizioni_atmosferiche)
  267. switch($item->condizioni_atmosferiche)
  268. {
  269. case 1: $met = 1; break;
  270. case 2: $met = 2; break;
  271. case 3: $met = 3; break;
  272. case 4: $met = 2; break;
  273. case 5: $met = 7; break;
  274. case 6: $met = 1; break;
  275. case 7: $met = 4; break;
  276. case 8: $met = 5; break;
  277. case 9: $met = 6; break;
  278. }
  279. $item_data .= $loc;
  280. $item_data .= str_pad($item->nomenclatura_strada_numero,3,' ',STR_PAD_RIGHT);
  281. $item_data .= str_repeat(' ',3);
  282. $item_data .= str_repeat(' ',2);
  283. $item_data .= $tipo_strada;
  284. $item_data .= $pav;
  285. $item_data .= $int;
  286. $item_data .= $fon;
  287. $item_data .= $seg;
  288. $item_data .= $met;
  289. //21-30
  290. $tipo_urto = str_repeat(' ',2);
  291. $types_saved = \App\Models\ReportTipoUrto::where('report_id', $report->id)->select('tipo_urto_id')->get()->toArray();
  292. // $types_saved = $report->getReportTipoUrto();
  293. if($types_saved)
  294. {
  295. if(in_array(1,$types_saved))
  296. $tipo_urto = '01';
  297. elseif(in_array(2,$types_saved))
  298. $tipo_urto = '02';
  299. elseif(in_array(3,$types_saved) || in_array(26,$types_saved) || in_array(27,$types_saved))
  300. $tipo_urto = '04';
  301. elseif(in_array(5,$types_saved))
  302. $tipo_urto = '03';
  303. elseif(in_array(7,$types_saved) || in_array(12,$types_saved))
  304. $tipo_urto = '05';
  305. elseif(in_array(4,$types_saved) || in_array(21,$types_saved))
  306. $tipo_urto = '07';
  307. elseif(in_array(8,$types_saved))
  308. $tipo_urto = '06';
  309. elseif(in_array(6,$types_saved) || in_array(19,$types_saved) || in_array(14,$types_saved))
  310. $tipo_urto = '08';
  311. elseif(in_array(9,$types_saved) || in_array(18,$types_saved))
  312. $tipo_urto = '10';
  313. elseif(in_array(25,$types_saved) || in_array(22,$types_saved) || in_array(13,$types_saved))
  314. $tipo_urto = '12';
  315. }
  316. $item_data .= $tipo_urto;
  317. $v_data = array(
  318. 'tipo' => '',
  319. 'circostanze' => '',
  320. 'veicolo' => '',
  321. 'conducente_passeggeri' => '',
  322. 'pedoni' => '',
  323. 'cittadinanza_conducente' => ''
  324. );
  325. $deceduti_24h = array();
  326. $deceduti_30gg = array();
  327. $feriti = array();
  328. $data_veicoli = \App\Models\ReportDataVeicoli::where('report_id', $report->id)->get()->toArray();
  329. for($i = 0; $i < 3; $i++)
  330. {
  331. $data_veicolo = isset($data_veicoli[$i]) ? $data_veicoli[$i] : false;
  332. $stato_psico_fisico = str_repeat(' ',2);
  333. if($data_veicolo)
  334. {
  335. if ($data_veicolo["veicoli"] > 0)
  336. {
  337. $veicolo = \App\Models\Vehicle::findOrFail($data_veicolo["veicoli"]); //(ArrayHelper::getValue($data_veicolo, 'veicoli', 0));
  338. $v_tipo = str_repeat(' ',2);
  339. switch($veicolo->tipo_id)
  340. {
  341. case 1:
  342. switch($veicolo->destinazione_uso)
  343. {
  344. case \App\Models\Vehicle::DESTINAZIONE_USO_PRIVATO: $v_tipo = '01'; break;
  345. case \App\Models\Vehicle::DESTINAZIONE_USO_PUBBLICO: $v_tipo = '03'; break;
  346. case \App\Models\Vehicle::DESTINAZIONE_USO_SOCCORSO_POLIZIA: $v_tipo = '04'; break;
  347. default: $v_tipo = '01'; break;
  348. }
  349. break;
  350. case 17: $v_tipo = '02'; break;
  351. case 7:
  352. if($veicolo->destinazione_uso == \App\Models\Vehicle::DESTINAZIONE_USO_SERVIZO_PUBBLICO_DI_LINEA)
  353. $v_tipo = '05';
  354. else
  355. $v_tipo = '06';
  356. break;
  357. case 16: $v_tipo = '08'; break;
  358. case 14:
  359. case 20: $v_tipo = '09'; break;
  360. case 18: $v_tipo = '10'; break;
  361. case 8: $v_tipo = '13'; break;
  362. case 6: $v_tipo = '14'; break;
  363. case 3: $v_tipo = '15'; break;
  364. case 5: $v_tipo = '17'; break;
  365. case 19: $v_tipo = '18'; break;
  366. case 4: $v_tipo = '21'; break;
  367. }
  368. $v_data['tipo'] .= $v_tipo;
  369. $v_data['veicolo'] .= $veicolo->foreign_country ?
  370. str_repeat(' ',8) :
  371. str_pad(substr($veicolo->targa,0,8),8,' ',STR_PAD_RIGHT);
  372. $v_data['veicolo'] .= $veicolo->foreign_country ?
  373. str_pad($veicolo->getCartaCircolazioneRilasciataDaDiValueAttribute(),3,' ',STR_PAD_RIGHT) :
  374. str_repeat(' ',3);
  375. $time = strtotime($veicolo->carta_circolazione_rilasciata_il);
  376. $v_data['veicolo'] .= str_pad($time && $time > 0 ? date('y',$time) : '',2,' ',STR_PAD_RIGHT);
  377. $v_data['veicolo'] .= str_repeat(' ',5);
  378. }
  379. if($data_veicolo["alcool_test"] && $data_veicolo["alcool_test_esito"] == Istat::ESITO_POSITIVO)
  380. $stato_psico_fisico = '90';
  381. elseif($data_veicolo["drug_test"] && $data_veicolo["drug_test_esito"] == Istat::ESITO_POSITIVO)
  382. $stato_psico_fisico = '94';
  383. $conducente = \App\Models\Anagrafica::findOrFail($data_veicolo["conducenti"]);
  384. $eta_conducente = '00';
  385. if($conducente->birth_date != '' && $report->verificatosi_in_data != '')
  386. {
  387. $eta_conducente = 0;// * * * DateHelper::calculateAge($conducente->birth_date,$report->verificatosi_in_data);
  388. if($eta_conducente > 100 || $eta_conducente < 0) $eta_conducente = 0;
  389. $eta_conducente = str_pad($eta_conducente,2,'0',STR_PAD_LEFT);
  390. }
  391. $v_data['conducente_passeggeri'] .= $eta_conducente;
  392. $v_data['conducente_passeggeri'] .= $conducente->gender == 'F' ? '2' : '1';
  393. $details = array(
  394. 'firstname' => $conducente->firstname,
  395. 'lastname' => $conducente->lastname,
  396. 'ospedale' => $data_veicolo["infortunato_ospedale"],
  397. );
  398. switch($data_veicolo["infortunato"])
  399. {
  400. case Istat::INFORTUNATO_DECEDUTO_30GG: $infortunato = 4; $deceduti_30gg[] = $details; break;
  401. case Istat::INFORTUNATO_DECEDUTO_24H: $infortunato = 3; $deceduti_24h[] = $details; break;
  402. case Istat::INFORTUNATO_FERITO: $infortunato = 2; $feriti[] = $details; break;
  403. default: $infortunato = 1; break;
  404. }
  405. $v_data['conducente_passeggeri'] .= $infortunato;
  406. switch($data_veicolo["conducente_patente_categoria"])
  407. {
  408. case Istat::PATENTE_CATEGORIA_AM: $patente = '0'; break;
  409. case Istat::PATENTE_CATEGORIA_A1:
  410. case Istat::PATENTE_CATEGORIA_A1B:
  411. case Istat::PATENTE_CATEGORIA_A2:
  412. case Istat::PATENTE_CATEGORIA_A: $patente = '1'; break;
  413. case Istat::PATENTE_CATEGORIA_AB:
  414. case Istat::PATENTE_CATEGORIA_B1:
  415. case Istat::PATENTE_CATEGORIA_B: $patente = '2'; break;
  416. case Istat::PATENTE_CATEGORIA_C1:
  417. case Istat::PATENTE_CATEGORIA_C: $patente = '3'; break;
  418. case Istat::PATENTE_CATEGORIA_D1: $patente = '4'; break;
  419. case Istat::PATENTE_CATEGORIA_BE:
  420. case Istat::PATENTE_CATEGORIA_C1E:
  421. case Istat::PATENTE_CATEGORIA_CE:
  422. case Istat::PATENTE_CATEGORIA_D1E:
  423. case Istat::PATENTE_CATEGORIA_DE:
  424. case Istat::PATENTE_CATEGORIA_E: $patente = '5'; break;
  425. default: $patente = '9'; break;
  426. }
  427. $v_data['conducente_passeggeri'] .= $patente;
  428. $patente_rilasciata_il = $data_veicolo["conducente_patente_rilasciata_il"];
  429. $v_data['conducente_passeggeri'] .= $patente_rilasciata_il ? date('y',strtotime($patente_rilasciata_il)) : str_repeat(' ',2);
  430. $v_data['conducente_passeggeri'] .= str_repeat(' ',4);
  431. $datiPasseggeri = \App\Models\ReportDataPasseggeri::where('progressive', $i)->where('report_id', $report->id)->get()->toArray();
  432. for ($j = 0; $j < 4; $j++)
  433. {
  434. $passeggero_data = str_repeat(' ',4);
  435. if(isset($datiPasseggeri[$j]))
  436. {
  437. $passeggero = $datiPasseggeri[$j]["passeggero"];
  438. if($passeggero)
  439. {
  440. $passeggero_infortunato = $datiPasseggeri[$j]["infortunato"];
  441. if($passeggero_infortunato != Istat::INFORTUNATO_INCOLUME)
  442. {
  443. $passeggero_anagrafica = \App\Models\Anagrafica::findOrFail($passeggero);
  444. $eta_passeggero = '00';
  445. if($passeggero_anagrafica->birth_date && $report->verificatosi_in_data)
  446. {
  447. $eta_passeggero = 0; // * * *DateHelper::calculateAge($passeggero_anagrafica->birth_date,$report->verificatosi_in_data);
  448. if($eta_passeggero == 0) $eta_passeggero = 1;
  449. elseif($eta_passeggero > 100 || $eta_passeggero < 0) $eta_passeggero = 0;
  450. $eta_passeggero = str_pad($eta_passeggero,2,'0',STR_PAD_LEFT);
  451. }
  452. $passeggero_data = sprintf('%s%s%s',
  453. $passeggero_infortunato == Istat::INFORTUNATO_FERITO ? '2' : '1',
  454. $eta_passeggero,
  455. $passeggero_anagrafica->gender == 'F' ? '4' : '3'
  456. );
  457. $details = array(
  458. 'firstname' => $passeggero_anagrafica->firstname,
  459. 'lastname' => $passeggero_anagrafica->lastname,
  460. 'ospedale' => $datiPasseggeri[$j]["infortunato_ospedale"]
  461. );
  462. switch($passeggero_infortunato)
  463. {
  464. case Istat::INFORTUNATO_DECEDUTO_30GG: $deceduti_30gg[] = $details; break;
  465. case Istat::INFORTUNATO_DECEDUTO_24H: $deceduti_24h[] = $details; break;
  466. case Istat::INFORTUNATO_FERITO: $feriti[] = $details; break;
  467. }
  468. }
  469. }
  470. }
  471. $v_data['conducente_passeggeri'] .= $passeggero_data;
  472. }
  473. $v_data['conducente_passeggeri'] .= str_repeat(' ',8);
  474. $v_data['cittadinanza_conducente'] .= $conducente->nazionalita == 0 ? '1' : '2';
  475. $country_id = 106;
  476. if($conducente->nazionalita != 0)
  477. $country_id = $conducente->nazione_straniera;
  478. if(!$country_id) $v_data['cittadinanza_conducente'] .= str_repeat(' ',33);
  479. else
  480. {
  481. $country = \App\Models\LocationCountry::findOrFail($country_id);
  482. $v_data['cittadinanza_conducente'] .= str_pad($country->istat_code,3,' ',STR_PAD_RIGHT);
  483. $v_data['cittadinanza_conducente'] .= str_pad(preg_replace('[^A-Z ]', '', strtoupper($country->name)),30,' ',STR_PAD_RIGHT);
  484. }
  485. }
  486. else
  487. {
  488. $v_data['tipo'] .= str_repeat(' ',2);
  489. $v_data['veicolo'] .= str_repeat(' ',18);
  490. $v_data['conducente_passeggeri'] .= str_repeat(' ',35);
  491. $v_data['pedoni'] .= str_repeat(' ',24);
  492. $v_data['cittadinanza_conducente'] .= str_repeat(' ',34);
  493. }
  494. if($i < 2)
  495. {
  496. $v_data['circostanze'] .= '00';
  497. $v_data['circostanze'] .= str_repeat(' ',2);
  498. $v_data['circostanze'] .= $stato_psico_fisico;
  499. }
  500. }
  501. $p_data = '';
  502. $datiPedoni = \App\Models\ReportDataPedoni::where('report_id', $report->id)->get()->toArray();
  503. for($j = 0; $j < 4; $j++)
  504. {
  505. $pedone_data = str_repeat(' ',6);
  506. if (isset($datiPedoni[$j]))
  507. {
  508. $pedone = $datiPedoni[$j]["pedoni"];
  509. if($pedone)
  510. {
  511. $pedone_infortunato = $infortunato;
  512. if($pedone_infortunato != Istat::INFORTUNATO_INCOLUME)
  513. {
  514. $pedone_anagrafica = new Anagrafica([$pedone]);
  515. $eta_pedone = '00';
  516. if(DateHelper::filterDate($pedone_anagrafica->get('birth_date')) && DateHelper::filterDate($report->get('verificatosi_in_data')))
  517. {
  518. $eta_pedone = DateHelper::calculateAge($pedone_anagrafica->get('birth_date'),$report->get('verificatosi_in_data'));
  519. if($eta_pedone == 0) $eta_pedone = 1;
  520. elseif($eta_pedone > 100 || $eta_pedone < 0) $eta_pedone = 0;
  521. $eta_pedone = str_pad($eta_pedone,2,'0',STR_PAD_LEFT);
  522. }
  523. if($pedone_infortunato == Istat::INFORTUNATO_FERITO)
  524. $pedone_data = sprintf('%s%s%s',
  525. str_repeat(' ',3),
  526. $pedone_anagrafica->get('gender') == 'F' ? '4' : '3',
  527. $eta_pedone
  528. );
  529. else
  530. $pedone_data = sprintf('%s%s%s',
  531. $pedone_anagrafica->get('gender') == 'F' ? '2' : '1',
  532. $eta_pedone,
  533. str_repeat(' ',3)
  534. );
  535. $details = array(
  536. 'firstname' => $pedone_anagrafica->get('firstname'),
  537. 'lastname' => $pedone_anagrafica->get('lastname'),
  538. 'ospedale' => ReportDataPedoni::where('report_id', $report->id)
  539. ->where('progressive', $j)
  540. ->value('infortunato_ospedale')
  541. );
  542. switch($pedone_infortunato)
  543. {
  544. case Istat::INFORTUNATO_DECEDUTO_30GG: $deceduti_30gg[] = $details; break;
  545. case Istat::INFORTUNATO_DECEDUTO_24H: $deceduti_24h[] = $details; break;
  546. case Istat::INFORTUNATO_FERITO: $feriti[] = $details; break;
  547. }
  548. }
  549. }
  550. }
  551. $p_data .= $pedone_data;
  552. }
  553. $item_data .= $v_data['tipo'];
  554. $item_data .= str_repeat(' ',24);
  555. $item_data .= $v_data['circostanze'];
  556. $item_data .= $v_data['veicolo'];
  557. $item_data .= $v_data['conducente_passeggeri'];
  558. $item_data .= $p_data;
  559. $item_data .= str_repeat(' ',10);
  560. $item_data .= str_pad(count($deceduti_24h),2,'0',STR_PAD_LEFT);
  561. $item_data .= str_pad(count($deceduti_30gg),2,'0',STR_PAD_LEFT);
  562. $item_data .= str_pad(count($feriti),2,'0',STR_PAD_LEFT);
  563. $item_data .= str_repeat(' ',9);
  564. /*
  565. $location_description = Helper::getStradarioAddressById($report->get('localita_due'));
  566. if($report->get('intersezione_con'))
  567. $location_description .= ' ' . sprintf(__('ISTAT_INTERSEZIONE_CON', 'reports'),Helper::getStradarioAddressById($report->get('intersezione_con')));
  568. if($report->get('prossimita_civico'))
  569. $location_description .= ' ' . sprintf(__('ISTAT_PROSSIMITA_CIVICO', 'reports'),$report->get('prossimita_civico'));
  570. if($report->get('prossimita_chilometro'))
  571. $location_description .= ' ' . sprintf(__('ISTAT_PROSSIMITA_CHILOMETRO', 'reports'),$report->get('prossimita_chilometro'));
  572. $item_data .= str_pad(substr(preg_replace('[^A-Za-z-0-9 ]','',strtoupper($location_description)),0,57),57,' ');
  573. $item_data .= str_repeat(' ',100);
  574. $deceduti = array_merge($deceduti_24h, $deceduti_30gg);
  575. for($i = 0; $i < 4; $i++)
  576. {
  577. $deceduto = ArrayHelper::getValue($deceduti, $i);
  578. $deceduto_data = str_repeat(' ',60);
  579. if($deceduto) $deceduto_data = sprintf('%s%s',
  580. str_pad(substr($deceduto['firstname'],0,30),30,' '),
  581. str_pad(substr($deceduto['lastname'],0,30),30,' ')
  582. );
  583. $item_data .= $deceduto_data;
  584. }
  585. for($i = 0; $i < 8; $i++)
  586. {
  587. $ferito = ArrayHelper::getValue($feriti, $i);
  588. $ferito_data = str_repeat(' ',90);
  589. if($ferito) $ferito_data = sprintf('%s%s%s',
  590. str_pad(substr($ferito['firstname'],0,30),30,' '),
  591. str_pad(substr($ferito['lastname'],0,30),30,' '),
  592. str_pad(substr($ferito['ospedale'],0,30),30,' ')
  593. );
  594. $item_data .= $ferito_data;
  595. }
  596. $item_data .= str_repeat(' ',10);
  597. $item_data .= str_repeat(' ',102);
  598. $item_data .= str_repeat(' ',8);
  599. $ora = $report->get('verificatosi_in_data_ora');
  600. $ora = $ora === '' ? '25' : str_pad(substr($ora,0,2),2,'0',STR_PAD_LEFT);
  601. $item_data .= $ora;
  602. $minuti = (int) $report->get('verificatosi_in_data_minuti');
  603. if($ora == '25') $minuti = ' ';
  604. $item_data .= str_pad(substr($minuti,0,2),2,'0',STR_PAD_LEFT);
  605. $item_data .= str_repeat(' ',30);
  606. $item_data .= str_repeat('0',7);
  607. $item_data .= str_repeat(' ',15);
  608. $item_data .= str_repeat(' ',4);
  609. $item_data .= str_repeat(' ',100);
  610. $item_data .= str_pad(substr(LocationHelper::getTown($report->get('localita_uno'))->title,0,40),40,' ');
  611. $item_data .= str_repeat(' ',40);
  612. $item_data .= str_repeat(' ',10);
  613. $item_data .= $v_data['cittadinanza_conducente'];
  614. */
  615. $txt[] = $item_data;
  616. }
  617. $txt_data = implode("\n", $txt);
  618. return response()->streamDownload(function () use($txt_data) {
  619. echo $txt_data;
  620. }, 'istat_' . $this->from . "_" . $this->to . '.txt');
  621. /*$txt_filename = sprintf(__('ISTAT_TXT_FILENAME', $mainframe->getOption()),
  622. $model->getState('filter_date_from'),
  623. $model->getState('filter_date_to')
  624. );*/
  625. // return view('livewire.istat');
  626. }
  627. }