Skip to content

Commit

Permalink
[IMP] account_invoice_mass_sending: Add EDI documents to mass sending…
Browse files Browse the repository at this point in the history
… email
  • Loading branch information
sergiobstoj committed Jan 8, 2025
1 parent 24d2441 commit 2e4c94a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
14 changes: 13 additions & 1 deletion account_invoice_mass_sending/models/account_move.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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()
Expand All @@ -67,3 +68,14 @@ 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:
if self._get_edi_attachment(edi.edi_format_id):
attachment_ids.append(
self._get_edi_attachment(edi.edi_format_id).id
)
return attachment_ids
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 2e4c94a

Please sign in to comment.