From 81a68544b36d842063e060d1944ae2359ffa5e54 Mon Sep 17 00:00:00 2001
From: "Ricardo Gomes Rodrigues (rigr)" <rigr@odoo.com>
Date: Tue, 8 Nov 2022 12:43:31 +0000
Subject: [PATCH] [FIX] account_edi{,ubl_cii}: disable move_type check

Currently, if the user uploads a facturx document whose move_type does not match the move_type chosen by the user, the document is not auto filled with the information contained in the facturx XML because of a restrictive check. Therefore, the document is empty and is sent to the OCR which acts as a backup.

Starting from this commit, we will remove this restrictive check. So, if the user uploads a facturx document, we will use the move_type define by facturx regardless of the move_type chosen by the user. This means that when we upload a facturx document, we will always use all the information that is in the xml to create the document and avoid using the OCR.

task-id 2961932

closes odoo/odoo#105287

Signed-off-by: Laurent Smet <las@odoo.com>
---
 addons/account_edi/models/account_edi_format.py     |  8 ++------
 .../models/account_edi_common.py                    | 13 ++++++++++---
 .../l10n_account_edi_ubl_cii_tests/tests/common.py  |  2 +-
 3 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/addons/account_edi/models/account_edi_format.py b/addons/account_edi/models/account_edi_format.py
index 34e003bee22f..723c86a6d449 100644
--- a/addons/account_edi/models/account_edi_format.py
+++ b/addons/account_edi/models/account_edi_format.py
@@ -490,13 +490,9 @@ class AccountEdiFormat(models.Model):
                 res = False
                 try:
                     if file_data['type'] == 'xml':
-                        res = edi_format\
-                            .with_context(default_move_type=invoice.move_type)\
-                            ._update_invoice_from_xml_tree(file_data['filename'], file_data['xml_tree'], invoice)
+                        res = edi_format._update_invoice_from_xml_tree(file_data['filename'], file_data['xml_tree'], invoice)
                     elif file_data['type'] == 'pdf':
-                        res = edi_format\
-                            .with_context(default_move_type=invoice.move_type)\
-                            ._update_invoice_from_pdf_reader(file_data['filename'], file_data['pdf_reader'], invoice)
+                        res = edi_format._update_invoice_from_pdf_reader(file_data['filename'], file_data['pdf_reader'], invoice)
                         file_data['pdf_reader'].stream.close()
                     else:  # file_data['type'] == 'binary'
                         res = edi_format._update_invoice_from_binary(file_data['filename'], file_data['content'], file_data['extension'], invoice)
diff --git a/addons/account_edi_ubl_cii/models/account_edi_common.py b/addons/account_edi_ubl_cii/models/account_edi_common.py
index 3c359212f863..44e8442cc5a5 100644
--- a/addons/account_edi_ubl_cii/models/account_edi_common.py
+++ b/addons/account_edi_ubl_cii/models/account_edi_common.py
@@ -247,9 +247,16 @@ class AccountEdiCommon(models.AbstractModel):
     # -------------------------------------------------------------------------
 
     def _import_invoice(self, journal, filename, tree, existing_invoice=None):
-        move_types_allowed, qty_factor = self._get_import_document_amount_sign(filename, tree)
-        move_type = self._context.get('default_move_type', None)
-        if not move_type or move_type not in move_types_allowed or (existing_invoice and existing_invoice.move_type != move_type):
+        move_types, qty_factor = self._get_import_document_amount_sign(filename, tree)
+        if not move_types:
+            return
+        if journal.type == 'purchase':
+            move_type = move_types[0]
+        elif journal.type == 'sale':
+            move_type = move_types[1]
+        else:
+            return
+        if existing_invoice and existing_invoice.move_type != move_type:
             return
 
         invoice = existing_invoice or self.env['account.move']
diff --git a/addons/l10n_account_edi_ubl_cii_tests/tests/common.py b/addons/l10n_account_edi_ubl_cii_tests/tests/common.py
index 84bf371c8e46..fcd8da4675ed 100644
--- a/addons/l10n_account_edi_ubl_cii_tests/tests/common.py
+++ b/addons/l10n_account_edi_ubl_cii_tests/tests/common.py
@@ -71,7 +71,7 @@ class TestUBLCommon(AccountEdiTestCommon):
         Create an account.move directly from an xml file, asserts the invoice obtained is the same as the expected
         invoice.
         """
-        new_invoice = self.edi_format.with_context(default_move_type=invoice.move_type)._create_invoice_from_xml_tree(
+        new_invoice = self.edi_format._create_invoice_from_xml_tree(
             xml_filename,
             xml_etree,
             invoice.journal_id
-- 
GitLab