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)