You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.2 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Lorenzo Battistini - Agile Business Group
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from openerp import models, fields, api
  5. class AccountTax(models.Model):
  6. _inherit = 'account.tax'
  7. balance = fields.Float(string="Balance", compute="_compute_balance")
  8. base_balance = fields.Float(
  9. string="Base Balance", compute="_compute_balance")
  10. def get_context_values(self):
  11. context = self.env.context
  12. return (
  13. context.get('from_date', fields.Date.context_today(self)),
  14. context.get('to_date', fields.Date.context_today(self)),
  15. context.get('company_id', self.env.user.company_id.id),
  16. context.get('target_move', 'posted')
  17. )
  18. def _compute_balance(self):
  19. for tax in self:
  20. tax.balance = tax.compute_balance(tax_or_base='tax')
  21. tax.base_balance = tax.compute_balance(tax_or_base='base')
  22. def get_target_state_list(self, target_move="posted"):
  23. if target_move == 'posted':
  24. state = ['posted']
  25. elif target_move == 'all':
  26. state = ['posted', 'draft']
  27. else:
  28. state = []
  29. return state
  30. def get_move_line_partial_domain(self, from_date, to_date, company_id):
  31. return [
  32. ('date', '<=', to_date),
  33. ('date', '>=', from_date),
  34. ('company_id', '=', company_id),
  35. ]
  36. def compute_balance(self, tax_or_base='tax'):
  37. self.ensure_one()
  38. move_lines = self.get_move_lines_domain(tax_or_base=tax_or_base)
  39. total = sum([l.balance for l in move_lines])
  40. return total
  41. def get_balance_domain(self, state_list):
  42. return [
  43. ('move_id.state', 'in', state_list),
  44. ('tax_line_id', '=', self.id),
  45. ]
  46. def get_base_balance_domain(self, state_list):
  47. return [
  48. ('move_id.state', 'in', state_list),
  49. ('tax_ids', 'in', self.id),
  50. ]
  51. def get_move_lines_domain(self, tax_or_base='tax'):
  52. move_line_model = self.env['account.move.line']
  53. from_date, to_date, company_id, target_move = self.get_context_values()
  54. state_list = self.get_target_state_list(target_move)
  55. domain = self.get_move_line_partial_domain(
  56. from_date, to_date, company_id)
  57. balance_domain = []
  58. if tax_or_base == 'tax':
  59. balance_domain = self.get_balance_domain(state_list)
  60. elif tax_or_base == 'base':
  61. balance_domain = self.get_base_balance_domain(state_list)
  62. domain.extend(balance_domain)
  63. return move_line_model.search(domain)
  64. def get_lines_action(self, tax_or_base='tax'):
  65. move_lines = self.get_move_lines_domain(tax_or_base=tax_or_base)
  66. move_line_ids = [l.id for l in move_lines]
  67. action = self.env.ref('account.action_account_moves_all_tree')
  68. vals = action.read()[0]
  69. vals['context'] = {}
  70. vals['domain'] = [('id', 'in', move_line_ids)]
  71. return vals
  72. @api.multi
  73. def view_tax_lines(self):
  74. self.ensure_one()
  75. return self.get_lines_action(tax_or_base='tax')
  76. @api.multi
  77. def view_base_lines(self):
  78. self.ensure_one()
  79. return self.get_lines_action(tax_or_base='base')