Skip to content

Commit 9f13a96

Browse files
committed
fix: log invio fatture da bulk
1 parent fc5d98c commit 9f13a96

4 files changed

Lines changed: 40 additions & 14 deletions

File tree

modules/fatture/bulk.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@
168168
$fattura_elettronica = new FatturaElettronica($fattura->id);
169169

170170
// 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') {
171+
// Accetta 'GEN' (generata), NULL/vuoto (appena generate), 'ERR' (trasmissione fallita)
172+
if (!empty($fattura->codice_stato_fe) && $fattura->codice_stato_fe != 'GEN' && $fattura->codice_stato_fe != 'ERR') {
173173
$skipped[] = $fattura->numero_esterno.' (stato: '.$fattura->codice_stato_fe.')';
174174
continue;
175175
}

plugins/exportFE/src/Interaction.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,20 @@ public static function sendInvoice($id_record)
7373
]);
7474
$body = static::responseBody($response);
7575

76+
// Validazione della risposta
77+
if (empty($body) || !isset($body['status'])) {
78+
logger()->error('Risposta API non valida per fattura '.$fattura->numero_esterno.': '.json_encode($body));
79+
80+
return [
81+
'code' => 500,
82+
'message' => tr('Risposta non valida dal server'),
83+
];
84+
}
85+
86+
$status_code = (int) $body['status'];
87+
7688
// Aggiornamento dello stato in base alla risposta
77-
if ($body['status'] == 200 || $body['status'] == 301) {
89+
if ($status_code == 200 || $status_code == 301) {
7890
// Invio riuscito
7991
database()->update('co_documenti', [
8092
'codice_stato_fe' => 'WAIT',
@@ -91,7 +103,7 @@ public static function sendInvoice($id_record)
91103
}
92104

93105
return [
94-
'code' => $body['status'],
106+
'code' => $status_code,
95107
'message' => $body['message'] ?? tr('Risposta non valida dal server'),
96108
];
97109
} catch (\UnexpectedValueException $e) {
@@ -120,9 +132,19 @@ public static function getInvoiceRecepits($id_record)
120132
]);
121133
$body = static::responseBody($response);
122134

135+
// Validazione della risposta
136+
if (empty($body) || !isset($body['status'])) {
137+
logger()->error('Risposta API non valida per ricevute fattura ID '.$id_record.': '.json_encode($body));
138+
139+
return [
140+
'code' => 500,
141+
'message' => tr('Risposta non valida dal server'),
142+
];
143+
}
144+
123145
return [
124-
'code' => $body['status'],
125-
'results' => $body['results'],
146+
'code' => (int) $body['status'],
147+
'results' => $body['results'] ?? [],
126148
];
127149
} catch (\UnexpectedValueException) {
128150
}

plugins/importFE/src/Interaction.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ public static function getRemoteList()
5757
$response = static::request('POST', 'fatture_da_importare');
5858
$body = static::responseBody($response);
5959

60-
if ($body['status'] == '200') {
61-
$list = $body['results'];
60+
if (!empty($body) && isset($body['status']) && (int) $body['status'] == 200) {
61+
$list = $body['results'] ?? [];
6262
}
6363
}
6464

@@ -119,8 +119,10 @@ public static function processInvoice($filename)
119119
$body = static::responseBody($response);
120120

121121
$message = '';
122-
if ($body['status'] != '200') {
123-
$message = $body['status'].' - '.$body['message'];
122+
if (empty($body) || !isset($body['status']) || (int) $body['status'] != 200) {
123+
$status = $body['status'] ?? 'unknown';
124+
$msg = $body['message'] ?? 'Errore sconosciuto';
125+
$message = $status.' - '.$msg;
124126
}
125127

126128
return $message;

plugins/receiptFE/src/Interaction.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ public static function getRemoteList()
5757
$response = static::request('POST', 'notifiche_da_importare');
5858
$body = static::responseBody($response);
5959

60-
if ($body['status'] == '200') {
61-
$results = $body['results'];
60+
if (!empty($body) && isset($body['status']) && (int) $body['status'] == 200) {
61+
$results = $body['results'] ?? [];
6262

6363
foreach ($results as $result) {
6464
$list[] = [
@@ -124,8 +124,10 @@ public static function processReceipt($filename)
124124
$body = static::responseBody($response);
125125

126126
$result = true;
127-
if ($body['status'] != '200') {
128-
$result = $body['status'].' - '.$body['message'];
127+
if (empty($body) || !isset($body['status']) || (int) $body['status'] != 200) {
128+
$status = $body['status'] ?? 'unknown';
129+
$msg = $body['message'] ?? 'Errore sconosciuto';
130+
$result = $status.' - '.$msg;
129131
}
130132

131133
return $result;

0 commit comments

Comments
 (0)