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.

217 lines
7.6 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 odoo import _, api, fields, models
  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_ids', [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. from_date, to_date, company_ids, _ = self.get_context_values()
  46. company_ids = tuple(company_ids)
  47. req = """
  48. SELECT id
  49. FROM account_tax at
  50. WHERE
  51. company_id in %s AND
  52. EXISTS (
  53. SELECT 1 FROM account_move_Line aml
  54. WHERE
  55. date >= %s AND
  56. date <= %s AND
  57. company_id in %s AND (
  58. tax_line_id = at.id OR
  59. EXISTS (
  60. SELECT 1 FROM account_move_line_account_tax_rel
  61. WHERE account_move_line_id = aml.id AND
  62. account_tax_id = at.id
  63. )
  64. )
  65. )
  66. """
  67. self.env.cr.execute(
  68. req, (company_ids, from_date, to_date, company_ids))
  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 _is_unsupported_search_operator(self, operator):
  77. return operator != '='
  78. @api.model
  79. def _search_has_moves(self, operator, value):
  80. if self._is_unsupported_search_operator(operator) or not value:
  81. raise ValueError(_("Unsupported search operator"))
  82. ids_with_moves = self._account_tax_ids_with_moves()
  83. return [('id', 'in', ids_with_moves)]
  84. def _compute_balance(self):
  85. for tax in self:
  86. tax.balance_regular = tax.compute_balance(
  87. tax_or_base='tax', move_type='regular')
  88. tax.base_balance_regular = tax.compute_balance(
  89. tax_or_base='base', move_type='regular')
  90. tax.balance_refund = tax.compute_balance(
  91. tax_or_base='tax', move_type='refund')
  92. tax.base_balance_refund = tax.compute_balance(
  93. tax_or_base='base', move_type='refund')
  94. tax.balance = tax.balance_regular + tax.balance_refund
  95. tax.base_balance = (
  96. tax.base_balance_regular + tax.base_balance_refund)
  97. def get_target_type_list(self, move_type=None):
  98. if move_type == 'refund':
  99. return ['receivable_refund', 'payable_refund']
  100. elif move_type == 'regular':
  101. return ['receivable', 'payable', 'liquidity', 'other']
  102. return []
  103. def get_target_state_list(self, target_move="posted"):
  104. if target_move == 'posted':
  105. state = ['posted']
  106. elif target_move == 'all':
  107. state = ['posted', 'draft']
  108. else:
  109. state = []
  110. return state
  111. def get_move_line_partial_domain(self, from_date, to_date, company_ids):
  112. return [
  113. ('date', '<=', to_date),
  114. ('date', '>=', from_date),
  115. ('company_id', 'in', company_ids),
  116. ]
  117. def compute_balance(self, tax_or_base='tax', move_type=None):
  118. self.ensure_one()
  119. domain = self.get_move_lines_domain(
  120. tax_or_base=tax_or_base, move_type=move_type)
  121. # balance is debit - credit whereas on tax return you want to see what
  122. # vat has to be paid so:
  123. # VAT on sales (credit) - VAT on purchases (debit).
  124. balance = self.env['account.move.line'].\
  125. read_group(domain, ['balance'], [])[0]['balance']
  126. return balance and -balance or 0
  127. def get_balance_domain(self, state_list, type_list):
  128. domain = [
  129. ('move_id.state', 'in', state_list),
  130. ('tax_line_id', '=', self.id),
  131. ('tax_exigible', '=', True)
  132. ]
  133. if type_list:
  134. domain.append(('move_id.move_type', 'in', type_list))
  135. return domain
  136. def get_base_balance_domain(self, state_list, type_list):
  137. domain = [
  138. ('move_id.state', 'in', state_list),
  139. ('tax_ids', 'in', self.id),
  140. ('tax_exigible', '=', True)
  141. ]
  142. if type_list:
  143. domain.append(('move_id.move_type', 'in', type_list))
  144. return domain
  145. def get_move_lines_domain(self, tax_or_base='tax', move_type=None):
  146. from_date, to_date, company_ids, target_move = \
  147. self.get_context_values()
  148. state_list = self.get_target_state_list(target_move)
  149. type_list = self.get_target_type_list(move_type)
  150. domain = self.get_move_line_partial_domain(
  151. from_date, to_date, company_ids)
  152. balance_domain = []
  153. if tax_or_base == 'tax':
  154. balance_domain = self.get_balance_domain(state_list, type_list)
  155. elif tax_or_base == 'base':
  156. balance_domain = self.get_base_balance_domain(
  157. state_list, type_list)
  158. domain.extend(balance_domain)
  159. return domain
  160. def get_lines_action(self, tax_or_base='tax', move_type=None):
  161. domain = self.get_move_lines_domain(
  162. tax_or_base=tax_or_base, move_type=move_type)
  163. action = self.env.ref('account.action_account_moves_all_tree')
  164. vals = action.read()[0]
  165. vals['context'] = {}
  166. vals['domain'] = domain
  167. return vals
  168. @api.multi
  169. def view_tax_lines(self):
  170. self.ensure_one()
  171. return self.get_lines_action(tax_or_base='tax')
  172. @api.multi
  173. def view_base_lines(self):
  174. self.ensure_one()
  175. return self.get_lines_action(tax_or_base='base')
  176. @api.multi
  177. def view_tax_regular_lines(self):
  178. self.ensure_one()
  179. return self.get_lines_action(tax_or_base='tax', move_type='regular')
  180. @api.multi
  181. def view_base_regular_lines(self):
  182. self.ensure_one()
  183. return self.get_lines_action(tax_or_base='base', move_type='regular')
  184. @api.multi
  185. def view_tax_refund_lines(self):
  186. self.ensure_one()
  187. return self.get_lines_action(tax_or_base='tax', move_type='refund')
  188. @api.multi
  189. def view_base_refund_lines(self):
  190. self.ensure_one()
  191. return self.get_lines_action(tax_or_base='base', move_type='refund')