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.

212 lines
7.5 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. ('tax_exigible', '=', True)
  129. ]
  130. if type_list:
  131. domain.append(('move_id.move_type', 'in', type_list))
  132. return domain
  133. def get_base_balance_domain(self, state_list, type_list):
  134. domain = [
  135. ('move_id.state', 'in', state_list),
  136. ('tax_ids', 'in', self.id),
  137. ('tax_exigible', '=', True)
  138. ]
  139. if type_list:
  140. domain.append(('move_id.move_type', 'in', type_list))
  141. return domain
  142. def get_move_lines_domain(self, tax_or_base='tax', move_type=None):
  143. from_date, to_date, company_id, target_move = self.get_context_values()
  144. state_list = self.get_target_state_list(target_move)
  145. type_list = self.get_target_type_list(move_type)
  146. domain = self.get_move_line_partial_domain(
  147. from_date, to_date, company_id)
  148. balance_domain = []
  149. if tax_or_base == 'tax':
  150. balance_domain = self.get_balance_domain(state_list, type_list)
  151. elif tax_or_base == 'base':
  152. balance_domain = self.get_base_balance_domain(
  153. state_list, type_list)
  154. domain.extend(balance_domain)
  155. return domain
  156. def get_lines_action(self, tax_or_base='tax', move_type=None):
  157. domain = self.get_move_lines_domain(
  158. tax_or_base=tax_or_base, move_type=move_type)
  159. action = self.env.ref('account.action_account_moves_all_tree')
  160. vals = action.read()[0]
  161. vals['context'] = {}
  162. vals['domain'] = domain
  163. return vals
  164. @api.multi
  165. def view_tax_lines(self):
  166. self.ensure_one()
  167. return self.get_lines_action(tax_or_base='tax')
  168. @api.multi
  169. def view_base_lines(self):
  170. self.ensure_one()
  171. return self.get_lines_action(tax_or_base='base')
  172. @api.multi
  173. def view_tax_regular_lines(self):
  174. self.ensure_one()
  175. return self.get_lines_action(tax_or_base='tax', move_type='regular')
  176. @api.multi
  177. def view_base_regular_lines(self):
  178. self.ensure_one()
  179. return self.get_lines_action(tax_or_base='base', move_type='regular')
  180. @api.multi
  181. def view_tax_refund_lines(self):
  182. self.ensure_one()
  183. return self.get_lines_action(tax_or_base='tax', move_type='refund')
  184. @api.multi
  185. def view_base_refund_lines(self):
  186. self.ensure_one()
  187. return self.get_lines_action(tax_or_base='base', move_type='refund')