From 60f6f7038f799cca28526c5ffcd6f8fdd7ecfab4 Mon Sep 17 00:00:00 2001 From: Julien Van Roy Date: Fri, 19 Aug 2022 08:15:25 +0000 Subject: [PATCH] [FIX] account_edi: send & print - prevent duplicating attachments Currently, the EDI attachments *content* is passed to the Send & Print wizard. Then, the wizard re-creates these attachments and link them to itself. At the end of the flow, when clicking 'Send & Print' button on the 'Send & Print' wizard, the attachments of the wizard are copied on the move. Thus, the EDI attachments are duplicated. Passing the ids of the EDI attachments rather than the content solved the issue, since they are simply linked to the wizard, without being re-created. task-2957823 closes odoo/odoo#98447 Related: odoo/enterprise#30596 Signed-off-by: Laurent Smet --- addons/account_edi/models/mail_template.py | 22 ++++++++++++++----- addons/account_edi_ubl_cii/models/__init__.py | 1 + .../models/mail_template.py | 14 ++++++++++++ addons/l10n_it_edi/models/mail_template.py | 2 +- 4 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 addons/account_edi_ubl_cii/models/mail_template.py diff --git a/addons/account_edi/models/mail_template.py b/addons/account_edi/models/mail_template.py index 3e3c07dbe59..37fbf401e83 100644 --- a/addons/account_edi/models/mail_template.py +++ b/addons/account_edi/models/mail_template.py @@ -8,14 +8,24 @@ class MailTemplate(models.Model): def _get_edi_attachments(self, document): """ - Will return the information about the attachment of the edi document for adding the attachment in the mail. + Will either return the information about the attachment of the edi document for adding the attachment in the + mail, or the attachment id to be linked to the 'send & print' wizard. Can be overridden where e.g. a zip-file needs to be sent with the individual files instead of the entire zip + IMPORTANT: + * If the attachment's id is returned, no new attachment will be created, the existing one on the move is linked + to the wizard (see _onchange_template_id in mail.compose.message). + * If the attachment's content is returned, a new one is created and linked to the wizard. Thus, when sending + the mail (clicking on 'send & print' in the wizard), a new attachment is added to the move (see + _action_send_mail in mail.compose.message). :param document: an edi document - :return: list with a tuple with the name and base64 content of the attachment + :return: dict: + {'attachments': tuple with the name and base64 content of the attachment} + OR + {'attachment_ids': list containing the id of the attachment} """ if not document.attachment_id: - return [] - return [(document.attachment_id.name, document.attachment_id.datas)] + return {} + return {'attachment_ids': [document.attachment_id.id]} def generate_email(self, res_ids, fields): res = super().generate_email(res_ids, fields) @@ -33,6 +43,8 @@ class MailTemplate(models.Model): record_data = (res[record.id] if multi_mode else res) for doc in record.edi_document_ids: record_data.setdefault('attachments', []) - record_data['attachments'] += self._get_edi_attachments(doc) + attachments = self._get_edi_attachments(doc) + record_data['attachment_ids'] += attachments.get('attachment_ids', []) + record_data['attachments'] += attachments.get('attachments', []) return res diff --git a/addons/account_edi_ubl_cii/models/__init__.py b/addons/account_edi_ubl_cii/models/__init__.py index fab68bf1e70..67afac14748 100644 --- a/addons/account_edi_ubl_cii/models/__init__.py +++ b/addons/account_edi_ubl_cii/models/__init__.py @@ -10,3 +10,4 @@ from . import account_edi_xml_ubl_xrechnung from . import account_edi_xml_ubl_nlcius from . import account_edi_xml_ubl_efff from . import ir_actions_report +from . import mail_template diff --git a/addons/account_edi_ubl_cii/models/mail_template.py b/addons/account_edi_ubl_cii/models/mail_template.py new file mode 100644 index 00000000000..43dd4f3d08e --- /dev/null +++ b/addons/account_edi_ubl_cii/models/mail_template.py @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -*- + +from odoo import models + + +class MailTemplate(models.Model): + _inherit = "mail.template" + + def _get_edi_attachments(self, document): + # EXTENDS account_edi + # Avoid having factur-x.xml in the 'send & print' wizard + if document.edi_format_id.code == 'facturx_1_0_05': + return {} + return super()._get_edi_attachments(document) diff --git a/addons/l10n_it_edi/models/mail_template.py b/addons/l10n_it_edi/models/mail_template.py index 1bd7999a07d..a41d9524233 100644 --- a/addons/l10n_it_edi/models/mail_template.py +++ b/addons/l10n_it_edi/models/mail_template.py @@ -14,5 +14,5 @@ class MailTemplate(models.Model): :return: list with a tuple with the name and base64 content of the attachment """ if document.edi_format_id.code == 'fattura_pa': - return [] + return {} return super()._get_edi_attachments(document)