Skip to content

Commit

Permalink
[IMP] account_analytic_parent: performance (OCA#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronHForgeFlow committed Nov 20, 2018
1 parent ffe206e commit 76d3854
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions account_analytic_parent/models/account_analytic_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# Copyright 2017 Deneroteam.
# Copyright 2017 Serpent Consulting Services Pvt. Ltd.
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

from odoo import _, api, fields, models
from odoo.exceptions import UserError

Expand All @@ -28,10 +27,25 @@ def _compute_debit_credit_balance(self):
of analytic account changes
"""
super(AccountAnalyticAccount, self)._compute_debit_credit_balance()
for account in self:
account.debit += sum(account.mapped("child_ids.debit"))
account.credit += sum(account.mapped("child_ids.credit"))
account.balance += sum(account.mapped("child_ids.balance"))
analytic_line_obj = self.env['account.analytic.line']
# compute only analytic line
for account in self.filtered(lambda x: x.child_ids):
domain = [('account_id', 'child_of', account.ids)]
credit_groups = analytic_line_obj.read_group(
domain=domain + [('amount', '>', 0.0)],
fields=['account_id', 'amount'],
groupby=['account_id']
)
data_credit = sum(l['amount'] for l in credit_groups)
debit_groups = analytic_line_obj.read_group(
domain=domain + [('amount', '<', 0.0)],
fields=['account_id', 'amount'],
groupby=['account_id']
)
data_debit = sum(l['amount'] for l in debit_groups)
account.debit = data_debit
account.credit = data_credit
account.balance = account.credit - account.debit

@api.multi
@api.constrains("parent_id")
Expand Down

0 comments on commit 76d3854

Please sign in to comment.