diff --git a/addons/sale/models/sale.py b/addons/sale/models/sale.py index 0836ec09673..69600688519 100644 --- a/addons/sale/models/sale.py +++ b/addons/sale/models/sale.py @@ -1515,7 +1515,20 @@ class SaleOrderLine(models.Model): # `price_reduce_taxexcl` cannot be used as it is computed from `price_subtotal` field. (see upper Note) price_subtotal = line.tax_id.compute_all(price_subtotal)['total_excluded'] - amount_to_invoice = price_subtotal - line.untaxed_amount_invoiced + if any(line.invoice_lines.mapped(lambda l: l.discount != line.discount)): + # In case of re-invoicing with different discount we try to calculate manually the + # remaining amount to invoice + amount = 0 + for l in line.invoice_lines: + if len(l.tax_ids.filtered(lambda tax: tax.price_include)) > 0: + amount += l.tax_ids.compute_all(l.currency_id._convert(l.price_unit, line.currency_id, line.company_id, l.date or fields.Date.today(), round=False) * l.quantity)['total_excluded'] + else: + amount += l.currency_id._convert(l.price_unit, line.currency_id, line.company_id, l.date or fields.Date.today(), round=False) * l.quantity + + amount_to_invoice = max(price_subtotal - amount, 0) + else: + amount_to_invoice = price_subtotal - line.untaxed_amount_invoiced + line.untaxed_amount_to_invoice = amount_to_invoice def _prepare_invoice_line(self, **optional_values):