[FIX] sale: calculate untaxed_amount_to_invoice based on invoiced quantity

Enable discounts on sales order lines
Create a service type product with invoicing based on timesheet and
task in sales order's project
Create so for 6 hours and deliver 8 hours.
Create 2 invoices: One for 6 hours with the regular price and 2 hours
h 100% discount.
Invoice status of the order will be Fully Invoiced

Go to Sales>Reporting>Sales Analysis report will show the untaxed amount
associated with 2nd invoice as still to invoice which is incorrect

This occur because when saving the invoice the untaxed amount is
calculated on the amount invoice after discount

opw-2338564

closes odoo/odoo#59638

X-original-commit: be5ec58d0a1a71fc84ca790555cac02a6be5b67a
Signed-off-by: agr-odoo <agr-odoo@users.noreply.github.com>
Signed-off-by: Nicolas Martinelli (nim) <nim@odoo.com>
This commit is contained in:
Andrea Grazioso (agr-odoo)
2020-10-09 12:40:46 +00:00
parent bb565bcd0e
commit 75e59aeff8
+14 -1
View File
@@ -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):