From 47905d21ab65f985291d78cb7ecb78f2f7cdcf67 Mon Sep 17 00:00:00 2001
From: Julien Van Roy <juvr@odoo.com>
Date: Fri, 28 Apr 2023 11:15:05 +0000
Subject: [PATCH] [FIX] account_edi_ubl_cii: handle paid invoice at import

The edi attachment (for every format in `account_edi_ubl_cii`) contains
a tag indicating whether the invoice is fully/partially paid.

Before this fix, when this tag was filled, a down payment section was
created on the invoice. For a fully paid invoice, the resulting amount
was then 0, so the invoice was marked as "Paid".

This was wrong. The correct amounts need to be kept on the invoices.
This fix no longer creates a down payment, instead a message is logged
in the chatter.

task-3264843
opw-3248200

closes odoo/odoo#118610

Signed-off-by: Laurent Smet <las@odoo.com>
---
 .../i18n/account_edi_ubl_cii.pot                  |  6 ++++++
 .../models/account_edi_common.py                  | 15 +++++++++++++++
 .../models/account_edi_xml_cii_facturx.py         |  4 ++--
 .../models/account_edi_xml_ubl_20.py              |  4 ++--
 4 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/addons/account_edi_ubl_cii/i18n/account_edi_ubl_cii.pot b/addons/account_edi_ubl_cii/i18n/account_edi_ubl_cii.pot
index 8fa0194b0f06..2064b37c2cc5 100644
--- a/addons/account_edi_ubl_cii/i18n/account_edi_ubl_cii.pot
+++ b/addons/account_edi_ubl_cii/i18n/account_edi_ubl_cii.pot
@@ -43,6 +43,12 @@ msgid ""
 "<strong>Format used to import the invoice: %s</strong> <p><li> %s </li></p>"
 msgstr ""
 
+#. module: account_edi_ubl_cii
+#: code:addons/account_edi_ubl_cii/models/account_edi_common.py:0
+#, python-format
+msgid "A payment of %s was detected."
+msgstr ""
+
 #. module: account_edi_ubl_cii
 #: code:addons/account_edi_ubl_cii/models/account_edi_common.py:0
 #, python-format
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 3591f5456942..81915b6db80a 100644
--- a/addons/account_edi_ubl_cii/models/account_edi_common.py
+++ b/addons/account_edi_ubl_cii/models/account_edi_common.py
@@ -5,6 +5,7 @@ from odoo.tools import float_repr
 from odoo.tests.common import Form
 from odoo.exceptions import UserError, ValidationError
 from odoo.tools.float_utils import float_round
+from odoo.tools.misc import formatLang
 
 from zeep import Client
 
@@ -396,6 +397,7 @@ class AccountEdiCommon(models.AbstractModel):
 
     def _import_fill_invoice_down_payment(self, invoice_form, prepaid_node, qty_factor):
         """
+        DEPRECATED: removed in master
         Creates a down payment line on the invoice at import if prepaid_node (TotalPrepaidAmount in CII,
         PrepaidAmount in UBL) exists.
         qty_factor -1 if the xml is labelled as an invoice but has negative amounts -> conversion into a credit note
@@ -418,6 +420,19 @@ class AccountEdiCommon(models.AbstractModel):
                 invoice_line_form.quantity = qty_factor * -1
                 invoice_line_form.tax_ids.clear()
 
+    def _import_log_prepaid_amount(self, invoice_form, prepaid_node, qty_factor):
+        """
+        Log a message in the chatter at import if prepaid_node (TotalPrepaidAmount in CII, PrepaidAmount in UBL) exists.
+        """
+        prepaid_amount = float(prepaid_node.text) if prepaid_node is not None else 0.0
+        if not invoice_form.currency_id.is_zero(prepaid_amount):
+            amount = prepaid_amount * qty_factor
+            formatted_amount = formatLang(self.env, amount, currency_obj=invoice_form.currency_id)
+            return [
+                _("A payment of %s was detected.", formatted_amount)
+            ]
+        return []
+
     def _import_fill_invoice_line_values(self, tree, xpath_dict, invoice_line_form, qty_factor):
         """
         Read the xml invoice, extract the invoice line values, compute the odoo values
diff --git a/addons/account_edi_ubl_cii/models/account_edi_xml_cii_facturx.py b/addons/account_edi_ubl_cii/models/account_edi_xml_cii_facturx.py
index 83d22be6489c..1ff7e11ab876 100644
--- a/addons/account_edi_ubl_cii/models/account_edi_xml_cii_facturx.py
+++ b/addons/account_edi_ubl_cii/models/account_edi_xml_cii_facturx.py
@@ -287,11 +287,11 @@ class AccountEdiXmlCII(models.AbstractModel):
 
         logs += self._import_fill_invoice_allowance_charge(tree, invoice_form, journal, qty_factor)
 
-        # ==== Down Payment (prepaid amount) ====
+        # ==== Prepaid amount ====
 
         prepaid_node = tree.find('.//{*}ApplicableHeaderTradeSettlement/'
                                  '{*}SpecifiedTradeSettlementHeaderMonetarySummation/{*}TotalPrepaidAmount')
-        self._import_fill_invoice_down_payment(invoice_form, prepaid_node, qty_factor)
+        logs += self._import_log_prepaid_amount(invoice_form, prepaid_node, qty_factor)
 
         # ==== invoice_line_ids ====
 
diff --git a/addons/account_edi_ubl_cii/models/account_edi_xml_ubl_20.py b/addons/account_edi_ubl_cii/models/account_edi_xml_ubl_20.py
index df5003d06494..213b2e7c7ed8 100644
--- a/addons/account_edi_ubl_cii/models/account_edi_xml_ubl_20.py
+++ b/addons/account_edi_ubl_cii/models/account_edi_xml_ubl_20.py
@@ -554,10 +554,10 @@ class AccountEdiXmlUBL20(models.AbstractModel):
 
         logs += self._import_fill_invoice_allowance_charge(tree, invoice_form, journal, qty_factor)
 
-        # ==== Down Payment (prepaid amount) ====
+        # ==== Prepaid amount ====
 
         prepaid_node = tree.find('./{*}LegalMonetaryTotal/{*}PrepaidAmount')
-        self._import_fill_invoice_down_payment(invoice_form, prepaid_node, qty_factor)
+        logs += self._import_log_prepaid_amount(invoice_form, prepaid_node, qty_factor)
 
         # ==== invoice_line_ids: InvoiceLine/CreditNoteLine ====
 
-- 
GitLab