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.

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