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.

205 lines
7.6 KiB

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