From 1775b2e59550dd9bcdd1d3457aeeb87f2863127a Mon Sep 17 00:00:00 2001 From: Adrien Minet Date: Wed, 9 Feb 2022 08:20:16 +0000 Subject: [PATCH] [FIX] account_factux: discount taken only for gross price MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit How to reproduce the bug: - Install the account app - Get a FacturX invoice with a discount on item - Upload the invoice - Check that the price of the discounted item takes the discount into account Bug: If you want to upload a FacturX invoice that contains a discount, Odoo will take the original price into account instead of the discounted one. opw-2705291 closes odoo/odoo#87509 X-original-commit: 43fb1c47b4867ac9b1fe7c3dd30e8bed7bd93f13 Signed-off-by: Adrien Minet Signed-off-by: William André (wan) --- .../models/account_edi_format.py | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/addons/account_edi_facturx/models/account_edi_format.py b/addons/account_edi_facturx/models/account_edi_format.py index 9c2992d5476..228fd1f5fcc 100644 --- a/addons/account_edi_facturx/models/account_edi_format.py +++ b/addons/account_edi_facturx/models/account_edi_format.py @@ -232,6 +232,24 @@ class AccountEdiFormat(models.Model): invoice_line_form.price_unit = float(line_elements[0].text) / float(quantity_elements[0].text) else: invoice_line_form.price_unit = float(line_elements[0].text) + # For Gross price, we need to check if a discount must be taken into account + discount_elements = element.xpath('.//ram:AppliedTradeAllowanceCharge', + namespaces=tree.nsmap) + if discount_elements: + discount_percent_elements = element.xpath( + './/ram:AppliedTradeAllowanceCharge/ram:CalculationPercent', namespaces=tree.nsmap) + if discount_percent_elements: + invoice_line_form.discount = float(discount_percent_elements[0].text) + else: + # if discount not available, it will be computed from the gross and net prices. + net_price_elements = element.xpath('.//ram:NetPriceProductTradePrice/ram:ChargeAmount', + namespaces=tree.nsmap) + if net_price_elements: + quantity_elements = element.xpath( + './/ram:NetPriceProductTradePrice/ram:BasisQuantity', namespaces=tree.nsmap) + net_unit_price = float(net_price_elements[0].text) / float(quantity_elements[0].text) \ + if quantity_elements else float(net_price_elements[0].text) + invoice_line_form.discount = (invoice_line_form.price_unit - net_unit_price) / invoice_line_form.price_unit * 100. else: line_elements = element.xpath('.//ram:NetPriceProductTradePrice/ram:ChargeAmount', namespaces=tree.nsmap) if line_elements: @@ -240,10 +258,6 @@ class AccountEdiFormat(models.Model): invoice_line_form.price_unit = float(line_elements[0].text) / float(quantity_elements[0].text) else: invoice_line_form.price_unit = float(line_elements[0].text) - # Discount. - line_elements = element.xpath('.//ram:AppliedTradeAllowanceCharge/ram:CalculationPercent', namespaces=tree.nsmap) - if line_elements: - invoice_line_form.discount = float(line_elements[0].text) # Taxes tax_element = element.xpath('.//ram:SpecifiedLineTradeSettlement/ram:ApplicableTradeTax/ram:RateApplicablePercent', namespaces=tree.nsmap)