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.

250 lines
8.9 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. def get_context_values(self):
  26. context = self.env.context
  27. return (
  28. context.get('from_date', fields.Date.context_today(self)),
  29. context.get('to_date', fields.Date.context_today(self)),
  30. context.get('company_id', self.env.user.company_id.id),
  31. context.get('target_move', 'posted'),
  32. )
  33. @api.model
  34. def _is_unsupported_search_operator(self, operator):
  35. return operator != '='
  36. def _compute_balance(self):
  37. total_balance_regular = self.compute_balance(
  38. tax_or_base="tax", move_type="regular"
  39. )
  40. total_base_balance_regular = self.compute_balance(
  41. tax_or_base="base", move_type="regular"
  42. )
  43. total_balance_refund = self.compute_balance(
  44. tax_or_base="tax", move_type="refund"
  45. )
  46. total_base_balance_refund = self.compute_balance(
  47. tax_or_base="base", move_type="refund"
  48. )
  49. for tax in self:
  50. tax.balance_regular = (
  51. total_balance_regular[tax.id]
  52. if tax.id in total_balance_regular.keys()
  53. else 0.0
  54. )
  55. tax.base_balance_regular = (
  56. total_base_balance_regular[tax.id]
  57. if tax.id in total_base_balance_regular.keys()
  58. else 0.0
  59. )
  60. tax.balance_refund = (
  61. total_balance_refund[tax.id]
  62. if tax.id in total_balance_refund.keys()
  63. else 0.0
  64. )
  65. tax.base_balance_refund = (
  66. total_base_balance_refund[tax.id]
  67. if tax.id in total_base_balance_refund.keys()
  68. else 0.0
  69. )
  70. tax.balance = tax.balance_regular + tax.balance_refund
  71. tax.base_balance = (
  72. tax.base_balance_regular + tax.base_balance_refund)
  73. def get_target_type_list(self, move_type=None):
  74. if move_type == 'refund':
  75. return ['receivable_refund', 'payable_refund']
  76. elif move_type == 'regular':
  77. return ['receivable', 'payable', 'liquidity', 'other']
  78. return []
  79. def get_target_state_list(self, target_move="posted"):
  80. if target_move == 'posted':
  81. state = ['posted']
  82. elif target_move == 'all':
  83. state = ['posted', 'draft']
  84. else:
  85. state = []
  86. return state
  87. def get_move_line_partial_where(self, from_date, to_date, company_ids):
  88. query = "aml.date <= %s AND aml.date >= %s AND aml.company_id IN %s"
  89. params = [to_date, from_date, tuple(company_ids)]
  90. return query, params
  91. def compute_balance(self, tax_or_base="tax", move_type=None):
  92. # There's really bad performace in m2m fields.
  93. # So we better do a direct query.
  94. # See https://github.com/odoo/odoo/issues/30350
  95. _select, _group_by, query, params = self.get_move_lines_query(
  96. tax_or_base=tax_or_base, move_type=move_type
  97. )
  98. query = query.format(select_clause=_select, group_by_clause=_group_by)
  99. self.env.cr.execute(query, params) # pylint: disable=E8103
  100. results = self.env.cr.fetchall()
  101. total_balance = {}
  102. for balance, tax_id in results:
  103. total_balance[tax_id] = balance
  104. return total_balance
  105. def get_move_lines_query(self, tax_or_base="tax", move_type=None):
  106. from_date, to_date, company_ids, target_move = self.get_context_values()
  107. state_list = self.get_target_state_list(target_move)
  108. type_list = self.get_target_type_list(move_type)
  109. base_query = self.get_move_lines_base_query()
  110. _where = ""
  111. _joins = ""
  112. _group_by = ""
  113. _params = []
  114. _select = "SELECT SUM(balance)"
  115. _group_by = " GROUP BY "
  116. where, params = self.get_move_line_partial_where(
  117. from_date, to_date, company_ids
  118. )
  119. _where += where
  120. _params += params
  121. if tax_or_base == "tax":
  122. select, where, group_by, params = self.get_balance_where(
  123. state_list, type_list
  124. )
  125. _where += where
  126. _params += params
  127. _select += select
  128. _group_by += group_by
  129. elif tax_or_base == "base":
  130. select, joins, where, group_by, params = self.get_base_balance_where(
  131. state_list, type_list
  132. )
  133. _where += where
  134. _joins += joins
  135. _params += params
  136. _select += select
  137. _group_by += group_by
  138. query = base_query.format(
  139. select_clause="{select_clause}",
  140. where_clause=_where,
  141. additional_joins=_joins,
  142. group_by_clause="{group_by_clause}",
  143. )
  144. return _select, _group_by, query, _params
  145. def get_move_lines_base_query(self):
  146. return (
  147. "{select_clause} FROM account_move_line AS aml "
  148. "INNER JOIN account_move AS am ON aml.move_id = am.id "
  149. "{additional_joins}"
  150. " WHERE {where_clause}"
  151. "{group_by_clause}"
  152. )
  153. def get_balance_where(self, state_list, type_list):
  154. select = ", aml.tax_line_id as tax_id"
  155. where = (
  156. " AND am.state IN %s"
  157. " AND aml.tax_line_id IS NOT NULL"
  158. " AND aml.tax_exigible = True"
  159. )
  160. group_by = "aml.tax_line_id"
  161. params = [tuple(state_list)]
  162. if type_list:
  163. where += " AND am.move_type IN %s"
  164. params += [tuple(type_list)]
  165. return select, where, group_by, params
  166. def get_base_balance_where(self, state_list, type_list):
  167. select = ", rel.account_tax_id as tax_id"
  168. joins = (
  169. " INNER JOIN account_move_line_account_tax_rel AS rel "
  170. "ON aml.id = rel.account_move_line_id"
  171. )
  172. group_by = "rel.account_tax_id"
  173. where = " AND am.state IN %s" " AND aml.tax_exigible = True "
  174. params = [tuple(state_list)]
  175. if type_list:
  176. where += " AND am.move_type IN %s"
  177. params += [tuple(type_list)]
  178. return select, joins, where, group_by, params
  179. def get_move_lines_domain(self, tax_or_base="tax", move_type=None):
  180. _select, _group_by, query, params = self.get_move_lines_query(
  181. tax_or_base=tax_or_base, move_type=move_type
  182. )
  183. _select = "SELECT aml.id"
  184. _group_by = ""
  185. if tax_or_base == "tax":
  186. query += " AND aml.tax_line_id = " + str(self.id)
  187. elif tax_or_base == "base":
  188. query += " AND rel.account_tax_id = " + str(self.id)
  189. query = query.format(select_clause=_select, group_by_clause=_group_by)
  190. self.env.cr.execute(query, params) # pylint: disable=E8103
  191. amls = []
  192. for (aml_id,) in self.env.cr.fetchall():
  193. amls.append(aml_id)
  194. domain = [("id", "in", amls)]
  195. return domain
  196. def get_lines_action(self, tax_or_base='tax', move_type=None):
  197. domain = self.get_move_lines_domain(
  198. tax_or_base=tax_or_base, move_type=move_type)
  199. action = self.env.ref('account.action_account_moves_all_tree')
  200. vals = action.read()[0]
  201. vals['context'] = {}
  202. vals['domain'] = domain
  203. return vals
  204. @api.multi
  205. def view_tax_lines(self):
  206. self.ensure_one()
  207. return self.get_lines_action(tax_or_base='tax')
  208. @api.multi
  209. def view_base_lines(self):
  210. self.ensure_one()
  211. return self.get_lines_action(tax_or_base='base')
  212. @api.multi
  213. def view_tax_regular_lines(self):
  214. self.ensure_one()
  215. return self.get_lines_action(tax_or_base='tax', move_type='regular')
  216. @api.multi
  217. def view_base_regular_lines(self):
  218. self.ensure_one()
  219. return self.get_lines_action(tax_or_base='base', move_type='regular')
  220. @api.multi
  221. def view_tax_refund_lines(self):
  222. self.ensure_one()
  223. return self.get_lines_action(tax_or_base='tax', move_type='refund')
  224. @api.multi
  225. def view_base_refund_lines(self):
  226. self.ensure_one()
  227. return self.get_lines_action(tax_or_base='base', move_type='refund')