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.

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