From 64e0ee442feaca4a636ff37e0735dff366f84b88 Mon Sep 17 00:00:00 2001 From: "Benjamin Frantzen (bfr)" Date: Wed, 16 Sep 2020 12:31:09 +0000 Subject: [PATCH] [FIX] l10n_be_edi: added the pdf report inside the xml as a base64 string This is a requirement for the e-fff format. TASK ID: 2339062 Ticket ID: 2394614 closes odoo/odoo#65683 X-original-commit: 5e16044b8d5194f9ce3aa8d43a9923896b8abfdf Signed-off-by: Laurent Smet Signed-off-by: bfr-o --- .../models/account_edi_format.py | 2 - addons/account_edi_ubl/models/account_move.py | 2 + addons/l10n_be_edi/models/__init__.py | 1 + .../l10n_be_edi/models/account_edi_format.py | 16 ++---- addons/l10n_be_edi/models/account_move.py | 6 +++ .../l10n_be_edi/models/ir_actions_report.py | 53 +++++++++++++++++++ 6 files changed, 65 insertions(+), 15 deletions(-) create mode 100644 addons/l10n_be_edi/models/ir_actions_report.py diff --git a/addons/account_edi_ubl/models/account_edi_format.py b/addons/account_edi_ubl/models/account_edi_format.py index d2133261fe2..8e0e8661538 100644 --- a/addons/account_edi_ubl/models/account_edi_format.py +++ b/addons/account_edi_ubl/models/account_edi_format.py @@ -6,9 +6,7 @@ from odoo.tests.common import Form from odoo.exceptions import UserError from odoo.osv import expression -from datetime import datetime from pathlib import PureWindowsPath -from xml.sax.saxutils import unescape import logging diff --git a/addons/account_edi_ubl/models/account_move.py b/addons/account_edi_ubl/models/account_move.py index ce845c29028..670339b58b4 100644 --- a/addons/account_edi_ubl/models/account_move.py +++ b/addons/account_edi_ubl/models/account_move.py @@ -8,6 +8,8 @@ class AccountMove(models.Model): _inherit = 'account.move' def _get_ubl_values(self): + self.ensure_one() + def format_monetary(amount): # Format the monetary values to avoid trailing decimals (e.g. 90.85000000000001). return float_repr(amount, self.currency_id.decimal_places) diff --git a/addons/l10n_be_edi/models/__init__.py b/addons/l10n_be_edi/models/__init__.py index 047fc3ef4d9..ae085e47aae 100644 --- a/addons/l10n_be_edi/models/__init__.py +++ b/addons/l10n_be_edi/models/__init__.py @@ -2,3 +2,4 @@ from . import account_edi_format from . import account_move +from . import ir_actions_report diff --git a/addons/l10n_be_edi/models/account_edi_format.py b/addons/l10n_be_edi/models/account_edi_format.py index 2c34d7f5fd6..25baffdcb9d 100644 --- a/addons/l10n_be_edi/models/account_edi_format.py +++ b/addons/l10n_be_edi/models/account_edi_format.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- -from odoo import api, models, fields -from odoo.tools import float_repr +from odoo import models import base64 @@ -46,18 +45,9 @@ class AccountEdiFormat(models.Model): # Create file content. xml_content = b"" xml_content += self.env.ref('account_edi_ubl.export_ubl_invoice')._render(invoice._get_ubl_values()) - vat = invoice.company_id.partner_id.commercial_partner_id.vat - xml_name = 'efff-%s%s%s.xml' % (vat or '', '-' if vat else '', invoice.name.replace('/', '_')) # official naming convention + xml_name = '%s.xml' % invoice._get_efff_name() return self.env['ir.attachment'].create({ 'name': xml_name, 'datas': base64.encodebytes(xml_content), - 'res_model': 'account.move', - 'res_id': invoice._origin.id, - 'mimetype': 'application/xml' + 'mimetype': 'application/xml', }) - - def _is_embedding_to_invoice_pdf_needed(self): - self.ensure_one() - if self.code != 'efff_1': - return super()._is_embedding_to_invoice_pdf_needed() - return False # ubl must not be embedded to PDF. diff --git a/addons/l10n_be_edi/models/account_move.py b/addons/l10n_be_edi/models/account_move.py index 50ce265a54a..de8ececd8c1 100644 --- a/addons/l10n_be_edi/models/account_move.py +++ b/addons/l10n_be_edi/models/account_move.py @@ -2,6 +2,7 @@ from odoo import models +import re class AccountMove(models.Model): _inherit = 'account.move' @@ -14,3 +15,8 @@ class AccountMove(models.Model): values['ubl_version'] = 2.0 return values + + def _get_efff_name(self): + self.ensure_one() + vat = self.company_id.partner_id.commercial_partner_id.vat + return 'efff_%s%s%s' % (vat or '', '_' if vat else '', re.sub(r'[\W_]', '', self.name)) # official naming convention diff --git a/addons/l10n_be_edi/models/ir_actions_report.py b/addons/l10n_be_edi/models/ir_actions_report.py new file mode 100644 index 00000000000..b8ff38642dd --- /dev/null +++ b/addons/l10n_be_edi/models/ir_actions_report.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import models + +from lxml import etree +import base64 +from xml.sax.saxutils import escape, quoteattr + + +class IrActionsReport(models.Model): + _inherit = 'ir.actions.report' + + def _postprocess_pdf_report(self, record, buffer): + '''Add the pdf report in the e-fff XML as base64 string. + ''' + result = super()._postprocess_pdf_report(record, buffer) + + if record._name == 'account.move': + edi_document = record.edi_document_ids.filtered(lambda d: d.edi_format_id.code == 'efff_1') + if edi_document: + edi_attachment = edi_document[0].attachment_id + old_xml = base64.b64decode(edi_attachment.with_context(bin_size=False).datas, validate=True) + tree = etree.fromstring(old_xml) + document_currency_code_elements = tree.xpath("//*[local-name()='DocumentCurrencyCode']") + additional_document_elements = tree.xpath("//*[local-name()='AdditionalDocumentReference']") + if document_currency_code_elements and not additional_document_elements: + pdf = base64.b64encode(buffer.getvalue()).decode() + pdf_name = '%s.pdf' % record._get_efff_name() + to_inject = ''' + + %s + + + %s + + + + ''' % (escape(pdf_name), quoteattr(pdf_name), pdf) + + document_currency_code_elements[0].addnext(etree.fromstring(to_inject)) + new_xml = etree.tostring(tree, pretty_print=True) + edi_attachment.write({ + 'res_model': 'account.move', + 'res_id': record.id, + 'datas': base64.b64encode(new_xml), + 'mimetype': 'application/xml', + }) + + return result