From 0ffd8d331faf5f5eb1a102042ed1173cd36e44c0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nshimiyimana=20S=C3=A9na?= <sesn@odoo.com>
Date: Thu, 11 May 2023 08:02:20 +0000
Subject: [PATCH] [FIX] l10n_it_edi: receive bill that has a document discount

Bug:
Currently, trying to import a fatturapa XML that has a document discount
doesn't do anything.

Setup:
- install l10n_it_edi and account_accountant
- have a fatturapa XML that has a document discount

Steps to reproduce:
- switch to the Italian company ()
- make sure that the VAT number on the document matches the one on the
  company
- attempt to upload the XML bill

You should be met with an empty vendor bill page.

Cause:
The issue comes from here: https://github.com/odoo/odoo/blob/43c9820b1d3020d89b1b7ca016754e29d1fc6b58/addons/l10n_it_edi/models/account_edi_format.py#LL725C21-L729C83
in 16.0, `invoice_form` is not an instance of `Form`, but a record.

opw-3193634

closes odoo/odoo#121010

Signed-off-by: Josse Colpaert <jco@odoo.com>
---
 .../l10n_it_edi/models/account_edi_format.py   | 12 ++++++------
 addons/l10n_it_edi/tests/test_edi_import.py    | 18 ++++++++++++++++++
 2 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/addons/l10n_it_edi/models/account_edi_format.py b/addons/l10n_it_edi/models/account_edi_format.py
index 49e705dfcc52..7bdc07fcc666 100644
--- a/addons/l10n_it_edi/models/account_edi_format.py
+++ b/addons/l10n_it_edi/models/account_edi_format.py
@@ -1,7 +1,7 @@
 # -*- coding:utf-8 -*-
 # Part of Odoo. See LICENSE file for full copyright and licensing details.
 
-from odoo import api, models, fields, _, _lt
+from odoo import Command, api, models, fields, _, _lt
 from odoo.exceptions import UserError
 from odoo.addons.account_edi_proxy_client.models.account_edi_proxy_user import AccountEdiProxyError
 from odoo.addons.l10n_it_edi.tools.remove_signature import remove_signature
@@ -722,11 +722,11 @@ class AccountEdiFormat(models.Model):
                     general_discount = discounted_amount - taxable_amount
                     sequence = len(elements) + 1
 
-                    with invoice_form.invoice_line_ids.new() as invoice_line_global_discount:
-                        invoice_line_global_discount.tax_ids.clear()
-                        invoice_line_global_discount.sequence = sequence
-                        invoice_line_global_discount.name = 'SCONTO' if general_discount < 0 else 'MAGGIORAZIONE'
-                        invoice_line_global_discount.price_unit = general_discount
+                    invoice_form.invoice_line_ids = [Command.create({
+                        'sequence': sequence,
+                        'name': 'SCONTO' if general_discount < 0 else 'MAGGIORAZIONE',
+                        'price_unit': general_discount,
+                    })]
 
             new_invoice = invoice_form
 
diff --git a/addons/l10n_it_edi/tests/test_edi_import.py b/addons/l10n_it_edi/tests/test_edi_import.py
index 5ef4727f8264..6fb77c310ef7 100644
--- a/addons/l10n_it_edi/tests/test_edi_import.py
+++ b/addons/l10n_it_edi/tests/test_edi_import.py
@@ -129,3 +129,21 @@ class TestItEdiImport(TestItEdi):
         self.assertEqual(len(attachments), 1)
         invoices = self.env['account.move'].search([('payment_reference', '=', 'TWICE_TEST')])
         self.assertEqual(len(invoices), 1)
+
+    def test_receive_bill_with_global_discount(self):
+        content = self.with_applied_xpath(
+            etree.fromstring(self.invoice_content),
+            '''
+                <xpath expr="//FatturaElettronicaBody/DatiGenerali/DatiGeneraliDocumento" position="inside">
+                    <ScontoMaggiorazione>
+                        <Tipo>SC</Tipo>
+                        <Importo>10</Importo>
+                    </ScontoMaggiorazione>
+                </xpath>
+            ''')
+        invoices = self.edi_format._create_invoice_from_xml_tree(self.invoice_filename2, content)
+
+        discount_line = invoices.invoice_line_ids.filtered(lambda line: line.name == 'SCONTO')
+        self.assertRecordValues(discount_line, [{
+            'price_unit': -10,
+        }])
-- 
GitLab