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.

1780 lines
58 KiB

8 years ago
8 years ago
  1. # -*- coding: utf-8 -*-
  2. # © 2016 Julien Coux (Camptocamp)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo import models, fields, api, _
  5. class GeneralLedgerReport(models.TransientModel):
  6. """ Here, we just define class fields.
  7. For methods, go more bottom at this file.
  8. The class hierarchy is :
  9. * GeneralLedgerReport
  10. ** GeneralLedgerReportAccount
  11. *** GeneralLedgerReportMoveLine
  12. For non receivable/payable accounts
  13. For receivable/payable centralized accounts
  14. *** GeneralLedgerReportPartner
  15. For receivable/payable and not centralized accounts
  16. **** GeneralLedgerReportMoveLine
  17. For receivable/payable and not centralized accounts
  18. """
  19. _name = 'report_general_ledger_qweb'
  20. _inherit = 'report_qweb_abstract'
  21. # Filters fields, used for data computation
  22. date_from = fields.Date()
  23. date_to = fields.Date()
  24. fy_start_date = fields.Date()
  25. only_posted_moves = fields.Boolean()
  26. hide_account_at_0 = fields.Boolean()
  27. foreign_currency = fields.Boolean()
  28. show_analytic_tags = fields.Boolean()
  29. company_id = fields.Many2one(comodel_name='res.company')
  30. filter_account_ids = fields.Many2many(comodel_name='account.account')
  31. filter_partner_ids = fields.Many2many(comodel_name='res.partner')
  32. filter_journal_ids = fields.Many2many(comodel_name='account.journal')
  33. filter_cost_center_ids = fields.Many2many(
  34. comodel_name='account.analytic.account'
  35. )
  36. filter_analytic_tag_ids = fields.Many2many(
  37. comodel_name='account.analytic.tag',
  38. )
  39. centralize = fields.Boolean()
  40. # Flag fields, used for report display
  41. show_cost_center = fields.Boolean(
  42. default=lambda self: self.env.user.has_group(
  43. 'analytic.group_analytic_accounting'
  44. )
  45. )
  46. # Data fields, used to browse report data
  47. account_ids = fields.One2many(
  48. comodel_name='report_general_ledger_qweb_account',
  49. inverse_name='report_id'
  50. )
  51. # Compute of unaffected earnings account
  52. @api.depends('company_id')
  53. def _compute_unaffected_earnings_account(self):
  54. account_type = self.env.ref('account.data_unaffected_earnings')
  55. self.unaffected_earnings_account = self.env['account.account'].search(
  56. [
  57. ('user_type_id', '=', account_type.id),
  58. ('company_id', '=', self.company_id.id)
  59. ])
  60. unaffected_earnings_account = fields.Many2one(
  61. comodel_name='account.account',
  62. compute='_compute_unaffected_earnings_account',
  63. store=True
  64. )
  65. class GeneralLedgerReportAccount(models.TransientModel):
  66. _name = 'report_general_ledger_qweb_account'
  67. _inherit = 'report_qweb_abstract'
  68. _order = 'code ASC'
  69. report_id = fields.Many2one(
  70. comodel_name='report_general_ledger_qweb',
  71. ondelete='cascade',
  72. index=True
  73. )
  74. # Data fields, used to keep link with real object
  75. account_id = fields.Many2one(
  76. 'account.account',
  77. index=True
  78. )
  79. # Data fields, used for report display
  80. code = fields.Char()
  81. name = fields.Char()
  82. initial_debit = fields.Float(digits=(16, 2))
  83. initial_credit = fields.Float(digits=(16, 2))
  84. initial_balance = fields.Float(digits=(16, 2))
  85. currency_id = fields.Many2one(comodel_name='res.currency')
  86. initial_balance_foreign_currency = fields.Float(digits=(16, 2))
  87. final_debit = fields.Float(digits=(16, 2))
  88. final_credit = fields.Float(digits=(16, 2))
  89. final_balance = fields.Float(digits=(16, 2))
  90. final_balance_foreign_currency = fields.Float(digits=(16, 2))
  91. # Flag fields, used for report display and for data computation
  92. is_partner_account = fields.Boolean()
  93. # Data fields, used to browse report data
  94. move_line_ids = fields.One2many(
  95. comodel_name='report_general_ledger_qweb_move_line',
  96. inverse_name='report_account_id'
  97. )
  98. partner_ids = fields.One2many(
  99. comodel_name='report_general_ledger_qweb_partner',
  100. inverse_name='report_account_id'
  101. )
  102. class GeneralLedgerReportPartner(models.TransientModel):
  103. _name = 'report_general_ledger_qweb_partner'
  104. _inherit = 'report_qweb_abstract'
  105. report_account_id = fields.Many2one(
  106. comodel_name='report_general_ledger_qweb_account',
  107. ondelete='cascade',
  108. index=True
  109. )
  110. # Data fields, used to keep link with real object
  111. partner_id = fields.Many2one(
  112. 'res.partner',
  113. index=True
  114. )
  115. # Data fields, used for report display
  116. name = fields.Char()
  117. initial_debit = fields.Float(digits=(16, 2))
  118. initial_credit = fields.Float(digits=(16, 2))
  119. initial_balance = fields.Float(digits=(16, 2))
  120. currency_id = fields.Many2one(comodel_name='res.currency')
  121. initial_balance_foreign_currency = fields.Float(digits=(16, 2))
  122. final_debit = fields.Float(digits=(16, 2))
  123. final_credit = fields.Float(digits=(16, 2))
  124. final_balance = fields.Float(digits=(16, 2))
  125. final_balance_foreign_currency = fields.Float(digits=(16, 2))
  126. # Data fields, used to browse report data
  127. move_line_ids = fields.One2many(
  128. comodel_name='report_general_ledger_qweb_move_line',
  129. inverse_name='report_partner_id'
  130. )
  131. @api.model
  132. def _generate_order_by(self, order_spec, query):
  133. """Custom order to display "No partner allocated" at last position."""
  134. return """
  135. ORDER BY
  136. CASE
  137. WHEN "report_general_ledger_qweb_partner"."partner_id" IS NOT NULL
  138. THEN 0
  139. ELSE 1
  140. END,
  141. "report_general_ledger_qweb_partner"."name"
  142. """
  143. class GeneralLedgerReportMoveLine(models.TransientModel):
  144. _name = 'report_general_ledger_qweb_move_line'
  145. _inherit = 'report_qweb_abstract'
  146. report_account_id = fields.Many2one(
  147. comodel_name='report_general_ledger_qweb_account',
  148. ondelete='cascade',
  149. index=True
  150. )
  151. report_partner_id = fields.Many2one(
  152. comodel_name='report_general_ledger_qweb_partner',
  153. ondelete='cascade',
  154. index=True
  155. )
  156. # Data fields, used to keep link with real object
  157. move_line_id = fields.Many2one('account.move.line')
  158. matched_ml_id = fields.Many2one("account.full.reconcile")
  159. # Data fields, used for report display
  160. date = fields.Date()
  161. entry = fields.Char()
  162. journal = fields.Char()
  163. account = fields.Char()
  164. taxes_description = fields.Char()
  165. partner = fields.Char()
  166. label = fields.Char()
  167. cost_center = fields.Char()
  168. tags = fields.Char()
  169. debit = fields.Float(digits=(16, 2))
  170. credit = fields.Float(digits=(16, 2))
  171. cumul_balance = fields.Float(digits=(16, 2))
  172. currency_id = fields.Many2one(comodel_name='res.currency')
  173. amount_currency = fields.Float(digits=(16, 2))
  174. class GeneralLedgerReportCompute(models.TransientModel):
  175. """ Here, we just define methods.
  176. For class fields, go more top at this file.
  177. """
  178. _inherit = 'report_general_ledger_qweb'
  179. @api.multi
  180. def print_report(self, report_type):
  181. self.ensure_one()
  182. if report_type == 'xlsx':
  183. report_name = 'account_financial_report_qweb.' \
  184. 'report_general_ledger_xlsx'
  185. else:
  186. report_name = 'account_financial_report_qweb.' \
  187. 'report_general_ledger_qweb'
  188. return self.env['report'].get_action(docids=self.ids,
  189. report_name=report_name)
  190. def _get_html(self):
  191. result = {}
  192. rcontext = {}
  193. context = dict(self.env.context)
  194. report = self.browse(context.get('active_id'))
  195. if report:
  196. rcontext['o'] = report
  197. result['html'] = self.env.ref(
  198. 'account_financial_report_qweb.'
  199. 'report_general_ledger_html').render(rcontext)
  200. return result
  201. @api.model
  202. def get_html(self, given_context=None):
  203. return self._get_html()
  204. @api.multi
  205. def compute_data_for_report(
  206. self, with_line_details=True, with_partners=True):
  207. self.ensure_one()
  208. # Compute report data
  209. self._inject_account_values()
  210. if with_partners:
  211. self._inject_partner_values()
  212. if not self.filter_partner_ids:
  213. self._inject_partner_values(only_empty_partner=True)
  214. # Add unaffected earnings account
  215. if (not self.filter_account_ids or
  216. self.unaffected_earnings_account.id in
  217. self.filter_account_ids.ids):
  218. self._inject_unaffected_earnings_account_values()
  219. # Call this function even if we don't want line details because,
  220. # we need to compute
  221. # at least the values for unaffected earnings account
  222. # In this case, only unaffected earnings account values are computed
  223. only_unaffected_earnings_account = not with_line_details
  224. self._inject_line_not_centralized_values(
  225. only_unaffected_earnings_account=only_unaffected_earnings_account
  226. )
  227. if with_line_details:
  228. self._inject_line_not_centralized_values(
  229. is_account_line=False,
  230. is_partner_line=True)
  231. self._inject_line_not_centralized_values(
  232. is_account_line=False,
  233. is_partner_line=True,
  234. only_empty_partner_line=True)
  235. if self.centralize:
  236. self._inject_line_centralized_values()
  237. if self.show_analytic_tags:
  238. # Compute analytic tags
  239. self._compute_analytic_tags()
  240. # Refresh cache because all data are computed with SQL requests
  241. self.invalidate_cache()
  242. def _get_account_sub_subquery_sum_amounts(
  243. self, include_initial_balance, date_included):
  244. """ Return subquery used to compute sum amounts on accounts """
  245. sub_subquery_sum_amounts = """
  246. SELECT
  247. a.id AS account_id,
  248. SUM(ml.debit) AS debit,
  249. SUM(ml.credit) AS credit,
  250. SUM(ml.balance) AS balance,
  251. c.id AS currency_id,
  252. CASE
  253. WHEN c.id IS NOT NULL
  254. THEN SUM(ml.amount_currency)
  255. ELSE NULL
  256. END AS balance_currency
  257. FROM
  258. accounts a
  259. INNER JOIN
  260. account_account_type at ON a.user_type_id = at.id
  261. INNER JOIN
  262. account_move_line ml
  263. ON a.id = ml.account_id
  264. """
  265. if date_included:
  266. sub_subquery_sum_amounts += """
  267. AND ml.date <= %s
  268. """
  269. else:
  270. sub_subquery_sum_amounts += """
  271. AND ml.date < %s
  272. """
  273. if not include_initial_balance:
  274. sub_subquery_sum_amounts += """
  275. AND at.include_initial_balance != TRUE AND ml.date >= %s
  276. """
  277. else:
  278. sub_subquery_sum_amounts += """
  279. AND at.include_initial_balance = TRUE
  280. """
  281. if self.filter_journal_ids:
  282. sub_subquery_sum_amounts += """
  283. AND
  284. ml.journal_id IN (%s)
  285. """ % ', '.join(map(str, self.filter_journal_ids.ids))
  286. if self.only_posted_moves:
  287. sub_subquery_sum_amounts += """
  288. INNER JOIN
  289. account_move m ON ml.move_id = m.id AND m.state = 'posted'
  290. """
  291. if self.filter_cost_center_ids:
  292. sub_subquery_sum_amounts += """
  293. INNER JOIN
  294. account_analytic_account aa
  295. ON
  296. ml.analytic_account_id = aa.id
  297. AND aa.id IN %s
  298. """
  299. if self.filter_analytic_tag_ids:
  300. sub_subquery_sum_amounts += """
  301. INNER JOIN
  302. move_lines_on_tags ON ml.id = move_lines_on_tags.ml_id
  303. """
  304. sub_subquery_sum_amounts += """
  305. LEFT JOIN
  306. res_currency c ON a.currency_id = c.id
  307. """
  308. sub_subquery_sum_amounts += """
  309. GROUP BY
  310. a.id, c.id
  311. """
  312. return sub_subquery_sum_amounts
  313. def _get_final_account_sub_subquery_sum_amounts(self, date_included):
  314. """ Return final subquery used to compute sum amounts on accounts """
  315. subquery_sum_amounts = """
  316. SELECT
  317. sub.account_id AS account_id,
  318. SUM(COALESCE(sub.debit, 0.0)) AS debit,
  319. SUM(COALESCE(sub.credit, 0.0)) AS credit,
  320. SUM(COALESCE(sub.balance, 0.0)) AS balance,
  321. MAX(sub.currency_id) AS currency_id,
  322. SUM(COALESCE(sub.balance_currency, 0.0)) AS balance_currency
  323. FROM
  324. (
  325. """
  326. subquery_sum_amounts += self._get_account_sub_subquery_sum_amounts(
  327. include_initial_balance=False, date_included=date_included
  328. )
  329. subquery_sum_amounts += """
  330. UNION
  331. """
  332. subquery_sum_amounts += self._get_account_sub_subquery_sum_amounts(
  333. include_initial_balance=True, date_included=date_included
  334. )
  335. subquery_sum_amounts += """
  336. ) sub
  337. GROUP BY
  338. sub.account_id
  339. """
  340. return subquery_sum_amounts
  341. def _inject_account_values(self):
  342. """Inject report values for report_general_ledger_qweb_account."""
  343. query_inject_account = """
  344. WITH
  345. accounts AS
  346. (
  347. SELECT
  348. a.id,
  349. a.code,
  350. coalesce(t.value, a.name) AS name,
  351. a.internal_type IN ('payable', 'receivable')
  352. AS is_partner_account,
  353. a.user_type_id,
  354. a.currency_id
  355. FROM
  356. account_account a
  357. """
  358. if (
  359. self.filter_partner_ids or
  360. self.filter_cost_center_ids or
  361. self.filter_analytic_tag_ids
  362. ):
  363. query_inject_account += """
  364. INNER JOIN
  365. account_move_line ml ON a.id = ml.account_id
  366. """
  367. if self.filter_partner_ids:
  368. query_inject_account += """
  369. INNER JOIN
  370. res_partner p ON ml.partner_id = p.id
  371. """
  372. if self.filter_cost_center_ids:
  373. query_inject_account += """
  374. INNER JOIN
  375. account_analytic_account aa
  376. ON
  377. ml.analytic_account_id = aa.id
  378. AND aa.id IN %s
  379. """
  380. if self.filter_analytic_tag_ids:
  381. query_inject_account += """
  382. INNER JOIN
  383. account_analytic_tag_account_move_line_rel atml
  384. ON atml.account_move_line_id = ml.id
  385. INNER JOIN
  386. account_analytic_tag aat
  387. ON
  388. atml.account_analytic_tag_id = aat.id
  389. AND aat.id IN %s
  390. """
  391. query_inject_account += """
  392. LEFT JOIN
  393. ir_translation t
  394. ON
  395. a.id = t.res_id
  396. AND t.name = 'account.account,name'
  397. AND t.lang = %s
  398. """
  399. query_inject_account += """
  400. WHERE
  401. a.company_id = %s
  402. AND a.id != %s
  403. """
  404. if self.filter_account_ids:
  405. query_inject_account += """
  406. AND
  407. a.id IN %s
  408. """
  409. if self.filter_partner_ids:
  410. query_inject_account += """
  411. AND
  412. p.id IN %s
  413. """
  414. if (
  415. self.filter_partner_ids or
  416. self.filter_cost_center_ids or
  417. self.filter_analytic_tag_ids
  418. ):
  419. query_inject_account += """
  420. GROUP BY
  421. a.id, t.value
  422. """
  423. query_inject_account += """
  424. ),
  425. """
  426. if self.filter_analytic_tag_ids:
  427. query_inject_account += """
  428. move_lines_on_tags AS
  429. (
  430. SELECT
  431. DISTINCT ml.id AS ml_id
  432. FROM
  433. accounts a
  434. INNER JOIN
  435. account_move_line ml
  436. ON a.id = ml.account_id
  437. INNER JOIN
  438. account_analytic_tag_account_move_line_rel atml
  439. ON atml.account_move_line_id = ml.id
  440. INNER JOIN
  441. account_analytic_tag aat
  442. ON
  443. atml.account_analytic_tag_id = aat.id
  444. WHERE
  445. aat.id IN %s
  446. ),
  447. """
  448. init_subquery = self._get_final_account_sub_subquery_sum_amounts(
  449. date_included=False
  450. )
  451. final_subquery = self._get_final_account_sub_subquery_sum_amounts(
  452. date_included=True
  453. )
  454. query_inject_account += """
  455. initial_sum_amounts AS ( """ + init_subquery + """ ),
  456. final_sum_amounts AS ( """ + final_subquery + """ )
  457. INSERT INTO
  458. report_general_ledger_qweb_account
  459. (
  460. report_id,
  461. create_uid,
  462. create_date,
  463. account_id,
  464. code,
  465. name,
  466. initial_debit,
  467. initial_credit,
  468. initial_balance,
  469. currency_id,
  470. initial_balance_foreign_currency,
  471. final_debit,
  472. final_credit,
  473. final_balance,
  474. final_balance_foreign_currency,
  475. is_partner_account
  476. )
  477. SELECT
  478. %s AS report_id,
  479. %s AS create_uid,
  480. NOW() AS create_date,
  481. a.id AS account_id,
  482. a.code,
  483. a.name,
  484. COALESCE(i.debit, 0.0) AS initial_debit,
  485. COALESCE(i.credit, 0.0) AS initial_credit,
  486. COALESCE(i.balance, 0.0) AS initial_balance,
  487. c.id AS currency_id,
  488. COALESCE(i.balance_currency, 0.0) AS initial_balance_foreign_currency,
  489. COALESCE(f.debit, 0.0) AS final_debit,
  490. COALESCE(f.credit, 0.0) AS final_credit,
  491. COALESCE(f.balance, 0.0) AS final_balance,
  492. COALESCE(f.balance_currency, 0.0) AS final_balance_foreign_currency,
  493. a.is_partner_account
  494. FROM
  495. accounts a
  496. LEFT JOIN
  497. initial_sum_amounts i ON a.id = i.account_id
  498. LEFT JOIN
  499. final_sum_amounts f ON a.id = f.account_id
  500. LEFT JOIN
  501. res_currency c ON c.id = a.currency_id
  502. WHERE
  503. (
  504. i.debit IS NOT NULL AND i.debit != 0
  505. OR i.credit IS NOT NULL AND i.credit != 0
  506. OR i.balance IS NOT NULL AND i.balance != 0
  507. OR f.debit IS NOT NULL AND f.debit != 0
  508. OR f.credit IS NOT NULL AND f.credit != 0
  509. OR f.balance IS NOT NULL AND f.balance != 0
  510. )
  511. """
  512. if self.hide_account_at_0:
  513. query_inject_account += """
  514. AND
  515. f.balance IS NOT NULL AND f.balance != 0
  516. """
  517. query_inject_account_params = ()
  518. if self.filter_cost_center_ids:
  519. query_inject_account_params += (
  520. tuple(self.filter_cost_center_ids.ids),
  521. )
  522. if self.filter_analytic_tag_ids:
  523. query_inject_account_params += (
  524. tuple(self.filter_analytic_tag_ids.ids),
  525. )
  526. query_inject_account_params += (
  527. self.env.user.lang,
  528. self.company_id.id,
  529. self.unaffected_earnings_account.id,
  530. )
  531. if self.filter_account_ids:
  532. query_inject_account_params += (
  533. tuple(self.filter_account_ids.ids),
  534. )
  535. if self.filter_partner_ids:
  536. query_inject_account_params += (
  537. tuple(self.filter_partner_ids.ids),
  538. )
  539. if self.filter_analytic_tag_ids:
  540. query_inject_account_params += (
  541. tuple(self.filter_analytic_tag_ids.ids),
  542. )
  543. query_inject_account_params += (
  544. self.date_from,
  545. self.fy_start_date,
  546. )
  547. if self.filter_cost_center_ids:
  548. query_inject_account_params += (
  549. tuple(self.filter_cost_center_ids.ids),
  550. )
  551. query_inject_account_params += (
  552. self.date_from,
  553. )
  554. if self.filter_cost_center_ids:
  555. query_inject_account_params += (
  556. tuple(self.filter_cost_center_ids.ids),
  557. )
  558. query_inject_account_params += (
  559. self.date_to,
  560. self.fy_start_date,
  561. )
  562. if self.filter_cost_center_ids:
  563. query_inject_account_params += (
  564. tuple(self.filter_cost_center_ids.ids),
  565. )
  566. query_inject_account_params += (
  567. self.date_to,
  568. )
  569. if self.filter_cost_center_ids:
  570. query_inject_account_params += (
  571. tuple(self.filter_cost_center_ids.ids),
  572. )
  573. query_inject_account_params += (
  574. self.id,
  575. self.env.uid,
  576. )
  577. self.env.cr.execute(query_inject_account, query_inject_account_params)
  578. def _get_partner_sub_subquery_sum_amounts(
  579. self, only_empty_partner, include_initial_balance, date_included
  580. ):
  581. """ Return subquery used to compute sum amounts on partners """
  582. sub_subquery_sum_amounts = """
  583. SELECT
  584. ap.account_id AS account_id,
  585. ap.partner_id AS partner_id,
  586. SUM(ml.debit) AS debit,
  587. SUM(ml.credit) AS credit,
  588. SUM(ml.balance) AS balance,
  589. c.id as currency_id,
  590. CASE
  591. WHEN c.id IS NOT NULL
  592. THEN SUM(ml.amount_currency)
  593. ELSE NULL
  594. END AS balance_currency
  595. FROM
  596. accounts_partners ap
  597. INNER JOIN account_account ac
  598. ON ac.id = ap.account_id
  599. LEFT JOIN
  600. res_currency c ON ac.currency_id = c.id
  601. INNER JOIN
  602. account_move_line ml
  603. ON ap.account_id = ml.account_id
  604. """
  605. if date_included:
  606. sub_subquery_sum_amounts += """
  607. AND ml.date <= %s
  608. """
  609. else:
  610. sub_subquery_sum_amounts += """
  611. AND ml.date < %s
  612. """
  613. if not only_empty_partner:
  614. sub_subquery_sum_amounts += """
  615. AND ap.partner_id = ml.partner_id
  616. """
  617. else:
  618. sub_subquery_sum_amounts += """
  619. AND ap.partner_id IS NULL AND ml.partner_id IS NULL
  620. """
  621. if not include_initial_balance:
  622. sub_subquery_sum_amounts += """
  623. AND ap.include_initial_balance != TRUE AND ml.date >= %s
  624. """
  625. else:
  626. sub_subquery_sum_amounts += """
  627. AND ap.include_initial_balance = TRUE
  628. """
  629. if self.only_posted_moves:
  630. sub_subquery_sum_amounts += """
  631. INNER JOIN
  632. account_move m ON ml.move_id = m.id AND m.state = 'posted'
  633. """
  634. if self.filter_cost_center_ids:
  635. sub_subquery_sum_amounts += """
  636. INNER JOIN
  637. account_analytic_account aa
  638. ON
  639. ml.analytic_account_id = aa.id
  640. AND aa.id IN %s
  641. """
  642. if self.filter_analytic_tag_ids:
  643. sub_subquery_sum_amounts += """
  644. INNER JOIN
  645. move_lines_on_tags ON ml.id = move_lines_on_tags.ml_id
  646. """
  647. sub_subquery_sum_amounts += """
  648. GROUP BY
  649. ap.account_id, ap.partner_id, c.id
  650. """
  651. return sub_subquery_sum_amounts
  652. def _get_final_partner_sub_subquery_sum_amounts(self, only_empty_partner,
  653. date_included):
  654. """Return final subquery used to compute sum amounts on partners"""
  655. subquery_sum_amounts = """
  656. SELECT
  657. sub.account_id AS account_id,
  658. sub.partner_id AS partner_id,
  659. SUM(COALESCE(sub.debit, 0.0)) AS debit,
  660. SUM(COALESCE(sub.credit, 0.0)) AS credit,
  661. SUM(COALESCE(sub.balance, 0.0)) AS balance,
  662. MAX(sub.currency_id) AS currency_id,
  663. SUM(COALESCE(sub.balance_currency, 0.0)) AS balance_currency
  664. FROM
  665. (
  666. """
  667. subquery_sum_amounts += self._get_partner_sub_subquery_sum_amounts(
  668. only_empty_partner,
  669. include_initial_balance=False,
  670. date_included=date_included
  671. )
  672. subquery_sum_amounts += """
  673. UNION
  674. """
  675. subquery_sum_amounts += self._get_partner_sub_subquery_sum_amounts(
  676. only_empty_partner,
  677. include_initial_balance=True,
  678. date_included=date_included
  679. )
  680. subquery_sum_amounts += """
  681. ) sub
  682. GROUP BY
  683. sub.account_id, sub.partner_id
  684. """
  685. return subquery_sum_amounts
  686. def _inject_partner_values(self, only_empty_partner=False):
  687. """ Inject report values for report_general_ledger_qweb_partner.
  688. Only for "partner" accounts (payable and receivable).
  689. """
  690. # pylint: disable=sql-injection
  691. query_inject_partner = """
  692. WITH
  693. accounts_partners AS
  694. (
  695. SELECT
  696. ra.id AS report_account_id,
  697. a.id AS account_id,
  698. at.include_initial_balance AS include_initial_balance,
  699. p.id AS partner_id,
  700. COALESCE(
  701. CASE
  702. WHEN
  703. NULLIF(p.name, '') IS NOT NULL
  704. AND NULLIF(p.ref, '') IS NOT NULL
  705. THEN p.name || ' (' || p.ref || ')'
  706. ELSE p.name
  707. END,
  708. '""" + _('No partner allocated') + """'
  709. ) AS partner_name
  710. FROM
  711. report_general_ledger_qweb_account ra
  712. INNER JOIN
  713. account_account a ON ra.account_id = a.id
  714. INNER JOIN
  715. account_account_type at ON a.user_type_id = at.id
  716. INNER JOIN
  717. account_move_line ml ON a.id = ml.account_id
  718. LEFT JOIN
  719. res_partner p ON ml.partner_id = p.id
  720. """
  721. if self.filter_cost_center_ids:
  722. query_inject_partner += """
  723. INNER JOIN
  724. account_analytic_account aa
  725. ON
  726. ml.analytic_account_id = aa.id
  727. AND aa.id IN %s
  728. """
  729. if self.filter_analytic_tag_ids:
  730. query_inject_partner += """
  731. INNER JOIN
  732. account_analytic_tag_account_move_line_rel atml
  733. ON atml.account_move_line_id = ml.id
  734. INNER JOIN
  735. account_analytic_tag aat
  736. ON
  737. atml.account_analytic_tag_id = aat.id
  738. AND aat.id IN %s
  739. """
  740. query_inject_partner += """
  741. WHERE
  742. ra.report_id = %s
  743. AND
  744. ra.is_partner_account = TRUE
  745. """
  746. if not only_empty_partner:
  747. query_inject_partner += """
  748. AND
  749. p.id IS NOT NULL
  750. """
  751. else:
  752. query_inject_partner += """
  753. AND
  754. p.id IS NULL
  755. """
  756. query_inject_partner += """
  757. """
  758. if self.centralize:
  759. query_inject_partner += """
  760. AND (a.centralized IS NULL OR a.centralized != TRUE)
  761. """
  762. if self.filter_partner_ids:
  763. query_inject_partner += """
  764. AND
  765. p.id IN %s
  766. """
  767. init_subquery = self._get_final_partner_sub_subquery_sum_amounts(
  768. only_empty_partner,
  769. date_included=False
  770. )
  771. final_subquery = self._get_final_partner_sub_subquery_sum_amounts(
  772. only_empty_partner,
  773. date_included=True
  774. )
  775. query_inject_partner += """
  776. GROUP BY
  777. ra.id,
  778. a.id,
  779. p.id,
  780. at.include_initial_balance
  781. ),
  782. """
  783. if self.filter_analytic_tag_ids:
  784. query_inject_partner += """
  785. move_lines_on_tags AS
  786. (
  787. SELECT
  788. DISTINCT ml.id AS ml_id
  789. FROM
  790. accounts_partners ap
  791. INNER JOIN
  792. account_move_line ml
  793. ON ap.account_id = ml.account_id
  794. INNER JOIN
  795. account_analytic_tag_account_move_line_rel atml
  796. ON atml.account_move_line_id = ml.id
  797. INNER JOIN
  798. account_analytic_tag aat
  799. ON
  800. atml.account_analytic_tag_id = aat.id
  801. WHERE
  802. aat.id IN %s
  803. ),
  804. """
  805. query_inject_partner += """
  806. initial_sum_amounts AS ( """ + init_subquery + """ ),
  807. final_sum_amounts AS ( """ + final_subquery + """ )
  808. INSERT INTO
  809. report_general_ledger_qweb_partner
  810. (
  811. report_account_id,
  812. create_uid,
  813. create_date,
  814. partner_id,
  815. name,
  816. initial_debit,
  817. initial_credit,
  818. initial_balance,
  819. currency_id,
  820. initial_balance_foreign_currency,
  821. final_debit,
  822. final_credit,
  823. final_balance,
  824. final_balance_foreign_currency
  825. )
  826. SELECT
  827. ap.report_account_id,
  828. %s AS create_uid,
  829. NOW() AS create_date,
  830. ap.partner_id,
  831. ap.partner_name,
  832. COALESCE(i.debit, 0.0) AS initial_debit,
  833. COALESCE(i.credit, 0.0) AS initial_credit,
  834. COALESCE(i.balance, 0.0) AS initial_balance,
  835. i.currency_id AS currency_id,
  836. COALESCE(i.balance_currency, 0.0) AS initial_balance_foreign_currency,
  837. COALESCE(f.debit, 0.0) AS final_debit,
  838. COALESCE(f.credit, 0.0) AS final_credit,
  839. COALESCE(f.balance, 0.0) AS final_balance,
  840. COALESCE(f.balance_currency, 0.0) AS final_balance_foreign_currency
  841. FROM
  842. accounts_partners ap
  843. LEFT JOIN
  844. initial_sum_amounts i
  845. ON
  846. (
  847. """
  848. if not only_empty_partner:
  849. query_inject_partner += """
  850. ap.partner_id = i.partner_id
  851. """
  852. else:
  853. query_inject_partner += """
  854. ap.partner_id IS NULL AND i.partner_id IS NULL
  855. """
  856. query_inject_partner += """
  857. )
  858. AND ap.account_id = i.account_id
  859. LEFT JOIN
  860. final_sum_amounts f
  861. ON
  862. (
  863. """
  864. if not only_empty_partner:
  865. query_inject_partner += """
  866. ap.partner_id = f.partner_id
  867. """
  868. else:
  869. query_inject_partner += """
  870. ap.partner_id IS NULL AND f.partner_id IS NULL
  871. """
  872. query_inject_partner += """
  873. )
  874. AND ap.account_id = f.account_id
  875. WHERE
  876. (
  877. i.debit IS NOT NULL AND i.debit != 0
  878. OR i.credit IS NOT NULL AND i.credit != 0
  879. OR i.balance IS NOT NULL AND i.balance != 0
  880. OR f.debit IS NOT NULL AND f.debit != 0
  881. OR f.credit IS NOT NULL AND f.credit != 0
  882. OR f.balance IS NOT NULL AND f.balance != 0
  883. )
  884. """
  885. if self.hide_account_at_0:
  886. query_inject_partner += """
  887. AND
  888. f.balance IS NOT NULL AND f.balance != 0
  889. """
  890. query_inject_partner_params = ()
  891. if self.filter_cost_center_ids:
  892. query_inject_partner_params += (
  893. tuple(self.filter_cost_center_ids.ids),
  894. )
  895. if self.filter_analytic_tag_ids:
  896. query_inject_partner_params += (
  897. tuple(self.filter_analytic_tag_ids.ids),
  898. )
  899. query_inject_partner_params += (
  900. self.id,
  901. )
  902. if self.filter_partner_ids:
  903. query_inject_partner_params += (
  904. tuple(self.filter_partner_ids.ids),
  905. )
  906. if self.filter_analytic_tag_ids:
  907. query_inject_partner_params += (
  908. tuple(self.filter_analytic_tag_ids.ids),
  909. )
  910. query_inject_partner_params += (
  911. self.date_from,
  912. self.fy_start_date,
  913. )
  914. if self.filter_cost_center_ids:
  915. query_inject_partner_params += (
  916. tuple(self.filter_cost_center_ids.ids),
  917. )
  918. query_inject_partner_params += (
  919. self.date_from,
  920. )
  921. if self.filter_cost_center_ids:
  922. query_inject_partner_params += (
  923. tuple(self.filter_cost_center_ids.ids),
  924. )
  925. query_inject_partner_params += (
  926. self.date_to,
  927. self.fy_start_date,
  928. )
  929. if self.filter_cost_center_ids:
  930. query_inject_partner_params += (
  931. tuple(self.filter_cost_center_ids.ids),
  932. )
  933. query_inject_partner_params += (
  934. self.date_to,
  935. )
  936. if self.filter_cost_center_ids:
  937. query_inject_partner_params += (
  938. tuple(self.filter_cost_center_ids.ids),
  939. )
  940. query_inject_partner_params += (
  941. self.env.uid,
  942. )
  943. self.env.cr.execute(query_inject_partner, query_inject_partner_params)
  944. def _inject_line_not_centralized_values(
  945. self,
  946. is_account_line=True,
  947. is_partner_line=False,
  948. only_empty_partner_line=False,
  949. only_unaffected_earnings_account=False):
  950. """ Inject report values for report_general_ledger_qweb_move_line.
  951. If centralized option have been chosen,
  952. only non centralized accounts are computed.
  953. In function of `is_account_line` and `is_partner_line` values,
  954. the move_line link is made either with account or either with partner.
  955. The "only_empty_partner_line" value is used
  956. to compute data without partner.
  957. """
  958. query_inject_move_line = ""
  959. if self.filter_analytic_tag_ids:
  960. query_inject_move_line += """
  961. WITH
  962. move_lines_on_tags AS
  963. (
  964. SELECT
  965. DISTINCT ml.id AS ml_id
  966. FROM
  967. """
  968. if is_account_line:
  969. query_inject_move_line += """
  970. report_general_ledger_qweb_account ra
  971. """
  972. elif is_partner_line:
  973. query_inject_move_line += """
  974. report_general_ledger_qweb_partner rp
  975. INNER JOIN
  976. report_general_ledger_qweb_account ra
  977. ON rp.report_account_id = ra.id
  978. """
  979. query_inject_move_line += """
  980. INNER JOIN
  981. account_move_line ml
  982. ON ra.account_id = ml.account_id
  983. INNER JOIN
  984. account_analytic_tag_account_move_line_rel atml
  985. ON atml.account_move_line_id = ml.id
  986. INNER JOIN
  987. account_analytic_tag aat
  988. ON
  989. atml.account_analytic_tag_id = aat.id
  990. WHERE
  991. ra.report_id = %s
  992. AND
  993. aat.id IN %s
  994. )
  995. """
  996. query_inject_move_line += """
  997. INSERT INTO
  998. report_general_ledger_qweb_move_line
  999. (
  1000. """
  1001. if is_account_line:
  1002. query_inject_move_line += """
  1003. report_account_id,
  1004. """
  1005. elif is_partner_line:
  1006. query_inject_move_line += """
  1007. report_partner_id,
  1008. """
  1009. query_inject_move_line += """
  1010. create_uid,
  1011. create_date,
  1012. move_line_id,
  1013. matched_ml_id,
  1014. date,
  1015. entry,
  1016. journal,
  1017. account,
  1018. taxes_description,
  1019. partner,
  1020. label,
  1021. cost_center,
  1022. debit,
  1023. credit,
  1024. cumul_balance,
  1025. currency_id,
  1026. amount_currency
  1027. )
  1028. SELECT
  1029. """
  1030. if is_account_line:
  1031. query_inject_move_line += """
  1032. ra.id AS report_account_id,
  1033. """
  1034. elif is_partner_line:
  1035. query_inject_move_line += """
  1036. rp.id AS report_partner_id,
  1037. """
  1038. query_inject_move_line += """
  1039. %s AS create_uid,
  1040. NOW() AS create_date,
  1041. ml.id AS move_line_id,
  1042. fr.id AS matched_ml_id,
  1043. ml.date,
  1044. m.name AS entry,
  1045. j.code AS journal,
  1046. a.code AS account,
  1047. CASE
  1048. WHEN
  1049. ml.tax_line_id is not null
  1050. THEN
  1051. COALESCE(at.description, at.name)
  1052. WHEN
  1053. ml.tax_line_id is null
  1054. THEN
  1055. (SELECT
  1056. array_to_string(
  1057. array_agg(COALESCE(at.description, at.name)
  1058. ), ', ')
  1059. FROM
  1060. account_move_line_account_tax_rel aml_at_rel
  1061. LEFT JOIN
  1062. account_tax at on (at.id = aml_at_rel.account_tax_id)
  1063. WHERE
  1064. aml_at_rel.account_move_line_id = ml.id)
  1065. ELSE
  1066. ''
  1067. END as taxes_description,
  1068. """
  1069. if not only_empty_partner_line:
  1070. query_inject_move_line += """
  1071. CASE
  1072. WHEN
  1073. NULLIF(p.name, '') IS NOT NULL
  1074. AND NULLIF(p.ref, '') IS NOT NULL
  1075. THEN p.name || ' (' || p.ref || ')'
  1076. ELSE p.name
  1077. END AS partner,
  1078. """
  1079. elif only_empty_partner_line:
  1080. query_inject_move_line += """
  1081. '""" + _('No partner allocated') + """' AS partner,
  1082. """
  1083. query_inject_move_line += """
  1084. CONCAT_WS(' - ', NULLIF(ml.ref, ''), NULLIF(ml.name, '')) AS label,
  1085. aa.name AS cost_center,
  1086. ml.debit,
  1087. ml.credit,
  1088. """
  1089. if is_account_line:
  1090. query_inject_move_line += """
  1091. ra.initial_balance + (
  1092. SUM(ml.balance)
  1093. OVER (PARTITION BY a.code
  1094. ORDER BY a.code, ml.date, ml.id)
  1095. ) AS cumul_balance,
  1096. """
  1097. elif is_partner_line and not only_empty_partner_line:
  1098. query_inject_move_line += """
  1099. rp.initial_balance + (
  1100. SUM(ml.balance)
  1101. OVER (PARTITION BY a.code, p.name
  1102. ORDER BY a.code, p.name, ml.date, ml.id)
  1103. ) AS cumul_balance,
  1104. """
  1105. elif is_partner_line and only_empty_partner_line:
  1106. query_inject_move_line += """
  1107. rp.initial_balance + (
  1108. SUM(ml.balance)
  1109. OVER (PARTITION BY a.code
  1110. ORDER BY a.code, ml.date, ml.id)
  1111. ) AS cumul_balance,
  1112. """
  1113. query_inject_move_line += """
  1114. c.id AS currency_id,
  1115. ml.amount_currency
  1116. FROM
  1117. """
  1118. if is_account_line:
  1119. query_inject_move_line += """
  1120. report_general_ledger_qweb_account ra
  1121. """
  1122. elif is_partner_line:
  1123. query_inject_move_line += """
  1124. report_general_ledger_qweb_partner rp
  1125. INNER JOIN
  1126. report_general_ledger_qweb_account ra ON rp.report_account_id = ra.id
  1127. """
  1128. query_inject_move_line += """
  1129. INNER JOIN
  1130. account_move_line ml ON ra.account_id = ml.account_id
  1131. INNER JOIN
  1132. account_move m ON ml.move_id = m.id
  1133. INNER JOIN
  1134. account_journal j ON ml.journal_id = j.id
  1135. INNER JOIN
  1136. account_account a ON ml.account_id = a.id
  1137. LEFT JOIN
  1138. account_tax at ON ml.tax_line_id = at.id
  1139. """
  1140. if is_account_line:
  1141. query_inject_move_line += """
  1142. LEFT JOIN
  1143. res_partner p ON ml.partner_id = p.id
  1144. """
  1145. elif is_partner_line and not only_empty_partner_line:
  1146. query_inject_move_line += """
  1147. INNER JOIN
  1148. res_partner p
  1149. ON ml.partner_id = p.id AND rp.partner_id = p.id
  1150. """
  1151. query_inject_move_line += """
  1152. LEFT JOIN
  1153. account_full_reconcile fr ON ml.full_reconcile_id = fr.id
  1154. LEFT JOIN
  1155. res_currency c ON ml.currency_id = c.id
  1156. """
  1157. if self.filter_cost_center_ids:
  1158. query_inject_move_line += """
  1159. INNER JOIN
  1160. account_analytic_account aa
  1161. ON
  1162. ml.analytic_account_id = aa.id
  1163. AND aa.id IN %s
  1164. """
  1165. else:
  1166. query_inject_move_line += """
  1167. LEFT JOIN
  1168. account_analytic_account aa ON ml.analytic_account_id = aa.id
  1169. """
  1170. if self.filter_analytic_tag_ids:
  1171. query_inject_move_line += """
  1172. INNER JOIN
  1173. move_lines_on_tags ON ml.id = move_lines_on_tags.ml_id
  1174. """
  1175. query_inject_move_line += """
  1176. WHERE
  1177. ra.report_id = %s
  1178. AND
  1179. """
  1180. if only_unaffected_earnings_account:
  1181. query_inject_move_line += """
  1182. a.id = %s
  1183. AND
  1184. """
  1185. if is_account_line:
  1186. query_inject_move_line += """
  1187. (ra.is_partner_account IS NULL OR ra.is_partner_account != TRUE)
  1188. """
  1189. elif is_partner_line:
  1190. query_inject_move_line += """
  1191. ra.is_partner_account = TRUE
  1192. """
  1193. if self.centralize:
  1194. query_inject_move_line += """
  1195. AND
  1196. (a.centralized IS NULL OR a.centralized != TRUE)
  1197. """
  1198. query_inject_move_line += """
  1199. AND
  1200. ml.date BETWEEN %s AND %s
  1201. """
  1202. if self.only_posted_moves:
  1203. query_inject_move_line += """
  1204. AND
  1205. m.state = 'posted'
  1206. """
  1207. if only_empty_partner_line:
  1208. query_inject_move_line += """
  1209. AND
  1210. ml.partner_id IS NULL
  1211. AND
  1212. rp.partner_id IS NULL
  1213. """
  1214. if self.filter_journal_ids:
  1215. query_inject_move_line += """
  1216. AND
  1217. j.id IN %s
  1218. """
  1219. if is_account_line:
  1220. query_inject_move_line += """
  1221. ORDER BY
  1222. a.code, ml.date, ml.id
  1223. """
  1224. elif is_partner_line and not only_empty_partner_line:
  1225. query_inject_move_line += """
  1226. ORDER BY
  1227. a.code, p.name, ml.date, ml.id
  1228. """
  1229. elif is_partner_line and only_empty_partner_line:
  1230. query_inject_move_line += """
  1231. ORDER BY
  1232. a.code, ml.date, ml.id
  1233. """
  1234. query_inject_move_line_params = ()
  1235. if self.filter_analytic_tag_ids:
  1236. query_inject_move_line_params += (
  1237. self.id,
  1238. tuple(self.filter_analytic_tag_ids.ids),
  1239. )
  1240. query_inject_move_line_params += (
  1241. self.env.uid,
  1242. )
  1243. if self.filter_cost_center_ids:
  1244. query_inject_move_line_params += (
  1245. tuple(self.filter_cost_center_ids.ids),
  1246. )
  1247. query_inject_move_line_params += (
  1248. self.id,
  1249. )
  1250. if only_unaffected_earnings_account:
  1251. query_inject_move_line_params += (
  1252. self.unaffected_earnings_account.id,
  1253. )
  1254. query_inject_move_line_params += (
  1255. self.date_from,
  1256. self.date_to,
  1257. )
  1258. if self.filter_journal_ids:
  1259. query_inject_move_line_params += (tuple(
  1260. self.filter_journal_ids.ids,
  1261. ),)
  1262. self.env.cr.execute(
  1263. query_inject_move_line,
  1264. query_inject_move_line_params
  1265. )
  1266. def _inject_line_centralized_values(self):
  1267. """ Inject report values for report_general_ledger_qweb_move_line.
  1268. Only centralized accounts are computed.
  1269. """
  1270. if self.filter_analytic_tag_ids:
  1271. query_inject_move_line_centralized = """
  1272. WITH
  1273. move_lines_on_tags AS
  1274. (
  1275. SELECT
  1276. DISTINCT ml.id AS ml_id
  1277. FROM
  1278. report_general_ledger_qweb_account ra
  1279. INNER JOIN
  1280. account_move_line ml
  1281. ON ra.account_id = ml.account_id
  1282. INNER JOIN
  1283. account_analytic_tag_account_move_line_rel atml
  1284. ON atml.account_move_line_id = ml.id
  1285. INNER JOIN
  1286. account_analytic_tag aat
  1287. ON
  1288. atml.account_analytic_tag_id = aat.id
  1289. WHERE
  1290. ra.report_id = %s
  1291. AND
  1292. aat.id IN %s
  1293. ),
  1294. """
  1295. else:
  1296. query_inject_move_line_centralized = """
  1297. WITH
  1298. """
  1299. query_inject_move_line_centralized += """
  1300. move_lines AS
  1301. (
  1302. SELECT
  1303. ml.account_id,
  1304. (
  1305. DATE_TRUNC('month', ml.date) + interval '1 month'
  1306. - interval '1 day'
  1307. )::date AS date,
  1308. SUM(ml.debit) AS debit,
  1309. SUM(ml.credit) AS credit,
  1310. SUM(ml.balance) AS balance,
  1311. ml.currency_id AS currency_id,
  1312. ml.journal_id as journal_id
  1313. FROM
  1314. report_general_ledger_qweb_account ra
  1315. INNER JOIN
  1316. account_move_line ml ON ra.account_id = ml.account_id
  1317. INNER JOIN
  1318. account_move m ON ml.move_id = m.id
  1319. INNER JOIN
  1320. account_account a ON ml.account_id = a.id
  1321. """
  1322. if self.filter_cost_center_ids:
  1323. query_inject_move_line_centralized += """
  1324. INNER JOIN
  1325. account_analytic_account aa
  1326. ON
  1327. ml.analytic_account_id = aa.id
  1328. AND aa.id IN %s
  1329. """
  1330. if self.filter_analytic_tag_ids:
  1331. query_inject_move_line_centralized += """
  1332. INNER JOIN
  1333. move_lines_on_tags ON ml.id = move_lines_on_tags.ml_id
  1334. """
  1335. query_inject_move_line_centralized += """
  1336. WHERE
  1337. ra.report_id = %s
  1338. AND
  1339. a.centralized = TRUE
  1340. AND
  1341. ml.date BETWEEN %s AND %s
  1342. """
  1343. if self.only_posted_moves:
  1344. query_inject_move_line_centralized += """
  1345. AND
  1346. m.state = 'posted'
  1347. """
  1348. query_inject_move_line_centralized += """
  1349. GROUP BY
  1350. ra.id, ml.account_id, a.code, 2, ml.currency_id, ml.journal_id
  1351. )
  1352. INSERT INTO
  1353. report_general_ledger_qweb_move_line
  1354. (
  1355. report_account_id,
  1356. create_uid,
  1357. create_date,
  1358. date,
  1359. account,
  1360. journal,
  1361. label,
  1362. debit,
  1363. credit,
  1364. cumul_balance
  1365. )
  1366. SELECT
  1367. ra.id AS report_account_id,
  1368. %s AS create_uid,
  1369. NOW() AS create_date,
  1370. ml.date,
  1371. a.code AS account,
  1372. j.code as journal,
  1373. '""" + _('Centralized Entries') + """' AS label,
  1374. ml.debit AS debit,
  1375. ml.credit AS credit,
  1376. ra.initial_balance + (
  1377. SUM(ml.balance)
  1378. OVER (PARTITION BY a.code ORDER BY ml.date)
  1379. ) AS cumul_balance
  1380. FROM
  1381. report_general_ledger_qweb_account ra
  1382. INNER JOIN
  1383. move_lines ml ON ra.account_id = ml.account_id
  1384. INNER JOIN
  1385. account_account a ON ml.account_id = a.id
  1386. INNER JOIN
  1387. account_journal j ON ml.journal_id = j.id
  1388. LEFT JOIN
  1389. res_currency c ON ml.currency_id = c.id
  1390. WHERE
  1391. ra.report_id = %s
  1392. AND
  1393. (a.centralized IS NOT NULL AND a.centralized = TRUE)
  1394. """
  1395. if self.filter_journal_ids:
  1396. query_inject_move_line_centralized += """
  1397. AND
  1398. j.id in %s
  1399. """
  1400. query_inject_move_line_centralized += """
  1401. ORDER BY
  1402. a.code, ml.date
  1403. """
  1404. query_inject_move_line_centralized_params = ()
  1405. if self.filter_analytic_tag_ids:
  1406. query_inject_move_line_centralized_params += (
  1407. self.id,
  1408. tuple(self.filter_analytic_tag_ids.ids),
  1409. )
  1410. if self.filter_cost_center_ids:
  1411. query_inject_move_line_centralized_params += (
  1412. tuple(self.filter_cost_center_ids.ids),
  1413. )
  1414. query_inject_move_line_centralized_params += (
  1415. self.id,
  1416. self.date_from,
  1417. self.date_to,
  1418. self.env.uid,
  1419. self.id,
  1420. )
  1421. if self.filter_journal_ids:
  1422. query_inject_move_line_centralized_params += (tuple(
  1423. self.filter_journal_ids.ids,
  1424. ),)
  1425. self.env.cr.execute(
  1426. query_inject_move_line_centralized,
  1427. query_inject_move_line_centralized_params
  1428. )
  1429. def _compute_analytic_tags(self):
  1430. """ Compute "tags" column"""
  1431. query_update_analytic_tags = """
  1432. UPDATE
  1433. report_general_ledger_qweb_move_line
  1434. SET
  1435. tags = tags_values.tags
  1436. FROM
  1437. (
  1438. (
  1439. SELECT
  1440. rml.id AS report_id,
  1441. array_to_string(array_agg(t.name ORDER BY t.name), ',') AS tags
  1442. FROM
  1443. account_move_line ml
  1444. INNER JOIN
  1445. report_general_ledger_qweb_move_line rml
  1446. ON ml.id = rml.move_line_id
  1447. INNER JOIN
  1448. report_general_ledger_qweb_account ra
  1449. ON rml.report_account_id = ra.id
  1450. INNER JOIN
  1451. account_analytic_tag_account_move_line_rel tml
  1452. ON ml.id = tml.account_move_line_id
  1453. INNER JOIN
  1454. account_analytic_tag t
  1455. ON tml.account_analytic_tag_id = t.id
  1456. WHERE
  1457. ra.report_id = %(report_id)s
  1458. GROUP BY
  1459. rml.id,
  1460. ml.id
  1461. )
  1462. UNION
  1463. (
  1464. SELECT
  1465. rml.id AS report_id,
  1466. array_to_string(array_agg(t.name ORDER BY t.name), ',') AS tags
  1467. FROM
  1468. account_move_line ml
  1469. INNER JOIN
  1470. report_general_ledger_qweb_move_line rml
  1471. ON ml.id = rml.move_line_id
  1472. INNER JOIN
  1473. report_general_ledger_qweb_partner rp
  1474. ON rml.report_partner_id = rp.id
  1475. INNER JOIN
  1476. report_general_ledger_qweb_account ra
  1477. ON rp.report_account_id = ra.id
  1478. INNER JOIN
  1479. account_analytic_tag_account_move_line_rel tml
  1480. ON ml.id = tml.account_move_line_id
  1481. INNER JOIN
  1482. account_analytic_tag t
  1483. ON tml.account_analytic_tag_id = t.id
  1484. WHERE
  1485. ra.report_id = %(report_id)s
  1486. GROUP BY
  1487. rml.id,
  1488. ml.id
  1489. )
  1490. ) AS tags_values
  1491. WHERE
  1492. report_general_ledger_qweb_move_line.id = tags_values.report_id
  1493. """
  1494. params = {
  1495. 'report_id': self.id,
  1496. }
  1497. self.env.cr.execute(query_update_analytic_tags, params)
  1498. def _inject_unaffected_earnings_account_values(self):
  1499. """Inject the report values of the unaffected earnings account
  1500. for report_general_ledger_qweb_account."""
  1501. # Fetch the profit and loss accounts
  1502. query_unaffected_earnings_account_ids = """
  1503. SELECT a.id
  1504. FROM account_account as a
  1505. INNER JOIN account_account_type as at
  1506. ON at.id = a.user_type_id
  1507. WHERE at.include_initial_balance = FALSE
  1508. """
  1509. self.env.cr.execute(query_unaffected_earnings_account_ids)
  1510. pl_account_ids = [r[0] for r in self.env.cr.fetchall()]
  1511. unaffected_earnings_account_ids = \
  1512. pl_account_ids + [self.unaffected_earnings_account.id]
  1513. # Fetch the current fiscal year start date
  1514. date = fields.Datetime.from_string(self.date_from)
  1515. res = self.company_id.compute_fiscalyear_dates(date)
  1516. fy_start_date = res['date_from']
  1517. query_select_previous_fy_unaffected_earnings_params = {
  1518. 'date_to': fy_start_date,
  1519. 'company_id': self.company_id.id,
  1520. 'account_ids': tuple(unaffected_earnings_account_ids),
  1521. 'analytic_tag_ids': tuple(self.filter_analytic_tag_ids.ids),
  1522. }
  1523. query_select_previous_fy_unaffected_earnings = ''
  1524. q_analytic_tags = ''
  1525. if self.filter_analytic_tag_ids:
  1526. q_analytic_tags = """
  1527. WITH move_lines_on_tags AS
  1528. (
  1529. SELECT
  1530. DISTINCT ml.id AS ml_id
  1531. FROM
  1532. account_account a
  1533. INNER JOIN
  1534. account_move_line ml
  1535. ON a.id = ml.account_id
  1536. INNER JOIN
  1537. account_analytic_tag_account_move_line_rel atml
  1538. ON atml.account_move_line_id = ml.id
  1539. INNER JOIN
  1540. account_analytic_tag aat
  1541. ON
  1542. atml.account_analytic_tag_id = aat.id
  1543. WHERE
  1544. aat.id IN %(analytic_tag_ids)s
  1545. )
  1546. """
  1547. query_select_previous_fy_unaffected_earnings += q_analytic_tags
  1548. query_select_previous_fy_unaffected_earnings += """
  1549. SELECT sum(ml.balance) as balance
  1550. FROM account_move_line as ml
  1551. INNER JOIN account_move as am
  1552. ON am.id = ml.move_id
  1553. INNER JOIN account_journal j
  1554. ON am.journal_id = j.id
  1555. """
  1556. if self.filter_cost_center_ids:
  1557. query_select_previous_fy_unaffected_earnings += """
  1558. INNER JOIN account_analytic_account aa
  1559. ON ml.analytic_account_id = aa.id
  1560. AND aa.id IN %(cost_center_ids)s
  1561. """
  1562. query_select_previous_fy_unaffected_earnings_params[
  1563. 'cost_center_ids'] = tuple(self.filter_cost_center_ids.ids)
  1564. if self.filter_analytic_tag_ids:
  1565. query_select_previous_fy_unaffected_earnings += """
  1566. INNER JOIN move_lines_on_tags ON ml.id =
  1567. move_lines_on_tags.ml_id
  1568. """
  1569. query_select_previous_fy_unaffected_earnings += """
  1570. WHERE ml.date < %(date_to)s
  1571. AND ml.company_id = %(company_id)s
  1572. AND ml.account_id IN %(account_ids)s
  1573. """
  1574. if self.filter_journal_ids:
  1575. query_select_previous_fy_unaffected_earnings += """
  1576. AND j.id IN %(journal_ids)s
  1577. """
  1578. query_select_previous_fy_unaffected_earnings_params[
  1579. 'journal_ids'] = tuple(self.filter_journal_ids.ids)
  1580. if self.only_posted_moves:
  1581. query_select_previous_fy_unaffected_earnings += """
  1582. AND am.state = 'posted'
  1583. """
  1584. self.env.cr.execute(
  1585. query_select_previous_fy_unaffected_earnings,
  1586. query_select_previous_fy_unaffected_earnings_params)
  1587. res = self.env.cr.fetchone()
  1588. unaffected_earnings_initial_balance = res[0] or 0.0
  1589. # Now select the current period unaffected earnings,
  1590. # excluding the current period P&L.
  1591. query_select_period_unaffected_earnings_params = {
  1592. 'date_from': self.date_from,
  1593. 'date_to': self.date_to,
  1594. 'company_id': self.company_id.id,
  1595. 'unaffected_earnings_id': self.unaffected_earnings_account.id,
  1596. 'analytic_tag_ids': tuple(self.filter_analytic_tag_ids.ids),
  1597. }
  1598. query_select_period_unaffected_earnings = ''
  1599. if self.filter_analytic_tag_ids:
  1600. query_select_period_unaffected_earnings += q_analytic_tags
  1601. query_select_period_unaffected_earnings += """
  1602. SELECT
  1603. sum(ml.debit) as sum_debit,
  1604. sum(ml.credit) as sum_credit,
  1605. sum(ml.balance) as balance
  1606. FROM account_move_line as ml
  1607. INNER JOIN account_move as am
  1608. ON am.id = ml.move_id
  1609. INNER JOIN account_journal j
  1610. ON am.journal_id = j.id
  1611. """
  1612. if self.filter_cost_center_ids:
  1613. query_select_period_unaffected_earnings += """
  1614. INNER JOIN account_analytic_account aa
  1615. ON ml.analytic_account_id = aa.id
  1616. AND aa.id IN %(cost_center_ids)s
  1617. """
  1618. query_select_period_unaffected_earnings_params[
  1619. 'cost_center_ids'] = tuple(self.filter_cost_center_ids.ids)
  1620. if self.filter_analytic_tag_ids:
  1621. query_select_period_unaffected_earnings += """
  1622. INNER JOIN move_lines_on_tags
  1623. ON ml.id = move_lines_on_tags.ml_id
  1624. """
  1625. query_select_period_unaffected_earnings += """
  1626. WHERE am.date >= %(date_from)s
  1627. AND ml.date <= %(date_to)s
  1628. AND ml.company_id = %(company_id)s
  1629. AND ml.account_id = %(unaffected_earnings_id)s
  1630. """
  1631. if self.filter_journal_ids:
  1632. query_select_period_unaffected_earnings += """
  1633. AND j.id IN %(journal_ids)s
  1634. """
  1635. query_select_period_unaffected_earnings_params[
  1636. 'journal_ids'] = tuple(self.filter_journal_ids.ids)
  1637. if self.only_posted_moves:
  1638. query_select_period_unaffected_earnings += """
  1639. AND am.state = 'posted'
  1640. """
  1641. self.env.cr.execute(query_select_period_unaffected_earnings,
  1642. query_select_period_unaffected_earnings_params)
  1643. res = self.env.cr.fetchone()
  1644. unaffected_earnings_period_debit = res[0] or 0.0
  1645. unaffected_earnings_period_credit = res[1] or 0.0
  1646. unaffected_earnings_period_balance = res[2] or 0.0
  1647. # pylint: disable=sql-injection
  1648. query_inject_account = """
  1649. INSERT INTO
  1650. report_general_ledger_qweb_account (
  1651. report_id,
  1652. create_uid,
  1653. create_date,
  1654. account_id,
  1655. code,
  1656. name,
  1657. is_partner_account,
  1658. initial_debit,
  1659. initial_credit,
  1660. initial_balance,
  1661. final_debit,
  1662. final_credit,
  1663. final_balance
  1664. )
  1665. VALUES (
  1666. %(report_id)s,
  1667. %(user_id)s,
  1668. NOW(),
  1669. %(account_id)s,
  1670. %(code)s,
  1671. %(name)s,
  1672. False,
  1673. %(initial_debit)s,
  1674. %(initial_credit)s,
  1675. %(initial_balance)s,
  1676. %(final_debit)s,
  1677. %(final_credit)s,
  1678. %(final_balance)s
  1679. )
  1680. """
  1681. initial_debit = unaffected_earnings_initial_balance >= 0 and \
  1682. unaffected_earnings_initial_balance or 0
  1683. initial_credit = unaffected_earnings_initial_balance < 0 and \
  1684. -1 * unaffected_earnings_initial_balance or 0
  1685. final_balance = unaffected_earnings_initial_balance + \
  1686. unaffected_earnings_period_balance
  1687. query_inject_account_params = {
  1688. 'report_id': self.id,
  1689. 'user_id': self.env.uid,
  1690. 'account_id': self.unaffected_earnings_account.id,
  1691. 'code': self.unaffected_earnings_account.code,
  1692. 'name': self.unaffected_earnings_account.name,
  1693. 'initial_debit': initial_debit,
  1694. 'initial_credit': initial_credit,
  1695. 'initial_balance': unaffected_earnings_initial_balance,
  1696. 'final_debit': initial_debit + unaffected_earnings_period_debit,
  1697. 'final_credit': initial_credit + unaffected_earnings_period_credit,
  1698. 'final_balance': final_balance,
  1699. }
  1700. self.env.cr.execute(query_inject_account,
  1701. query_inject_account_params)