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.

863 lines
25 KiB

  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 openerp 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. # Filters fields, used for data computation
  21. date_from = fields.Date()
  22. date_to = fields.Date()
  23. fy_start_date = fields.Date()
  24. only_posted_moves = fields.Boolean()
  25. hide_account_balance_at_0 = fields.Boolean()
  26. company_id = fields.Many2one(comodel_name='res.company')
  27. filter_account_ids = fields.Many2many(comodel_name='account.account')
  28. filter_partner_ids = fields.Many2many(comodel_name='res.partner')
  29. centralize = fields.Boolean()
  30. # Flag fields, used for report display
  31. has_second_currency = fields.Boolean()
  32. show_cost_center = fields.Boolean(
  33. default=lambda self: self.env.user.has_group(
  34. 'analytic.group_analytic_accounting'
  35. )
  36. )
  37. # Data fields, used to browse report data
  38. account_ids = fields.One2many(
  39. comodel_name='report_general_ledger_qweb_account',
  40. inverse_name='report_id'
  41. )
  42. class GeneralLedgerReportAccount(models.TransientModel):
  43. _name = 'report_general_ledger_qweb_account'
  44. _order = 'code ASC'
  45. report_id = fields.Many2one(
  46. comodel_name='report_general_ledger_qweb',
  47. ondelete='cascade',
  48. index=True
  49. )
  50. # Data fields, used to keep link with real object
  51. account_id = fields.Many2one(
  52. 'account.account',
  53. index=True
  54. )
  55. # Data fields, used for report display
  56. code = fields.Char()
  57. name = fields.Char()
  58. initial_debit = fields.Float(digits=(16, 2))
  59. initial_credit = fields.Float(digits=(16, 2))
  60. initial_balance = fields.Float(digits=(16, 2))
  61. final_debit = fields.Float(digits=(16, 2))
  62. final_credit = fields.Float(digits=(16, 2))
  63. final_balance = fields.Float(digits=(16, 2))
  64. # Flag fields, used for report display and for data computation
  65. is_partner_account = fields.Boolean()
  66. # Data fields, used to browse report data
  67. move_line_ids = fields.One2many(
  68. comodel_name='report_general_ledger_qweb_move_line',
  69. inverse_name='report_account_id'
  70. )
  71. partner_ids = fields.One2many(
  72. comodel_name='report_general_ledger_qweb_partner',
  73. inverse_name='report_account_id'
  74. )
  75. class GeneralLedgerReportPartner(models.TransientModel):
  76. _name = 'report_general_ledger_qweb_partner'
  77. report_account_id = fields.Many2one(
  78. comodel_name='report_general_ledger_qweb_account',
  79. ondelete='cascade',
  80. index=True
  81. )
  82. # Data fields, used to keep link with real object
  83. partner_id = fields.Many2one(
  84. 'res.partner',
  85. index=True
  86. )
  87. # Data fields, used for report display
  88. name = fields.Char()
  89. initial_debit = fields.Float(digits=(16, 2))
  90. initial_credit = fields.Float(digits=(16, 2))
  91. initial_balance = fields.Float(digits=(16, 2))
  92. final_debit = fields.Float(digits=(16, 2))
  93. final_credit = fields.Float(digits=(16, 2))
  94. final_balance = fields.Float(digits=(16, 2))
  95. # Data fields, used to browse report data
  96. move_line_ids = fields.One2many(
  97. comodel_name='report_general_ledger_qweb_move_line',
  98. inverse_name='report_partner_id'
  99. )
  100. @api.model
  101. def _generate_order_by(self, order_spec, query):
  102. """Custom order to display "No partner allocated" at last position."""
  103. return """
  104. ORDER BY
  105. CASE
  106. WHEN "report_general_ledger_qweb_partner"."partner_id" IS NOT NULL
  107. THEN 0
  108. ELSE 1
  109. END,
  110. "report_general_ledger_qweb_partner"."name"
  111. """
  112. class GeneralLedgerReportMoveLine(models.TransientModel):
  113. _name = 'report_general_ledger_qweb_move_line'
  114. report_account_id = fields.Many2one(
  115. comodel_name='report_general_ledger_qweb_account',
  116. ondelete='cascade',
  117. index=True
  118. )
  119. report_partner_id = fields.Many2one(
  120. comodel_name='report_general_ledger_qweb_partner',
  121. ondelete='cascade',
  122. index=True
  123. )
  124. # Data fields, used to keep link with real object
  125. move_line_id = fields.Many2one('account.move.line')
  126. # Data fields, used for report display
  127. date = fields.Date()
  128. entry = fields.Char()
  129. journal = fields.Char()
  130. account = fields.Char()
  131. partner = fields.Char()
  132. label = fields.Char()
  133. cost_center = fields.Char()
  134. matching_number = fields.Char()
  135. debit = fields.Float(digits=(16, 2))
  136. credit = fields.Float(digits=(16, 2))
  137. cumul_balance = fields.Float(digits=(16, 2))
  138. currency_name = fields.Char()
  139. amount_currency = fields.Float(digits=(16, 2))
  140. class GeneralLedgerReportCompute(models.TransientModel):
  141. """ Here, we just define methods.
  142. For class fields, go more top at this file.
  143. """
  144. _inherit = 'report_general_ledger_qweb'
  145. @api.multi
  146. def print_report(self, xlsx_report=False):
  147. self.ensure_one()
  148. self.compute_data_for_report()
  149. if xlsx_report:
  150. report_name = 'ledger.report.wizard.xlsx'
  151. else:
  152. report_name = 'account_financial_report_qweb.' \
  153. 'report_general_ledger_qweb'
  154. return {
  155. 'type': 'ir.actions.report.xml',
  156. 'report_name': report_name,
  157. 'datas': {'ids': [self.id]},
  158. }
  159. @api.multi
  160. def compute_data_for_report(self):
  161. self.ensure_one()
  162. # Compute report data
  163. self._inject_account_values()
  164. self._inject_partner_values()
  165. self._inject_line_not_centralized_values()
  166. self._inject_line_not_centralized_values(is_account_line=False,
  167. is_partner_line=True)
  168. self._inject_line_not_centralized_values(is_account_line=False,
  169. is_partner_line=True,
  170. only_empty_partner_line=True)
  171. if self.centralize:
  172. self._inject_line_centralized_values()
  173. # Compute display flag
  174. self._compute_has_second_currency()
  175. def _inject_account_values(self):
  176. """Inject report values for report_general_ledger_qweb_account."""
  177. subquery_sum_amounts = """
  178. SELECT
  179. a.id AS account_id,
  180. SUM(ml.debit) AS debit,
  181. SUM(ml.credit) AS credit,
  182. SUM(ml.balance) AS balance
  183. FROM
  184. accounts a
  185. INNER JOIN
  186. account_account_type at ON a.user_type_id = at.id
  187. INNER JOIN
  188. account_move_line ml
  189. ON a.id = ml.account_id
  190. AND ml.date <= %s
  191. AND
  192. (
  193. at.include_initial_balance != TRUE AND ml.date >= %s
  194. OR at.include_initial_balance = TRUE
  195. )
  196. """
  197. if self.only_posted_moves:
  198. subquery_sum_amounts += """
  199. INNER JOIN
  200. account_move m ON ml.move_id = m.id AND m.state = 'posted'
  201. """
  202. subquery_sum_amounts += """
  203. GROUP BY
  204. a.id
  205. """
  206. query_inject_account = """
  207. WITH
  208. accounts AS
  209. (
  210. SELECT
  211. a.id,
  212. a.code,
  213. a.name,
  214. a.internal_type IN ('payable', 'receivable')
  215. AS is_partner_account,
  216. a.user_type_id
  217. FROM
  218. account_account a
  219. """
  220. if self.filter_partner_ids:
  221. query_inject_account += """
  222. INNER JOIN
  223. account_move_line ml ON a.id = ml.account_id
  224. INNER JOIN
  225. res_partner p ON ml.partner_id = p.id
  226. """
  227. query_inject_account += """
  228. WHERE
  229. a.company_id = %s
  230. """
  231. if self.filter_account_ids:
  232. query_inject_account += """
  233. AND
  234. a.id IN %s
  235. """
  236. if self.filter_partner_ids:
  237. query_inject_account += """
  238. AND
  239. p.id IN %s
  240. GROUP BY
  241. a.id
  242. """
  243. query_inject_account += """
  244. ),
  245. initial_sum_amounts AS ( """ + subquery_sum_amounts + """ ),
  246. final_sum_amounts AS ( """ + subquery_sum_amounts + """ )
  247. INSERT INTO
  248. report_general_ledger_qweb_account
  249. (
  250. report_id,
  251. create_uid,
  252. create_date,
  253. account_id,
  254. code,
  255. name,
  256. initial_debit,
  257. initial_credit,
  258. initial_balance,
  259. final_debit,
  260. final_credit,
  261. final_balance,
  262. is_partner_account
  263. )
  264. SELECT
  265. %s AS report_id,
  266. %s AS create_uid,
  267. NOW() AS create_date,
  268. a.id AS account_id,
  269. a.code,
  270. a.name,
  271. COALESCE(i.debit, 0.0) AS initial_debit,
  272. COALESCE(i.credit, 0.0) AS initial_credit,
  273. COALESCE(i.balance, 0.0) AS initial_balance,
  274. COALESCE(f.debit, 0.0) AS final_debit,
  275. COALESCE(f.credit, 0.0) AS final_credit,
  276. COALESCE(f.balance, 0.0) AS final_balance,
  277. a.is_partner_account
  278. FROM
  279. accounts a
  280. LEFT JOIN
  281. initial_sum_amounts i ON a.id = i.account_id
  282. LEFT JOIN
  283. final_sum_amounts f ON a.id = f.account_id
  284. WHERE
  285. (
  286. i.debit IS NOT NULL AND i.debit != 0
  287. OR i.credit IS NOT NULL AND i.credit != 0
  288. OR i.balance IS NOT NULL AND i.balance != 0
  289. OR f.debit IS NOT NULL AND f.debit != 0
  290. OR f.credit IS NOT NULL AND f.credit != 0
  291. OR f.balance IS NOT NULL AND f.balance != 0
  292. )
  293. """
  294. if self.hide_account_balance_at_0:
  295. query_inject_account += """
  296. AND
  297. f.balance IS NOT NULL AND f.balance != 0
  298. """
  299. query_inject_account_params = (
  300. self.company_id.id,
  301. )
  302. if self.filter_account_ids:
  303. query_inject_account_params += (
  304. tuple(self.filter_account_ids.ids),
  305. )
  306. if self.filter_partner_ids:
  307. query_inject_account_params += (
  308. tuple(self.filter_partner_ids.ids),
  309. )
  310. query_inject_account_params += (
  311. self.date_from,
  312. self.fy_start_date,
  313. self.date_to,
  314. self.fy_start_date,
  315. self.id,
  316. self.env.uid,
  317. )
  318. self.env.cr.execute(query_inject_account, query_inject_account_params)
  319. def _inject_partner_values(self):
  320. """ Inject report values for report_general_ledger_qweb_partner.
  321. Only for "partner" accounts (payable and receivable).
  322. """
  323. subquery_sum_amounts = """
  324. SELECT
  325. ap.account_id AS account_id,
  326. ap.partner_id AS partner_id,
  327. SUM(ml.debit) AS debit,
  328. SUM(ml.credit) AS credit,
  329. SUM(ml.balance) AS balance
  330. FROM
  331. accounts_partners ap
  332. INNER JOIN
  333. account_move_line ml
  334. ON ap.account_id = ml.account_id
  335. AND (
  336. ap.partner_id = ml.partner_id
  337. OR ap.partner_id IS NULL AND ml.partner_id IS NULL
  338. )
  339. AND ml.date <= %s
  340. AND (
  341. ap.include_initial_balance != TRUE AND ml.date >= %s
  342. OR ap.include_initial_balance = TRUE
  343. )
  344. """
  345. if self.only_posted_moves:
  346. subquery_sum_amounts += """
  347. INNER JOIN
  348. account_move m ON ml.move_id = m.id AND m.state = 'posted'
  349. """
  350. subquery_sum_amounts += """
  351. GROUP BY
  352. ap.account_id, ap.partner_id
  353. """
  354. query_inject_partner = """
  355. WITH
  356. accounts_partners AS
  357. (
  358. SELECT
  359. ra.id AS report_account_id,
  360. a.id AS account_id,
  361. at.include_initial_balance AS include_initial_balance,
  362. p.id AS partner_id,
  363. COALESCE(
  364. CASE
  365. WHEN
  366. NULLIF(p.name, '') IS NOT NULL
  367. AND NULLIF(p.ref, '') IS NOT NULL
  368. THEN p.name || ' (' || p.ref || ')'
  369. ELSE p.name
  370. END,
  371. 'No partner allocated'
  372. ) AS partner_name
  373. FROM
  374. report_general_ledger_qweb_account ra
  375. INNER JOIN
  376. account_account a ON ra.account_id = a.id
  377. INNER JOIN
  378. account_account_type at ON a.user_type_id = at.id
  379. INNER JOIN
  380. account_move_line ml ON a.id = ml.account_id
  381. LEFT JOIN
  382. res_partner p ON ml.partner_id = p.id
  383. WHERE
  384. ra.report_id = %s
  385. AND
  386. ra.is_partner_account = TRUE
  387. """
  388. if self.centralize:
  389. query_inject_partner += """
  390. AND
  391. (a.centralized IS NULL OR a.centralized != TRUE)
  392. """
  393. if self.filter_partner_ids:
  394. query_inject_partner += """
  395. AND
  396. p.id IN %s
  397. """
  398. query_inject_partner += """
  399. GROUP BY
  400. ra.id,
  401. a.id,
  402. p.id,
  403. at.include_initial_balance
  404. ),
  405. initial_sum_amounts AS ( """ + subquery_sum_amounts + """ ),
  406. final_sum_amounts AS ( """ + subquery_sum_amounts + """ )
  407. INSERT INTO
  408. report_general_ledger_qweb_partner
  409. (
  410. report_account_id,
  411. create_uid,
  412. create_date,
  413. partner_id,
  414. name,
  415. initial_debit,
  416. initial_credit,
  417. initial_balance,
  418. final_debit,
  419. final_credit,
  420. final_balance
  421. )
  422. SELECT
  423. ap.report_account_id,
  424. %s AS create_uid,
  425. NOW() AS create_date,
  426. ap.partner_id,
  427. ap.partner_name,
  428. COALESCE(i.debit, 0.0) AS initial_debit,
  429. COALESCE(i.credit, 0.0) AS initial_credit,
  430. COALESCE(i.balance, 0.0) AS initial_balance,
  431. COALESCE(f.debit, 0.0) AS final_debit,
  432. COALESCE(f.credit, 0.0) AS final_credit,
  433. COALESCE(f.balance, 0.0) AS final_balance
  434. FROM
  435. accounts_partners ap
  436. LEFT JOIN
  437. initial_sum_amounts i
  438. ON
  439. (
  440. ap.partner_id = i.partner_id
  441. OR ap.partner_id IS NULL AND i.partner_id IS NULL
  442. )
  443. AND ap.account_id = i.account_id
  444. LEFT JOIN
  445. final_sum_amounts f
  446. ON
  447. (
  448. ap.partner_id = f.partner_id
  449. OR ap.partner_id IS NULL AND f.partner_id IS NULL
  450. )
  451. AND ap.account_id = f.account_id
  452. WHERE
  453. (
  454. i.debit IS NOT NULL AND i.debit != 0
  455. OR i.credit IS NOT NULL AND i.credit != 0
  456. OR i.balance IS NOT NULL AND i.balance != 0
  457. OR f.debit IS NOT NULL AND f.debit != 0
  458. OR f.credit IS NOT NULL AND f.credit != 0
  459. OR f.balance IS NOT NULL AND f.balance != 0
  460. )
  461. """
  462. if self.hide_account_balance_at_0:
  463. query_inject_partner += """
  464. AND
  465. f.balance IS NOT NULL AND f.balance != 0
  466. """
  467. query_inject_partner_params = (
  468. self.id,
  469. )
  470. if self.filter_partner_ids:
  471. query_inject_partner_params += (
  472. tuple(self.filter_partner_ids.ids),
  473. )
  474. query_inject_partner_params += (
  475. self.date_from,
  476. self.fy_start_date,
  477. self.date_to,
  478. self.fy_start_date,
  479. self.env.uid,
  480. )
  481. print query_inject_partner_params
  482. self.env.cr.execute(query_inject_partner, query_inject_partner_params)
  483. def _inject_line_not_centralized_values(self,
  484. is_account_line=True,
  485. is_partner_line=False,
  486. only_empty_partner_line=False):
  487. """ Inject report values for report_general_ledger_qweb_move_line.
  488. If centralized option have been chosen,
  489. only non centralized accounts are computed.
  490. In function of `is_account_line` and `is_partner_line` values,
  491. the move_line link is made either with account or either with partner.
  492. The "only_empty_partner_line" value is used
  493. to compute data without partner.
  494. """
  495. query_inject_move_line = """
  496. INSERT INTO
  497. report_general_ledger_qweb_move_line
  498. (
  499. """
  500. if is_account_line:
  501. query_inject_move_line += """
  502. report_account_id,
  503. """
  504. elif is_partner_line:
  505. query_inject_move_line += """
  506. report_partner_id,
  507. """
  508. query_inject_move_line += """
  509. create_uid,
  510. create_date,
  511. move_line_id,
  512. date,
  513. entry,
  514. journal,
  515. account,
  516. partner,
  517. label,
  518. cost_center,
  519. matching_number,
  520. debit,
  521. credit,
  522. cumul_balance,
  523. currency_name,
  524. amount_currency
  525. )
  526. SELECT
  527. """
  528. if is_account_line:
  529. query_inject_move_line += """
  530. ra.id AS report_account_id,
  531. """
  532. elif is_partner_line:
  533. query_inject_move_line += """
  534. rp.id AS report_partner_id,
  535. """
  536. query_inject_move_line += """
  537. %s AS create_uid,
  538. NOW() AS create_date,
  539. ml.id AS move_line_id,
  540. ml.date,
  541. m.name AS entry,
  542. j.code AS journal,
  543. a.code AS account,
  544. """
  545. if not only_empty_partner_line:
  546. query_inject_move_line += """
  547. CASE
  548. WHEN
  549. NULLIF(p.name, '') IS NOT NULL
  550. AND NULLIF(p.ref, '') IS NOT NULL
  551. THEN p.name || ' (' || p.ref || ')'
  552. ELSE p.name
  553. END AS partner,
  554. """
  555. elif only_empty_partner_line:
  556. query_inject_move_line += """
  557. 'No partner allocated' AS partner,
  558. """
  559. query_inject_move_line += """
  560. CONCAT_WS(' - ', NULLIF(ml.ref, ''), NULLIF(ml.name, '')) AS label,
  561. aa.name AS cost_center,
  562. fr.name AS matching_number,
  563. ml.debit,
  564. ml.credit,
  565. """
  566. if is_account_line:
  567. query_inject_move_line += """
  568. ra.initial_balance + (
  569. SUM(ml.balance)
  570. OVER (PARTITION BY a.code
  571. ORDER BY a.code, ml.date, ml.id)
  572. ) AS cumul_balance,
  573. """
  574. elif is_partner_line and not only_empty_partner_line:
  575. query_inject_move_line += """
  576. rp.initial_balance + (
  577. SUM(ml.balance)
  578. OVER (PARTITION BY a.code, p.name
  579. ORDER BY a.code, p.name, ml.date, ml.id)
  580. ) AS cumul_balance,
  581. """
  582. elif is_partner_line and only_empty_partner_line:
  583. query_inject_move_line += """
  584. rp.initial_balance + (
  585. SUM(ml.balance)
  586. OVER (PARTITION BY a.code
  587. ORDER BY a.code, ml.date, ml.id)
  588. ) AS cumul_balance,
  589. """
  590. query_inject_move_line += """
  591. c.name AS currency_name,
  592. ml.amount_currency
  593. FROM
  594. """
  595. if is_account_line:
  596. query_inject_move_line += """
  597. report_general_ledger_qweb_account ra
  598. """
  599. elif is_partner_line:
  600. query_inject_move_line += """
  601. report_general_ledger_qweb_partner rp
  602. INNER JOIN
  603. report_general_ledger_qweb_account ra ON rp.report_account_id = ra.id
  604. """
  605. query_inject_move_line += """
  606. INNER JOIN
  607. account_move_line ml ON ra.account_id = ml.account_id
  608. INNER JOIN
  609. account_move m ON ml.move_id = m.id
  610. INNER JOIN
  611. account_journal j ON ml.journal_id = j.id
  612. INNER JOIN
  613. account_account a ON ml.account_id = a.id
  614. """
  615. if is_account_line:
  616. query_inject_move_line += """
  617. LEFT JOIN
  618. res_partner p ON ml.partner_id = p.id
  619. """
  620. elif is_partner_line and not only_empty_partner_line:
  621. query_inject_move_line += """
  622. INNER JOIN
  623. res_partner p
  624. ON ml.partner_id = p.id AND rp.partner_id = p.id
  625. """
  626. query_inject_move_line += """
  627. LEFT JOIN
  628. account_full_reconcile fr ON ml.full_reconcile_id = fr.id
  629. LEFT JOIN
  630. res_currency c ON a.currency_id = c.id
  631. LEFT JOIN
  632. account_analytic_account aa ON ml.analytic_account_id = aa.id
  633. WHERE
  634. ra.report_id = %s
  635. AND
  636. """
  637. if is_account_line:
  638. query_inject_move_line += """
  639. (ra.is_partner_account IS NULL OR ra.is_partner_account != TRUE)
  640. """
  641. elif is_partner_line:
  642. query_inject_move_line += """
  643. ra.is_partner_account = TRUE
  644. """
  645. if self.centralize:
  646. query_inject_move_line += """
  647. AND
  648. (a.centralized IS NULL OR a.centralized != TRUE)
  649. """
  650. query_inject_move_line += """
  651. AND
  652. ml.date BETWEEN %s AND %s
  653. """
  654. if self.only_posted_moves:
  655. query_inject_move_line += """
  656. AND
  657. m.state = 'posted'
  658. """
  659. if only_empty_partner_line:
  660. query_inject_move_line += """
  661. AND
  662. ml.partner_id IS NULL
  663. AND
  664. rp.partner_id IS NULL
  665. """
  666. if is_account_line:
  667. query_inject_move_line += """
  668. ORDER BY
  669. a.code, ml.date, ml.id
  670. """
  671. elif is_partner_line and not only_empty_partner_line:
  672. query_inject_move_line += """
  673. ORDER BY
  674. a.code, p.name, ml.date, ml.id
  675. """
  676. elif is_partner_line and only_empty_partner_line:
  677. query_inject_move_line += """
  678. ORDER BY
  679. a.code, ml.date, ml.id
  680. """
  681. self.env.cr.execute(
  682. query_inject_move_line,
  683. (self.env.uid,
  684. self.id,
  685. self.date_from,
  686. self.date_to,)
  687. )
  688. def _inject_line_centralized_values(self):
  689. """ Inject report values for report_general_ledger_qweb_move_line.
  690. Only centralized accounts are computed.
  691. """
  692. query_inject_move_line_centralized = """
  693. WITH
  694. move_lines AS
  695. (
  696. SELECT
  697. ml.account_id,
  698. (
  699. DATE_TRUNC('month', ml.date) + interval '1 month'
  700. - interval '1 day'
  701. )::date AS date,
  702. SUM(ml.debit) AS debit,
  703. SUM(ml.credit) AS credit,
  704. SUM(ml.balance) AS balance
  705. FROM
  706. report_general_ledger_qweb_account ra
  707. INNER JOIN
  708. account_move_line ml ON ra.account_id = ml.account_id
  709. INNER JOIN
  710. account_move m ON ml.move_id = m.id
  711. INNER JOIN
  712. account_account a ON ml.account_id = a.id
  713. WHERE
  714. ra.report_id = %s
  715. AND
  716. a.centralized = TRUE
  717. AND
  718. ml.date BETWEEN %s AND %s
  719. """
  720. if self.only_posted_moves:
  721. query_inject_move_line_centralized += """
  722. AND
  723. m.state = 'posted'
  724. """
  725. query_inject_move_line_centralized += """
  726. GROUP BY
  727. ra.id, ml.account_id, a.code, 2
  728. )
  729. INSERT INTO
  730. report_general_ledger_qweb_move_line
  731. (
  732. report_account_id,
  733. create_uid,
  734. create_date,
  735. date,
  736. account,
  737. label,
  738. debit,
  739. credit,
  740. cumul_balance
  741. )
  742. SELECT
  743. ra.id AS report_account_id,
  744. %s AS create_uid,
  745. NOW() AS create_date,
  746. ml.date,
  747. a.code AS account,
  748. 'Centralized Entries' AS label,
  749. ml.debit AS debit,
  750. ml.credit AS credit,
  751. ra.initial_balance + (
  752. SUM(ml.balance)
  753. OVER (PARTITION BY a.code ORDER BY ml.date)
  754. ) AS cumul_balance
  755. FROM
  756. report_general_ledger_qweb_account ra
  757. INNER JOIN
  758. move_lines ml ON ra.account_id = ml.account_id
  759. INNER JOIN
  760. account_account a ON ml.account_id = a.id
  761. LEFT JOIN
  762. res_currency c ON a.currency_id = c.id
  763. WHERE
  764. ra.report_id = %s
  765. AND
  766. (a.centralized IS NOT NULL AND a.centralized = TRUE)
  767. ORDER BY
  768. a.code, ml.date
  769. """
  770. self.env.cr.execute(
  771. query_inject_move_line_centralized,
  772. (self.id,
  773. self.date_from,
  774. self.date_to,
  775. self.env.uid,
  776. self.id,)
  777. )
  778. def _compute_has_second_currency(self):
  779. """ Compute "has_second_currency" flag which will used for display."""
  780. query_update_has_second_currency = """
  781. UPDATE
  782. report_general_ledger_qweb
  783. SET
  784. has_second_currency =
  785. (
  786. SELECT
  787. TRUE
  788. FROM
  789. report_general_ledger_qweb_move_line l
  790. INNER JOIN
  791. report_general_ledger_qweb_account a
  792. ON l.report_account_id = a.id
  793. WHERE
  794. a.report_id = %s
  795. AND l.currency_name IS NOT NULL
  796. LIMIT 1
  797. )
  798. OR
  799. (
  800. SELECT
  801. TRUE
  802. FROM
  803. report_general_ledger_qweb_move_line l
  804. INNER JOIN
  805. report_general_ledger_qweb_partner p
  806. ON l.report_partner_id = p.id
  807. INNER JOIN
  808. report_general_ledger_qweb_account a
  809. ON p.report_account_id = a.id
  810. WHERE
  811. a.report_id = %s
  812. AND l.currency_name IS NOT NULL
  813. LIMIT 1
  814. )
  815. WHERE id = %s
  816. """
  817. params = (self.id,) * 3
  818. self.env.cr.execute(query_update_has_second_currency, params)