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.

211 lines
7.4 KiB

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