From 097f29d0f6a5b295c81fd074665dbd261eaf12fb Mon Sep 17 00:00:00 2001 From: "Anh Thao Pham (pta)" Date: Wed, 3 Feb 2021 14:23:37 +0000 Subject: [PATCH] [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#65777 X-original-commit: 8c7feb45f1ee8581e259f3c2aaff7a139f516d94 Signed-off-by: Anh Thao PHAM --- addons/product_margin/models/product_product.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/addons/product_margin/models/product_product.py b/addons/product_margin/models/product_product.py index d5c5c6c151b..97562fce0c5 100644 --- a/addons/product_margin/models/product_product.py +++ b/addons/product_margin/models/product_product.py @@ -109,7 +109,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.move_type IN ('out_invoice', 'in_invoice') THEN 1 ELSE -1 END) * ((100 - l.discount) * 0.01) + ) / NULLIF(SUM(l.quantity * (CASE WHEN i.move_type IN ('out_invoice', 'in_invoice') THEN 1 ELSE -1 END)), 0) AS avg_unit_price, SUM(l.quantity * (CASE WHEN i.move_type IN ('out_invoice', 'in_invoice') THEN 1 ELSE -1 END)) AS num_qty, SUM(ABS(l.balance) * (CASE WHEN i.move_type IN ('out_invoice', 'in_invoice') THEN 1 ELSE -1 END)) AS total, SUM(l.quantity * pt.list_price * (CASE WHEN i.move_type IN ('out_invoice', 'in_invoice') THEN 1 ELSE -1 END)) AS sale_expected