From 9c2b77633d5e12a181fb52e46efd29e76d112a26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul=20=28ACSONE=29?= Date: Fri, 24 Mar 2017 19:05:19 +0100 Subject: [PATCH] [IMP] account_tax_balance: performance Let the database do some computations (sum) and do not put large lists of ids in action domain. --- account_tax_balance/models/account_tax.py | 15 +++++++-------- .../tests/test_account_tax_balance.py | 12 ++++++------ 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/account_tax_balance/models/account_tax.py b/account_tax_balance/models/account_tax.py index 5c79daa4..6fdbf58b 100644 --- a/account_tax_balance/models/account_tax.py +++ b/account_tax_balance/models/account_tax.py @@ -112,13 +112,14 @@ class AccountTax(models.Model): def compute_balance(self, tax_or_base='tax', move_type=None): self.ensure_one() - move_lines = self.get_move_lines_domain( + domain = self.get_move_lines_domain( tax_or_base=tax_or_base, move_type=move_type) # balance is debit - credit whereas on tax return you want to see what # vat has to be paid so: # VAT on sales (credit) - VAT on purchases (debit). - total = -sum([l.balance for l in move_lines]) - return total + balance = self.env['account.move.line'].\ + read_group(domain, ['balance'], [])[0]['balance'] + return balance and -balance or 0 def get_balance_domain(self, state_list, type_list): domain = [ @@ -139,7 +140,6 @@ class AccountTax(models.Model): return domain def get_move_lines_domain(self, tax_or_base='tax', move_type=None): - move_line_model = self.env['account.move.line'] from_date, to_date, company_id, target_move = self.get_context_values() state_list = self.get_target_state_list(target_move) type_list = self.get_target_type_list(move_type) @@ -152,16 +152,15 @@ class AccountTax(models.Model): balance_domain = self.get_base_balance_domain( state_list, type_list) domain.extend(balance_domain) - return move_line_model.search(domain) + return domain def get_lines_action(self, tax_or_base='tax', move_type=None): - move_lines = self.get_move_lines_domain( + domain = self.get_move_lines_domain( tax_or_base=tax_or_base, move_type=move_type) - move_line_ids = [l.id for l in move_lines] action = self.env.ref('account.action_account_moves_all_tree') vals = action.read()[0] vals['context'] = {} - vals['domain'] = [('id', 'in', move_line_ids)] + vals['domain'] = domain return vals @api.multi diff --git a/account_tax_balance/tests/test_account_tax_balance.py b/account_tax_balance/tests/test_account_tax_balance.py index ed3e3237..99102833 100644 --- a/account_tax_balance/tests/test_account_tax_balance.py +++ b/account_tax_balance/tests/test_account_tax_balance.py @@ -99,14 +99,14 @@ class TestAccountTaxBalance(TransactionCase): # testing buttons tax_action = tax.view_tax_lines() base_action = tax.view_base_lines() - self.assertTrue( - tax_action['domain'][0][2][0] in - [l.id for l in invoice.move_id.line_ids]) + tax_action_move_lines = self.env['account.move.line'].\ + search(tax_action['domain']) + self.assertTrue(invoice.move_id.line_ids & tax_action_move_lines) self.assertEqual( tax_action['xml_id'], 'account.action_account_moves_all_tree') - self.assertTrue( - base_action['domain'][0][2][0] in - [l.id for l in invoice.move_id.line_ids]) + base_action_move_lines = self.env['account.move.line'].\ + search(base_action['domain']) + self.assertTrue(invoice.move_id.line_ids & base_action_move_lines) self.assertEqual( base_action['xml_id'], 'account.action_account_moves_all_tree')