From ccaea94a66f75dc8cfe8e412080e098ffbd720d1 Mon Sep 17 00:00:00 2001 From: rmu-odoo Date: Sun, 22 Oct 2017 16:12:12 +0530 Subject: [PATCH] [IMP] anlaytic: display total of balance/credit/debit on group_by Purpose ======= User not able to see his total balance per customer if user has several projects with same customer.. see here : https://www.odoo.com/web/content/3287924 Specification ============= User should be able to see the sum of the debit, credit and balance when group by partner The difficulty here is that debit,credit and balance are non-stored computed fields depending on the company and the date passed with the context. As these parameters depend on the user and the current date, we can't store the field. The simplest way to achieve this is to override the read_group method and compute the sum by hand when the values are aggregated (means when a group_by is applied). --- addons/analytic/models/analytic_account.py | 18 ++++++++++++++++++ .../analytic/views/analytic_account_views.xml | 6 +++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/addons/analytic/models/analytic_account.py b/addons/analytic/models/analytic_account.py index d6ee2b69275..ad6d3b0508f 100644 --- a/addons/analytic/models/analytic_account.py +++ b/addons/analytic/models/analytic_account.py @@ -59,6 +59,24 @@ class AccountAnalyticAccount(models.Model): _description = 'Analytic Account' _order = 'code, name asc' + @api.model + def read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True): + """ + Override read_group to calculate the sum of the non-stored fields that depend on the user context + """ + res = super(AccountAnalyticAccount, self).read_group(domain, fields, groupby, offset=offset, limit=limit, orderby=orderby, lazy=lazy) + accounts = self.env['account.analytic.account'] + for line in res: + if '__domain' in line: + accounts = self.search(line['__domain']) + if 'balance' in fields: + line['balance'] = sum(accounts.mapped('balance')) + if 'debit' in fields: + line['debit'] = sum(accounts.mapped('debit')) + if 'credit' in fields: + line['credit'] = sum(accounts.mapped('credit')) + return res + @api.multi def _compute_debit_credit_balance(self): res_currency_obj = self.env['res.currency'] diff --git a/addons/analytic/views/analytic_account_views.xml b/addons/analytic/views/analytic_account_views.xml index f5afe619774..1d7d129e7c1 100644 --- a/addons/analytic/views/analytic_account_views.xml +++ b/addons/analytic/views/analytic_account_views.xml @@ -190,9 +190,9 @@ - - - + + +