[FIX] l10n_sa_edi: fix qr code for ZATCA Phase 1 invoices

Currently, when we try to print a ZATCA invoice after installing the l10n_sa_edi module, the system will always default to printing the Phase 2 QR code on the invoice reports, irrespective of whether or not the invoice itself is for Phase 1 or 2. To fix this, we check if the Invoice has edi_documents, in which case it is marked as Phase 2 and runs as usual. Otherwise, we mark it as Phase 1 and call the initial function.

closes odoo/odoo#148928

X-original-commit: 3d33d18bbe5c462e0b986cb4891a67a14c63eb62
Signed-off-by: Mehdi Bendali Hacine (mebe) <mebe@odoo.com>
Signed-off-by: Josse Colpaert <jco@odoo.com>
This commit is contained in:
mebe-odoo
2024-01-10 21:04:38 +00:00
parent a5df9781be
commit 2eec0b650c
2 changed files with 18 additions and 10 deletions
@@ -319,6 +319,7 @@ class AccountEdiFormat(models.Model):
'blocking_level': 'error',
'response': None,
}}
xml_content = None
if not invoice.l10n_sa_chain_index:
# If the Invoice doesn't have a chain index, it means it either has not been submitted before,
+17 -10
View File
@@ -32,25 +32,32 @@ class AccountMove(models.Model):
return self.partner_id.company_type == 'person'
@api.depends('amount_total_signed', 'amount_tax_signed', 'l10n_sa_confirmation_datetime', 'company_id',
'company_id.vat', 'journal_id', 'journal_id.l10n_sa_production_csid_json',
'l10n_sa_invoice_signature', 'l10n_sa_chain_index')
'company_id.vat', 'journal_id', 'journal_id.l10n_sa_production_csid_json', 'edi_document_ids',
'l10n_sa_invoice_signature', 'l10n_sa_chain_index', 'state')
def _compute_qr_code_str(self):
""" Override to update QR code generation in accordance with ZATCA Phase 2"""
phase_one_moves = self.env['account.move']
for move in self:
move.l10n_sa_qr_code_str = ''
if move.country_code == 'SA' and move.move_type in ('out_invoice', 'out_refund') and move.l10n_sa_chain_index:
edi_format = self.env.ref('l10n_sa_edi.edi_sa_zatca')
zatca_document = move.edi_document_ids.filtered(lambda d: d.edi_format_id == edi_format)
zatca_document = move.edi_document_ids.filtered(lambda d: d.edi_format_id.code == 'sa_zatca')
if move.country_code == 'SA' and move.move_type in ('out_invoice', 'out_refund') and zatca_document and move.state != 'draft':
qr_code_str = ''
if move._l10n_sa_is_simplified():
x509_cert = json.loads(move.journal_id.l10n_sa_production_csid_json)['binarySecurityToken']
x509_cert = json.loads(move.journal_id.sudo().l10n_sa_production_csid_json)['binarySecurityToken']
xml_content = self.env.ref('l10n_sa_edi.edi_sa_zatca')._l10n_sa_generate_zatca_template(move)
qr_code_str = move._l10n_sa_get_qr_code(move.journal_id, xml_content, b64decode(x509_cert), move.l10n_sa_invoice_signature, move._l10n_sa_is_simplified())
move.l10n_sa_qr_code_str = b64encode(qr_code_str).decode()
qr_code_str = move._l10n_sa_get_qr_code(move.journal_id, xml_content, b64decode(x509_cert),
move.l10n_sa_invoice_signature, True)
qr_code_str = b64encode(qr_code_str).decode()
elif zatca_document.state == 'sent' and zatca_document.attachment_id.datas:
document_xml = zatca_document.attachment_id.with_context(bin_size=False).datas.decode()
root = etree.fromstring(b64decode(document_xml))
qr_node = root.xpath('//*[local-name()="ID"][text()="QR"]/following-sibling::*/*')[0]
move.l10n_sa_qr_code_str = qr_node.text
qr_code_str = qr_node.text
move.l10n_sa_qr_code_str = qr_code_str
else:
# In the case where the Invoice is not a ZATCA invoice, or is Phase 1, or is not confirmed,
# we call super to trigger the initial QR code generation for Phase 1
phase_one_moves |= move
super(AccountMove, phase_one_moves)._compute_qr_code_str()
def _l10n_sa_get_qr_code_encoding(self, tag, field, int_length=1):