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.

86 lines
3.1 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
  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. if not self.env.context.get('from_date'):
  12. from_date = fields.Date.context_today(self)
  13. else:
  14. from_date = self.env.context['from_date']
  15. if not self.env.context.get('to_date'):
  16. to_date = fields.Date.context_today(self)
  17. else:
  18. to_date = self.env.context['to_date']
  19. if not self.env.context.get('target_move'):
  20. target_move = 'posted'
  21. else:
  22. target_move = self.env.context['target_move']
  23. if not self.env.context.get('company_id'):
  24. company_id = self.env.user.company_id.id
  25. else:
  26. company_id = self.env.context['company_id']
  27. return from_date, to_date, company_id, target_move
  28. def _compute_balance(self):
  29. from_date, to_date, company_id, target_move = self.get_context_values()
  30. for tax in self:
  31. tax.balance = tax.compute_balance(
  32. from_date, to_date, company_id, target_move)
  33. tax.base_balance = tax.compute_base_balance(
  34. from_date, to_date, company_id, target_move)
  35. def get_target_state_list(self, target_move="posted"):
  36. if target_move == 'posted':
  37. state = ['posted']
  38. elif target_move == 'all':
  39. state = ['posted', 'draft']
  40. else:
  41. state = []
  42. return state
  43. def get_move_line_domain(self, from_date, to_date, company_id):
  44. return [
  45. ('date', '<=', to_date),
  46. ('date', '>=', from_date),
  47. ('company_id', '=', company_id),
  48. ]
  49. def compute_balance(
  50. self, from_date, to_date, company_id, target_move="posted"
  51. ):
  52. self.ensure_one()
  53. move_line_model = self.env['account.move.line']
  54. state_list = self.get_target_state_list(target_move)
  55. domain = self.get_move_line_domain(from_date, to_date, company_id)
  56. domain.extend([
  57. ('move_id.state', 'in', state_list),
  58. ('tax_line_id', '=', self.id),
  59. ])
  60. move_lines = move_line_model.search(domain)
  61. total = sum([l.balance for l in move_lines])
  62. return total
  63. def compute_base_balance(
  64. self, from_date, to_date, company_id, target_move="posted"
  65. ):
  66. self.ensure_one()
  67. move_line_model = self.env['account.move.line']
  68. state_list = self.get_target_state_list(target_move)
  69. domain = self.get_move_line_domain(from_date, to_date, company_id)
  70. domain.extend([
  71. ('move_id.state', 'in', state_list),
  72. ('tax_ids', 'in', self.id),
  73. ])
  74. move_lines = move_line_model.search(domain)
  75. total = sum([l.balance for l in move_lines])
  76. return total