From 8062ea7558ba5c2befc07df1c79b55e6b7153b10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aar=C3=B3n=20Henr=C3=ADquez?= Date: Fri, 16 Nov 2018 11:49:28 +0100 Subject: [PATCH 1/2] [IMP] account_analytic_parent: performance (#200) --- .../models/account_analytic_account.py | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/account_analytic_parent/models/account_analytic_account.py b/account_analytic_parent/models/account_analytic_account.py index 4154a094ae..b8758e94c3 100644 --- a/account_analytic_parent/models/account_analytic_account.py +++ b/account_analytic_parent/models/account_analytic_account.py @@ -28,10 +28,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") From d41255aa2a821b13c3d89389da488e085cb415f8 Mon Sep 17 00:00:00 2001 From: aheficent Date: Wed, 10 Oct 2018 16:07:13 +0200 Subject: [PATCH 2/2] [IMP]propagate active through hierarchy --- .../models/account_analytic_account.py | 17 +++++++++++++++++ .../tests/test_account_analytic_account.py | 9 +++++++++ 2 files changed, 26 insertions(+) diff --git a/account_analytic_parent/models/account_analytic_account.py b/account_analytic_parent/models/account_analytic_account.py index b8758e94c3..9667db8a22 100644 --- a/account_analytic_parent/models/account_analytic_account.py +++ b/account_analytic_parent/models/account_analytic_account.py @@ -75,3 +75,20 @@ def name_get(self): current = current.parent_id res.append((account.id, name)) return res + + @api.multi + @api.constrains('active') + def check_parent_active(self): + for account in self: + if (account.active and account.parent_id and + account.parent_id not in self and + not account.parent_id.active): + raise UserError( + _('Please activate first parent account %s') + % account.parent_id.display_name) + + @api.multi + def write(self, vals): + if self and 'active' in vals and not vals['active']: + self.mapped('child_ids').write({'active': False}) + return super(AccountAnalyticAccount, self).write(vals) diff --git a/account_analytic_parent/tests/test_account_analytic_account.py b/account_analytic_parent/tests/test_account_analytic_account.py index 83ca74c868..420c960697 100644 --- a/account_analytic_parent/tests/test_account_analytic_account.py +++ b/account_analytic_parent/tests/test_account_analytic_account.py @@ -71,3 +71,12 @@ def test_debit_credit_balance(self): "Analytic account in the debit side") self.assertEqual(self.analytic_parent2.credit, 50, "Wrong amount") self.assertEqual(self.analytic_parent2.balance, 50, "Wrong amount") + + def test_archive(self): + self.analytic_parent1.toggle_active() + self.assertEqual(self.analytic_son.active, False) + self.analytic_parent1.toggle_active() + self.assertEqual(self.analytic_son.active, False) + self.analytic_parent1.toggle_active() + with self.assertRaises(ValidationError): + self.analytic_son.toggle_active()