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.

209 lines
7.4 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Lorenzo Battistini - Agile Business Group
  3. # © 2016 Antonio Espinosa <antonio.espinosa@tecnativa.com>
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  5. from openerp import models, fields, api, _
  6. class AccountTax(models.Model):
  7. _inherit = 'account.tax'
  8. balance = fields.Float(
  9. string="Total Balance", compute="_compute_balance",
  10. )
  11. base_balance = fields.Float(
  12. string="Total Base Balance", compute="_compute_balance",
  13. )
  14. balance_regular = fields.Float(
  15. string="Balance", compute="_compute_balance",
  16. )
  17. base_balance_regular = fields.Float(
  18. string="Base Balance", compute="_compute_balance",
  19. )
  20. balance_refund = fields.Float(
  21. string="Balance Refund", compute="_compute_balance",
  22. )
  23. base_balance_refund = fields.Float(
  24. string="Base Balance Refund", compute="_compute_balance",
  25. )
  26. has_moves = fields.Boolean(
  27. string="Has balance in period",
  28. compute="_compute_has_moves",
  29. search="_search_has_moves",
  30. )
  31. def get_context_values(self):
  32. context = self.env.context
  33. return (
  34. context.get('from_date', fields.Date.context_today(self)),
  35. context.get('to_date', fields.Date.context_today(self)),
  36. context.get('company_id', self.env.user.company_id.id),
  37. context.get('target_move', 'posted')
  38. )
  39. def _account_tax_ids_with_moves(self):
  40. """ Return all account.tax ids for which there is at least
  41. one account.move.line in the context period
  42. for the user company.
  43. Caveat: this ignores record rules and ACL but it is good
  44. enough for filtering taxes with activity during the period.
  45. """
  46. req = """
  47. SELECT id
  48. FROM account_tax at
  49. WHERE
  50. company_id = %s AND
  51. EXISTS (
  52. SELECT 1 FROM account_move_Line aml
  53. WHERE
  54. date >= %s AND
  55. date <= %s AND
  56. company_id = %s AND (
  57. tax_line_id = at.id OR
  58. EXISTS (
  59. SELECT 1 FROM account_move_line_account_tax_rel
  60. WHERE account_move_line_id = aml.id AND
  61. account_tax_id = at.id
  62. )
  63. )
  64. )
  65. """
  66. from_date, to_date, company_id, target_move = self.get_context_values()
  67. self.env.cr.execute(
  68. req, (company_id, from_date, to_date, company_id))
  69. return [r[0] for r in self.env.cr.fetchall()]
  70. @api.multi
  71. def _compute_has_moves(self):
  72. ids_with_moves = set(self._account_tax_ids_with_moves())
  73. for tax in self:
  74. tax.has_moves = tax.id in ids_with_moves
  75. @api.model
  76. def _search_has_moves(self, operator, value):
  77. if operator != '=' or not value:
  78. raise ValueError(_("Unsupported search operator"))
  79. ids_with_moves = self._account_tax_ids_with_moves()
  80. return [('id', 'in', ids_with_moves)]
  81. def _compute_balance(self):
  82. for tax in self:
  83. tax.balance_regular = tax.compute_balance(
  84. tax_or_base='tax', move_type='regular')
  85. tax.base_balance_regular = tax.compute_balance(
  86. tax_or_base='base', move_type='regular')
  87. tax.balance_refund = tax.compute_balance(
  88. tax_or_base='tax', move_type='refund')
  89. tax.base_balance_refund = tax.compute_balance(
  90. tax_or_base='base', move_type='refund')
  91. tax.balance = tax.balance_regular + tax.balance_refund
  92. tax.base_balance = (
  93. tax.base_balance_regular + tax.base_balance_refund)
  94. def get_target_type_list(self, move_type=None):
  95. if move_type == 'refund':
  96. return ['receivable_refund', 'payable_refund']
  97. elif move_type == 'regular':
  98. return ['receivable', 'payable', 'liquidity', 'other']
  99. return []
  100. def get_target_state_list(self, target_move="posted"):
  101. if target_move == 'posted':
  102. state = ['posted']
  103. elif target_move == 'all':
  104. state = ['posted', 'draft']
  105. else:
  106. state = []
  107. return state
  108. def get_move_line_partial_domain(self, from_date, to_date, company_id):
  109. return [
  110. ('date', '<=', to_date),
  111. ('date', '>=', from_date),
  112. ('company_id', '=', company_id),
  113. ]
  114. def compute_balance(self, tax_or_base='tax', move_type=None):
  115. self.ensure_one()
  116. domain = self.get_move_lines_domain(
  117. tax_or_base=tax_or_base, move_type=move_type)
  118. # balance is debit - credit whereas on tax return you want to see what
  119. # vat has to be paid so:
  120. # VAT on sales (credit) - VAT on purchases (debit).
  121. balance = self.env['account.move.line'].\
  122. read_group(domain, ['balance'], [])[0]['balance']
  123. return balance and -balance or 0
  124. def get_balance_domain(self, state_list, type_list):
  125. domain = [
  126. ('move_id.state', 'in', state_list),
  127. ('tax_line_id', '=', self.id),
  128. ]
  129. if type_list:
  130. domain.append(('move_id.move_type', 'in', type_list))
  131. return domain
  132. def get_base_balance_domain(self, state_list, type_list):
  133. domain = [
  134. ('move_id.state', 'in', state_list),
  135. ('tax_ids', 'in', self.id),
  136. ]
  137. if type_list:
  138. domain.append(('move_id.move_type', 'in', type_list))
  139. return domain
  140. def get_move_lines_domain(self, tax_or_base='tax', move_type=None):
  141. from_date, to_date, company_id, target_move = self.get_context_values()
  142. state_list = self.get_target_state_list(target_move)
  143. type_list = self.get_target_type_list(move_type)
  144. domain = self.get_move_line_partial_domain(
  145. from_date, to_date, company_id)
  146. balance_domain = []
  147. if tax_or_base == 'tax':
  148. balance_domain = self.get_balance_domain(state_list, type_list)
  149. elif tax_or_base == 'base':
  150. balance_domain = self.get_base_balance_domain(
  151. state_list, type_list)
  152. domain.extend(balance_domain)
  153. return domain
  154. def get_lines_action(self, tax_or_base='tax', move_type=None):
  155. domain = self.get_move_lines_domain(
  156. tax_or_base=tax_or_base, move_type=move_type)
  157. action = self.env.ref('account.action_account_moves_all_tree')
  158. vals = action.read()[0]
  159. vals['context'] = {}
  160. vals['domain'] = domain
  161. return vals
  162. @api.multi
  163. def view_tax_lines(self):
  164. self.ensure_one()
  165. return self.get_lines_action(tax_or_base='tax')
  166. @api.multi
  167. def view_base_lines(self):
  168. self.ensure_one()
  169. return self.get_lines_action(tax_or_base='base')
  170. @api.multi
  171. def view_tax_regular_lines(self):
  172. self.ensure_one()
  173. return self.get_lines_action(tax_or_base='tax', move_type='regular')
  174. @api.multi
  175. def view_base_regular_lines(self):
  176. self.ensure_one()
  177. return self.get_lines_action(tax_or_base='base', move_type='regular')
  178. @api.multi
  179. def view_tax_refund_lines(self):
  180. self.ensure_one()
  181. return self.get_lines_action(tax_or_base='tax', move_type='refund')
  182. @api.multi
  183. def view_base_refund_lines(self):
  184. self.ensure_one()
  185. return self.get_lines_action(tax_or_base='base', move_type='refund')