@@ -31,7 +31,11 @@ public function execute()
3131 'response ' => 1 ,
3232 'message ' => tr ('Fatture elettroniche inviate correttamente! ' ),
3333 ];
34-
34+
35+ $ inviate = 0 ;
36+ $ errori = 0 ;
37+ $ fatture_errore = [];
38+
3539 try {
3640 $ fatture = Fattura::where ('hook_send ' , 1 )
3741 ->where ('codice_stato_fe ' , 'QUEUE ' )
@@ -40,22 +44,81 @@ public function execute()
4044
4145 if ( $ fatture ->isEmpty () ){
4246 $ result ['message ' ] = tr ('Nessuna fattura da inviare ' );
47+ return $ result ;
4348 }
4449
45- foreach ($ fatture as $ fattura ){
46- $ response_invio = Interaction::sendInvoice ($ fattura ->id );
50+ foreach ($ fatture as $ fattura ) {
51+ try {
52+ // Verifica che la fattura elettronica sia ancora valida
53+ $ fattura_elettronica = new FatturaElettronica ($ fattura ->id );
54+ if (!$ fattura_elettronica ->isGenerated ()) {
55+ // Fattura elettronica non più valida, rimuovi dalla coda
56+ $ fattura ->hook_send = false ;
57+ $ fattura ->codice_stato_fe = 'GEN ' ;
58+ $ fattura ->data_stato_fe = date ('Y-m-d H:i:s ' );
59+ $ fattura ->save ();
60+ continue ;
61+ }
62+
63+ $ response_invio = Interaction::sendInvoice ($ fattura ->id );
4764
48- if ($ response_invio ['code ' ] == 200 || $ response_invio ['code ' ] == 301 ) {
65+ if ($ response_invio ['code ' ] == 200 || $ response_invio ['code ' ] == 301 ) {
66+ // Invio riuscito
67+ $ fattura ->hook_send = false ;
68+ $ fattura ->save ();
69+ $ inviate ++;
70+ } else {
71+ // Qualsiasi errore, rimuovi dalla coda e imposta errore
72+ $ fattura ->hook_send = false ;
73+ $ fattura ->codice_stato_fe = 'ERR ' ;
74+ $ fattura ->save ();
75+ $ errori ++;
76+ $ fatture_errore [] = $ fattura ->numero_esterno .' ( ' .$ response_invio ['message ' ].') ' ;
77+ }
78+ } catch (\UnexpectedValueException $ e ) {
79+ // Fattura elettronica non valida, rimuovi dalla coda
80+ $ fattura ->hook_send = false ;
81+ $ fattura ->codice_stato_fe = 'GEN ' ;
82+ $ fattura ->save ();
83+ } catch (\Exception $ e ) {
84+ // Errore generico, rimuovi dalla coda e imposta errore
4985 $ fattura ->hook_send = false ;
86+ $ fattura ->codice_stato_fe = 'ERR ' ;
5087 $ fattura ->save ();
88+ $ errori ++;
89+ $ fatture_errore [] = $ fattura ->numero_esterno .' (errore: ' .$ e ->getMessage ().') ' ;
90+
91+ // Log dell'errore per debugging
92+ logger ()->error ('Errore invio FE per fattura ' .$ fattura ->numero_esterno .': ' .$ e ->getMessage ());
5193 }
5294 }
53-
95+
96+ // Costruzione messaggio di risposta
97+ if ($ inviate > 0 && $ errori == 0 ) {
98+ $ result ['message ' ] = tr ('_NUM_ fatture elettroniche inviate correttamente! ' , ['_NUM_ ' => $ inviate ]);
99+ } elseif ($ inviate > 0 && $ errori > 0 ) {
100+ $ result ['response ' ] = 2 ;
101+ $ result ['message ' ] = tr ('_SENT_ fatture inviate, _ERR_ con errori: _LIST_ ' , [
102+ '_SENT_ ' => $ inviate ,
103+ '_ERR_ ' => $ errori ,
104+ '_LIST_ ' => implode (', ' , $ fatture_errore )
105+ ]);
106+ } elseif ($ errori > 0 ) {
107+ $ result ['response ' ] = 2 ;
108+ $ result ['message ' ] = tr ('Errori nell \'invio di _ERR_ fatture: _LIST_ ' , [
109+ '_ERR_ ' => $ errori ,
110+ '_LIST_ ' => implode (', ' , $ fatture_errore )
111+ ]);
112+ }
113+
54114 } catch (\Exception $ e ) {
55115 $ result ['response ' ] = 2 ;
56116 $ result ['message ' ] = tr ('Errore durante l \'invio delle fatture elettroniche: _ERR_ ' , [
57117 '_ERR_ ' => $ e ->getMessage (),
58118 ])."<br> " ;
119+
120+ // Log dell'errore critico
121+ logger ()->error ('Errore critico nel task invio FE: ' .$ e ->getMessage ());
59122 }
60123
61124 return $ result ;
0 commit comments