diff --git a/addons/l10n_sa_edi/models/account_edi_format.py b/addons/l10n_sa_edi/models/account_edi_format.py index 239ae968790..24b0b20fe92 100644 --- a/addons/l10n_sa_edi/models/account_edi_format.py +++ b/addons/l10n_sa_edi/models/account_edi_format.py @@ -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, diff --git a/addons/l10n_sa_edi/models/account_move.py b/addons/l10n_sa_edi/models/account_move.py index ba2bc44d7ae..aba3bbdeb3c 100644 --- a/addons/l10n_sa_edi/models/account_move.py +++ b/addons/l10n_sa_edi/models/account_move.py @@ -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):