-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathfunctions.cancellation.php
More file actions
332 lines (287 loc) · 11.9 KB
/
Copy pathfunctions.cancellation.php
File metadata and controls
332 lines (287 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
/* Copyright (C) 2025 Alberto SuperAdmin <aluquerivasdev@gmail.com>
* Copyright (C) 2025 Germán Luis Aracil Boned <garacilb@gmail.com>
*
* Based on original code from verifactu module by Alberto SuperAdmin (easysoft.es)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* \file verifactu/lib/functions/functions.cancellation.php
* \ingroup verifactu
* \brief Invoice cancellation functions for VeriFactu
*/
use Sietekas\Verifactu\VerifactuInvoiceCancel;
/**
* Handles invoice cancellation in VeriFactu
*
* @param VerifactuManager $manager Manager instance
* @param Facture $facture Invoice object
* @param array $certOptions Certificate options
* @param string $issuerNif Issuer NIF
* @param string $issuerName Issuer name
* @param string $cancellationType Type of cancellation: 'normal', 'rechazo_previo', 'sin_registro_previo'
* @return bool|object True/Response if success, false if error
*/
function handleInvoiceCancellation($manager, Facture $facture, $certOptions, $issuerNif, $issuerName, $cancellationType = 'normal')
{
global $langs;
// Get invoice data for cancellation
$invoiceNumberToCancel = $facture->ref;
$invoiceDateToCancel = date('d-m-Y', $facture->date);
$issuerNifToCancel = $issuerNif;
// Create cancellation according to requested type
$cancellation = createCancellationByType($cancellationType, $invoiceNumberToCancel, $invoiceDateToCancel, $issuerNifToCancel, $issuerNif, $issuerName);
if (!$cancellation) {
dol_syslog("VERIFACTU: Invalid cancellation type: " . $cancellationType, LOG_ERR);
return false;
}
// Configure chaining
$previousHash = getLastInvoiceHash();
if ($previousHash) {
$cancellation->setChainLink(
$issuerNif,
$previousHash['numero'],
$previousHash['fecha'],
$previousHash['hash']
);
dol_syslog("VERIFACTU: Chaining configured with previous invoice: " . $previousHash['numero'], LOG_DEBUG);
}
// Send cancellation
$response = $manager->sendCancellation($cancellation, $certOptions);
// Process response
return processCancellationResponse($response, $facture, $invoiceNumberToCancel, $langs);
}
/**
* Creates cancellation object according to type
*
* @param string $cancellationType Type of cancellation
* @param string $invoiceNumber Invoice number
* @param string $invoiceDate Invoice date
* @param string $issuerNifToCancel NIF of the issuer of invoice to cancel
* @param string $issuerNif Current issuer NIF
* @param string $issuerName Current issuer name
* @return VerifactuInvoiceCancel|false Cancellation object or false if invalid type
*/
function createCancellationByType($cancellationType, $invoiceNumber, $invoiceDate, $issuerNifToCancel, $issuerNif, $issuerName)
{
switch ($cancellationType) {
case 'normal':
dol_syslog("VERIFACTU: Creating normal cancellation for invoice " . $invoiceNumber, LOG_INFO);
return VerifactuInvoiceCancel::createNormal(
$invoiceNumber,
$invoiceDate,
$issuerNifToCancel,
$issuerNif,
$issuerName
);
case 'rechazo_previo':
dol_syslog("VERIFACTU: Creating prior rejection cancellation for invoice " . $invoiceNumber, LOG_INFO);
return VerifactuInvoiceCancel::createForPreviousRejection(
$invoiceNumber,
$invoiceDate,
$issuerNifToCancel,
$issuerNif,
$issuerName
);
case 'sin_registro_previo':
dol_syslog("VERIFACTU: Creating cancellation without prior record for invoice " . $invoiceNumber, LOG_INFO);
return VerifactuInvoiceCancel::createWithoutPreviousRecord(
$invoiceNumber,
$invoiceDate,
$issuerNifToCancel,
$issuerNif,
$issuerName
);
default:
return false;
}
}
/**
* Processes AEAT cancellation response
*
* @param object $response AEAT response
* @param Facture $facture Invoice
* @param string $invoiceNumber Invoice number
* @param Translate $langs Languages
* @return bool|object True/Response if success, false if error
*/
function processCancellationResponse($response, $facture, $invoiceNumber, $langs)
{
if ($response) {
if (isAEATResponseAccepted($response)) {
return processCancellationSuccess($response, $facture, $invoiceNumber, $langs);
} else {
return processCancellationError($response, $facture, $invoiceNumber, $langs);
}
} else {
// No response
dol_syslog("VERIFACTU: No response received from server for invoice cancellation " . $invoiceNumber, LOG_ERR);
$facture->array_options['options_verifactu_error'] = 'No response received from server';
$facture->array_options['options_verifactu_estado'] = 'Error';
$facture->array_options['options_verifactu_ultimafecha_modificacion'] = date('Y-m-d H:i:s');
$facture->insertExtraFields();
return false;
}
}
/**
* Processes a successful cancellation response
*
* @param object $response AEAT response
* @param Facture $facture Invoice
* @param string $invoiceNumber Invoice number
* @param Translate $langs Languages
* @return object Response
*/
function processCancellationSuccess($response, $facture, $invoiceNumber, $langs)
{
$statusMessage = $response->EstadoEnvio === 'Correcto' ? 'successfully' : 'with partial success';
dol_syslog("VERIFACTU: Cancellation sent {$statusMessage} for invoice " . $invoiceNumber, LOG_INFO);
// Cancelled success badge
$cancelledBadge = '<div class="center"><span class="badge badge-status6 classfortooltip badge-status" attr-status="' . $langs->trans('VERIFACTU_STATUS_ANULADA') . '">' . $langs->trans('VERIFACTU_STATUS_ANULADA') . '</span></div>';
$facture->array_options['options_verifactu_estado'] = $cancelledBadge;
$facture->array_options['options_verifactu_csv_factura'] = $response->CSV ?? '';
$facture->array_options['options_verifactu_error'] = '';
// Update response data if available
if (isset($response->RespuestaLinea)) {
$respuestaLinea = $response->RespuestaLinea;
if (isset($respuestaLinea->IDFactura)) {
$invoiceIdData = array(
'IDEmisorFactura' => $respuestaLinea->IDFactura->IDEmisorFactura ?? '',
'NumSerieFactura' => $respuestaLinea->IDFactura->NumSerieFactura ?? '',
'FechaExpedicionFactura' => $respuestaLinea->IDFactura->FechaExpedicionFactura ?? ''
);
$facture->array_options['options_verifactu_id_factura'] = json_encode($invoiceIdData);
}
if (isset($respuestaLinea->Operacion->TipoOperacion)) {
$facture->array_options['options_verifactu_tipo_operacion'] = $respuestaLinea->Operacion->TipoOperacion;
}
}
if (isset($response->DatosPresentacion)) {
$facture->array_options['options_verifactu_fecha_hora_generacion'] = $response->DatosPresentacion->TimestampPresentacion ?? '';
}
$facture->array_options['options_verifactu_ultimafecha_modificacion'] = date('Y-m-d H:i:s');
$facture->array_options['options_verifactu_ultima_salida'] = json_encode($response);
$facture->insertExtraFields();
dol_syslog("VERIFACTU: Extra fields updated correctly for cancelled invoice " . $invoiceNumber, LOG_INFO);
return $response;
}
/**
* Processes a cancellation error response
*
* @param object $response AEAT response
* @param Facture $facture Invoice
* @param string $invoiceNumber Invoice number
* @param Translate $langs Languages
* @return bool|object False if error, Response if already cancelled
*/
function processCancellationError($response, $facture, $invoiceNumber, $langs)
{
$errorMsg = "Error in VERIFACTU cancellation";
$finalStatus = 'Error';
if (isset($response->EstadoEnvio)) {
$errorMsg .= " - Status: " . $response->EstadoEnvio;
}
if (isset($response->RespuestaLinea)) {
$respuestaLinea = $response->RespuestaLinea;
if (isset($respuestaLinea->IDFactura)) {
$invoiceIdData = array(
'IDEmisorFactura' => $respuestaLinea->IDFactura->IDEmisorFactura ?? '',
'NumSerieFactura' => $respuestaLinea->IDFactura->NumSerieFactura ?? '',
'FechaExpedicionFactura' => $respuestaLinea->IDFactura->FechaExpedicionFactura ?? ''
);
$facture->array_options['options_verifactu_id_factura'] = json_encode($invoiceIdData);
}
if (isset($respuestaLinea->EstadoRegistro)) {
$recordStatus = $respuestaLinea->EstadoRegistro;
$errorMsg .= " - Record Status: " . $recordStatus;
if ($recordStatus === 'Incorrecto') {
$finalStatus = 'Incorrecto';
} else if ($recordStatus === 'Anulada') {
$finalStatus = 'Anulada';
}
}
if (isset($respuestaLinea->CodigoErrorRegistro)) {
$errorMsg .= " - Code: " . $respuestaLinea->CodigoErrorRegistro;
}
if (isset($respuestaLinea->DescripcionErrorRegistro)) {
$errorMsg .= " - Description: " . $respuestaLinea->DescripcionErrorRegistro;
}
// Handle duplicate record specific case
if (isset($respuestaLinea->RegistroDuplicado)) {
$duplicateRecord = $respuestaLinea->RegistroDuplicado;
$errorMsg .= " - Duplicate record";
if (isset($duplicateRecord->IdPeticionRegistroDuplicado)) {
$errorMsg .= " (ID: " . $duplicateRecord->IdPeticionRegistroDuplicado . ")";
}
if (isset($duplicateRecord->EstadoRegistroDuplicado)) {
$errorMsg .= " - Duplicate status: " . $duplicateRecord->EstadoRegistroDuplicado;
if ($duplicateRecord->EstadoRegistroDuplicado === 'Anulada') {
$finalStatus = 'Anulada';
}
}
}
if (isset($respuestaLinea->Operacion->TipoOperacion)) {
$facture->array_options['options_verifactu_tipo_operacion'] = $respuestaLinea->Operacion->TipoOperacion;
}
}
// Check errors at general response level (fallback)
if (!isset($response->RespuestaLinea)) {
if (isset($response->CodigoErrorRegistro)) {
$errorMsg .= " - Code: " . $response->CodigoErrorRegistro;
}
if (isset($response->DescripcionErrorRegistro)) {
$errorMsg .= " - Description: " . $response->DescripcionErrorRegistro;
}
}
dol_syslog("VERIFACTU: " . $errorMsg, LOG_ERR);
// Prepare error data to save with independent connection
$errorData = array(
'estado' => $finalStatus,
'error' => $errorMsg,
'ultima_salida' => json_encode($response),
'fecha_modificacion' => date('Y-m-d H:i:s')
);
if (isset($response->DatosPresentacion)) {
$errorData['fecha_hora_generacion'] = $response->DatosPresentacion->TimestampPresentacion ?? '';
}
$saved = saveVerifactuErrorData($facture, $errorData);
if (!$saved) {
dol_syslog("VERIFACTU: Fallback to insertExtraFields() for response errors", LOG_WARNING);
$facture->array_options['options_verifactu_error'] = $errorMsg;
$facture->array_options['options_verifactu_estado'] = $finalStatus;
$facture->array_options['options_verifactu_ultima_salida'] = json_encode($response);
$facture->array_options['options_verifactu_ultimafecha_modificacion'] = date('Y-m-d H:i:s');
if (isset($response->DatosPresentacion)) {
$facture->array_options['options_verifactu_fecha_hora_generacion'] = $response->DatosPresentacion->TimestampPresentacion ?? '';
}
$facture->insertExtraFields();
}
// If duplicate record is already cancelled, consider this as success
if ($finalStatus === 'Anulada') {
$cancelledBadge = '<div class="center"><span class="badge badge-status6 classfortooltip badge-status" attr-status="' . $langs->trans('VERIFACTU_STATUS_ANULADA') . '">' . $langs->trans('VERIFACTU_STATUS_ANULADA') . '</span></div>';
$cancellationData = array(
'estado' => $cancelledBadge
);
$saved = saveVerifactuErrorData($facture, $cancellationData);
if (!$saved) {
dol_syslog("VERIFACTU: Fallback to insertExtraFields() for cancelled badge", LOG_WARNING);
$facture->array_options['options_verifactu_estado'] = $cancelledBadge;
$facture->insertExtraFields();
}
dol_syslog("VERIFACTU: Invoice was already cancelled - operation completed", LOG_INFO);
return $response;
}
return false;
}