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.

981 lines
30 KiB

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