Skip to content
Snippets Groups Projects
Commit a3ee7adf authored by Anh Thao Pham (pta)'s avatar Anh Thao Pham (pta)
Browse files

[FIX] product_margin: fix Avg. Sale Unit Price


- Install product_margin
- Go to Invoicing > Customers > Invoices and create an Invoice
- Add a Credit Note
- Go to Accounting > Reporting > Product Margins and check Product used in the Invoice
The "Avg. Sale Unit Price" is incorrect. It should be 0 as it is for "# Invoiced in Sale"

Credit Notes are not taken into consideration when computing "Avg. Sale Unit Price".
This is also the case for discount.

opw-2382797

closes odoo/odoo#65464

Signed-off-by: default avatarAnh Thao PHAM <kitan191@users.noreply.github.com>
parent 63c60549
No related branches found
No related tags found
No related merge requests found
......@@ -112,7 +112,10 @@ class ProductProduct(models.Model):
sqlstr = """
WITH currency_rate AS ({})
SELECT
SUM(l.price_unit / (CASE COALESCE(cr.rate, 0) WHEN 0 THEN 1.0 ELSE cr.rate END) * l.quantity) / NULLIF(SUM(l.quantity),0) AS avg_unit_price,
SUM(
l.price_unit / (CASE COALESCE(cr.rate, 0) WHEN 0 THEN 1.0 ELSE cr.rate END) *
l.quantity * (CASE WHEN i.type IN ('out_invoice', 'in_invoice') THEN 1 ELSE -1 END) * ((100 - l.discount) * 0.01)
) / NULLIF(SUM(l.quantity * (CASE WHEN i.type IN ('out_invoice', 'in_invoice') THEN 1 ELSE -1 END)), 0) AS avg_unit_price,
SUM(l.quantity * (CASE WHEN i.type IN ('out_invoice', 'in_invoice') THEN 1 ELSE -1 END)) AS num_qty,
SUM(ABS(l.balance) * (CASE WHEN i.type IN ('out_invoice', 'in_invoice') THEN 1 ELSE -1 END)) AS total,
SUM(l.quantity * pt.list_price * (CASE WHEN i.type IN ('out_invoice', 'in_invoice') THEN 1 ELSE -1 END)) AS sale_expected
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment