Skip to content

Commit 35ea04f

Browse files
committed
refactor: aggiunta fatture a coda di invio
1 parent fee2f50 commit 35ea04f

3 files changed

Lines changed: 166 additions & 18 deletions

File tree

modules/fatture/bulk.php

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,26 +152,76 @@
152152
break;
153153

154154
case 'hook_send':
155+
$added = [];
156+
$failed = [];
157+
$skipped = [];
158+
155159
foreach ($id_records as $id) {
156160
$fattura = Fattura::find($id);
157161

162+
if (!$fattura) {
163+
$failed[] = 'ID '.$id.' (non trovata)';
164+
continue;
165+
}
166+
158167
try {
159168
$fattura_elettronica = new FatturaElettronica($fattura->id);
160169

161-
if (!empty($fattura_elettronica) && $fattura_elettronica->isGenerated() && $fattura->codice_stato_fe == 'GEN') {
170+
// Verifica che la fattura sia in stato corretto per l'invio
171+
// Accetta sia 'GEN' che NULL/vuoto (fatture appena generate)
172+
if (!empty($fattura->codice_stato_fe) && $fattura->codice_stato_fe != 'GEN') {
173+
$skipped[] = $fattura->numero_esterno.' (stato: '.$fattura->codice_stato_fe.')';
174+
continue;
175+
}
176+
177+
// Verifica che la fattura elettronica sia generata e valida
178+
if (!empty($fattura_elettronica) && $fattura_elettronica->isGenerated()) {
162179
$fattura->codice_stato_fe = 'QUEUE';
163180
$fattura->data_stato_fe = date('Y-m-d H:i:s');
164181
$fattura->hook_send = true;
165182
$fattura->save();
166183

167184
$added[] = $fattura->numero_esterno;
185+
} else {
186+
// Se la FE non è generata ma lo stato è vuoto, impostalo a GEN
187+
if (empty($fattura->codice_stato_fe)) {
188+
$fattura->codice_stato_fe = 'GEN';
189+
$fattura->save();
190+
}
191+
$failed[] = $fattura->numero_esterno.' (FE non generata)';
168192
}
169-
} catch (UnexpectedValueException) {
170-
$failed[] = $fattura->numero_esterno;
193+
} catch (UnexpectedValueException $e) {
194+
$failed[] = $fattura->numero_esterno.' (FE non valida)';
195+
} catch (Exception $e) {
196+
$failed[] = $fattura->numero_esterno.' (errore: '.$e->getMessage().')';
171197
}
172198
}
173199

174-
flash()->info(tr('Le fatture elettroniche sono state aggiunte alla coda di invio'));
200+
// Messaggi di feedback
201+
if (!empty($added)) {
202+
flash()->info(tr('_NUM_ fatture elettroniche aggiunte alla coda di invio: _LIST_', [
203+
'_NUM_' => count($added),
204+
'_LIST_' => implode(', ', $added)
205+
]));
206+
}
207+
208+
if (!empty($skipped)) {
209+
flash()->warning(tr('_NUM_ fatture saltate (stato non corretto): _LIST_', [
210+
'_NUM_' => count($skipped),
211+
'_LIST_' => implode(', ', $skipped)
212+
]));
213+
}
214+
215+
if (!empty($failed)) {
216+
flash()->error(tr('_NUM_ fatture non aggiunte alla coda (errori): _LIST_', [
217+
'_NUM_' => count($failed),
218+
'_LIST_' => implode(', ', $failed)
219+
]));
220+
}
221+
222+
if (empty($added) && empty($skipped) && empty($failed)) {
223+
flash()->warning(tr('Nessuna fattura elaborata'));
224+
}
175225

176226
break;
177227

plugins/exportFE/src/Interaction.php

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,38 +40,73 @@ public static function sendInvoice($id_record)
4040
try {
4141
$fattura_elettronica = new FatturaElettronica($id_record);
4242
$fattura = Fattura::find($id_record);
43+
44+
// Verifica che la fattura esista
45+
if (!$fattura) {
46+
return [
47+
'code' => 404,
48+
'message' => tr('Fattura non trovata'),
49+
];
50+
}
51+
4352
$file = $fattura->getFatturaElettronica();
4453

54+
// Verifica che il file della fattura elettronica esista
55+
if (!$file) {
56+
return [
57+
'code' => 400,
58+
'message' => tr('File della fattura elettronica non trovato'),
59+
];
60+
}
61+
62+
// Verifica che la fattura elettronica sia valida
63+
if (!$fattura_elettronica->isGenerated()) {
64+
return [
65+
'code' => 400,
66+
'message' => tr('Fattura elettronica non generata correttamente'),
67+
];
68+
}
69+
4570
$response = static::request('POST', 'invio_fattura_xml', [
4671
'xml' => $file->getContent(),
4772
'filename' => $fattura_elettronica->getFilename(),
4873
]);
4974
$body = static::responseBody($response);
5075

51-
// Aggiornamento dello stato
76+
// Aggiornamento dello stato in base alla risposta
5277
if ($body['status'] == 200 || $body['status'] == 301) {
78+
// Invio riuscito
5379
database()->update('co_documenti', [
5480
'codice_stato_fe' => 'WAIT',
5581
'data_stato_fe' => date('Y-m-d H:i:s'),
5682
], ['id' => $id_record]);
57-
} elseif ($body['status'] == 405) {
83+
} else {
84+
// Qualsiasi errore, imposta stato errore
5885
database()->update('co_documenti', [
5986
'codice_stato_fe' => 'ERR',
6087
'data_stato_fe' => date('Y-m-d H:i:s'),
6188
], ['id' => $id_record]);
89+
90+
logger()->warning('Errore invio FE fattura '.$fattura->numero_esterno.': '.$body['message']);
6291
}
6392

6493
return [
6594
'code' => $body['status'],
66-
'message' => $body['message'],
95+
'message' => $body['message'] ?? tr('Risposta non valida dal server'),
96+
];
97+
} catch (\UnexpectedValueException $e) {
98+
logger()->error('Fattura elettronica non valida per ID '.$id_record.': '.$e->getMessage());
99+
return [
100+
'code' => 400,
101+
'message' => tr('Fattura elettronica non valida'),
102+
];
103+
} catch (\Exception $e) {
104+
logger()->error('Errore durante invio fattura elettronica ID '.$id_record.': '.$e->getMessage());
105+
return [
106+
'code' => 500,
107+
'message' => tr('Errore interno durante l\'invio: _ERR_', ['_ERR_' => $e->getMessage()]),
67108
];
68-
} catch (\UnexpectedValueException) {
69109
}
70-
71-
return [
72-
'code' => 400,
73-
'message' => tr('Fattura non generata correttamente'),
74-
];
75110
}
76111

77112
public static function getInvoiceRecepits($id_record)

plugins/exportFE/src/InvoiceHookTask.php

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)