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.

1764 lines
57 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.only_posted_moves:
  282. sub_subquery_sum_amounts += """
  283. INNER JOIN
  284. account_move m ON ml.move_id = m.id AND m.state = 'posted'
  285. """
  286. if self.filter_cost_center_ids:
  287. sub_subquery_sum_amounts += """
  288. INNER JOIN
  289. account_analytic_account aa
  290. ON
  291. ml.analytic_account_id = aa.id
  292. AND aa.id IN %s
  293. """
  294. if self.filter_analytic_tag_ids:
  295. sub_subquery_sum_amounts += """
  296. INNER JOIN
  297. move_lines_on_tags ON ml.id = move_lines_on_tags.ml_id
  298. """
  299. sub_subquery_sum_amounts += """
  300. LEFT JOIN
  301. res_currency c ON a.currency_id = c.id
  302. """
  303. sub_subquery_sum_amounts += """
  304. GROUP BY
  305. a.id, c.id
  306. """
  307. return sub_subquery_sum_amounts
  308. def _get_final_account_sub_subquery_sum_amounts(self, date_included):
  309. """ Return final subquery used to compute sum amounts on accounts """
  310. subquery_sum_amounts = """
  311. SELECT
  312. sub.account_id AS account_id,
  313. SUM(COALESCE(sub.debit, 0.0)) AS debit,
  314. SUM(COALESCE(sub.credit, 0.0)) AS credit,
  315. SUM(COALESCE(sub.balance, 0.0)) AS balance,
  316. MAX(sub.currency_id) AS currency_id,
  317. SUM(COALESCE(sub.balance_currency, 0.0)) AS balance_currency
  318. FROM
  319. (
  320. """
  321. subquery_sum_amounts += self._get_account_sub_subquery_sum_amounts(
  322. include_initial_balance=False, date_included=date_included
  323. )
  324. subquery_sum_amounts += """
  325. UNION
  326. """
  327. subquery_sum_amounts += self._get_account_sub_subquery_sum_amounts(
  328. include_initial_balance=True, date_included=date_included
  329. )
  330. subquery_sum_amounts += """
  331. ) sub
  332. GROUP BY
  333. sub.account_id
  334. """
  335. return subquery_sum_amounts
  336. def _inject_account_values(self):
  337. """Inject report values for report_general_ledger_qweb_account."""
  338. query_inject_account = """
  339. WITH
  340. accounts AS
  341. (
  342. SELECT
  343. a.id,
  344. a.code,
  345. a.name,
  346. a.internal_type IN ('payable', 'receivable')
  347. AS is_partner_account,
  348. a.user_type_id,
  349. a.currency_id
  350. FROM
  351. account_account a
  352. """
  353. if (
  354. self.filter_partner_ids or
  355. self.filter_cost_center_ids or
  356. self.filter_analytic_tag_ids
  357. ):
  358. query_inject_account += """
  359. INNER JOIN
  360. account_move_line ml ON a.id = ml.account_id
  361. """
  362. if self.filter_partner_ids:
  363. query_inject_account += """
  364. INNER JOIN
  365. res_partner p ON ml.partner_id = p.id
  366. """
  367. if self.filter_cost_center_ids:
  368. query_inject_account += """
  369. INNER JOIN
  370. account_analytic_account aa
  371. ON
  372. ml.analytic_account_id = aa.id
  373. AND aa.id IN %s
  374. """
  375. if self.filter_analytic_tag_ids:
  376. query_inject_account += """
  377. INNER JOIN
  378. account_analytic_tag_account_move_line_rel atml
  379. ON atml.account_move_line_id = ml.id
  380. INNER JOIN
  381. account_analytic_tag aat
  382. ON
  383. atml.account_analytic_tag_id = aat.id
  384. AND aat.id IN %s
  385. """
  386. query_inject_account += """
  387. WHERE
  388. a.company_id = %s
  389. AND a.id != %s
  390. """
  391. if self.filter_account_ids:
  392. query_inject_account += """
  393. AND
  394. a.id IN %s
  395. """
  396. if self.filter_partner_ids:
  397. query_inject_account += """
  398. AND
  399. p.id IN %s
  400. """
  401. if (
  402. self.filter_partner_ids or
  403. self.filter_cost_center_ids or
  404. self.filter_analytic_tag_ids
  405. ):
  406. query_inject_account += """
  407. GROUP BY
  408. a.id
  409. """
  410. query_inject_account += """
  411. ),
  412. """
  413. if self.filter_analytic_tag_ids:
  414. query_inject_account += """
  415. move_lines_on_tags AS
  416. (
  417. SELECT
  418. DISTINCT ml.id AS ml_id
  419. FROM
  420. accounts a
  421. INNER JOIN
  422. account_move_line ml
  423. ON a.id = ml.account_id
  424. INNER JOIN
  425. account_analytic_tag_account_move_line_rel atml
  426. ON atml.account_move_line_id = ml.id
  427. INNER JOIN
  428. account_analytic_tag aat
  429. ON
  430. atml.account_analytic_tag_id = aat.id
  431. WHERE
  432. aat.id IN %s
  433. ),
  434. """
  435. init_subquery = self._get_final_account_sub_subquery_sum_amounts(
  436. date_included=False
  437. )
  438. final_subquery = self._get_final_account_sub_subquery_sum_amounts(
  439. date_included=True
  440. )
  441. query_inject_account += """
  442. initial_sum_amounts AS ( """ + init_subquery + """ ),
  443. final_sum_amounts AS ( """ + final_subquery + """ )
  444. INSERT INTO
  445. report_general_ledger_qweb_account
  446. (
  447. report_id,
  448. create_uid,
  449. create_date,
  450. account_id,
  451. code,
  452. name,
  453. initial_debit,
  454. initial_credit,
  455. initial_balance,
  456. currency_id,
  457. initial_balance_foreign_currency,
  458. final_debit,
  459. final_credit,
  460. final_balance,
  461. final_balance_foreign_currency,
  462. is_partner_account
  463. )
  464. SELECT
  465. %s AS report_id,
  466. %s AS create_uid,
  467. NOW() AS create_date,
  468. a.id AS account_id,
  469. a.code,
  470. a.name,
  471. COALESCE(i.debit, 0.0) AS initial_debit,
  472. COALESCE(i.credit, 0.0) AS initial_credit,
  473. COALESCE(i.balance, 0.0) AS initial_balance,
  474. c.id AS currency_id,
  475. COALESCE(i.balance_currency, 0.0) AS initial_balance_foreign_currency,
  476. COALESCE(f.debit, 0.0) AS final_debit,
  477. COALESCE(f.credit, 0.0) AS final_credit,
  478. COALESCE(f.balance, 0.0) AS final_balance,
  479. COALESCE(f.balance_currency, 0.0) AS final_balance_foreign_currency,
  480. a.is_partner_account
  481. FROM
  482. accounts a
  483. LEFT JOIN
  484. initial_sum_amounts i ON a.id = i.account_id
  485. LEFT JOIN
  486. final_sum_amounts f ON a.id = f.account_id
  487. LEFT JOIN
  488. res_currency c ON c.id = a.currency_id
  489. WHERE
  490. (
  491. i.debit IS NOT NULL AND i.debit != 0
  492. OR i.credit IS NOT NULL AND i.credit != 0
  493. OR i.balance IS NOT NULL AND i.balance != 0
  494. OR f.debit IS NOT NULL AND f.debit != 0
  495. OR f.credit IS NOT NULL AND f.credit != 0
  496. OR f.balance IS NOT NULL AND f.balance != 0
  497. )
  498. """
  499. if self.hide_account_at_0:
  500. query_inject_account += """ AND (f.balance IS NOT NULL AND f.balance != 0) OR
  501. (i.balance IS NOT NULL AND i.balance != 0) """
  502. query_inject_account_params = ()
  503. if self.filter_cost_center_ids:
  504. query_inject_account_params += (
  505. tuple(self.filter_cost_center_ids.ids),
  506. )
  507. if self.filter_analytic_tag_ids:
  508. query_inject_account_params += (
  509. tuple(self.filter_analytic_tag_ids.ids),
  510. )
  511. query_inject_account_params += (
  512. self.company_id.id,
  513. self.unaffected_earnings_account.id,
  514. )
  515. if self.filter_account_ids:
  516. query_inject_account_params += (
  517. tuple(self.filter_account_ids.ids),
  518. )
  519. if self.filter_partner_ids:
  520. query_inject_account_params += (
  521. tuple(self.filter_partner_ids.ids),
  522. )
  523. if self.filter_analytic_tag_ids:
  524. query_inject_account_params += (
  525. tuple(self.filter_analytic_tag_ids.ids),
  526. )
  527. query_inject_account_params += (
  528. self.date_from,
  529. self.fy_start_date,
  530. )
  531. if self.filter_cost_center_ids:
  532. query_inject_account_params += (
  533. tuple(self.filter_cost_center_ids.ids),
  534. )
  535. query_inject_account_params += (
  536. self.date_from,
  537. )
  538. if self.filter_cost_center_ids:
  539. query_inject_account_params += (
  540. tuple(self.filter_cost_center_ids.ids),
  541. )
  542. query_inject_account_params += (
  543. self.date_to,
  544. self.fy_start_date,
  545. )
  546. if self.filter_cost_center_ids:
  547. query_inject_account_params += (
  548. tuple(self.filter_cost_center_ids.ids),
  549. )
  550. query_inject_account_params += (
  551. self.date_to,
  552. )
  553. if self.filter_cost_center_ids:
  554. query_inject_account_params += (
  555. tuple(self.filter_cost_center_ids.ids),
  556. )
  557. query_inject_account_params += (
  558. self.id,
  559. self.env.uid,
  560. )
  561. self.env.cr.execute(query_inject_account, query_inject_account_params)
  562. def _get_partner_sub_subquery_sum_amounts(
  563. self, only_empty_partner, include_initial_balance, date_included
  564. ):
  565. """ Return subquery used to compute sum amounts on partners """
  566. sub_subquery_sum_amounts = """
  567. SELECT
  568. ap.account_id AS account_id,
  569. ap.partner_id AS partner_id,
  570. SUM(ml.debit) AS debit,
  571. SUM(ml.credit) AS credit,
  572. SUM(ml.balance) AS balance,
  573. c.id as currency_id,
  574. CASE
  575. WHEN c.id IS NOT NULL
  576. THEN SUM(ml.amount_currency)
  577. ELSE NULL
  578. END AS balance_currency
  579. FROM
  580. accounts_partners ap
  581. INNER JOIN account_account ac
  582. ON ac.id = ap.account_id
  583. LEFT JOIN
  584. res_currency c ON ac.currency_id = c.id
  585. INNER JOIN
  586. account_move_line ml
  587. ON ap.account_id = ml.account_id
  588. """
  589. if date_included:
  590. sub_subquery_sum_amounts += """
  591. AND ml.date <= %s
  592. """
  593. else:
  594. sub_subquery_sum_amounts += """
  595. AND ml.date < %s
  596. """
  597. if not only_empty_partner:
  598. sub_subquery_sum_amounts += """
  599. AND ap.partner_id = ml.partner_id
  600. """
  601. else:
  602. sub_subquery_sum_amounts += """
  603. AND ap.partner_id IS NULL AND ml.partner_id IS NULL
  604. """
  605. if not include_initial_balance:
  606. sub_subquery_sum_amounts += """
  607. AND ap.include_initial_balance != TRUE AND ml.date >= %s
  608. """
  609. else:
  610. sub_subquery_sum_amounts += """
  611. AND ap.include_initial_balance = TRUE
  612. """
  613. if self.only_posted_moves:
  614. sub_subquery_sum_amounts += """
  615. INNER JOIN
  616. account_move m ON ml.move_id = m.id AND m.state = 'posted'
  617. """
  618. if self.filter_cost_center_ids:
  619. sub_subquery_sum_amounts += """
  620. INNER JOIN
  621. account_analytic_account aa
  622. ON
  623. ml.analytic_account_id = aa.id
  624. AND aa.id IN %s
  625. """
  626. if self.filter_analytic_tag_ids:
  627. sub_subquery_sum_amounts += """
  628. INNER JOIN
  629. move_lines_on_tags ON ml.id = move_lines_on_tags.ml_id
  630. """
  631. sub_subquery_sum_amounts += """
  632. GROUP BY
  633. ap.account_id, ap.partner_id, c.id
  634. """
  635. return sub_subquery_sum_amounts
  636. def _get_final_partner_sub_subquery_sum_amounts(self, only_empty_partner,
  637. date_included):
  638. """Return final subquery used to compute sum amounts on partners"""
  639. subquery_sum_amounts = """
  640. SELECT
  641. sub.account_id AS account_id,
  642. sub.partner_id AS partner_id,
  643. SUM(COALESCE(sub.debit, 0.0)) AS debit,
  644. SUM(COALESCE(sub.credit, 0.0)) AS credit,
  645. SUM(COALESCE(sub.balance, 0.0)) AS balance,
  646. MAX(sub.currency_id) AS currency_id,
  647. SUM(COALESCE(sub.balance_currency, 0.0)) AS balance_currency
  648. FROM
  649. (
  650. """
  651. subquery_sum_amounts += self._get_partner_sub_subquery_sum_amounts(
  652. only_empty_partner,
  653. include_initial_balance=False,
  654. date_included=date_included
  655. )
  656. subquery_sum_amounts += """
  657. UNION
  658. """
  659. subquery_sum_amounts += self._get_partner_sub_subquery_sum_amounts(
  660. only_empty_partner,
  661. include_initial_balance=True,
  662. date_included=date_included
  663. )
  664. subquery_sum_amounts += """
  665. ) sub
  666. GROUP BY
  667. sub.account_id, sub.partner_id
  668. """
  669. return subquery_sum_amounts
  670. def _inject_partner_values(self, only_empty_partner=False):
  671. """ Inject report values for report_general_ledger_qweb_partner.
  672. Only for "partner" accounts (payable and receivable).
  673. """
  674. # pylint: disable=sql-injection
  675. query_inject_partner = """
  676. WITH
  677. accounts_partners AS
  678. (
  679. SELECT
  680. ra.id AS report_account_id,
  681. a.id AS account_id,
  682. at.include_initial_balance AS include_initial_balance,
  683. p.id AS partner_id,
  684. COALESCE(
  685. CASE
  686. WHEN
  687. NULLIF(p.name, '') IS NOT NULL
  688. AND NULLIF(p.ref, '') IS NOT NULL
  689. THEN p.name || ' (' || p.ref || ')'
  690. ELSE p.name
  691. END,
  692. '""" + _('No partner allocated') + """'
  693. ) AS partner_name
  694. FROM
  695. report_general_ledger_qweb_account ra
  696. INNER JOIN
  697. account_account a ON ra.account_id = a.id
  698. INNER JOIN
  699. account_account_type at ON a.user_type_id = at.id
  700. INNER JOIN
  701. account_move_line ml ON a.id = ml.account_id
  702. LEFT JOIN
  703. res_partner p ON ml.partner_id = p.id
  704. """
  705. if self.filter_cost_center_ids:
  706. query_inject_partner += """
  707. INNER JOIN
  708. account_analytic_account aa
  709. ON
  710. ml.analytic_account_id = aa.id
  711. AND aa.id IN %s
  712. """
  713. if self.filter_analytic_tag_ids:
  714. query_inject_partner += """
  715. INNER JOIN
  716. account_analytic_tag_account_move_line_rel atml
  717. ON atml.account_move_line_id = ml.id
  718. INNER JOIN
  719. account_analytic_tag aat
  720. ON
  721. atml.account_analytic_tag_id = aat.id
  722. AND aat.id IN %s
  723. """
  724. query_inject_partner += """
  725. WHERE
  726. ra.report_id = %s
  727. AND
  728. ra.is_partner_account = TRUE
  729. """
  730. if not only_empty_partner:
  731. query_inject_partner += """
  732. AND
  733. p.id IS NOT NULL
  734. """
  735. else:
  736. query_inject_partner += """
  737. AND
  738. p.id IS NULL
  739. """
  740. query_inject_partner += """
  741. """
  742. if self.centralize:
  743. query_inject_partner += """
  744. AND (a.centralized IS NULL OR a.centralized != TRUE)
  745. """
  746. if self.filter_partner_ids:
  747. query_inject_partner += """
  748. AND
  749. p.id IN %s
  750. """
  751. init_subquery = self._get_final_partner_sub_subquery_sum_amounts(
  752. only_empty_partner,
  753. date_included=False
  754. )
  755. final_subquery = self._get_final_partner_sub_subquery_sum_amounts(
  756. only_empty_partner,
  757. date_included=True
  758. )
  759. query_inject_partner += """
  760. GROUP BY
  761. ra.id,
  762. a.id,
  763. p.id,
  764. at.include_initial_balance
  765. ),
  766. """
  767. if self.filter_analytic_tag_ids:
  768. query_inject_partner += """
  769. move_lines_on_tags AS
  770. (
  771. SELECT
  772. DISTINCT ml.id AS ml_id
  773. FROM
  774. accounts_partners ap
  775. INNER JOIN
  776. account_move_line ml
  777. ON ap.account_id = ml.account_id
  778. INNER JOIN
  779. account_analytic_tag_account_move_line_rel atml
  780. ON atml.account_move_line_id = ml.id
  781. INNER JOIN
  782. account_analytic_tag aat
  783. ON
  784. atml.account_analytic_tag_id = aat.id
  785. WHERE
  786. aat.id IN %s
  787. ),
  788. """
  789. query_inject_partner += """
  790. initial_sum_amounts AS ( """ + init_subquery + """ ),
  791. final_sum_amounts AS ( """ + final_subquery + """ )
  792. INSERT INTO
  793. report_general_ledger_qweb_partner
  794. (
  795. report_account_id,
  796. create_uid,
  797. create_date,
  798. partner_id,
  799. name,
  800. initial_debit,
  801. initial_credit,
  802. initial_balance,
  803. currency_id,
  804. initial_balance_foreign_currency,
  805. final_debit,
  806. final_credit,
  807. final_balance,
  808. final_balance_foreign_currency
  809. )
  810. SELECT
  811. ap.report_account_id,
  812. %s AS create_uid,
  813. NOW() AS create_date,
  814. ap.partner_id,
  815. ap.partner_name,
  816. COALESCE(i.debit, 0.0) AS initial_debit,
  817. COALESCE(i.credit, 0.0) AS initial_credit,
  818. COALESCE(i.balance, 0.0) AS initial_balance,
  819. i.currency_id AS currency_id,
  820. COALESCE(i.balance_currency, 0.0) AS initial_balance_foreign_currency,
  821. COALESCE(f.debit, 0.0) AS final_debit,
  822. COALESCE(f.credit, 0.0) AS final_credit,
  823. COALESCE(f.balance, 0.0) AS final_balance,
  824. COALESCE(f.balance_currency, 0.0) AS final_balance_foreign_currency
  825. FROM
  826. accounts_partners ap
  827. LEFT JOIN
  828. initial_sum_amounts i
  829. ON
  830. (
  831. """
  832. if not only_empty_partner:
  833. query_inject_partner += """
  834. ap.partner_id = i.partner_id
  835. """
  836. else:
  837. query_inject_partner += """
  838. ap.partner_id IS NULL AND i.partner_id IS NULL
  839. """
  840. query_inject_partner += """
  841. )
  842. AND ap.account_id = i.account_id
  843. LEFT JOIN
  844. final_sum_amounts f
  845. ON
  846. (
  847. """
  848. if not only_empty_partner:
  849. query_inject_partner += """
  850. ap.partner_id = f.partner_id
  851. """
  852. else:
  853. query_inject_partner += """
  854. ap.partner_id IS NULL AND f.partner_id IS NULL
  855. """
  856. query_inject_partner += """
  857. )
  858. AND ap.account_id = f.account_id
  859. WHERE
  860. (
  861. i.debit IS NOT NULL AND i.debit != 0
  862. OR i.credit IS NOT NULL AND i.credit != 0
  863. OR i.balance IS NOT NULL AND i.balance != 0
  864. OR f.debit IS NOT NULL AND f.debit != 0
  865. OR f.credit IS NOT NULL AND f.credit != 0
  866. OR f.balance IS NOT NULL AND f.balance != 0
  867. )
  868. """
  869. if self.hide_account_at_0:
  870. query_inject_partner += """
  871. AND
  872. f.balance IS NOT NULL AND f.balance != 0
  873. """
  874. query_inject_partner_params = ()
  875. if self.filter_cost_center_ids:
  876. query_inject_partner_params += (
  877. tuple(self.filter_cost_center_ids.ids),
  878. )
  879. if self.filter_analytic_tag_ids:
  880. query_inject_partner_params += (
  881. tuple(self.filter_analytic_tag_ids.ids),
  882. )
  883. query_inject_partner_params += (
  884. self.id,
  885. )
  886. if self.filter_partner_ids:
  887. query_inject_partner_params += (
  888. tuple(self.filter_partner_ids.ids),
  889. )
  890. if self.filter_analytic_tag_ids:
  891. query_inject_partner_params += (
  892. tuple(self.filter_analytic_tag_ids.ids),
  893. )
  894. query_inject_partner_params += (
  895. self.date_from,
  896. self.fy_start_date,
  897. )
  898. if self.filter_cost_center_ids:
  899. query_inject_partner_params += (
  900. tuple(self.filter_cost_center_ids.ids),
  901. )
  902. query_inject_partner_params += (
  903. self.date_from,
  904. )
  905. if self.filter_cost_center_ids:
  906. query_inject_partner_params += (
  907. tuple(self.filter_cost_center_ids.ids),
  908. )
  909. query_inject_partner_params += (
  910. self.date_to,
  911. self.fy_start_date,
  912. )
  913. if self.filter_cost_center_ids:
  914. query_inject_partner_params += (
  915. tuple(self.filter_cost_center_ids.ids),
  916. )
  917. query_inject_partner_params += (
  918. self.date_to,
  919. )
  920. if self.filter_cost_center_ids:
  921. query_inject_partner_params += (
  922. tuple(self.filter_cost_center_ids.ids),
  923. )
  924. query_inject_partner_params += (
  925. self.env.uid,
  926. )
  927. self.env.cr.execute(query_inject_partner, query_inject_partner_params)
  928. def _inject_line_not_centralized_values(
  929. self,
  930. is_account_line=True,
  931. is_partner_line=False,
  932. only_empty_partner_line=False,
  933. only_unaffected_earnings_account=False):
  934. """ Inject report values for report_general_ledger_qweb_move_line.
  935. If centralized option have been chosen,
  936. only non centralized accounts are computed.
  937. In function of `is_account_line` and `is_partner_line` values,
  938. the move_line link is made either with account or either with partner.
  939. The "only_empty_partner_line" value is used
  940. to compute data without partner.
  941. """
  942. query_inject_move_line = ""
  943. if self.filter_analytic_tag_ids:
  944. query_inject_move_line += """
  945. WITH
  946. move_lines_on_tags AS
  947. (
  948. SELECT
  949. DISTINCT ml.id AS ml_id
  950. FROM
  951. """
  952. if is_account_line:
  953. query_inject_move_line += """
  954. report_general_ledger_qweb_account ra
  955. """
  956. elif is_partner_line:
  957. query_inject_move_line += """
  958. report_general_ledger_qweb_partner rp
  959. INNER JOIN
  960. report_general_ledger_qweb_account ra
  961. ON rp.report_account_id = ra.id
  962. """
  963. query_inject_move_line += """
  964. INNER JOIN
  965. account_move_line ml
  966. ON ra.account_id = ml.account_id
  967. INNER JOIN
  968. account_analytic_tag_account_move_line_rel atml
  969. ON atml.account_move_line_id = ml.id
  970. INNER JOIN
  971. account_analytic_tag aat
  972. ON
  973. atml.account_analytic_tag_id = aat.id
  974. WHERE
  975. ra.report_id = %s
  976. AND
  977. aat.id IN %s
  978. )
  979. """
  980. query_inject_move_line += """
  981. INSERT INTO
  982. report_general_ledger_qweb_move_line
  983. (
  984. """
  985. if is_account_line:
  986. query_inject_move_line += """
  987. report_account_id,
  988. """
  989. elif is_partner_line:
  990. query_inject_move_line += """
  991. report_partner_id,
  992. """
  993. query_inject_move_line += """
  994. create_uid,
  995. create_date,
  996. move_line_id,
  997. matched_ml_id,
  998. date,
  999. entry,
  1000. journal,
  1001. account,
  1002. taxes_description,
  1003. partner,
  1004. label,
  1005. cost_center,
  1006. debit,
  1007. credit,
  1008. cumul_balance,
  1009. currency_id,
  1010. amount_currency
  1011. )
  1012. SELECT
  1013. """
  1014. if is_account_line:
  1015. query_inject_move_line += """
  1016. ra.id AS report_account_id,
  1017. """
  1018. elif is_partner_line:
  1019. query_inject_move_line += """
  1020. rp.id AS report_partner_id,
  1021. """
  1022. query_inject_move_line += """
  1023. %s AS create_uid,
  1024. NOW() AS create_date,
  1025. ml.id AS move_line_id,
  1026. fr.id AS matched_ml_id,
  1027. ml.date,
  1028. m.name AS entry,
  1029. j.code AS journal,
  1030. a.code AS account,
  1031. CASE
  1032. WHEN
  1033. ml.tax_line_id is not null
  1034. THEN
  1035. COALESCE(at.description, at.name)
  1036. WHEN
  1037. ml.tax_line_id is null
  1038. THEN
  1039. (SELECT
  1040. array_to_string(
  1041. array_agg(COALESCE(at.description, at.name)
  1042. ), ', ')
  1043. FROM
  1044. account_move_line_account_tax_rel aml_at_rel
  1045. LEFT JOIN
  1046. account_tax at on (at.id = aml_at_rel.account_tax_id)
  1047. WHERE
  1048. aml_at_rel.account_move_line_id = ml.id)
  1049. ELSE
  1050. ''
  1051. END as taxes_description,
  1052. """
  1053. if not only_empty_partner_line:
  1054. query_inject_move_line += """
  1055. CASE
  1056. WHEN
  1057. NULLIF(p.name, '') IS NOT NULL
  1058. AND NULLIF(p.ref, '') IS NOT NULL
  1059. THEN p.name || ' (' || p.ref || ')'
  1060. ELSE p.name
  1061. END AS partner,
  1062. """
  1063. elif only_empty_partner_line:
  1064. query_inject_move_line += """
  1065. '""" + _('No partner allocated') + """' AS partner,
  1066. """
  1067. query_inject_move_line += """
  1068. CONCAT_WS(' - ', NULLIF(ml.ref, ''), NULLIF(ml.name, '')) AS label,
  1069. aa.name AS cost_center,
  1070. ml.debit,
  1071. ml.credit,
  1072. """
  1073. if is_account_line:
  1074. query_inject_move_line += """
  1075. ra.initial_balance + (
  1076. SUM(ml.balance)
  1077. OVER (PARTITION BY a.code
  1078. ORDER BY a.code, ml.date, ml.id)
  1079. ) AS cumul_balance,
  1080. """
  1081. elif is_partner_line and not only_empty_partner_line:
  1082. query_inject_move_line += """
  1083. rp.initial_balance + (
  1084. SUM(ml.balance)
  1085. OVER (PARTITION BY a.code, p.name
  1086. ORDER BY a.code, p.name, ml.date, ml.id)
  1087. ) AS cumul_balance,
  1088. """
  1089. elif is_partner_line and only_empty_partner_line:
  1090. query_inject_move_line += """
  1091. rp.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. query_inject_move_line += """
  1098. c.id AS currency_id,
  1099. ml.amount_currency
  1100. FROM
  1101. """
  1102. if is_account_line:
  1103. query_inject_move_line += """
  1104. report_general_ledger_qweb_account ra
  1105. """
  1106. elif is_partner_line:
  1107. query_inject_move_line += """
  1108. report_general_ledger_qweb_partner rp
  1109. INNER JOIN
  1110. report_general_ledger_qweb_account ra ON rp.report_account_id = ra.id
  1111. """
  1112. query_inject_move_line += """
  1113. INNER JOIN
  1114. account_move_line ml ON ra.account_id = ml.account_id
  1115. INNER JOIN
  1116. account_move m ON ml.move_id = m.id
  1117. INNER JOIN
  1118. account_journal j ON ml.journal_id = j.id
  1119. INNER JOIN
  1120. account_account a ON ml.account_id = a.id
  1121. LEFT JOIN
  1122. account_tax at ON ml.tax_line_id = at.id
  1123. """
  1124. if is_account_line:
  1125. query_inject_move_line += """
  1126. LEFT JOIN
  1127. res_partner p ON ml.partner_id = p.id
  1128. """
  1129. elif is_partner_line and not only_empty_partner_line:
  1130. query_inject_move_line += """
  1131. INNER JOIN
  1132. res_partner p
  1133. ON ml.partner_id = p.id AND rp.partner_id = p.id
  1134. """
  1135. query_inject_move_line += """
  1136. LEFT JOIN
  1137. account_full_reconcile fr ON ml.full_reconcile_id = fr.id
  1138. LEFT JOIN
  1139. res_currency c ON ml.currency_id = c.id
  1140. """
  1141. if self.filter_cost_center_ids:
  1142. query_inject_move_line += """
  1143. INNER JOIN
  1144. account_analytic_account aa
  1145. ON
  1146. ml.analytic_account_id = aa.id
  1147. AND aa.id IN %s
  1148. """
  1149. else:
  1150. query_inject_move_line += """
  1151. LEFT JOIN
  1152. account_analytic_account aa ON ml.analytic_account_id = aa.id
  1153. """
  1154. if self.filter_analytic_tag_ids:
  1155. query_inject_move_line += """
  1156. INNER JOIN
  1157. move_lines_on_tags ON ml.id = move_lines_on_tags.ml_id
  1158. """
  1159. query_inject_move_line += """
  1160. WHERE
  1161. ra.report_id = %s
  1162. AND
  1163. """
  1164. if only_unaffected_earnings_account:
  1165. query_inject_move_line += """
  1166. a.id = %s
  1167. AND
  1168. """
  1169. if is_account_line:
  1170. query_inject_move_line += """
  1171. (ra.is_partner_account IS NULL OR ra.is_partner_account != TRUE)
  1172. """
  1173. elif is_partner_line:
  1174. query_inject_move_line += """
  1175. ra.is_partner_account = TRUE
  1176. """
  1177. if self.centralize:
  1178. query_inject_move_line += """
  1179. AND
  1180. (a.centralized IS NULL OR a.centralized != TRUE)
  1181. """
  1182. query_inject_move_line += """
  1183. AND
  1184. ml.date BETWEEN %s AND %s
  1185. """
  1186. if self.only_posted_moves:
  1187. query_inject_move_line += """
  1188. AND
  1189. m.state = 'posted'
  1190. """
  1191. if only_empty_partner_line:
  1192. query_inject_move_line += """
  1193. AND
  1194. ml.partner_id IS NULL
  1195. AND
  1196. rp.partner_id IS NULL
  1197. """
  1198. if self.filter_journal_ids:
  1199. query_inject_move_line += """
  1200. AND
  1201. j.id IN %s
  1202. """
  1203. if is_account_line:
  1204. query_inject_move_line += """
  1205. ORDER BY
  1206. a.code, ml.date, ml.id
  1207. """
  1208. elif is_partner_line and not only_empty_partner_line:
  1209. query_inject_move_line += """
  1210. ORDER BY
  1211. a.code, p.name, ml.date, ml.id
  1212. """
  1213. elif is_partner_line and only_empty_partner_line:
  1214. query_inject_move_line += """
  1215. ORDER BY
  1216. a.code, ml.date, ml.id
  1217. """
  1218. query_inject_move_line_params = ()
  1219. if self.filter_analytic_tag_ids:
  1220. query_inject_move_line_params += (
  1221. self.id,
  1222. tuple(self.filter_analytic_tag_ids.ids),
  1223. )
  1224. query_inject_move_line_params += (
  1225. self.env.uid,
  1226. )
  1227. if self.filter_cost_center_ids:
  1228. query_inject_move_line_params += (
  1229. tuple(self.filter_cost_center_ids.ids),
  1230. )
  1231. query_inject_move_line_params += (
  1232. self.id,
  1233. )
  1234. if only_unaffected_earnings_account:
  1235. query_inject_move_line_params += (
  1236. self.unaffected_earnings_account.id,
  1237. )
  1238. query_inject_move_line_params += (
  1239. self.date_from,
  1240. self.date_to,
  1241. )
  1242. if self.filter_journal_ids:
  1243. query_inject_move_line_params += (tuple(
  1244. self.filter_journal_ids.ids,
  1245. ),)
  1246. self.env.cr.execute(
  1247. query_inject_move_line,
  1248. query_inject_move_line_params
  1249. )
  1250. def _inject_line_centralized_values(self):
  1251. """ Inject report values for report_general_ledger_qweb_move_line.
  1252. Only centralized accounts are computed.
  1253. """
  1254. if self.filter_analytic_tag_ids:
  1255. query_inject_move_line_centralized = """
  1256. WITH
  1257. move_lines_on_tags AS
  1258. (
  1259. SELECT
  1260. DISTINCT ml.id AS ml_id
  1261. FROM
  1262. report_general_ledger_qweb_account ra
  1263. INNER JOIN
  1264. account_move_line ml
  1265. ON ra.account_id = ml.account_id
  1266. INNER JOIN
  1267. account_analytic_tag_account_move_line_rel atml
  1268. ON atml.account_move_line_id = ml.id
  1269. INNER JOIN
  1270. account_analytic_tag aat
  1271. ON
  1272. atml.account_analytic_tag_id = aat.id
  1273. WHERE
  1274. ra.report_id = %s
  1275. AND
  1276. aat.id IN %s
  1277. ),
  1278. """
  1279. else:
  1280. query_inject_move_line_centralized = """
  1281. WITH
  1282. """
  1283. query_inject_move_line_centralized += """
  1284. move_lines AS
  1285. (
  1286. SELECT
  1287. ml.account_id,
  1288. (
  1289. DATE_TRUNC('month', ml.date) + interval '1 month'
  1290. - interval '1 day'
  1291. )::date AS date,
  1292. SUM(ml.debit) AS debit,
  1293. SUM(ml.credit) AS credit,
  1294. SUM(ml.balance) AS balance,
  1295. ml.currency_id AS currency_id,
  1296. ml.journal_id as journal_id
  1297. FROM
  1298. report_general_ledger_qweb_account ra
  1299. INNER JOIN
  1300. account_move_line ml ON ra.account_id = ml.account_id
  1301. INNER JOIN
  1302. account_move m ON ml.move_id = m.id
  1303. INNER JOIN
  1304. account_account a ON ml.account_id = a.id
  1305. """
  1306. if self.filter_cost_center_ids:
  1307. query_inject_move_line_centralized += """
  1308. INNER JOIN
  1309. account_analytic_account aa
  1310. ON
  1311. ml.analytic_account_id = aa.id
  1312. AND aa.id IN %s
  1313. """
  1314. if self.filter_analytic_tag_ids:
  1315. query_inject_move_line_centralized += """
  1316. INNER JOIN
  1317. move_lines_on_tags ON ml.id = move_lines_on_tags.ml_id
  1318. """
  1319. query_inject_move_line_centralized += """
  1320. WHERE
  1321. ra.report_id = %s
  1322. AND
  1323. a.centralized = TRUE
  1324. AND
  1325. ml.date BETWEEN %s AND %s
  1326. """
  1327. if self.only_posted_moves:
  1328. query_inject_move_line_centralized += """
  1329. AND
  1330. m.state = 'posted'
  1331. """
  1332. query_inject_move_line_centralized += """
  1333. GROUP BY
  1334. ra.id, ml.account_id, a.code, 2, ml.currency_id, ml.journal_id
  1335. )
  1336. INSERT INTO
  1337. report_general_ledger_qweb_move_line
  1338. (
  1339. report_account_id,
  1340. create_uid,
  1341. create_date,
  1342. date,
  1343. account,
  1344. journal,
  1345. label,
  1346. debit,
  1347. credit,
  1348. cumul_balance
  1349. )
  1350. SELECT
  1351. ra.id AS report_account_id,
  1352. %s AS create_uid,
  1353. NOW() AS create_date,
  1354. ml.date,
  1355. a.code AS account,
  1356. j.code as journal,
  1357. '""" + _('Centralized Entries') + """' AS label,
  1358. ml.debit AS debit,
  1359. ml.credit AS credit,
  1360. ra.initial_balance + (
  1361. SUM(ml.balance)
  1362. OVER (PARTITION BY a.code ORDER BY ml.date)
  1363. ) AS cumul_balance
  1364. FROM
  1365. report_general_ledger_qweb_account ra
  1366. INNER JOIN
  1367. move_lines ml ON ra.account_id = ml.account_id
  1368. INNER JOIN
  1369. account_account a ON ml.account_id = a.id
  1370. INNER JOIN
  1371. account_journal j ON ml.journal_id = j.id
  1372. LEFT JOIN
  1373. res_currency c ON ml.currency_id = c.id
  1374. WHERE
  1375. ra.report_id = %s
  1376. AND
  1377. (a.centralized IS NOT NULL AND a.centralized = TRUE)
  1378. """
  1379. if self.filter_journal_ids:
  1380. query_inject_move_line_centralized += """
  1381. AND
  1382. j.id in %s
  1383. """
  1384. query_inject_move_line_centralized += """
  1385. ORDER BY
  1386. a.code, ml.date
  1387. """
  1388. query_inject_move_line_centralized_params = ()
  1389. if self.filter_analytic_tag_ids:
  1390. query_inject_move_line_centralized_params += (
  1391. self.id,
  1392. tuple(self.filter_analytic_tag_ids.ids),
  1393. )
  1394. if self.filter_cost_center_ids:
  1395. query_inject_move_line_centralized_params += (
  1396. tuple(self.filter_cost_center_ids.ids),
  1397. )
  1398. query_inject_move_line_centralized_params += (
  1399. self.id,
  1400. self.date_from,
  1401. self.date_to,
  1402. self.env.uid,
  1403. self.id,
  1404. )
  1405. if self.filter_journal_ids:
  1406. query_inject_move_line_centralized_params += (tuple(
  1407. self.filter_journal_ids.ids,
  1408. ),)
  1409. self.env.cr.execute(
  1410. query_inject_move_line_centralized,
  1411. query_inject_move_line_centralized_params
  1412. )
  1413. def _compute_analytic_tags(self):
  1414. """ Compute "tags" column"""
  1415. query_update_analytic_tags = """
  1416. UPDATE
  1417. report_general_ledger_qweb_move_line
  1418. SET
  1419. tags = tags_values.tags
  1420. FROM
  1421. (
  1422. (
  1423. SELECT
  1424. rml.id AS report_id,
  1425. array_to_string(array_agg(t.name ORDER BY t.name), ',') AS tags
  1426. FROM
  1427. account_move_line ml
  1428. INNER JOIN
  1429. report_general_ledger_qweb_move_line rml
  1430. ON ml.id = rml.move_line_id
  1431. INNER JOIN
  1432. report_general_ledger_qweb_account ra
  1433. ON rml.report_account_id = ra.id
  1434. INNER JOIN
  1435. account_analytic_tag_account_move_line_rel tml
  1436. ON ml.id = tml.account_move_line_id
  1437. INNER JOIN
  1438. account_analytic_tag t
  1439. ON tml.account_analytic_tag_id = t.id
  1440. WHERE
  1441. ra.report_id = %(report_id)s
  1442. GROUP BY
  1443. rml.id,
  1444. ml.id
  1445. )
  1446. UNION
  1447. (
  1448. SELECT
  1449. rml.id AS report_id,
  1450. array_to_string(array_agg(t.name ORDER BY t.name), ',') AS tags
  1451. FROM
  1452. account_move_line ml
  1453. INNER JOIN
  1454. report_general_ledger_qweb_move_line rml
  1455. ON ml.id = rml.move_line_id
  1456. INNER JOIN
  1457. report_general_ledger_qweb_partner rp
  1458. ON rml.report_partner_id = rp.id
  1459. INNER JOIN
  1460. report_general_ledger_qweb_account ra
  1461. ON rp.report_account_id = ra.id
  1462. INNER JOIN
  1463. account_analytic_tag_account_move_line_rel tml
  1464. ON ml.id = tml.account_move_line_id
  1465. INNER JOIN
  1466. account_analytic_tag t
  1467. ON tml.account_analytic_tag_id = t.id
  1468. WHERE
  1469. ra.report_id = %(report_id)s
  1470. GROUP BY
  1471. rml.id,
  1472. ml.id
  1473. )
  1474. ) AS tags_values
  1475. WHERE
  1476. report_general_ledger_qweb_move_line.id = tags_values.report_id
  1477. """
  1478. params = {
  1479. 'report_id': self.id,
  1480. }
  1481. self.env.cr.execute(query_update_analytic_tags, params)
  1482. def _inject_unaffected_earnings_account_values(self):
  1483. """Inject the report values of the unaffected earnings account
  1484. for report_general_ledger_qweb_account."""
  1485. # Fetch the profit and loss accounts
  1486. query_unaffected_earnings_account_ids = """
  1487. SELECT a.id
  1488. FROM account_account as a
  1489. INNER JOIN account_account_type as at
  1490. ON at.id = a.user_type_id
  1491. WHERE at.include_initial_balance = FALSE
  1492. """
  1493. self.env.cr.execute(query_unaffected_earnings_account_ids)
  1494. pl_account_ids = [r[0] for r in self.env.cr.fetchall()]
  1495. unaffected_earnings_account_ids = \
  1496. pl_account_ids + [self.unaffected_earnings_account.id]
  1497. # Fetch the current fiscal year start date
  1498. date = fields.Datetime.from_string(self.date_from)
  1499. res = self.company_id.compute_fiscalyear_dates(date)
  1500. fy_start_date = res['date_from']
  1501. query_select_previous_fy_unaffected_earnings_params = {
  1502. 'date_to': fy_start_date,
  1503. 'company_id': self.company_id.id,
  1504. 'account_ids': tuple(unaffected_earnings_account_ids),
  1505. 'analytic_tag_ids': tuple(self.filter_analytic_tag_ids.ids),
  1506. }
  1507. query_select_previous_fy_unaffected_earnings = ''
  1508. q_analytic_tags = ''
  1509. if self.filter_analytic_tag_ids:
  1510. q_analytic_tags = """
  1511. WITH move_lines_on_tags AS
  1512. (
  1513. SELECT
  1514. DISTINCT ml.id AS ml_id
  1515. FROM
  1516. account_account a
  1517. INNER JOIN
  1518. account_move_line ml
  1519. ON a.id = ml.account_id
  1520. INNER JOIN
  1521. account_analytic_tag_account_move_line_rel atml
  1522. ON atml.account_move_line_id = ml.id
  1523. INNER JOIN
  1524. account_analytic_tag aat
  1525. ON
  1526. atml.account_analytic_tag_id = aat.id
  1527. WHERE
  1528. aat.id IN %(analytic_tag_ids)s
  1529. )
  1530. """
  1531. query_select_previous_fy_unaffected_earnings += q_analytic_tags
  1532. query_select_previous_fy_unaffected_earnings += """
  1533. SELECT sum(ml.balance) as balance
  1534. FROM account_move_line as ml
  1535. INNER JOIN account_move as am
  1536. ON am.id = ml.move_id
  1537. INNER JOIN account_journal j
  1538. ON am.journal_id = j.id
  1539. """
  1540. if self.filter_cost_center_ids:
  1541. query_select_previous_fy_unaffected_earnings += """
  1542. INNER JOIN account_analytic_account aa
  1543. ON ml.analytic_account_id = aa.id
  1544. AND aa.id IN %(cost_center_ids)s
  1545. """
  1546. query_select_previous_fy_unaffected_earnings_params[
  1547. 'cost_center_ids'] = tuple(self.filter_cost_center_ids.ids)
  1548. if self.filter_analytic_tag_ids:
  1549. query_select_previous_fy_unaffected_earnings += """
  1550. INNER JOIN move_lines_on_tags ON ml.id =
  1551. move_lines_on_tags.ml_id
  1552. """
  1553. query_select_previous_fy_unaffected_earnings += """
  1554. WHERE ml.date < %(date_to)s
  1555. AND ml.company_id = %(company_id)s
  1556. AND ml.account_id IN %(account_ids)s
  1557. """
  1558. if self.filter_journal_ids:
  1559. query_select_previous_fy_unaffected_earnings += """
  1560. AND j.id IN %(journal_ids)s
  1561. """
  1562. query_select_previous_fy_unaffected_earnings_params[
  1563. 'journal_ids'] = tuple(self.filter_journal_ids.ids)
  1564. if self.only_posted_moves:
  1565. query_select_previous_fy_unaffected_earnings += """
  1566. AND am.state = 'posted'
  1567. """
  1568. self.env.cr.execute(
  1569. query_select_previous_fy_unaffected_earnings,
  1570. query_select_previous_fy_unaffected_earnings_params)
  1571. res = self.env.cr.fetchone()
  1572. unaffected_earnings_initial_balance = res[0] or 0.0
  1573. # Now select the current period unaffected earnings,
  1574. # excluding the current period P&L.
  1575. query_select_period_unaffected_earnings_params = {
  1576. 'date_from': self.date_from,
  1577. 'date_to': self.date_to,
  1578. 'company_id': self.company_id.id,
  1579. 'unaffected_earnings_id': self.unaffected_earnings_account.id,
  1580. 'analytic_tag_ids': tuple(self.filter_analytic_tag_ids.ids),
  1581. }
  1582. query_select_period_unaffected_earnings = ''
  1583. if self.filter_analytic_tag_ids:
  1584. query_select_period_unaffected_earnings += q_analytic_tags
  1585. query_select_period_unaffected_earnings += """
  1586. SELECT
  1587. sum(ml.debit) as sum_debit,
  1588. sum(ml.credit) as sum_credit,
  1589. sum(ml.balance) as balance
  1590. FROM account_move_line as ml
  1591. INNER JOIN account_move as am
  1592. ON am.id = ml.move_id
  1593. INNER JOIN account_journal j
  1594. ON am.journal_id = j.id
  1595. """
  1596. if self.filter_cost_center_ids:
  1597. query_select_period_unaffected_earnings += """
  1598. INNER JOIN account_analytic_account aa
  1599. ON ml.analytic_account_id = aa.id
  1600. AND aa.id IN %(cost_center_ids)s
  1601. """
  1602. query_select_period_unaffected_earnings_params[
  1603. 'cost_center_ids'] = tuple(self.filter_cost_center_ids.ids)
  1604. if self.filter_analytic_tag_ids:
  1605. query_select_period_unaffected_earnings += """
  1606. INNER JOIN move_lines_on_tags
  1607. ON ml.id = move_lines_on_tags.ml_id
  1608. """
  1609. query_select_period_unaffected_earnings += """
  1610. WHERE am.date >= %(date_from)s
  1611. AND ml.date <= %(date_to)s
  1612. AND ml.company_id = %(company_id)s
  1613. AND ml.account_id = %(unaffected_earnings_id)s
  1614. """
  1615. if self.filter_journal_ids:
  1616. query_select_period_unaffected_earnings += """
  1617. AND j.id IN %(journal_ids)s
  1618. """
  1619. query_select_period_unaffected_earnings_params[
  1620. 'journal_ids'] = tuple(self.filter_journal_ids.ids)
  1621. if self.only_posted_moves:
  1622. query_select_period_unaffected_earnings += """
  1623. AND am.state = 'posted'
  1624. """
  1625. self.env.cr.execute(query_select_period_unaffected_earnings,
  1626. query_select_period_unaffected_earnings_params)
  1627. res = self.env.cr.fetchone()
  1628. unaffected_earnings_period_debit = res[0] or 0.0
  1629. unaffected_earnings_period_credit = res[1] or 0.0
  1630. unaffected_earnings_period_balance = res[2] or 0.0
  1631. # pylint: disable=sql-injection
  1632. query_inject_account = """
  1633. INSERT INTO
  1634. report_general_ledger_qweb_account (
  1635. report_id,
  1636. create_uid,
  1637. create_date,
  1638. account_id,
  1639. code,
  1640. name,
  1641. is_partner_account,
  1642. initial_debit,
  1643. initial_credit,
  1644. initial_balance,
  1645. final_debit,
  1646. final_credit,
  1647. final_balance
  1648. )
  1649. VALUES (
  1650. %(report_id)s,
  1651. %(user_id)s,
  1652. NOW(),
  1653. %(account_id)s,
  1654. %(code)s,
  1655. %(name)s,
  1656. False,
  1657. %(initial_debit)s,
  1658. %(initial_credit)s,
  1659. %(initial_balance)s,
  1660. %(final_debit)s,
  1661. %(final_credit)s,
  1662. %(final_balance)s
  1663. )
  1664. """
  1665. initial_debit = unaffected_earnings_initial_balance >= 0 and \
  1666. unaffected_earnings_initial_balance or 0
  1667. initial_credit = unaffected_earnings_initial_balance < 0 and \
  1668. -1 * unaffected_earnings_initial_balance or 0
  1669. final_balance = unaffected_earnings_initial_balance + \
  1670. unaffected_earnings_period_balance
  1671. query_inject_account_params = {
  1672. 'report_id': self.id,
  1673. 'user_id': self.env.uid,
  1674. 'account_id': self.unaffected_earnings_account.id,
  1675. 'code': self.unaffected_earnings_account.code,
  1676. 'name': self.unaffected_earnings_account.name,
  1677. 'initial_debit': initial_debit,
  1678. 'initial_credit': initial_credit,
  1679. 'initial_balance': unaffected_earnings_initial_balance,
  1680. 'final_debit': initial_debit + unaffected_earnings_period_debit,
  1681. 'final_credit': initial_credit + unaffected_earnings_period_credit,
  1682. 'final_balance': final_balance,
  1683. }
  1684. self.env.cr.execute(query_inject_account,
  1685. query_inject_account_params)