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.

274 lines
9.8 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  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. )
  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 _is_unsupported_search_operator(self, operator):
  76. return operator != '='
  77. @api.model
  78. def _search_has_moves(self, operator, value):
  79. if self._is_unsupported_search_operator(operator) or not value:
  80. raise ValueError(_("Unsupported search operator"))
  81. ids_with_moves = self._account_tax_ids_with_moves()
  82. return [('id', 'in', ids_with_moves)]
  83. def _compute_balance(self):
  84. for tax in self:
  85. tax.balance_regular = tax.compute_balance(
  86. tax_or_base='tax', move_type='regular')
  87. tax.base_balance_regular = tax.compute_balance(
  88. tax_or_base='base', move_type='regular')
  89. tax.balance_refund = tax.compute_balance(
  90. tax_or_base='tax', move_type='refund')
  91. tax.base_balance_refund = tax.compute_balance(
  92. tax_or_base='base', move_type='refund')
  93. tax.balance = tax.balance_regular + tax.balance_refund
  94. tax.base_balance = (
  95. tax.base_balance_regular + tax.base_balance_refund)
  96. def get_target_type_list(self, move_type=None):
  97. if move_type == 'refund':
  98. return ['receivable_refund', 'payable_refund']
  99. elif move_type == 'regular':
  100. return ['receivable', 'payable', 'liquidity', 'other']
  101. return []
  102. def get_target_state_list(self, target_move="posted"):
  103. if target_move == 'posted':
  104. state = ['posted']
  105. elif target_move == 'all':
  106. state = ['posted', 'draft']
  107. else:
  108. state = []
  109. return state
  110. def get_move_line_partial_where(self, from_date, to_date, company_ids):
  111. query = "aml.date <= %s AND aml.date >= %s AND aml.company_id IN %s"
  112. params = [to_date, from_date, tuple(company_ids)]
  113. return query, params
  114. def compute_balance(self, tax_or_base="tax", move_type=None):
  115. # There's really bad performace in m2m fields.
  116. # So we better do a direct query.
  117. # See https://github.com/odoo/odoo/issues/30350
  118. self.ensure_one()
  119. _select, _group_by, query, params = self.get_move_lines_query(
  120. tax_or_base=tax_or_base, move_type=move_type
  121. )
  122. _select = "SUM(aml.balance)"
  123. query = query.format(select_clause=_select, group_by_clause=_group_by)
  124. self.env.cr.execute(query, params) # pylint: disable=E8103
  125. total_balance = {}
  126. for balance, tax_id in self.env.cr.fetchall():
  127. total_balance[tax_id] = balance
  128. return False # TODO
  129. def get_move_lines_query(self, tax_or_base="tax", move_type=None):
  130. from_date, to_date, company_ids, target_move = self.get_context_values()
  131. state_list = self.get_target_state_list(target_move)
  132. type_list = self.get_target_type_list(move_type)
  133. base_query = self.get_move_lines_base_query()
  134. _where = ""
  135. _joins = ""
  136. _group_by = ""
  137. _params = []
  138. _select = "SELECT SUM(balance)"
  139. _group_by = "GROUP BY "
  140. where, params = self.get_move_line_partial_where(
  141. from_date, to_date, company_ids
  142. )
  143. _where += where
  144. _params += params
  145. if tax_or_base == "tax":
  146. select, where, group_by, params = self.get_balance_where(
  147. state_list, type_list
  148. )
  149. _where += where
  150. _params += params
  151. _select += select
  152. _group_by += group_by
  153. elif tax_or_base == "base":
  154. select, joins, where, group_by, params = self.get_base_balance_where(
  155. state_list, type_list
  156. )
  157. _where += where
  158. _joins += joins
  159. _params += params
  160. _select += select
  161. _group_by += group_by
  162. query = base_query.format(
  163. select_clause="{select_clause}",
  164. where_clause=_where,
  165. additional_joins=_joins,
  166. group_by="{group_by_clause}",
  167. )
  168. return _select, _group_by, query, _params
  169. def get_move_lines_base_query(self):
  170. return (
  171. "SELECT {select_clause} FROM account_move_line AS aml "
  172. "INNER JOIN account_move AS am ON aml.move_id = am.id "
  173. "{additional_joins}"
  174. " WHERE {where_clause}"
  175. "{group_by_clause}"
  176. )
  177. def get_balance_where(self, state_list, type_list):
  178. select = ", tax_line_id as tax_id"
  179. where = (
  180. " AND am.state IN %s AND "
  181. "aml.tax_line_id = %s AND "
  182. "aml.tax_exigible = True"
  183. )
  184. group_by = "aml.tax_line_id"
  185. params = [tuple(state_list), self.id]
  186. if type_list:
  187. where += " AND am.move_type IN %s"
  188. params += [tuple(type_list)]
  189. return select, where, group_by, params
  190. def get_base_balance_where(self, state_list, type_list):
  191. select = ", rel.account_tax_id as tax_id"
  192. joins = (
  193. " INNER JOIN account_move_line_account_tax_rel AS rel "
  194. "ON aml.id = rel.account_move_line_id"
  195. )
  196. group_by = "rel.account_tax_id"
  197. where = " AND am.state IN %s" " AND tax.id = %s" " AND aml.tax_exigible = True "
  198. params = [tuple(state_list), self.id]
  199. if type_list:
  200. where += " AND am.move_type IN %s"
  201. params += [tuple(type_list)]
  202. return select, joins, where, group_by, params
  203. def get_move_lines_domain(self, tax_or_base="tax", move_type=None):
  204. _select, _group_by, query, params = self.get_move_lines_query(
  205. tax_or_base=tax_or_base, move_type=move_type
  206. )
  207. _select = "aml.id"
  208. _group_by = ""
  209. query = query.format(select_clause=_select, group_by_clause=_group_by)
  210. self.env.cr.execute(query, params) # pylint: disable=E8103
  211. amls = []
  212. for (aml_id,) in self.env.cr.fetchall():
  213. amls.append(aml_id)
  214. domain = [("id", "in", amls)]
  215. return domain
  216. def get_lines_action(self, tax_or_base='tax', move_type=None):
  217. domain = self.get_move_lines_domain(
  218. tax_or_base=tax_or_base, move_type=move_type)
  219. action = self.env.ref('account.action_account_moves_all_tree')
  220. vals = action.read()[0]
  221. vals['context'] = {}
  222. vals['domain'] = domain
  223. return vals
  224. @api.multi
  225. def view_tax_lines(self):
  226. self.ensure_one()
  227. return self.get_lines_action(tax_or_base='tax')
  228. @api.multi
  229. def view_base_lines(self):
  230. self.ensure_one()
  231. return self.get_lines_action(tax_or_base='base')
  232. @api.multi
  233. def view_tax_regular_lines(self):
  234. self.ensure_one()
  235. return self.get_lines_action(tax_or_base='tax', move_type='regular')
  236. @api.multi
  237. def view_base_regular_lines(self):
  238. self.ensure_one()
  239. return self.get_lines_action(tax_or_base='base', move_type='regular')
  240. @api.multi
  241. def view_tax_refund_lines(self):
  242. self.ensure_one()
  243. return self.get_lines_action(tax_or_base='tax', move_type='refund')
  244. @api.multi
  245. def view_base_refund_lines(self):
  246. self.ensure_one()
  247. return self.get_lines_action(tax_or_base='base', move_type='refund')