Skip to content

Commit

Permalink
[FIX] l10n_es_aeat_verifactu: Invoice chain filter and adapt tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aritzolea committed Feb 21, 2025
1 parent 7f97dc3 commit b4b4863
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
16 changes: 12 additions & 4 deletions l10n_es_aeat_verifactu/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,10 @@ def _post(self, soft=True):
self.write({"verifactu_registration_date": verifactu_reg_date})
res = super()._post(soft=soft)
# TODO: review retry strategy
for record in self:
verifactu_invoices = self.filtered(
lambda move: self._is_verifactu_invoice(move)
)
for record in verifactu_invoices:
for attempt in range(SEND_TO_VERIFACTU_MAX_RETRIES):
try:
record._set_chaining_invoice()
Expand All @@ -465,12 +468,17 @@ def _post(self, soft=True):
invoice.send_verifactu()
return res

def _should_send_to_verifactu(self, invoice):
def _is_verifactu_invoice(self, invoice):
return (
not config["test_enable"]
and invoice.exists()
invoice.exists()
and invoice.is_invoice()
and invoice.verifactu_enabled
)

def _should_send_to_verifactu(self, invoice):
return (
self._is_verifactu_invoice(invoice)
and not config["test_enable"]
and invoice.state in VERIFACTU_VALID_INVOICE_STATES
)

Expand Down
37 changes: 19 additions & 18 deletions l10n_es_aeat_verifactu/tests/test_l10n_es_aeat_verifactu.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ def setUpClass(cls):
def test_get_chaining_invoice_dict_first_record(self):
"""Test chaining dict when there's no previous invoice."""
self.company.verifactu_last_invoice_id = False
self.invoice.action_post()
result = self.invoice._get_chaining_invoice_dict()
self.assertEqual(
result,
Expand All @@ -332,35 +333,35 @@ def test_get_chaining_invoice_dict_first_record(self):

def test_get_chaining_invoice_dict_with_previous(self):
"""Test chaining dict when there's a previous invoice."""
prev_invoice = self.invoice.copy(
self.invoice.action_post()
new_invoice = self.invoice.copy(
{
"invoice_date": "2024-01-01",
"name": "PREV001",
}
)
prev_invoice._get_verifactu_invoice_dict()
self.assertEqual(self.company.verifactu_last_invoice_id, prev_invoice)

result = self.invoice._get_chaining_invoice_dict()
new_invoice.action_post()
new_invoice._get_verifactu_invoice_dict()
self.assertEqual(self.company.verifactu_last_invoice_id, new_invoice)

expected = {
"RegistroAnterior": {
"IDEmisorFactura": prev_invoice._get_verifactu_issuer(),
"NumSerieFactura": prev_invoice._get_document_serial_number(),
"FechaExpedicionFactura": prev_invoice._change_date_format(
prev_invoice._get_document_date()
"IDEmisorFactura": self.invoice._get_verifactu_issuer(),
"NumSerieFactura": self.invoice._get_document_serial_number(),
"FechaExpedicionFactura": self.invoice._change_date_format(
self.invoice._get_document_date()
),
"Huella": prev_invoice.verifactu_hash,
"Huella": self.invoice.verifactu_hash,
}
}
self.assertEqual(
result,
new_invoice._get_chaining_invoice_dict(),
expected,
"Should return previous invoice data in correct format",
)
self.assertEqual(
self.company.verifactu_last_invoice_id,
self.invoice,
new_invoice,
"Should update company's last invoice reference",
)

Expand All @@ -371,23 +372,23 @@ def mock_execute(*args, **kwargs):
raise OperationalError("Test lock error")

self.company.verifactu_last_invoice_id = False
prev_invoice = self.invoice.copy(
self.invoice.action_post()
self.assertEqual(self.company.verifactu_last_invoice_id, self.invoice)
new_invoice = self.invoice.copy(
{
"invoice_date": "2024-01-01",
"name": "PREV001",
}
)
prev_invoice._get_verifactu_invoice_dict()
self.assertEqual(self.company.verifactu_last_invoice_id, prev_invoice)

old_execute = self.cr.execute
with self.assertRaises(OperationalError):
with self.cr.savepoint():
self.cr.execute = mock_execute
self.invoice._get_chaining_invoice_dict()
new_invoice.action_post()
self.cr.execute = old_execute
self.assertEqual(new_invoice.state, "draft")
self.assertEqual(
self.company.verifactu_last_invoice_id,
prev_invoice,
self.invoice,
"Should not update company's last invoice reference on error",
)

0 comments on commit b4b4863

Please sign in to comment.