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.

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