From 9d3c5523ddb959d3dd34de0ffa2bc2371f2de109 Mon Sep 17 00:00:00 2001 From: Michael Tietz Date: Tue, 12 May 2026 08:50:56 +0200 Subject: [PATCH] [IMP] account_invoice_import: Make import compatible with odoo default behavior --- .../models/account_journal.py | 57 +++++++++++++++---- account_invoice_import/readme/CONTRIBUTORS.md | 1 + 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/account_invoice_import/models/account_journal.py b/account_invoice_import/models/account_journal.py index 3d7a77e9ab..0fceb41910 100644 --- a/account_invoice_import/models/account_journal.py +++ b/account_invoice_import/models/account_journal.py @@ -1,4 +1,5 @@ # Copyright 2025 Akretion France (https://www.akretion.com/) +# Copyright 2026 Michael Tietz (MT Software) # @author: Alexis de Lattre # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). @@ -11,15 +12,49 @@ class AccountJournal(models.Model): def create_document_from_attachment(self, attachment_ids): """Inherit native method used when clicking on the 'Upload' button from the Vendor Bill tree view""" - if self: - company_id = self.company_id.id - else: - company_id = self.env.company.id - wiz = self.env["account.invoice.import"].create( - { - "company_id": company_id, - "invoice_attachment_ids": [Command.set(attachment_ids)], - } - ) - action = wiz.import_invoices() + MOVE = self.env["account.move"] + attachment_ids_by_odoo = [] + attachment_ids_by_oca = [] + attachments = self.env["ir.attachment"].browse(attachment_ids) + for attachment in attachments: + to_process = attachment._unwrap_edi_attachments() + decoder = False + for file_data in to_process: + decoder = MOVE._get_edi_decoder(file_data) + if decoder: + attachment_ids_by_odoo.append(attachment.id) + break + if not decoder: + attachment_ids_by_oca.append(attachment.id) + move_ids = [] + if attachment_ids_by_odoo: + res = super().create_document_from_attachment(attachment_ids_by_odoo) + if not attachment_ids_by_oca: + return res + move_ids = res["domain"][0][2] + if attachment_ids_by_oca: + if self: + company_id = self.company_id.id + else: + company_id = self.env.company.id + wiz = self.env["account.invoice.import"].create( + { + "company_id": company_id, + "invoice_attachment_ids": [Command.set(attachment_ids)], + } + ) + action = wiz.import_invoices() + if not attachment_ids_by_odoo: + return action + next_action = action["params"]["next"] + move_id = next_action.get("res_id") + if move_id: + move_ids.append(move_id) + else: + move_ids += next_action["domain"][0][2] + new_next_action = self.env["ir.actions.actions"]._for_xml_id( + "account.action_move_in_invoice_type" + ) + new_next_action["domain"] = [("id", "in", move_ids)] + action["params"]["next"] = new_next_action return action diff --git a/account_invoice_import/readme/CONTRIBUTORS.md b/account_invoice_import/readme/CONTRIBUTORS.md index f096d21025..779cd3578c 100644 --- a/account_invoice_import/readme/CONTRIBUTORS.md +++ b/account_invoice_import/readme/CONTRIBUTORS.md @@ -4,3 +4,4 @@ - Yannick Vaucher \<\> - Ronald Portier \<\> - Simone Orsi \<\> +- Michael Tietz (MT Software) \<\>