account_move_line_tax_details.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. # -*- coding: utf-8 -*-
  2. from odoo import api, models
  3. class AccountMoveLine(models.Model):
  4. _inherit = 'account.move.line'
  5. @api.model
  6. def _get_query_tax_details_from_domain(self, domain, fallback=True):
  7. """ Create the tax details sub-query based on the orm domain passed as parameter.
  8. :param domain: An orm domain on account.move.line.
  9. :param fallback: Fallback on an approximated mapping if the mapping failed.
  10. :return: A tuple <query, params>.
  11. """
  12. self.env['account.move.line'].check_access_rights('read')
  13. query = self.env['account.move.line']._where_calc(domain)
  14. # Wrap the query with 'company_id IN (...)' to avoid bypassing company access rights.
  15. self.env['account.move.line']._apply_ir_rules(query)
  16. tables, where_clause, where_params = query.get_sql()
  17. return self._get_query_tax_details(tables, where_clause, where_params, fallback=fallback)
  18. @api.model
  19. def _get_extra_query_base_tax_line_mapping(self):
  20. #TO OVERRIDE
  21. return ''
  22. @api.model
  23. def _get_query_tax_details(self, tables, where_clause, where_params, fallback=True):
  24. """ Create the tax details sub-query based on the orm domain passed as parameter.
  25. :param tables: The 'tables' query to inject after the FROM.
  26. :param where_clause: The 'where_clause' query computed based on an orm domain.
  27. :param where_params: The params to fill the 'where_clause' query.
  28. :param fallback: Fallback on an approximated mapping if the mapping failed.
  29. :return: A tuple <query, params>.
  30. """
  31. group_taxes = self.env['account.tax'].search([('amount_type', '=', 'group')])
  32. group_taxes_query_list = []
  33. group_taxes_params = []
  34. for group_tax in group_taxes:
  35. children_taxes = group_tax.children_tax_ids
  36. if not children_taxes:
  37. continue
  38. children_taxes_in_query = ','.join('%s' for dummy in children_taxes)
  39. group_taxes_query_list.append(f'WHEN tax.id = %s THEN ARRAY[{children_taxes_in_query}]')
  40. group_taxes_params.append(group_tax.id)
  41. group_taxes_params.extend(children_taxes.ids)
  42. if group_taxes_query_list:
  43. group_taxes_query = f'''UNNEST(CASE {' '.join(group_taxes_query_list)} ELSE ARRAY[tax.id] END)'''
  44. else:
  45. group_taxes_query = 'tax.id'
  46. if fallback:
  47. fallback_query = f'''
  48. UNION ALL
  49. SELECT
  50. account_move_line.id AS tax_line_id,
  51. base_line.id AS base_line_id,
  52. base_line.id AS src_line_id,
  53. base_line.balance AS base_amount,
  54. base_line.amount_currency AS base_amount_currency
  55. FROM {tables}
  56. LEFT JOIN base_tax_line_mapping ON
  57. base_tax_line_mapping.tax_line_id = account_move_line.id
  58. JOIN account_move_line_account_tax_rel tax_rel ON
  59. tax_rel.account_tax_id = COALESCE(account_move_line.group_tax_id, account_move_line.tax_line_id)
  60. JOIN account_move_line base_line ON
  61. base_line.id = tax_rel.account_move_line_id
  62. AND base_line.tax_repartition_line_id IS NULL
  63. AND base_line.move_id = account_move_line.move_id
  64. AND base_line.currency_id = account_move_line.currency_id
  65. WHERE base_tax_line_mapping.tax_line_id IS NULL
  66. AND {where_clause}
  67. '''
  68. fallback_params = where_params
  69. else:
  70. fallback_query = ''
  71. fallback_params = []
  72. extra_query_base_tax_line_mapping = self._get_extra_query_base_tax_line_mapping()
  73. return f'''
  74. /*
  75. As example to explain the different parts of the query, we'll consider a move with the following lines:
  76. Name Tax_line_id Tax_ids Debit Credit Base lines
  77. ---------------------------------------------------------------------------------------------------
  78. base_line_1 10_affect_base, 20 1000
  79. base_line_2 10_affect_base, 5 2000
  80. base_line_3 10_affect_base, 5 3000
  81. tax_line_1 10_affect_base 20 100 base_line_1
  82. tax_line_2 20 220 base_line_1
  83. tax_line_3 10_affect_base 5 500 base_line_2/3
  84. tax_line_4 5 275 base_line_2/3
  85. */
  86. WITH affecting_base_tax_ids AS (
  87. /*
  88. This CTE builds a reference table based on the tax_ids field, with the following changes:
  89. - flatten the group of taxes
  90. - exclude the taxes having 'is_base_affected' set to False.
  91. Those allow to match only base_line_1 when finding the base lines of tax_line_1, as we need to find
  92. base lines having a 'affecting_base_tax_ids' ending with [10_affect_base, 20], not only containing
  93. '10_affect_base'. Otherwise, base_line_2/3 would also be matched.
  94. In our example, as all the taxes are set to be affected by previous ones affecting the base, the
  95. result is similar to the table 'account_move_line_account_tax_rel':
  96. Id Tax_ids
  97. -------------------------------------------
  98. base_line_1 [10_affect_base, 20]
  99. base_line_2 [10_affect_base, 5]
  100. base_line_3 [10_affect_base, 5]
  101. */
  102. SELECT
  103. sub.line_id AS id,
  104. ARRAY_AGG(sub.tax_id ORDER BY sub.sequence, sub.tax_id) AS tax_ids
  105. FROM (
  106. SELECT
  107. tax_rel.account_move_line_id AS line_id,
  108. {group_taxes_query} AS tax_id,
  109. tax.sequence
  110. FROM {tables}
  111. JOIN account_move_line_account_tax_rel tax_rel ON account_move_line.id = tax_rel.account_move_line_id
  112. JOIN account_tax tax ON tax.id = tax_rel.account_tax_id
  113. WHERE tax.is_base_affected
  114. AND {where_clause}
  115. ) AS sub
  116. GROUP BY sub.line_id
  117. ),
  118. base_tax_line_mapping AS (
  119. /*
  120. Create the mapping of each tax lines with their corresponding base lines.
  121. In the example, it will give the following values:
  122. base_line_id tax_line_id base_amount
  123. -------------------------------------------
  124. base_line_1 tax_line_1 1000
  125. base_line_1 tax_line_2 1000
  126. base_line_2 tax_line_3 2000
  127. base_line_2 tax_line_4 2000
  128. base_line_3 tax_line_3 3000
  129. base_line_3 tax_line_4 3000
  130. */
  131. SELECT
  132. account_move_line.id AS tax_line_id,
  133. base_line.id AS base_line_id,
  134. base_line.balance AS base_amount,
  135. base_line.amount_currency AS base_amount_currency
  136. FROM {tables}
  137. JOIN account_tax_repartition_line tax_rep ON
  138. tax_rep.id = account_move_line.tax_repartition_line_id
  139. JOIN account_tax tax ON
  140. tax.id = account_move_line.tax_line_id
  141. JOIN res_currency curr ON
  142. curr.id = account_move_line.currency_id
  143. JOIN res_currency comp_curr ON
  144. comp_curr.id = account_move_line.company_currency_id
  145. JOIN account_move_line_account_tax_rel tax_rel ON
  146. tax_rel.account_tax_id = COALESCE(account_move_line.group_tax_id, account_move_line.tax_line_id)
  147. JOIN account_move move ON
  148. move.id = account_move_line.move_id
  149. JOIN account_move_line base_line ON
  150. base_line.id = tax_rel.account_move_line_id
  151. AND base_line.tax_repartition_line_id IS NULL
  152. AND base_line.move_id = account_move_line.move_id
  153. AND (
  154. move.move_type != 'entry'
  155. OR
  156. sign(account_move_line.balance) = sign(base_line.balance * tax.amount * tax_rep.factor_percent)
  157. )
  158. AND COALESCE(base_line.partner_id, 0) = COALESCE(account_move_line.partner_id, 0)
  159. AND base_line.currency_id = account_move_line.currency_id
  160. AND (
  161. COALESCE(tax_rep.account_id, base_line.account_id) = account_move_line.account_id
  162. OR (tax.tax_exigibility = 'on_payment' AND tax.cash_basis_transition_account_id IS NOT NULL)
  163. )
  164. AND (
  165. NOT tax.analytic
  166. OR (base_line.analytic_distribution IS NULL AND account_move_line.analytic_distribution IS NULL)
  167. OR base_line.analytic_distribution = account_move_line.analytic_distribution
  168. )
  169. {extra_query_base_tax_line_mapping}
  170. LEFT JOIN affecting_base_tax_ids tax_line_tax_ids ON tax_line_tax_ids.id = account_move_line.id
  171. JOIN affecting_base_tax_ids base_line_tax_ids ON base_line_tax_ids.id = base_line.id
  172. WHERE account_move_line.tax_repartition_line_id IS NOT NULL
  173. AND {where_clause}
  174. AND (
  175. -- keeping only the rows from affecting_base_tax_lines that end with the same taxes applied (see comment in affecting_base_tax_ids)
  176. NOT tax.include_base_amount
  177. OR base_line_tax_ids.tax_ids[ARRAY_LENGTH(base_line_tax_ids.tax_ids, 1) - COALESCE(ARRAY_LENGTH(tax_line_tax_ids.tax_ids, 1), 0):ARRAY_LENGTH(base_line_tax_ids.tax_ids, 1)]
  178. = ARRAY[account_move_line.tax_line_id] || COALESCE(tax_line_tax_ids.tax_ids, ARRAY[]::INTEGER[])
  179. )
  180. ),
  181. tax_amount_affecting_base_to_dispatch AS (
  182. /*
  183. Computes the total amount to dispatch in case of tax lines affecting the base of subsequent taxes.
  184. Such tax lines are an additional base amount for others lines, that will be truly dispatch in next
  185. CTE.
  186. In the example:
  187. - tax_line_1 is an additional base of 100.0 from base_line_1 for tax_line_2.
  188. - tax_line_3 is an additional base of 2/5 * 500.0 = 200.0 from base_line_2 for tax_line_4.
  189. - tax_line_3 is an additional base of 3/5 * 500.0 = 300.0 from base_line_3 for tax_line_4.
  190. src_line_id base_line_id tax_line_id total_base_amount
  191. -------------------------------------------------------------
  192. tax_line_1 base_line_1 tax_line_2 1000
  193. tax_line_3 base_line_2 tax_line_4 5000
  194. tax_line_3 base_line_3 tax_line_4 5000
  195. */
  196. SELECT
  197. tax_line.id AS tax_line_id,
  198. base_line.id AS base_line_id,
  199. account_move_line.id AS src_line_id,
  200. tax_line.company_id,
  201. comp_curr.id AS company_currency_id,
  202. comp_curr.decimal_places AS comp_curr_prec,
  203. curr.id AS currency_id,
  204. curr.decimal_places AS curr_prec,
  205. tax_line.tax_line_id AS tax_id,
  206. base_line.balance AS base_amount,
  207. SUM(
  208. CASE WHEN tax.amount_type = 'fixed'
  209. THEN CASE WHEN base_line.balance < 0 THEN -1 ELSE 1 END * ABS(COALESCE(base_line.quantity, 1.0))
  210. ELSE base_line.balance
  211. END
  212. ) OVER (PARTITION BY tax_line.id, account_move_line.id ORDER BY tax_line.tax_line_id, base_line.id) AS cumulated_base_amount,
  213. SUM(
  214. CASE WHEN tax.amount_type = 'fixed'
  215. THEN CASE WHEN base_line.balance < 0 THEN -1 ELSE 1 END * ABS(COALESCE(base_line.quantity, 1.0))
  216. ELSE base_line.balance
  217. END
  218. ) OVER (PARTITION BY tax_line.id, account_move_line.id) AS total_base_amount,
  219. account_move_line.balance AS total_tax_amount,
  220. base_line.amount_currency AS base_amount_currency,
  221. SUM(
  222. CASE WHEN tax.amount_type = 'fixed'
  223. THEN CASE WHEN base_line.amount_currency < 0 THEN -1 ELSE 1 END * ABS(COALESCE(base_line.quantity, 1.0))
  224. ELSE base_line.amount_currency
  225. END
  226. ) OVER (PARTITION BY tax_line.id, account_move_line.id ORDER BY tax_line.tax_line_id, base_line.id) AS cumulated_base_amount_currency,
  227. SUM(
  228. CASE WHEN tax.amount_type = 'fixed'
  229. THEN CASE WHEN base_line.amount_currency < 0 THEN -1 ELSE 1 END * ABS(COALESCE(base_line.quantity, 1.0))
  230. ELSE base_line.amount_currency
  231. END
  232. ) OVER (PARTITION BY tax_line.id, account_move_line.id) AS total_base_amount_currency,
  233. account_move_line.amount_currency AS total_tax_amount_currency
  234. FROM {tables}
  235. JOIN account_tax tax_include_base_amount ON
  236. tax_include_base_amount.include_base_amount
  237. AND tax_include_base_amount.id = account_move_line.tax_line_id
  238. JOIN base_tax_line_mapping base_tax_line_mapping ON
  239. base_tax_line_mapping.tax_line_id = account_move_line.id
  240. JOIN account_move_line_account_tax_rel tax_rel ON
  241. tax_rel.account_move_line_id = base_tax_line_mapping.tax_line_id
  242. JOIN account_tax tax ON
  243. tax.id = tax_rel.account_tax_id
  244. JOIN base_tax_line_mapping tax_line_matching ON
  245. tax_line_matching.base_line_id = base_tax_line_mapping.base_line_id
  246. JOIN account_move_line tax_line ON
  247. tax_line.id = tax_line_matching.tax_line_id
  248. AND tax_line.tax_line_id = tax_rel.account_tax_id
  249. JOIN res_currency curr ON
  250. curr.id = tax_line.currency_id
  251. JOIN res_currency comp_curr ON
  252. comp_curr.id = tax_line.company_currency_id
  253. JOIN account_move_line base_line ON
  254. base_line.id = base_tax_line_mapping.base_line_id
  255. WHERE {where_clause}
  256. ),
  257. base_tax_matching_base_amounts AS (
  258. /*
  259. Build here the full mapping tax lines <=> base lines containing the final base amounts.
  260. This is done in a 3-parts union.
  261. Note: src_line_id is used only to build a unique ID.
  262. */
  263. /*
  264. PART 1: raw mapping computed in base_tax_line_mapping.
  265. */
  266. SELECT
  267. tax_line_id,
  268. base_line_id,
  269. base_line_id AS src_line_id,
  270. base_amount,
  271. base_amount_currency
  272. FROM base_tax_line_mapping
  273. UNION ALL
  274. /*
  275. PART 2: Dispatch the tax amount of tax lines affecting the base of subsequent ones, using
  276. tax_amount_affecting_base_to_dispatch.
  277. This will effectively add the following rows:
  278. base_line_id tax_line_id src_line_id base_amount
  279. -------------------------------------------------------------
  280. base_line_1 tax_line_2 tax_line_1 100
  281. base_line_2 tax_line_4 tax_line_3 200
  282. base_line_3 tax_line_4 tax_line_3 300
  283. */
  284. SELECT
  285. sub.tax_line_id,
  286. sub.base_line_id,
  287. sub.src_line_id,
  288. ROUND(
  289. COALESCE(SIGN(sub.cumulated_base_amount) * sub.total_tax_amount * ABS(sub.cumulated_base_amount) / NULLIF(sub.total_base_amount, 0.0), 0.0),
  290. sub.comp_curr_prec
  291. )
  292. - LAG(ROUND(
  293. COALESCE(SIGN(sub.cumulated_base_amount) * sub.total_tax_amount * ABS(sub.cumulated_base_amount) / NULLIF(sub.total_base_amount, 0.0), 0.0),
  294. sub.comp_curr_prec
  295. ), 1, 0.0)
  296. OVER (
  297. PARTITION BY sub.tax_line_id, sub.src_line_id ORDER BY sub.tax_id, sub.base_line_id
  298. ) AS base_amount,
  299. ROUND(
  300. COALESCE(SIGN(sub.cumulated_base_amount_currency) * sub.total_tax_amount_currency * ABS(sub.cumulated_base_amount_currency) / NULLIF(sub.total_base_amount_currency, 0.0), 0.0),
  301. sub.curr_prec
  302. )
  303. - LAG(ROUND(
  304. COALESCE(SIGN(sub.cumulated_base_amount_currency) * sub.total_tax_amount_currency * ABS(sub.cumulated_base_amount_currency) / NULLIF(sub.total_base_amount_currency, 0.0), 0.0),
  305. sub.curr_prec
  306. ), 1, 0.0)
  307. OVER (
  308. PARTITION BY sub.tax_line_id, sub.src_line_id ORDER BY sub.tax_id, sub.base_line_id
  309. ) AS base_amount_currency
  310. FROM tax_amount_affecting_base_to_dispatch sub
  311. JOIN account_move_line tax_line ON
  312. tax_line.id = sub.tax_line_id
  313. /*
  314. PART 3: In case of the matching failed because the configuration changed or some journal entries
  315. have been imported, construct a simple mapping as a fallback. This mapping is super naive and only
  316. build based on the 'tax_ids' and 'tax_line_id' fields, nothing else. Hence, the mapping will not be
  317. exact but will give an acceptable approximation.
  318. Skipped if the 'fallback' method parameter is False.
  319. */
  320. {fallback_query}
  321. ),
  322. base_tax_matching_all_amounts AS (
  323. /*
  324. Complete base_tax_matching_base_amounts with the tax amounts (prorata):
  325. base_line_id tax_line_id src_line_id base_amount tax_amount
  326. --------------------------------------------------------------------------
  327. base_line_1 tax_line_1 base_line_1 1000 100
  328. base_line_1 tax_line_2 base_line_1 1000 (1000 / 1100) * 220 = 200
  329. base_line_1 tax_line_2 tax_line_1 100 (100 / 1100) * 220 = 20
  330. base_line_2 tax_line_3 base_line_2 2000 (2000 / 5000) * 500 = 200
  331. base_line_2 tax_line_4 base_line_2 2000 (2000 / 5500) * 275 = 100
  332. base_line_2 tax_line_4 tax_line_3 200 (200 / 5500) * 275 = 10
  333. base_line_3 tax_line_3 base_line_3 3000 (3000 / 5000) * 500 = 300
  334. base_line_3 tax_line_4 base_line_3 3000 (3000 / 5500) * 275 = 150
  335. base_line_3 tax_line_4 tax_line_3 300 (300 / 5500) * 275 = 15
  336. */
  337. SELECT
  338. sub.tax_line_id,
  339. sub.base_line_id,
  340. sub.src_line_id,
  341. tax_line.tax_line_id AS tax_id,
  342. tax_line.group_tax_id,
  343. tax_line.tax_repartition_line_id,
  344. tax_line.company_id,
  345. tax_line.display_type AS display_type,
  346. comp_curr.id AS company_currency_id,
  347. comp_curr.decimal_places AS comp_curr_prec,
  348. curr.id AS currency_id,
  349. curr.decimal_places AS curr_prec,
  350. (
  351. tax.tax_exigibility != 'on_payment'
  352. OR tax_move.tax_cash_basis_rec_id IS NOT NULL
  353. OR tax_move.always_tax_exigible
  354. ) AS tax_exigible,
  355. base_line.account_id AS base_account_id,
  356. sub.base_amount,
  357. SUM(
  358. CASE WHEN tax.amount_type = 'fixed'
  359. THEN CASE WHEN base_line.balance < 0 THEN -1 ELSE 1 END * ABS(COALESCE(base_line.quantity, 1.0))
  360. ELSE sub.base_amount
  361. END
  362. ) OVER (PARTITION BY tax_line.id ORDER BY tax_line.tax_line_id, sub.base_line_id, sub.src_line_id) AS cumulated_base_amount,
  363. SUM(
  364. CASE WHEN tax.amount_type = 'fixed'
  365. THEN CASE WHEN base_line.balance < 0 THEN -1 ELSE 1 END * ABS(COALESCE(base_line.quantity, 1.0))
  366. ELSE sub.base_amount
  367. END
  368. ) OVER (PARTITION BY tax_line.id) AS total_base_amount,
  369. tax_line.balance AS total_tax_amount,
  370. sub.base_amount_currency,
  371. SUM(
  372. CASE WHEN tax.amount_type = 'fixed'
  373. THEN CASE WHEN base_line.amount_currency < 0 THEN -1 ELSE 1 END * ABS(COALESCE(base_line.quantity, 1.0))
  374. ELSE sub.base_amount_currency
  375. END
  376. ) OVER (PARTITION BY tax_line.id ORDER BY tax_line.tax_line_id, sub.base_line_id, sub.src_line_id) AS cumulated_base_amount_currency,
  377. SUM(
  378. CASE WHEN tax.amount_type = 'fixed'
  379. THEN CASE WHEN base_line.amount_currency < 0 THEN -1 ELSE 1 END * ABS(COALESCE(base_line.quantity, 1.0))
  380. ELSE sub.base_amount_currency
  381. END
  382. ) OVER (PARTITION BY tax_line.id) AS total_base_amount_currency,
  383. tax_line.amount_currency AS total_tax_amount_currency
  384. FROM base_tax_matching_base_amounts sub
  385. JOIN account_move_line tax_line ON
  386. tax_line.id = sub.tax_line_id
  387. JOIN account_move tax_move ON
  388. tax_move.id = tax_line.move_id
  389. JOIN account_move_line base_line ON
  390. base_line.id = sub.base_line_id
  391. JOIN account_tax tax ON
  392. tax.id = tax_line.tax_line_id
  393. JOIN res_currency curr ON
  394. curr.id = tax_line.currency_id
  395. JOIN res_currency comp_curr ON
  396. comp_curr.id = tax_line.company_currency_id
  397. )
  398. /* Final select that makes sure to deal with rounding errors, using LAG to dispatch the last cents. */
  399. SELECT
  400. sub.tax_line_id || '-' || sub.base_line_id || '-' || sub.src_line_id AS id,
  401. sub.base_line_id,
  402. sub.tax_line_id,
  403. sub.display_type,
  404. sub.src_line_id,
  405. sub.tax_id,
  406. sub.group_tax_id,
  407. sub.tax_exigible,
  408. sub.base_account_id,
  409. sub.tax_repartition_line_id,
  410. sub.base_amount,
  411. COALESCE(
  412. ROUND(
  413. COALESCE(SIGN(sub.cumulated_base_amount) * sub.total_tax_amount * ABS(sub.cumulated_base_amount) / NULLIF(sub.total_base_amount, 0.0), 0.0),
  414. sub.comp_curr_prec
  415. )
  416. - LAG(ROUND(
  417. COALESCE(SIGN(sub.cumulated_base_amount) * sub.total_tax_amount * ABS(sub.cumulated_base_amount) / NULLIF(sub.total_base_amount, 0.0), 0.0),
  418. sub.comp_curr_prec
  419. ), 1, 0.0)
  420. OVER (
  421. PARTITION BY sub.tax_line_id ORDER BY sub.tax_id, sub.base_line_id
  422. ),
  423. 0.0
  424. ) AS tax_amount,
  425. sub.base_amount_currency,
  426. COALESCE(
  427. ROUND(
  428. COALESCE(SIGN(sub.cumulated_base_amount_currency) * sub.total_tax_amount_currency * ABS(sub.cumulated_base_amount_currency) / NULLIF(sub.total_base_amount_currency, 0.0), 0.0),
  429. sub.curr_prec
  430. )
  431. - LAG(ROUND(
  432. COALESCE(SIGN(sub.cumulated_base_amount_currency) * sub.total_tax_amount_currency * ABS(sub.cumulated_base_amount_currency) / NULLIF(sub.total_base_amount_currency, 0.0), 0.0),
  433. sub.curr_prec
  434. ), 1, 0.0)
  435. OVER (
  436. PARTITION BY sub.tax_line_id ORDER BY sub.tax_id, sub.base_line_id
  437. ),
  438. 0.0
  439. ) AS tax_amount_currency
  440. FROM base_tax_matching_all_amounts sub
  441. ''', group_taxes_params + where_params + where_params + where_params + fallback_params