[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 <smetl@users.noreply.github.com>
Signed-off-by: bfr-o <bfr-o@users.noreply.github.com>
This commit is contained in:
Benjamin Frantzen (bfr)
2021-02-08 09:05:35 +00:00
parent 548d08bdc6
commit 64e0ee442f
6 changed files with 65 additions and 15 deletions
@@ -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
@@ -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)
+1
View File
@@ -2,3 +2,4 @@
from . import account_edi_format
from . import account_move
from . import ir_actions_report
@@ -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 version='1.0' encoding='UTF-8'?>"
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.
@@ -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
@@ -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 = '''
<cac:AdditionalDocumentReference
xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2">
<cbc:ID>%s</cbc:ID>
<cac:Attachment>
<cbc:EmbeddedDocumentBinaryObject mimeCode="application/pdf" filename=%s>
%s
</cbc:EmbeddedDocumentBinaryObject>
</cac:Attachment>
</cac:AdditionalDocumentReference>
''' % (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