From 425bf0bc6f4e80666c9df7ffeb7cafebce4f00a0 Mon Sep 17 00:00:00 2001 From: Sergio Bustamante Date: Wed, 8 Jan 2025 11:11:33 +0100 Subject: [PATCH] [IMP] account_invoice_mass_sending: Add EDI documents to mass sending email --- .../models/account_move.py | 13 ++++- .../test_account_invoice_mass_sending.py | 54 +++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/account_invoice_mass_sending/models/account_move.py b/account_invoice_mass_sending/models/account_move.py index cabe10d6aa7..7e81f08d4c7 100644 --- a/account_invoice_mass_sending/models/account_move.py +++ b/account_invoice_mass_sending/models/account_move.py @@ -1,7 +1,7 @@ # Copyright 2019 ACSONE SA/NV # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo import _, fields, models +from odoo import Command, _, fields, models class AccountInvoice(models.Model): @@ -58,6 +58,7 @@ def _send_invoice_individually(self, template=None): "is_email": True, "template_id": template.id, "composition_mode": "comment", + "attachment_ids": [Command.set(self._get_edi_documents())], } ) wiz.onchange_template_id() @@ -67,3 +68,13 @@ def _send_invoice_individually(self, template=None): } ) return wiz.send_and_print_action() + + def _get_edi_documents(self): + self.ensure_one() + attachment_ids = [] + if getattr(self, "edi_document_ids", False): + for edi in self.edi_document_ids: + attachment = self._get_edi_attachment(edi.edi_format_id) + if attachment: + attachment_ids.append(attachment.id) + return attachment_ids diff --git a/account_invoice_mass_sending/tests/test_account_invoice_mass_sending.py b/account_invoice_mass_sending/tests/test_account_invoice_mass_sending.py index 26e39739aab..6c5505e6fdc 100644 --- a/account_invoice_mass_sending/tests/test_account_invoice_mass_sending.py +++ b/account_invoice_mass_sending/tests/test_account_invoice_mass_sending.py @@ -161,3 +161,57 @@ def test_invoice_mass_sending_3(self): ) trap.perform_enqueued_jobs() self.assertFalse(self.first_eligible_invoice.sending_in_progress) + + def _create_attachment(self, invoice): + document = self.env["ir.attachment"].create( + { + "name": "EDI Document", + "res_model": "account.move", + "res_id": invoice.id, + "mimetype": "application/xml", + } + ) + return document + + def create_edi_document(self, state, move=None, move_type=None): + edi_format = ( + self.env["account.edi.format"] + .sudo() + .create( + { + "name": "test_edi_format", + "code": "test_edi_format", + } + ) + ) + return self.env["account.edi.document"].create( + {"edi_format_id": edi_format.id, "move_id": move.id, "state": state} + ) + + def test_invoice_mass_sending_4(self): + # test one invoice to send with edi document attached + invoice = self.first_eligible_invoice + attachment = self._create_attachment(invoice) + edi_document = self.create_edi_document("sent", invoice, invoice.move_type) + edi_document.attachment_id = attachment.id + self.assertEqual(len(invoice.edi_document_ids), 1) + self.assertEqual(len(invoice.attachment_ids), 1) + with trap_jobs() as trap: + wizard = self.wizard_obj.with_context( + active_ids=invoice.ids, + active_model=self.first_eligible_invoice._name, + discard_logo_check=True, + ).create({}) + wizard.enqueue_invoices() + trap.assert_jobs_count(1) + trap.assert_enqueued_job( + invoice._send_invoice_individually, + kwargs={"template": self.mail_template_obj}, + ) + trap.perform_enqueued_jobs() + mail = self.env["mail.mail"].search( + [("model", "=", "account.move"), ("res_id", "=", invoice.id)] + ) + self.assertTrue(mail) + attachment_ids = mail.attachment_ids.ids + self.assertIn(attachment_ids[0], invoice.attachment_ids.ids)