| 1 |
import datetime |
| 2 |
from freezegun import freeze_time |
| 3 |
from unittest import mock |
| 4 |
|
| 5 |
from odoo import _, Command |
| 6 |
from odoo.exceptions import UserError, RedirectWarning, AccessError |
| 7 |
from odoo.tests import tagged |
| 8 |
from odoo.tools import zeep |
| 9 |
from .common import TestL10nEsEdiVerifactuCommon |
| 10 |
|
| 11 |
|
| 12 |
@tagged('post_install_l10n', 'post_install', '-at_install') |
| 13 |
class TestL10nEsEdiVerifactuDocument(TestL10nEsEdiVerifactuCommon): |
| 14 |
|
| 15 |
@classmethod |
| 16 |
def setUpClass(cls): |
| 17 |
super().setUpClass() |
| 18 |
cls.fakenow = datetime.datetime(2024, 12, 5) |
| 19 |
cls.startClassPatcher(freeze_time(cls.fakenow)) |
| 20 |
|
| 21 |
def test_record_identifier(self): |
| 22 |
invoice = self._create_dummy_invoice(name='INV/2019/00006', invoice_date='2024-12-11') |
| 23 |
with self._mock_last_document(None): |
| 24 |
document = invoice._l10n_es_edi_verifactu_create_documents()[invoice] |
| 25 |
expected_record_identifier = { |
| 26 |
'IDEmisorFactura': 'A39200019', |
| 27 |
'NumSerieFactura': 'INV/2019/00006', |
| 28 |
'FechaExpedicionFactura': '11-12-2024', |
| 29 |
} |
| 30 |
record_identifier = document._get_record_identifier() |
| 31 |
self.assertDictEqual(record_identifier, expected_record_identifier | record_identifier) |
| 32 |
|
| 33 |
def test_cannot_delete_chained_document(self): |
| 34 |
invoice = self._create_dummy_invoice(name='INV/2019/00006', invoice_date='2024-12-11') |
| 35 |
with self._mock_last_document(None): |
| 36 |
document = invoice._l10n_es_edi_verifactu_create_documents()[invoice] |
| 37 |
with self.assertRaises(UserError): |
| 38 |
document.unlink() |
| 39 |
|
| 40 |
def test_generation_error(self): |
| 41 |
check_function_path = 'odoo.addons.l10n_es_edi_verifactu.models.verifactu_document.L10nEsEdiVerifactuDocument._check_record_values' |
| 42 |
mock_errors = ["Problem 1", "Problem 2"] |
| 43 |
patched_render_xml_node = mock.patch(check_function_path, return_value=mock_errors) |
| 44 |
invoice = self._create_dummy_invoice() |
| 45 |
with self._mock_last_document(None), patched_render_xml_node: |
| 46 |
document = invoice._l10n_es_edi_verifactu_create_documents()[invoice] |
| 47 |
|
| 48 |
expected_document_values = { |
| 49 |
'document_type': 'submission', |
| 50 |
'state': False, |
| 51 |
'errors': self._mock_format_document_generation_errors(mock_errors), |
| 52 |
'response_csv': False, |
| 53 |
} |
| 54 |
self.assertRecordValues(document, [expected_document_values]) |
| 55 |
|
| 56 |
expected_record_values = { |
| 57 |
'l10n_es_edi_verifactu_state': False, |
| 58 |
'l10n_es_edi_verifactu_warning': expected_document_values['errors'], |
| 59 |
'l10n_es_edi_verifactu_warning_level': 'danger', |
| 60 |
} |
| 61 |
self.assertRecordValues(invoice, [expected_record_values]) |
| 62 |
|
| 63 |
def test_refund_without_refunded_error(self): |
| 64 |
"Asserts no error is raised during the generation of the document." |
| 65 |
invoice = self._create_dummy_invoice(name='INV/2019/00026', invoice_date='2024-12-30') |
| 66 |
credit_note = invoice._reverse_moves() |
| 67 |
credit_note.action_post() |
| 68 |
|
| 69 |
wizard = self.env['account.move.send.wizard'].with_context( |
| 70 |
active_model='account.move', |
| 71 |
active_ids=credit_note.ids, |
| 72 |
).create({ |
| 73 |
'sending_methods': ['manual'], |
| 74 |
}) |
| 75 |
with self._mock_last_document(None), self.assertRaisesRegex(RedirectWarning, r".*There is no Veri\*Factu document for the refunded record\..*"): |
| 76 |
wizard.action_send_and_print() |
| 77 |
|
| 78 |
with self._mock_last_document(None): |
| 79 |
credit_note._l10n_es_edi_verifactu_create_documents() |
| 80 |
|
| 81 |
errors = ["There is no Veri*Factu document for the refunded record.", "The refund reason is not specified."] |
| 82 |
expected_record_values = { |
| 83 |
'l10n_es_edi_verifactu_state': False, |
| 84 |
'l10n_es_edi_verifactu_warning': self._mock_format_document_generation_errors(errors), |
| 85 |
'l10n_es_edi_verifactu_warning_level': 'danger', |
| 86 |
} |
| 87 |
self.assertRecordValues(credit_note, [expected_record_values]) |
| 88 |
|
| 89 |
def test_substitution_without_documents_errors(self): |
| 90 |
invoice = self._create_dummy_invoice(name='INV/2019/00026', invoice_date='2019-01-30') |
| 91 |
|
| 92 |
self.env['account.move.reversal'].with_company(self.company).create( |
| 93 |
{ |
| 94 |
'move_ids': [Command.set((invoice.id,))], |
| 95 |
'date': '2019-02-10', |
| 96 |
'journal_id': invoice.journal_id.id, |
| 97 |
} |
| 98 |
).reverse_moves(is_modify=True) |
| 99 |
credit_note = invoice.reversal_move_ids |
| 100 |
|
| 101 |
substitution_move = invoice.l10n_es_edi_verifactu_substitution_move_ids |
| 102 |
substitution_move.invoice_date = '2019-02-11' |
| 103 |
substitution_move.action_post() |
| 104 |
wizard = self.env['account.move.send.wizard'].with_context( |
| 105 |
active_model='account.move', |
| 106 |
active_ids=substitution_move.ids, |
| 107 |
).create({ |
| 108 |
'sending_methods': ['manual'], |
| 109 |
}) |
| 110 |
|
| 111 |
with self.assertRaisesRegex(RedirectWarning, r".*There is no Veri\*Factu document for the substituted record\..*"): |
| 112 |
wizard.action_send_and_print() |
| 113 |
|
| 114 |
substitution_move._l10n_es_edi_verifactu_create_documents() |
| 115 |
errors = ["There is no Veri*Factu document for the substituted record.", "There is no Veri*Factu document for the reversal of the substituted record."] |
| 116 |
expected_record_values = { |
| 117 |
'l10n_es_edi_verifactu_state': False, |
| 118 |
'l10n_es_edi_verifactu_warning': self._mock_format_document_generation_errors(errors), |
| 119 |
'l10n_es_edi_verifactu_warning_level': 'danger', |
| 120 |
} |
| 121 |
self.assertRecordValues(substitution_move, [expected_record_values]) |
| 122 |
|
| 123 |
with self._mock_last_document(None): |
| 124 |
invoice._l10n_es_edi_verifactu_create_documents() |
| 125 |
|
| 126 |
with self._mock_zeep_registration_operation_certificate_issue(), self.assertRaisesRegex(RedirectWarning, r".*There is no Veri\*Factu document for the reversal of the substituted record\..*"): |
| 127 |
wizard.action_send_and_print() |
| 128 |
|
| 129 |
substitution_move._l10n_es_edi_verifactu_create_documents() |
| 130 |
errors = ["There is no Veri*Factu document for the reversal of the substituted record."] |
| 131 |
expected_record_values = { |
| 132 |
'l10n_es_edi_verifactu_state': False, |
| 133 |
'l10n_es_edi_verifactu_warning': self._mock_format_document_generation_errors(errors), |
| 134 |
'l10n_es_edi_verifactu_warning_level': 'danger', |
| 135 |
} |
| 136 |
self.assertRecordValues(substitution_move, [expected_record_values]) |
| 137 |
|
| 138 |
credit_note._l10n_es_edi_verifactu_create_documents() |
| 139 |
|
| 140 |
with self._mock_zeep_registration_operation_certificate_issue(): |
| 141 |
wizard.action_send_and_print() |
| 142 |
|
| 143 |
self.assertTrue(substitution_move.l10n_es_edi_verifactu_document_ids.json_attachment_id) |
| 144 |
|
| 145 |
def test_certificate_issue(self): |
| 146 |
invoice = self._create_dummy_invoice() |
| 147 |
with self._mock_last_document(None): |
| 148 |
document = invoice._l10n_es_edi_verifactu_create_documents()[invoice] |
| 149 |
with self._mock_zeep_registration_operation_certificate_issue(): |
| 150 |
_batch_xml, info = document._send_as_batch() |
| 151 |
|
| 152 |
expected_response_info = { |
| 153 |
'errors': ["The document could not be sent; the access was denied due to a problem with the certificate."], |
| 154 |
'record_info': {}, |
| 155 |
} |
| 156 |
self.assertDictEqual(info, expected_response_info | info) |
| 157 |
|
| 158 |
self.assertFalse(self.company.l10n_es_edi_verifactu_next_batch_time) |
| 159 |
|
| 160 |
expected_document_values = { |
| 161 |
'document_type': 'submission', |
| 162 |
'response_csv': False, |
| 163 |
'state': False, |
| 164 |
'errors': self._mock_format_document_generic_errors(expected_response_info['errors']), |
| 165 |
} |
| 166 |
self.assertRecordValues(document, [expected_document_values]) |
| 167 |
|
| 168 |
waiting_warning = "A Veri*Factu document is waiting to be sent as soon as possible." |
| 169 |
expected_record_values = { |
| 170 |
'l10n_es_edi_verifactu_state': False, |
| 171 |
'l10n_es_edi_verifactu_warning': expected_document_values['errors'] + "\n" + waiting_warning, |
| 172 |
'l10n_es_edi_verifactu_warning_level': 'danger', |
| 173 |
} |
| 174 |
self.assertRecordValues(invoice, [expected_record_values]) |
| 175 |
|
| 176 |
def test_soapfault(self): |
| 177 |
def _raise_soapfault(*args, **kwargs): |
| 178 |
message = "Codigo[4102].El XML no cumple el esquema. Falta informar campo obligatorio.: NombreRazon" |
| 179 |
code = "env:Client" |
| 180 |
raise zeep.exceptions.Fault(message, code=code) |
| 181 |
|
| 182 |
invoice = self._create_dummy_invoice() |
| 183 |
with self._mock_last_document(None): |
| 184 |
document = invoice._l10n_es_edi_verifactu_create_documents()[invoice] |
| 185 |
|
| 186 |
with self._mock_get_zeep_operation(registration_return_value=_raise_soapfault): |
| 187 |
_batch_xml, info = document._send_as_batch() |
| 188 |
|
| 189 |
expected_response_info = { |
| 190 |
'errors': [ |
| 191 |
'[env:Client] Codigo[4102].El XML no cumple el esquema. Falta informar campo obligatorio.: NombreRazon', |
| 192 |
], |
| 193 |
'record_info': {}, |
| 194 |
} |
| 195 |
self.assertDictEqual(info, expected_response_info | info) |
| 196 |
|
| 197 |
self.assertFalse(self.company.l10n_es_edi_verifactu_next_batch_time) |
| 198 |
|
| 199 |
expected_document_values = { |
| 200 |
'document_type': 'submission', |
| 201 |
'response_csv': False, |
| 202 |
'state': 'rejected', |
| 203 |
'errors': self._mock_format_document_aeat_errors(expected_response_info['errors']), |
| 204 |
} |
| 205 |
self.assertRecordValues(document, [expected_document_values]) |
| 206 |
|
| 207 |
expected_record_values = { |
| 208 |
'l10n_es_edi_verifactu_state': 'rejected', |
| 209 |
'l10n_es_edi_verifactu_warning': expected_document_values['errors'], |
| 210 |
'l10n_es_edi_verifactu_warning_level': 'danger', |
| 211 |
} |
| 212 |
self.assertRecordValues(invoice, [expected_record_values]) |
| 213 |
|
| 214 |
def test_batch_single_accepted_registration(self): |
| 215 |
invoice = self._create_dummy_invoice(name='INV/2019/00026', invoice_date='2024-12-30') |
| 216 |
with self._mock_last_document(None): |
| 217 |
document = invoice._l10n_es_edi_verifactu_create_documents()[invoice] |
| 218 |
with self._mock_zeep_registration_operation('l10n_es_edi_verifactu/tests/responses/batch_single_accepted_registration.json'): |
| 219 |
_batch_xml, info = document._send_as_batch() |
| 220 |
|
| 221 |
expected_response_info = { |
| 222 |
'response_csv': 'A-YDSW8NLFLANWPM', |
| 223 |
'waiting_time_seconds': 60, |
| 224 |
'errors': [], |
| 225 |
'record_info': { |
| 226 |
"('A39200019', 'INV/2019/00026')": { |
| 227 |
'state': 'accepted', |
| 228 |
'cancellation': False, |
| 229 |
'errors': [], |
| 230 |
}, |
| 231 |
}, |
| 232 |
} |
| 233 |
self.assertDictEqual(info, expected_response_info | info) |
| 234 |
|
| 235 |
self.assertEqual(self.company.l10n_es_edi_verifactu_next_batch_time, |
| 236 |
datetime.datetime(2024, 12, 5, 0, 1, 0)) |
| 237 |
|
| 238 |
expected_document_values = { |
| 239 |
'document_type': 'submission', |
| 240 |
'response_csv': 'A-YDSW8NLFLANWPM', |
| 241 |
'state': 'accepted', |
| 242 |
'errors': False, |
| 243 |
} |
| 244 |
self.assertRecordValues(document, [expected_document_values]) |
| 245 |
|
| 246 |
expected_record_values = { |
| 247 |
'l10n_es_edi_verifactu_state': 'accepted', |
| 248 |
'l10n_es_edi_verifactu_warning': False, |
| 249 |
'l10n_es_edi_verifactu_warning_level': False, |
| 250 |
} |
| 251 |
self.assertRecordValues(invoice, [expected_record_values]) |
| 252 |
|
| 253 |
def test_batch_single_accepted_cancellation(self): |
| 254 |
invoice = self._create_dummy_invoice(name='INV/2019/00026', invoice_date='2024-12-30') |
| 255 |
with self._mock_last_document(None): |
| 256 |
submission_document = invoice._l10n_es_edi_verifactu_create_documents()[invoice] |
| 257 |
self.assertFalse(submission_document.errors) |
| 258 |
with self._mock_zeep_registration_operation('l10n_es_edi_verifactu/tests/responses/batch_single_accepted_registration.json'): |
| 259 |
submission_document._send_as_batch() |
| 260 |
self.assertEqual(invoice.l10n_es_edi_verifactu_state, 'accepted') |
| 261 |
document = invoice._l10n_es_edi_verifactu_create_documents(cancellation=True)[invoice] |
| 262 |
with self._mock_zeep_registration_operation('l10n_es_edi_verifactu/tests/responses/batch_single_accepted_cancellation.json'): |
| 263 |
_batch_xml, info = document._send_as_batch() |
| 264 |
|
| 265 |
expected_response_info = { |
| 266 |
'response_csv': 'A-JJ2XWTUCTVV3TQ', |
| 267 |
'waiting_time_seconds': 60, |
| 268 |
'errors': [], |
| 269 |
'record_info': { |
| 270 |
"('A39200019', 'INV/2019/00047')": { |
| 271 |
'state': 'accepted', |
| 272 |
'cancellation': False, |
| 273 |
'errors': [], |
| 274 |
}, |
| 275 |
}, |
| 276 |
} |
| 277 |
self.assertDictEqual(info, expected_response_info | info) |
| 278 |
|
| 279 |
self.assertEqual(self.company.l10n_es_edi_verifactu_next_batch_time, |
| 280 |
datetime.datetime(2024, 12, 5, 0, 1, 0)) |
| 281 |
|
| 282 |
expected_document_values = { |
| 283 |
'document_type': 'cancellation', |
| 284 |
'response_csv': 'A-JJ2XWTUCTVV3TQ', |
| 285 |
'state': 'accepted', |
| 286 |
'errors': False, |
| 287 |
} |
| 288 |
self.assertRecordValues(document, [expected_document_values]) |
| 289 |
|
| 290 |
expected_record_values = { |
| 291 |
'l10n_es_edi_verifactu_state': 'cancelled', |
| 292 |
'l10n_es_edi_verifactu_warning': False, |
| 293 |
'l10n_es_edi_verifactu_warning_level': False, |
| 294 |
'state': 'cancel', |
| 295 |
} |
| 296 |
self.assertRecordValues(invoice, [expected_record_values]) |
| 297 |
|
| 298 |
def test_batch_single_rejected_registration(self): |
| 299 |
invoice = self._create_dummy_invoice(name='INV/2019/00006', invoice_date='2024-12-11') |
| 300 |
with self._mock_last_document(None): |
| 301 |
document = invoice._l10n_es_edi_verifactu_create_documents()[invoice] |
| 302 |
with self._mock_zeep_registration_operation('l10n_es_edi_verifactu/tests/responses/batch_single_rejected_registration.json'): |
| 303 |
_batch_xml, info = document._send_as_batch() |
| 304 |
|
| 305 |
record_info = { |
| 306 |
'state': 'rejected', |
| 307 |
'cancellation': False, |
| 308 |
'errors': [ |
| 309 |
'[1244] El campo FechaHoraHusoGenRegistro tiene un formato incorrecto.', |
| 310 |
], |
| 311 |
} |
| 312 |
expected_response_info = { |
| 313 |
'response_csv': False, |
| 314 |
'waiting_time_seconds': 60, |
| 315 |
'errors': [], |
| 316 |
'record_info': { |
| 317 |
"('A39200019', 'INV/2019/00006')": record_info, |
| 318 |
}, |
| 319 |
} |
| 320 |
self.assertDictEqual(info, expected_response_info | info) |
| 321 |
|
| 322 |
self.assertEqual(self.company.l10n_es_edi_verifactu_next_batch_time, |
| 323 |
datetime.datetime(2024, 12, 5, 0, 1, 0)) |
| 324 |
|
| 325 |
expected_document_values = { |
| 326 |
'document_type': 'submission', |
| 327 |
'response_csv': False, |
| 328 |
'state': 'rejected', |
| 329 |
'errors': self._mock_format_document_aeat_errors(record_info['errors']), |
| 330 |
} |
| 331 |
self.assertRecordValues(document, [expected_document_values]) |
| 332 |
|
| 333 |
expected_record_values = { |
| 334 |
'l10n_es_edi_verifactu_state': 'rejected', |
| 335 |
'l10n_es_edi_verifactu_warning': expected_document_values['errors'], |
| 336 |
'l10n_es_edi_verifactu_warning_level': 'danger', |
| 337 |
} |
| 338 |
self.assertRecordValues(invoice, [expected_record_values]) |
| 339 |
|
| 340 |
def test_batch_single_registered_with_errors_registration(self): |
| 341 |
invoice = self._create_dummy_invoice(name='INV/2019/00007', invoice_date='2024-12-17') |
| 342 |
with self._mock_last_document(None): |
| 343 |
document = invoice._l10n_es_edi_verifactu_create_documents()[invoice] |
| 344 |
with self._mock_zeep_registration_operation('l10n_es_edi_verifactu/tests/responses/batch_single_registered_with_errors_registration.json'): |
| 345 |
_batch_xml, info = document._send_as_batch() |
| 346 |
|
| 347 |
record_info = { |
| 348 |
'state': 'registered_with_errors', |
| 349 |
'cancellation': False, |
| 350 |
'errors': [ |
| 351 |
'[2005] El campo ImporteTotal tiene un valor incorrecto para el valor de los campos BaseImponibleOimporteNoSujeto, CuotaRepercutida y CuotaRecargoEquivalencia suministrados.', |
| 352 |
], |
| 353 |
} |
| 354 |
expected_response_info = { |
| 355 |
'response_csv': 'A-X2CPJ3HE3AFADY', |
| 356 |
'waiting_time_seconds': 60, |
| 357 |
'errors': [], |
| 358 |
'record_info': { |
| 359 |
"('A39200019', 'INV/2019/00007')": record_info, |
| 360 |
}, |
| 361 |
} |
| 362 |
self.assertDictEqual(info, expected_response_info | info) |
| 363 |
|
| 364 |
self.assertEqual(self.company.l10n_es_edi_verifactu_next_batch_time, |
| 365 |
datetime.datetime(2024, 12, 5, 0, 1, 0)) |
| 366 |
|
| 367 |
expected_document_values = { |
| 368 |
'document_type': 'submission', |
| 369 |
'response_csv': 'A-X2CPJ3HE3AFADY', |
| 370 |
'state': 'registered_with_errors', |
| 371 |
'errors': self._mock_format_document_aeat_errors(record_info['errors']), |
| 372 |
} |
| 373 |
self.assertRecordValues(document, [expected_document_values]) |
| 374 |
|
| 375 |
expected_record_values = { |
| 376 |
'l10n_es_edi_verifactu_state': 'registered_with_errors', |
| 377 |
'l10n_es_edi_verifactu_warning': expected_document_values['errors'], |
| 378 |
'l10n_es_edi_verifactu_warning_level': 'warning', |
| 379 |
} |
| 380 |
self.assertRecordValues(invoice, [expected_record_values]) |
| 381 |
|
| 382 |
def test_batch_single_duplicate_original_registered_with_errors_registration_without_timeout(self): |
| 383 |
invoice = self._create_dummy_invoice(name='INV/2019/00006', invoice_date='2024-12-11') |
| 384 |
with self._mock_last_document(None): |
| 385 |
document = invoice._l10n_es_edi_verifactu_create_documents()[invoice] |
| 386 |
with self._mock_zeep_registration_operation('l10n_es_edi_verifactu/tests/responses/batch_single_duplicate_original_registered_with_errors.json'): |
| 387 |
_batch_xml, info = document._send_as_batch() |
| 388 |
|
| 389 |
|
| 390 |
record_info = { |
| 391 |
'state': 'rejected', |
| 392 |
'cancellation': False, |
| 393 |
'errors': [ |
| 394 |
'[3000] Registro de facturación duplicado.', |
| 395 |
], |
| 396 |
} |
| 397 |
expected_response_info = { |
| 398 |
'response_csv': False, |
| 399 |
'waiting_time_seconds': 60, |
| 400 |
'errors': [], |
| 401 |
'record_info': { |
| 402 |
"('A39200019', 'INV/2019/00007')": record_info, |
| 403 |
}, |
| 404 |
} |
| 405 |
self.assertDictEqual(info, expected_response_info | info) |
| 406 |
|
| 407 |
self.assertEqual(self.company.l10n_es_edi_verifactu_next_batch_time, |
| 408 |
datetime.datetime(2024, 12, 5, 0, 1, 0)) |
| 409 |
|
| 410 |
expected_document_values = { |
| 411 |
'document_type': 'submission', |
| 412 |
'response_csv': False, |
| 413 |
'state': 'rejected', |
| 414 |
'errors': self._mock_format_document_aeat_errors(record_info['errors']), |
| 415 |
} |
| 416 |
self.assertRecordValues(document, [expected_document_values]) |
| 417 |
|
| 418 |
expected_record_values = { |
| 419 |
'l10n_es_edi_verifactu_state': 'rejected', |
| 420 |
'l10n_es_edi_verifactu_warning': expected_document_values['errors'], |
| 421 |
'l10n_es_edi_verifactu_warning_level': 'danger', |
| 422 |
} |
| 423 |
self.assertRecordValues(invoice, [expected_record_values]) |
| 424 |
|
| 425 |
def test_batch_single_duplicate_original_registered_with_errors_registration_with_timeout(self): |
| 426 |
invoice = self._create_dummy_invoice(name='INV/2019/00006', invoice_date='2024-12-11') |
| 427 |
with self._mock_last_document(None): |
| 428 |
document = invoice._l10n_es_edi_verifactu_create_documents()[invoice] |
| 429 |
|
| 430 |
document.errors = self._mock_format_document_generic_errors( |
| 431 |
["[Read-Timeout] Timeout while waiting for the response from the server:\nHTTPSConnectionPool(host='prewww1.aeat.es', port=443): Read timed out. (read timeout=0.07)"] |
| 432 |
) |
| 433 |
with self._mock_zeep_registration_operation('l10n_es_edi_verifactu/tests/responses/batch_single_duplicate_original_registered_with_errors.json'): |
| 434 |
_batch_xml, info = document._send_as_batch() |
| 435 |
|
| 436 |
|
| 437 |
record_info = { |
| 438 |
'state': 'registered_with_errors', |
| 439 |
'cancellation': False, |
| 440 |
'errors': [ |
| 441 |
'[2005] El campo ImporteTotal tiene un valor incorrecto para el valor de los campos BaseImponibleOimporteNoSujeto, CuotaRepercutida y CuotaRecargoEquivalencia suministrados.', |
| 442 |
], |
| 443 |
} |
| 444 |
expected_response_info = { |
| 445 |
'response_csv': False, |
| 446 |
'waiting_time_seconds': 60, |
| 447 |
'errors': [], |
| 448 |
'record_info': { |
| 449 |
"('A39200019', 'INV/2019/00007')": record_info, |
| 450 |
}, |
| 451 |
} |
| 452 |
self.assertDictEqual(info, expected_response_info | info) |
| 453 |
|
| 454 |
self.assertEqual(self.company.l10n_es_edi_verifactu_next_batch_time, |
| 455 |
datetime.datetime(2024, 12, 5, 0, 1, 0)) |
| 456 |
|
| 457 |
expected_document_values = { |
| 458 |
'document_type': 'submission', |
| 459 |
'response_csv': False, |
| 460 |
'state': 'registered_with_errors', |
| 461 |
'errors': self._mock_format_document_aeat_errors(record_info['errors']), |
| 462 |
} |
| 463 |
self.assertRecordValues(document, [expected_document_values]) |
| 464 |
|
| 465 |
expected_record_values = { |
| 466 |
'l10n_es_edi_verifactu_state': 'registered_with_errors', |
| 467 |
'l10n_es_edi_verifactu_warning': expected_document_values['errors'], |
| 468 |
'l10n_es_edi_verifactu_warning_level': 'warning', |
| 469 |
} |
| 470 |
self.assertRecordValues(invoice, [expected_record_values]) |
| 471 |
|
| 472 |
def test_response_parsing_error_document_not_found(self): |
| 473 |
invoice = self._create_dummy_invoice(name='INV/2019/00500', invoice_date='2024-12-17') |
| 474 |
with self._mock_last_document(None): |
| 475 |
document = invoice._l10n_es_edi_verifactu_create_documents()[invoice] |
| 476 |
with self._mock_zeep_registration_operation('l10n_es_edi_verifactu/tests/responses/batch_single_registered_with_errors_registration.json'): |
| 477 |
_batch_xml, info = document._send_as_batch() |
| 478 |
|
| 479 |
record_info = { |
| 480 |
'state': 'registered_with_errors', |
| 481 |
'cancellation': False, |
| 482 |
'errors': [ |
| 483 |
'[2005] El campo ImporteTotal tiene un valor incorrecto para el valor de los campos BaseImponibleOimporteNoSujeto, CuotaRepercutida y CuotaRecargoEquivalencia suministrados.', |
| 484 |
], |
| 485 |
} |
| 486 |
expected_response_info = { |
| 487 |
'waiting_time_seconds': 60, |
| 488 |
'errors': [], |
| 489 |
'record_info': { |
| 490 |
"('A39200019', 'INV/2019/00007')": record_info, |
| 491 |
}, |
| 492 |
} |
| 493 |
self.assertDictEqual(info, expected_response_info | info) |
| 494 |
|
| 495 |
|
| 496 |
self.assertEqual(self.company.l10n_es_edi_verifactu_next_batch_time, |
| 497 |
datetime.datetime(2024, 12, 5, 0, 1, 0)) |
| 498 |
|
| 499 |
errors = [_("We could not find any information about the record in the linked batch document.")] |
| 500 |
expected_document_values = { |
| 501 |
'document_type': 'submission', |
| 502 |
'response_csv': 'A-X2CPJ3HE3AFADY', |
| 503 |
'state': False, |
| 504 |
'errors': self._mock_format_document_generic_errors(errors), |
| 505 |
} |
| 506 |
self.assertRecordValues(document, [expected_document_values]) |
| 507 |
|
| 508 |
waiting_warning = "A Veri*Factu document is waiting to be sent as soon as possible." |
| 509 |
expected_record_values = { |
| 510 |
'l10n_es_edi_verifactu_state': False, |
| 511 |
'l10n_es_edi_verifactu_warning': expected_document_values['errors'] + "\n" + waiting_warning, |
| 512 |
'l10n_es_edi_verifactu_warning_level': 'danger', |
| 513 |
} |
| 514 |
self.assertRecordValues(invoice, [expected_record_values]) |
| 515 |
|
| 516 |
def test_mark_for_next_batch(self): |
| 517 |
|
| 518 |
self.assertFalse(self.company.l10n_es_edi_verifactu_next_batch_time) |
| 519 |
mock_accept = self._mock_zeep_registration_operation('l10n_es_edi_verifactu/tests/responses/batch_single_accepted_registration.json') |
| 520 |
|
| 521 |
invoice = self._create_dummy_invoice(name='INV/2019/00026', invoice_date='2024-12-30') |
| 522 |
with self._mock_last_document(None), mock_accept: |
| 523 |
created_documents = invoice._l10n_es_edi_verifactu_mark_for_next_batch() |
| 524 |
document = created_documents[invoice] |
| 525 |
|
| 526 |
expected_document_values = { |
| 527 |
'document_type': 'submission', |
| 528 |
'state': 'accepted', |
| 529 |
'errors': False, |
| 530 |
'response_csv': 'A-YDSW8NLFLANWPM', |
| 531 |
} |
| 532 |
self.assertRecordValues(document, [expected_document_values]) |
| 533 |
|
| 534 |
expected_record_values = { |
| 535 |
'l10n_es_edi_verifactu_state': 'accepted', |
| 536 |
'l10n_es_edi_verifactu_warning': False, |
| 537 |
'l10n_es_edi_verifactu_warning_level': False, |
| 538 |
} |
| 539 |
self.assertRecordValues(invoice, [expected_record_values]) |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
self.assertEqual(self.company.l10n_es_edi_verifactu_next_batch_time, |
| 544 |
datetime.datetime(2024, 12, 5, 0, 1, 0)) |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
invoice = self._create_dummy_invoice(name='INV/2019/00027', invoice_date='2024-12-30') |
| 551 |
cron_trigger_result_dict = {} |
| 552 |
with mock_accept, self._mock_cron_trigger(cron_trigger_result_dict): |
| 553 |
created_documents = invoice._l10n_es_edi_verifactu_mark_for_next_batch() |
| 554 |
document = created_documents[invoice] |
| 555 |
self.assertEqual(cron_trigger_result_dict['at'], datetime.datetime(2024, 12, 5, 0, 1, 0)) |
| 556 |
|
| 557 |
expected_document_values = { |
| 558 |
'document_type': 'submission', |
| 559 |
'response_csv': False, |
| 560 |
'state': False, |
| 561 |
'errors': False, |
| 562 |
} |
| 563 |
self.assertRecordValues(document, [expected_document_values]) |
| 564 |
|
| 565 |
def test_response_issue(self): |
| 566 |
|
| 567 |
self.assertFalse(self.company.l10n_es_edi_verifactu_next_batch_time) |
| 568 |
|
| 569 |
|
| 570 |
invoice = self._create_dummy_invoice(name='INV/2019/00500', invoice_date='2024-12-17') |
| 571 |
cron_trigger_result_dict = {} |
| 572 |
with self._mock_zeep_registration_operation_certificate_issue(), self._mock_cron_trigger(cron_trigger_result_dict): |
| 573 |
created_documents = invoice._l10n_es_edi_verifactu_mark_for_next_batch() |
| 574 |
document = created_documents[invoice] |
| 575 |
|
| 576 |
|
| 577 |
self.assertFalse(document.state) |
| 578 |
self.assertFalse(self.company.l10n_es_edi_verifactu_next_batch_time) |
| 579 |
|
| 580 |
self.assertEqual(cron_trigger_result_dict['at'], datetime.datetime(2024, 12, 5, 0, 1, 0)) |
| 581 |
|
| 582 |
def test_verifactu_document_reading_access_right(self): |
| 583 |
move = self.env['account.move'].create({}) |
| 584 |
self.user.group_ids = self.env.ref('base.group_user') |
| 585 |
with self.assertRaises(AccessError): |
| 586 |
move.with_user(self.user).read(['l10n_es_edi_verifactu_document_ids']) |
| 587 |
for group in ('account.group_account_invoice', 'account.group_account_readonly'): |
| 588 |
self.user.group_ids = self.env.ref(group) |
| 589 |
|
| 590 |
move.with_user(self.user).read(['l10n_es_edi_verifactu_document_ids']) |