From b8e2ec85c7bc9f6951a9a995a5620bed9f0bbb70 Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Thu, 25 Jul 2024 14:38:19 +0200 Subject: [PATCH] [IMP] account_invoice_import: add support for discount Add support for discount on lines in the multi-line create scenario --- account_invoice_import/tests/test_invoice_import.py | 9 ++++++--- account_invoice_import/wizard/account_invoice_import.py | 8 +++++++- .../tests/test_invoice_import.py | 4 ++-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/account_invoice_import/tests/test_invoice_import.py b/account_invoice_import/tests/test_invoice_import.py index d38559b8cf..56087a7981 100644 --- a/account_invoice_import/tests/test_invoice_import.py +++ b/account_invoice_import/tests/test_invoice_import.py @@ -312,7 +312,8 @@ def test_import_out_invoice(self): "product": {"code": "AII-TEST-PRODUCT"}, "name": "Super product", "qty": 3, - "price_unit": 10.22, + "discount": 10, + "price_unit": 100, "date_start": "2017-08-01", "date_end": "2017-08-31", "taxes": [ @@ -334,8 +335,10 @@ def test_import_out_invoice(self): .with_company(self.company.id) .create_invoice(parsed_inv, import_config) ) - self.assertFalse(inv.currency_id.compare_amounts(inv.amount_untaxed, 30.66)) - self.assertFalse(inv.currency_id.compare_amounts(inv.amount_total, 30.97)) + self.assertFalse( + inv.currency_id.compare_amounts(inv.amount_untaxed, 270.00) + ) + self.assertFalse(inv.currency_id.compare_amounts(inv.amount_total, 272.70)) self.assertEqual( fields.Date.to_string(inv.invoice_date), parsed_inv["date"] ) diff --git a/account_invoice_import/wizard/account_invoice_import.py b/account_invoice_import/wizard/account_invoice_import.py index 9d1152c565..f62d0ebc15 100644 --- a/account_invoice_import/wizard/account_invoice_import.py +++ b/account_invoice_import/wizard/account_invoice_import.py @@ -164,8 +164,9 @@ def fallback_parse_pdf_invoice(self, file_data): # }, # 'name': 'Gelierzucker Extra 250g', # 'price_unit': 1.45, # price_unit without taxes + # 'discount': 10.0, # for 10% discount # 'qty': 2.0, - # 'price_subtotal': 2.90, # not required, but needed + # 'price_subtotal': 2.61, # not required, but needed # to be able to generate adjustment lines when decimal # precision is not high enough in Odoo # 'uom': {'unece_code': 'C62'}, @@ -424,6 +425,7 @@ def _prepare_line_vals_nline(self, partner, vals, parsed_inv, import_config): { "quantity": line["qty"], "price_unit": line["price_unit"], # TODO fix for tax incl + "discount": line.get("discount", 0), } ) if start_end_dates_installed: @@ -540,6 +542,7 @@ def pre_process_parsed_inv(self, parsed_inv): if not parsed_inv.get("currency_rounding"): self.get_precision_rounding_from_currency_helper(parsed_inv) prec_pp = self.env["decimal.precision"].precision_get("Product Price") + prec_disc = self.env["decimal.precision"].precision_get("Discount") prec_uom = self.env["decimal.precision"].precision_get( "Product Unit of Measure" ) @@ -583,6 +586,9 @@ def pre_process_parsed_inv(self, parsed_inv): line["price_unit"] = float_round( line["price_unit"], precision_digits=prec_pp ) + line["discount"] = float_round( + line.get("discount", 0), precision_digits=prec_disc + ) parsed_inv_for_log = dict(parsed_inv) if "attachments" in parsed_inv_for_log: parsed_inv_for_log.pop("attachments") diff --git a/account_invoice_import_invoice2data/tests/test_invoice_import.py b/account_invoice_import_invoice2data/tests/test_invoice_import.py index 90220ca42c..09ce1d90f5 100644 --- a/account_invoice_import_invoice2data/tests/test_invoice_import.py +++ b/account_invoice_import_invoice2data/tests/test_invoice_import.py @@ -174,7 +174,7 @@ def test_import_azure_interior_invoice(self): # self.assertEqual(inv.journal_id.payment_reference, "202309097001") # self.assertEqual(inv.journal_id.incoterm_id, self.env.ref("account.incoterm_DPU") - self.assertEqual(len(inv.invoice_line_ids), 8) + self.assertEqual(len(inv.invoice_line_ids), 7) iline = inv.invoice_line_ids[0] self.assertEqual(iline.name, "--- Non Food ---") self.assertEqual(iline.display_type, "line_section") @@ -205,9 +205,9 @@ def test_import_azure_interior_invoice(self): iline.product_id, self.env.ref("account_invoice_import_invoice2data.olive_oil"), ) - # todo test discount, not yet implemented in account_invoice_import self.assertEqual(float_compare(iline.quantity, 1.0, precision_digits=0), 0) self.assertEqual(float_compare(iline.price_unit, 1.00, precision_digits=2), 0) + self.assertEqual(float_compare(iline.discount, 10, precision_digits=2), 0) iline = inv.invoice_line_ids[5] self.assertEqual( iline.name, "Our Olive Oil is delivered in a re-usable glass container"