From 9fe040e59237ebdacd4dd3c299a3e79428354651 Mon Sep 17 00:00:00 2001 From: Denis Ledoux Date: Wed, 11 Feb 2015 13:10:54 +0100 Subject: [PATCH] [FIX] account: invoice analysis residual amount When having an invoice with multiple lines having the same product_id and account_id, the residual amount was wrong. This is due to the fact the residual amount of each line was computed on the residual amount of the invoice divided by the number of lines of the invoice, and the fact the main select of the sql view was grouped by product_id, account_id. So, for an invoice defined as Product Account Total A 1 10 A 1 10 B 1 10 The invoice analysis, grouped by product, account, computed Product Account Total Residual A 1 20 10 B 1 10 10 The residual amount '10' of the first line being 30 (the residual amount of the invoice) divided by 3 (the number of lines in the invoice) The residual amount of the invoice should actually be divided by the number of lines in the invoice * the count of occurences in the group by clause So, in this case, (30 / 3) * 2 = 20 Replacing the big jointure by SELECT count(*) FROM account_invoice_line l where invoice_id = ai.id to get the number of lines in the invoice is just an optimization for performances opw-621672 --- addons/account/report/account_invoice_report.py | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/addons/account/report/account_invoice_report.py b/addons/account/report/account_invoice_report.py index 31725a46b46..0d2756ad23d 100644 --- a/addons/account/report/account_invoice_report.py +++ b/addons/account/report/account_invoice_report.py @@ -160,17 +160,8 @@ class account_invoice_report(osv.osv): WHEN ai.type::text = ANY (ARRAY['out_refund'::character varying::text, 'in_invoice'::character varying::text]) THEN - ai.residual ELSE ai.residual - END / CASE - WHEN (( SELECT count(l.id) AS count - FROM account_invoice_line l - LEFT JOIN account_invoice a ON a.id = l.invoice_id - WHERE a.id = ai.id)) <> 0 - THEN ( SELECT count(l.id) AS count - FROM account_invoice_line l - LEFT JOIN account_invoice a ON a.id = l.invoice_id - WHERE a.id = ai.id) - ELSE 1::bigint - END::numeric AS residual + END / (SELECT count(*) FROM account_invoice_line l where invoice_id = ai.id) * + count(*) AS residual """ return select_str