From 578fbbf52be65cb450ecc5898a7b0d6bc65ef9a6 Mon Sep 17 00:00:00 2001 From: eLBati Date: Sun, 22 May 2016 16:29:02 +0200 Subject: [PATCH] better get_context_values unify method compute_balance ADD open move lines linked to balance --- account_tax_balance/models/account_tax.py | 102 ++++++++++-------- .../views/account_tax_view.xml | 2 + 2 files changed, 57 insertions(+), 47 deletions(-) diff --git a/account_tax_balance/models/account_tax.py b/account_tax_balance/models/account_tax.py index fbcfbce2..2f6bdabd 100644 --- a/account_tax_balance/models/account_tax.py +++ b/account_tax_balance/models/account_tax.py @@ -2,7 +2,7 @@ # © 2016 Lorenzo Battistini - Agile Business Group # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from openerp import models, fields +from openerp import models, fields, api class AccountTax(models.Model): @@ -13,31 +13,18 @@ class AccountTax(models.Model): string="Base Balance", compute="_compute_balance") def get_context_values(self): - if not self.env.context.get('from_date'): - from_date = fields.Date.context_today(self) - else: - from_date = self.env.context['from_date'] - if not self.env.context.get('to_date'): - to_date = fields.Date.context_today(self) - else: - to_date = self.env.context['to_date'] - if not self.env.context.get('target_move'): - target_move = 'posted' - else: - target_move = self.env.context['target_move'] - if not self.env.context.get('company_id'): - company_id = self.env.user.company_id.id - else: - company_id = self.env.context['company_id'] - return from_date, to_date, company_id, target_move + context = self.env.context + return ( + context.get('from_date', fields.Date.context_today(self)), + context.get('to_date', fields.Date.context_today(self)), + context.get('company_id', self.env.user.company_id.id), + context.get('target_move', 'posted') + ) def _compute_balance(self): - from_date, to_date, company_id, target_move = self.get_context_values() for tax in self: - tax.balance = tax.compute_balance( - from_date, to_date, company_id, target_move) - tax.base_balance = tax.compute_base_balance( - from_date, to_date, company_id, target_move) + tax.balance = tax.compute_balance(tax_or_base='tax') + tax.base_balance = tax.compute_balance(tax_or_base='base') def get_target_state_list(self, target_move="posted"): if target_move == 'posted': @@ -48,39 +35,60 @@ class AccountTax(models.Model): state = [] return state - def get_move_line_domain(self, from_date, to_date, company_id): + def get_move_line_partial_domain(self, from_date, to_date, company_id): return [ ('date', '<=', to_date), ('date', '>=', from_date), ('company_id', '=', company_id), ] - def compute_balance( - self, from_date, to_date, company_id, target_move="posted" - ): + def compute_balance(self, tax_or_base='tax'): self.ensure_one() - move_line_model = self.env['account.move.line'] - state_list = self.get_target_state_list(target_move) - domain = self.get_move_line_domain(from_date, to_date, company_id) - domain.extend([ - ('move_id.state', 'in', state_list), - ('tax_line_id', '=', self.id), - ]) - move_lines = move_line_model.search(domain) + move_lines = self.get_move_lines_domain(tax_or_base=tax_or_base) total = sum([l.balance for l in move_lines]) return total - def compute_base_balance( - self, from_date, to_date, company_id, target_move="posted" - ): - self.ensure_one() - move_line_model = self.env['account.move.line'] - state_list = self.get_target_state_list(target_move) - domain = self.get_move_line_domain(from_date, to_date, company_id) - domain.extend([ + def get_balance_domain(self, state_list): + return [ + ('move_id.state', 'in', state_list), + ('tax_line_id', '=', self.id), + ] + + def get_base_balance_domain(self, state_list): + return [ ('move_id.state', 'in', state_list), ('tax_ids', 'in', self.id), - ]) - move_lines = move_line_model.search(domain) - total = sum([l.balance for l in move_lines]) - return total + ] + + def get_move_lines_domain(self, tax_or_base='tax'): + 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) + domain = self.get_move_line_partial_domain( + from_date, to_date, company_id) + balance_domain = [] + if tax_or_base == 'tax': + balance_domain = self.get_balance_domain(state_list) + elif tax_or_base == 'base': + balance_domain = self.get_base_balance_domain(state_list) + domain.extend(balance_domain) + return move_line_model.search(domain) + + def get_lines_action(self, tax_or_base='tax'): + move_lines = self.get_move_lines_domain(tax_or_base=tax_or_base) + 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)] + return vals + + @api.multi + def view_tax_lines(self): + self.ensure_one() + return self.get_lines_action(tax_or_base='tax') + + @api.multi + def view_base_lines(self): + self.ensure_one() + return self.get_lines_action(tax_or_base='base') diff --git a/account_tax_balance/views/account_tax_view.xml b/account_tax_balance/views/account_tax_view.xml index a204056d..ab6b6a35 100644 --- a/account_tax_balance/views/account_tax_view.xml +++ b/account_tax_balance/views/account_tax_view.xml @@ -13,6 +13,8 @@ + +