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.

223 lines
7.9 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_id', self.env.user.company_id.id),
  36. context.get('target_move', 'posted'),
  37. context.get('display_all', False),
  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, display_all \
  67. = self.get_context_values()
  68. self.env.cr.execute(
  69. req, (company_id, from_date, to_date, company_id))
  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. from_date, to_date, company_id, target_move, display_all \
  82. = self.get_context_values()
  83. if self._is_unsupported_search_operator(operator) or not value:
  84. raise ValueError(_("Unsupported search operator"))
  85. if display_all:
  86. ids_with_moves = self.env['account.tax'].search([]).ids
  87. else:
  88. ids_with_moves = self._account_tax_ids_with_moves()
  89. return [('id', 'in', ids_with_moves)]
  90. def _compute_balance(self):
  91. for tax in self:
  92. tax.balance_regular = tax.compute_balance(
  93. tax_or_base='tax', move_type='regular')
  94. tax.base_balance_regular = tax.compute_balance(
  95. tax_or_base='base', move_type='regular')
  96. tax.balance_refund = tax.compute_balance(
  97. tax_or_base='tax', move_type='refund')
  98. tax.base_balance_refund = tax.compute_balance(
  99. tax_or_base='base', move_type='refund')
  100. tax.balance = tax.balance_regular + tax.balance_refund
  101. tax.base_balance = (
  102. tax.base_balance_regular + tax.base_balance_refund)
  103. def get_target_type_list(self, move_type=None):
  104. if move_type == 'refund':
  105. return ['receivable_refund', 'payable_refund']
  106. elif move_type == 'regular':
  107. return ['receivable', 'payable', 'liquidity', 'other']
  108. return []
  109. def get_target_state_list(self, target_move="posted"):
  110. if target_move == 'posted':
  111. state = ['posted']
  112. elif target_move == 'all':
  113. state = ['posted', 'draft']
  114. else:
  115. state = []
  116. return state
  117. def get_move_line_partial_domain(self, from_date, to_date, company_id):
  118. return [
  119. ('date', '<=', to_date),
  120. ('date', '>=', from_date),
  121. ('company_id', '=', company_id),
  122. ]
  123. def compute_balance(self, tax_or_base='tax', move_type=None):
  124. self.ensure_one()
  125. domain = self.get_move_lines_domain(
  126. tax_or_base=tax_or_base, move_type=move_type)
  127. # balance is debit - credit whereas on tax return you want to see what
  128. # vat has to be paid so:
  129. # VAT on sales (credit) - VAT on purchases (debit).
  130. balance = self.env['account.move.line'].\
  131. read_group(domain, ['balance'], [])[0]['balance']
  132. return balance and -balance or 0
  133. def get_balance_domain(self, state_list, type_list):
  134. domain = [
  135. ('move_id.state', 'in', state_list),
  136. ('tax_line_id', '=', 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_base_balance_domain(self, state_list, type_list):
  143. domain = [
  144. ('move_id.state', 'in', state_list),
  145. ('tax_ids', 'in', self.id),
  146. ('tax_exigible', '=', True)
  147. ]
  148. if type_list:
  149. domain.append(('move_id.move_type', 'in', type_list))
  150. return domain
  151. def get_move_lines_domain(self, tax_or_base='tax', move_type=None):
  152. from_date, to_date, company_id, target_move, display_all \
  153. = self.get_context_values()
  154. state_list = self.get_target_state_list(target_move)
  155. type_list = self.get_target_type_list(move_type)
  156. domain = self.get_move_line_partial_domain(
  157. from_date, to_date, company_id)
  158. balance_domain = []
  159. if tax_or_base == 'tax':
  160. balance_domain = self.get_balance_domain(state_list, type_list)
  161. elif tax_or_base == 'base':
  162. balance_domain = self.get_base_balance_domain(
  163. state_list, type_list)
  164. domain.extend(balance_domain)
  165. return domain
  166. def get_lines_action(self, tax_or_base='tax', move_type=None):
  167. domain = self.get_move_lines_domain(
  168. tax_or_base=tax_or_base, move_type=move_type)
  169. action = self.env.ref('account.action_account_moves_all_tree')
  170. vals = action.read()[0]
  171. vals['context'] = {}
  172. vals['domain'] = domain
  173. return vals
  174. @api.multi
  175. def view_tax_lines(self):
  176. self.ensure_one()
  177. return self.get_lines_action(tax_or_base='tax')
  178. @api.multi
  179. def view_base_lines(self):
  180. self.ensure_one()
  181. return self.get_lines_action(tax_or_base='base')
  182. @api.multi
  183. def view_tax_regular_lines(self):
  184. self.ensure_one()
  185. return self.get_lines_action(tax_or_base='tax', move_type='regular')
  186. @api.multi
  187. def view_base_regular_lines(self):
  188. self.ensure_one()
  189. return self.get_lines_action(tax_or_base='base', move_type='regular')
  190. @api.multi
  191. def view_tax_refund_lines(self):
  192. self.ensure_one()
  193. return self.get_lines_action(tax_or_base='tax', move_type='refund')
  194. @api.multi
  195. def view_base_refund_lines(self):
  196. self.ensure_one()
  197. return self.get_lines_action(tax_or_base='base', move_type='refund')