From 548d08bdc645c44dcdf0717ac9dd9b047a720017 Mon Sep 17 00:00:00 2001 From: "Benjamin Frantzen (bfr)" Date: Tue, 15 Sep 2020 11:46:41 +0000 Subject: [PATCH] [FIX] l10n_be_edi: pdf extracted from e-fff file have correct extension The text of the node 'cac:AdditionalDocumentReference/cbc:ID', which should contain the name of the pdf file in an e-fff document is incorrectly set by some software, and may contain the path to the file and/or the name of the xml instead of the pdf. Based on this information, we extract the stem only and append the .pdf extension to form a valid filename for the pdf. TASK ID: 2339062 X-original-commit: f998d96d68e2e153e6061a273eb65ec3699501e4 --- addons/account_edi_ubl/models/account_edi_format.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/addons/account_edi_ubl/models/account_edi_format.py b/addons/account_edi_ubl/models/account_edi_format.py index 8a7ea95e078..d2133261fe2 100644 --- a/addons/account_edi_ubl/models/account_edi_format.py +++ b/addons/account_edi_ubl/models/account_edi_format.py @@ -7,6 +7,8 @@ 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 @@ -99,12 +101,17 @@ class AccountEdiFormat(models.Model): attachment_data = element.xpath('cac:Attachment//cbc:EmbeddedDocumentBinaryObject', namespaces=namespaces) if attachment_name and attachment_data: text = attachment_data[0].text + # Normalize the name of the file : some e-fff emitters put the full path of the file + # (Windows or Linux style) and/or the name of the xml instead of the pdf. + # Get only the filename with a pdf extension. + name = PureWindowsPath(attachment_name[0].text).stem + '.pdf' attachments |= self.env['ir.attachment'].create({ - 'name': attachment_name[0].text, + 'name': name, 'res_id': invoice.id, 'res_model': 'account.move', 'datas': text + '=' * (len(text) % 3), # Fix incorrect padding 'type': 'binary', + 'mimetype': 'application/pdf', }) if attachments: invoice.with_context(no_new_invoice=True).message_post(attachment_ids=attachments.ids)