[IMP] account_tax_balance: performance
Let the database do some computations (sum) and do not put large lists of ids in action domain.pull/293/head
parent
678d5cf6cd
commit
bafc286380
|
@ -112,13 +112,14 @@ class AccountTax(models.Model):
|
||||||
|
|
||||||
def compute_balance(self, tax_or_base='tax', move_type=None):
|
def compute_balance(self, tax_or_base='tax', move_type=None):
|
||||||
self.ensure_one()
|
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)
|
tax_or_base=tax_or_base, move_type=move_type)
|
||||||
# balance is debit - credit whereas on tax return you want to see what
|
# balance is debit - credit whereas on tax return you want to see what
|
||||||
# vat has to be paid so:
|
# vat has to be paid so:
|
||||||
# VAT on sales (credit) - VAT on purchases (debit).
|
# VAT on sales (credit) - VAT on purchases (debit).
|
||||||
total = -sum([l.balance for l in move_lines])
|
balance = self.env['account.move.line'].\
|
||||||
return total
|
read_group(domain, ['balance'], [])[0]['balance']
|
||||||
|
return balance and -balance or 0
|
||||||
|
|
||||||
def get_balance_domain(self, state_list, type_list):
|
def get_balance_domain(self, state_list, type_list):
|
||||||
domain = [
|
domain = [
|
||||||
|
@ -139,7 +140,6 @@ class AccountTax(models.Model):
|
||||||
return domain
|
return domain
|
||||||
|
|
||||||
def get_move_lines_domain(self, tax_or_base='tax', move_type=None):
|
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()
|
from_date, to_date, company_id, target_move = self.get_context_values()
|
||||||
state_list = self.get_target_state_list(target_move)
|
state_list = self.get_target_state_list(target_move)
|
||||||
type_list = self.get_target_type_list(move_type)
|
type_list = self.get_target_type_list(move_type)
|
||||||
|
@ -152,16 +152,15 @@ class AccountTax(models.Model):
|
||||||
balance_domain = self.get_base_balance_domain(
|
balance_domain = self.get_base_balance_domain(
|
||||||
state_list, type_list)
|
state_list, type_list)
|
||||||
domain.extend(balance_domain)
|
domain.extend(balance_domain)
|
||||||
return move_line_model.search(domain)
|
return domain
|
||||||
|
|
||||||
def get_lines_action(self, tax_or_base='tax', move_type=None):
|
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)
|
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')
|
action = self.env.ref('account.action_account_moves_all_tree')
|
||||||
vals = action.read()[0]
|
vals = action.read()[0]
|
||||||
vals['context'] = {}
|
vals['context'] = {}
|
||||||
vals['domain'] = [('id', 'in', move_line_ids)]
|
vals['domain'] = domain
|
||||||
return vals
|
return vals
|
||||||
|
|
||||||
@api.multi
|
@api.multi
|
||||||
|
|
|
@ -99,14 +99,14 @@ class TestAccountTaxBalance(TransactionCase):
|
||||||
# testing buttons
|
# testing buttons
|
||||||
tax_action = tax.view_tax_lines()
|
tax_action = tax.view_tax_lines()
|
||||||
base_action = tax.view_base_lines()
|
base_action = tax.view_base_lines()
|
||||||
self.assertTrue(
|
tax_action_move_lines = self.env['account.move.line'].\
|
||||||
tax_action['domain'][0][2][0] in
|
search(tax_action['domain'])
|
||||||
[l.id for l in invoice.move_id.line_ids])
|
self.assertTrue(invoice.move_id.line_ids & tax_action_move_lines)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
tax_action['xml_id'], 'account.action_account_moves_all_tree')
|
tax_action['xml_id'], 'account.action_account_moves_all_tree')
|
||||||
self.assertTrue(
|
base_action_move_lines = self.env['account.move.line'].\
|
||||||
base_action['domain'][0][2][0] in
|
search(base_action['domain'])
|
||||||
[l.id for l in invoice.move_id.line_ids])
|
self.assertTrue(invoice.move_id.line_ids & base_action_move_lines)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
base_action['xml_id'], 'account.action_account_moves_all_tree')
|
base_action['xml_id'], 'account.action_account_moves_all_tree')
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue