diff options
| author | Christophe Simonis <chs@odoo.com> | 2018-05-23 19:33:40 +0200 |
|---|---|---|
| committer | Christophe Simonis <chs@odoo.com> | 2018-05-23 19:33:40 +0200 |
| commit | 2bc6ea1b378bc8a660219b0502c93118f0bb1b6a (patch) | |
| tree | 806acef55473ca422ca6f1d5d46041e3c5e1e590 /addons/analytic | |
| parent | 2a43614c3c00d57a4376eea44cd8cd1ed2d316b7 (diff) | |
| parent | 02ee3fd88eff00ecc058c060aa9b3b39b7c08e15 (diff) | |
[MERGE] forward port branch 11.0 up to 02ee3fd88ef
Diffstat (limited to 'addons/analytic')
| -rw-r--r-- | addons/analytic/models/analytic_account.py | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/addons/analytic/models/analytic_account.py b/addons/analytic/models/analytic_account.py index fbf7951aa4e..9b3a4a1eec0 100644 --- a/addons/analytic/models/analytic_account.py +++ b/addons/analytic/models/analytic_account.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. +from collections import defaultdict from odoo import api, fields, models, _ from odoo.osv import expression from odoo.exceptions import ValidationError @@ -61,9 +62,9 @@ class AccountAnalyticAccount(models.Model): @api.multi def _compute_debit_credit_balance(self): - res_currency_obj = self.env['res.currency'] + Curr = self.env['res.currency'] analytic_line_obj = self.env['account.analytic.line'] - domain = [('account_id', 'in', self.mapped('id'))] + domain = [('account_id', 'in', self.ids)] if self._context.get('from_date', False): domain.append(('date', '>=', self._context['from_date'])) if self._context.get('to_date', False): @@ -75,17 +76,23 @@ class AccountAnalyticAccount(models.Model): domain.append(('company_id', 'in', self._context['company_ids'])) user_currency = self.env.user.company_id.currency_id - account_amounts = analytic_line_obj.search_read(domain, ['account_id', 'amount', 'currency_id']) - account_ids = set([line['account_id'][0] for line in account_amounts]) - data_debit = {account_id: 0.0 for account_id in account_ids} - data_credit = {account_id: 0.0 for account_id in account_ids} - for account_amount in account_amounts: - currency_id = account_amount['currency_id'][0] - amount = res_currency_obj.browse(currency_id).compute(account_amount['amount'], user_currency) - if amount < 0.0: - data_debit[account_amount['account_id'][0]] += amount - else: - data_credit[account_amount['account_id'][0]] += amount + credit_groups = analytic_line_obj.read_group( + domain=domain + [('amount', '>=', 0.0)], + fields=['account_id', 'currency_id', 'amount'], + groupby=['account_id', 'currency_id'], + ) + data_credit = defaultdict(float) + for l in credit_groups: + data_credit[l['account_id'][0]] += Curr.browse(l['currency_id'][0]).compute(l['amount'], user_currency) + + debit_groups = analytic_line_obj.read_group( + domain=domain + [('amount', '<', 0.0)], + fields=['account_id', 'currency_id', 'amount'], + groupby=['account_id', 'currency_id'], + ) + data_debit = defaultdict(float) + for l in debit_groups: + data_debit[l['account_id'][0]] += Curr.browse(l['currency_id'][0]).compute(l['amount'], user_currency) for account in self: account.debit = abs(data_debit.get(account.id, 0.0)) |
