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.

97 lines
3.4 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. # balance is debit - credit whereas on tax return you want to see what
  40. # vat has to be paid so:
  41. # VAT on sales (credit) - VAT on purchases (debit).
  42. total = -sum([l.balance for l in move_lines])
  43. return total
  44. def get_balance_domain(self, state_list):
  45. return [
  46. ('move_id.state', 'in', state_list),
  47. ('tax_line_id', '=', self.id),
  48. ]
  49. def get_base_balance_domain(self, state_list):
  50. return [
  51. ('move_id.state', 'in', state_list),
  52. ('tax_ids', 'in', self.id),
  53. ]
  54. def get_move_lines_domain(self, tax_or_base='tax'):
  55. move_line_model = self.env['account.move.line']
  56. from_date, to_date, company_id, target_move = self.get_context_values()
  57. state_list = self.get_target_state_list(target_move)
  58. domain = self.get_move_line_partial_domain(
  59. from_date, to_date, company_id)
  60. balance_domain = []
  61. if tax_or_base == 'tax':
  62. balance_domain = self.get_balance_domain(state_list)
  63. elif tax_or_base == 'base':
  64. balance_domain = self.get_base_balance_domain(state_list)
  65. domain.extend(balance_domain)
  66. return move_line_model.search(domain)
  67. def get_lines_action(self, tax_or_base='tax'):
  68. move_lines = self.get_move_lines_domain(tax_or_base=tax_or_base)
  69. move_line_ids = [l.id for l in move_lines]
  70. action = self.env.ref('account.action_account_moves_all_tree')
  71. vals = action.read()[0]
  72. vals['context'] = {}
  73. vals['domain'] = [('id', 'in', move_line_ids)]
  74. return vals
  75. @api.multi
  76. def view_tax_lines(self):
  77. self.ensure_one()
  78. return self.get_lines_action(tax_or_base='tax')
  79. @api.multi
  80. def view_base_lines(self):
  81. self.ensure_one()
  82. return self.get_lines_action(tax_or_base='base')