[FIX] l10n_sa_edi: skip QR code generation on invoices without signature

Steps to reproduce
------------------
* install `l10n_sa_edi`
* switch to a Saudi company
* deactivate the "EDI : Perform web services operations" scheduled
  action. (this will help ease the reproduction process)
* create and post two invoices for a partner that's an individual (not a
  company)
* manually run that scheduled action.

You should be met with a traceback.

Cause
-----
When a ZATCA document is submitted, the system performs several
operations, two of which are important for this issue:
1. Generating a signature (`l10n_sa_invoice_signature`)
2. Using this signature to generate a QR code (`l10n_sa_qr_code_str`)

The QR code generation relies on the assumption that the signature
(`l10n_sa_invoice_signature`) already exists, which is a reasonable
expectation as the signature is typically created prior to the QR code.

However, complications arise when multiple documents are submitted
simultaneously in a batch process. During batch processing, each
document in the batch undergoes the same two operations mentioned above.
The issue emerges due to the behavior of non-stored computed fields .

In Odoo, computed fields are evaluated in batches, meaning that if you
access a computed field for one record, Odoo may also compute the same
field for other records fetched in the same operation. For example, if
you have two records, `A` and `B` such that `B.id in A._prefetch_ids`,
and you access a computed field on `A`, Odoo will also compute this
field for `B` at the same time.

This behavior leads to an issue when submitting two documents (`D1` and
`D2`) together. The system will generate a signature and then compute
the QR code for `D1`. However, when computing the QR code for `D1`, it
inadvertently attempts to also compute the QR code for `D2` due to the
batch computation behavior. Since `D2`'s signature has not yet been
generated at this point, this results in an error.

opw-3696146

closes odoo/odoo#155269

X-original-commit: 97e67514c0e72730882cc093b36cb5477eba496d
Signed-off-by: William André (wan) <wan@odoo.com>
This commit is contained in:
sesn-odoo
2024-02-23 19:27:36 +00:00
parent f7dc38f18d
commit f8ffd62aa2
@@ -210,6 +210,9 @@ class AccountEdiFormat(models.Model):
"""
signed_xml = self._l10n_sa_sign_xml(unsigned_xml, x509_cert, invoice.l10n_sa_invoice_signature)
if invoice._l10n_sa_is_simplified():
# Applying with_prefetch() to set the _prefetch_ids = _ids,
# preventing premature QR code computation for other invoices.
invoice = invoice.with_prefetch()
return self._l10n_sa_apply_qr_code(invoice, signed_xml)
return signed_xml