From 649b289fa10fcfd654a68ef6c923a07475f10296 Mon Sep 17 00:00:00 2001
From: qdp-odoo <qdp@odoo.com>
Date: Thu, 2 Jun 2016 12:22:42 +0200
Subject: [PATCH] [FIX] account: invoice analysis report fixed. * price_average
 is now always positive regardless of the invoice type, because it's an
 average and there's no point in summing those values * price_total is now
 computed by using ABS() because of a migration bug of price_subtotal_signed
 introduced in saas-6, where in_invoice and in_refund invoices were set with a
 negative value while on the help tooltip it was saying that in/out refunds
 are negative and in/out invoices are positive. That bug was only revealed in
 v9 -since saas-6 wasn't using that field at all-, with the effect of having
 wrong values in report for migrated databases. This workaround using ABS()
 will be removed in a further version with a migration script to fix people's
 databases. * residual field is now negative for in_invoices and out_refunds.
 * These 2 points ensure that we can group by invoice type and sum the
 price_total or the residual fields to get the gross income of a month for
 example.

---
 .../account/report/account_invoice_report.py  | 30 +++++++++++--------
 1 file changed, 17 insertions(+), 13 deletions(-)

diff --git a/addons/account/report/account_invoice_report.py b/addons/account/report/account_invoice_report.py
index 319983c65f22..ece67bf8a832 100644
--- a/addons/account/report/account_invoice_report.py
+++ b/addons/account/report/account_invoice_report.py
@@ -111,22 +111,26 @@ class AccountInvoiceReport(models.Model):
                     ai.type, ai.state, pt.categ_id, ai.date_due, ai.account_id, ail.account_id AS account_line_id,
                     ai.partner_bank_id,
                     SUM(CASE
-                         WHEN ai.type::text = ANY (ARRAY['out_refund'::character varying::text, 'in_invoice'::character varying::text])
+                        WHEN ai.type::text = ANY (ARRAY['out_refund'::character varying::text, 'in_invoice'::character varying::text])
                             THEN (- ail.quantity) / u.factor * u2.factor
                             ELSE ail.quantity / u.factor * u2.factor
                         END) AS product_qty,
-                    SUM(ail.price_subtotal_signed) AS price_total,
-                    SUM(ail.price_subtotal_signed) / CASE
-                           WHEN SUM(ail.quantity / u.factor * u2.factor) <> 0::numeric
-                               THEN CASE
-                                     WHEN ai.type::text = ANY (ARRAY['out_refund'::character varying::text, 'in_invoice'::character varying::text])
-                                        THEN SUM((- ail.quantity) / u.factor * u2.factor)
-                                        ELSE SUM(ail.quantity / u.factor * u2.factor)
-                                    END
-                               ELSE 1::numeric
-                          END AS price_average,
-                    ai.residual_company_signed / (SELECT count(*) FROM account_invoice_line l where invoice_id = ai.id) *
-                    count(*) AS residual,
+                    SUM(ABS(ail.price_subtotal_signed) * CASE
+                        WHEN ai.type::text = ANY (ARRAY['out_refund'::character varying::text, 'in_invoice'::character varying::text])
+                            THEN -1
+                            ELSE 1
+                        END
+                    ) AS price_total,
+                    SUM(ABS(ail.price_subtotal_signed)) / CASE
+                        WHEN SUM(ail.quantity / u.factor * u2.factor) <> 0::numeric
+                            THEN SUM(ail.quantity / u.factor * u2.factor)
+                            ELSE 1::numeric
+                        END AS price_average,
+                    ai.residual_company_signed / (SELECT count(*) FROM account_invoice_line l where invoice_id = ai.id) * count(*) * CASE
+                        WHEN ai.type::text = ANY (ARRAY['in_refund'::character varying::text, 'in_invoice'::character varying::text])
+                            THEN -1
+                            ELSE 1
+                        END AS residual,
                     ai.commercial_partner_id as commercial_partner_id,
                     partner.country_id
         """
-- 
GitLab