From 631cee1c3ac789c61f9600433b92854d2843a605 Mon Sep 17 00:00:00 2001 From: Pablo Castelo Date: Fri, 9 Oct 2026 10:09:30 +0200 Subject: [PATCH] [IMP] base_business_document_import: set imported PDF as main attachment Attachments created by post_create_or_update() were not registered as main attachment, so the 'Original Bills' report could not find them on imported invoices (e.g. account_invoice_download_ovh). Use _message_set_main_attachment_id() so PDF is preferred over XML, as message_post() does. --- .../models/business_document_import.py | 8 ++++++- .../tests/test_business_document_import.py | 22 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/base_business_document_import/models/business_document_import.py b/base_business_document_import/models/business_document_import.py index 8e8c3c3570..85a438f4b7 100644 --- a/base_business_document_import/models/business_document_import.py +++ b/base_business_document_import/models/business_document_import.py @@ -1506,8 +1506,9 @@ def get_xml_files_from_pdf(self, pdf_file): @api.model def post_create_or_update(self, parsed_dict, record, doc_filename=None): if parsed_dict.get("attachments"): + attachments = self.env["ir.attachment"] for filename, data_base64 in parsed_dict["attachments"].items(): - self.env["ir.attachment"].create( + attachments |= self.env["ir.attachment"].create( { "name": filename, "res_id": record.id, @@ -1515,6 +1516,11 @@ def post_create_or_update(self, parsed_dict, record, doc_filename=None): "datas": data_base64, } ) + + if hasattr(record, "_message_set_main_attachment_id"): + record.sudo()._message_set_main_attachment_id( + [(4, attach.id) for attach in attachments] + ) chatter_msg_html = [] for msg in parsed_dict["chatter_msg"]: chatter_msg_html.append(msg.replace("\n", "
")) diff --git a/base_business_document_import/tests/test_business_document_import.py b/base_business_document_import/tests/test_business_document_import.py index 9cf36750c9..0b66a1ba7d 100644 --- a/base_business_document_import/tests/test_business_document_import.py +++ b/base_business_document_import/tests/test_business_document_import.py @@ -3,6 +3,7 @@ # @author: Alexis de Lattre # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +import base64 import logging from odoo.exceptions import UserError @@ -481,3 +482,24 @@ def test_match_account_smaller_in(self): res = bdio._match_account({"code": "898999"}, chatter) self.assertEqual(acc, res) self.assertEqual(len(chatter), 1) + + def test_post_create_or_update_main_attachment(self): + partner = self.env["res.partner"].create({"name": "Main attachment test"}) + xml_b64 = base64.b64encode(b"") + pdf_b64 = base64.b64encode(b"%PDF-1.4\n%%EOF\n") + parsed_dict = { + "attachments": {"invoice.xml": xml_b64, "invoice.pdf": pdf_b64}, + "chatter_msg": [], + } + self.env["business.document.import"].post_create_or_update(parsed_dict, partner) + self.assertEqual(partner.message_main_attachment_id.name, "invoice.pdf") + + def test_post_create_or_update_main_attachment_xml_only(self): + partner = self.env["res.partner"].create({"name": "Main attachment test"}) + xml_b64 = base64.b64encode(b"") + parsed_dict = { + "attachments": {"invoice.xml": xml_b64}, + "chatter_msg": [], + } + self.env["business.document.import"].post_create_or_update(parsed_dict, partner) + self.assertFalse(partner.message_main_attachment_id)