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.

1358 lines
42 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 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. # Compute of unaffected earnings account
  46. @api.depends('company_id')
  47. def _compute_unaffected_earnings_account(self):
  48. account_type = self.env.ref('account.data_unaffected_earnings')
  49. self.unaffected_earnings_account = self.env['account.account'].search(
  50. [
  51. ('user_type_id', '=', account_type.id),
  52. ('company_id', '=', self.company_id.id)
  53. ])
  54. unaffected_earnings_account = fields.Many2one(
  55. comodel_name='account.account',
  56. compute='_compute_unaffected_earnings_account',
  57. store=True
  58. )
  59. class GeneralLedgerReportAccount(models.TransientModel):
  60. _name = 'report_general_ledger_qweb_account'
  61. _order = 'code ASC'
  62. report_id = fields.Many2one(
  63. comodel_name='report_general_ledger_qweb',
  64. ondelete='cascade',
  65. index=True
  66. )
  67. # Data fields, used to keep link with real object
  68. account_id = fields.Many2one(
  69. 'account.account',
  70. index=True
  71. )
  72. # Data fields, used for report display
  73. code = fields.Char()
  74. name = fields.Char()
  75. initial_debit = fields.Float(digits=(16, 2))
  76. initial_credit = fields.Float(digits=(16, 2))
  77. initial_balance = fields.Float(digits=(16, 2))
  78. final_debit = fields.Float(digits=(16, 2))
  79. final_credit = fields.Float(digits=(16, 2))
  80. final_balance = fields.Float(digits=(16, 2))
  81. # Flag fields, used for report display and for data computation
  82. is_partner_account = fields.Boolean()
  83. # Data fields, used to browse report data
  84. move_line_ids = fields.One2many(
  85. comodel_name='report_general_ledger_qweb_move_line',
  86. inverse_name='report_account_id'
  87. )
  88. partner_ids = fields.One2many(
  89. comodel_name='report_general_ledger_qweb_partner',
  90. inverse_name='report_account_id'
  91. )
  92. class GeneralLedgerReportPartner(models.TransientModel):
  93. _name = 'report_general_ledger_qweb_partner'
  94. report_account_id = fields.Many2one(
  95. comodel_name='report_general_ledger_qweb_account',
  96. ondelete='cascade',
  97. index=True
  98. )
  99. # Data fields, used to keep link with real object
  100. partner_id = fields.Many2one(
  101. 'res.partner',
  102. index=True
  103. )
  104. # Data fields, used for report display
  105. name = fields.Char()
  106. initial_debit = fields.Float(digits=(16, 2))
  107. initial_credit = fields.Float(digits=(16, 2))
  108. initial_balance = fields.Float(digits=(16, 2))
  109. final_debit = fields.Float(digits=(16, 2))
  110. final_credit = fields.Float(digits=(16, 2))
  111. final_balance = fields.Float(digits=(16, 2))
  112. # Data fields, used to browse report data
  113. move_line_ids = fields.One2many(
  114. comodel_name='report_general_ledger_qweb_move_line',
  115. inverse_name='report_partner_id'
  116. )
  117. @api.model
  118. def _generate_order_by(self, order_spec, query):
  119. """Custom order to display "No partner allocated" at last position."""
  120. return """
  121. ORDER BY
  122. CASE
  123. WHEN "report_general_ledger_qweb_partner"."partner_id" IS NOT NULL
  124. THEN 0
  125. ELSE 1
  126. END,
  127. "report_general_ledger_qweb_partner"."name"
  128. """
  129. class GeneralLedgerReportMoveLine(models.TransientModel):
  130. _name = 'report_general_ledger_qweb_move_line'
  131. report_account_id = fields.Many2one(
  132. comodel_name='report_general_ledger_qweb_account',
  133. ondelete='cascade',
  134. index=True
  135. )
  136. report_partner_id = fields.Many2one(
  137. comodel_name='report_general_ledger_qweb_partner',
  138. ondelete='cascade',
  139. index=True
  140. )
  141. # Data fields, used to keep link with real object
  142. move_line_id = fields.Many2one('account.move.line')
  143. # Data fields, used for report display
  144. date = fields.Date()
  145. entry = fields.Char()
  146. journal = fields.Char()
  147. account = fields.Char()
  148. partner = fields.Char()
  149. label = fields.Char()
  150. cost_center = fields.Char()
  151. matching_number = fields.Char()
  152. debit = fields.Float(digits=(16, 2))
  153. credit = fields.Float(digits=(16, 2))
  154. cumul_balance = fields.Float(digits=(16, 2))
  155. currency_name = fields.Char()
  156. amount_currency = fields.Float(digits=(16, 2))
  157. class GeneralLedgerReportCompute(models.TransientModel):
  158. """ Here, we just define methods.
  159. For class fields, go more top at this file.
  160. """
  161. _inherit = 'report_general_ledger_qweb'
  162. @api.multi
  163. def print_report(self, xlsx_report=False):
  164. self.ensure_one()
  165. self.compute_data_for_report()
  166. if xlsx_report:
  167. report_name = 'account_financial_report_qweb.' \
  168. 'report_general_ledger_xlsx'
  169. else:
  170. report_name = 'account_financial_report_qweb.' \
  171. 'report_general_ledger_qweb'
  172. return self.env['report'].get_action(records=self,
  173. report_name=report_name)
  174. @api.multi
  175. def compute_data_for_report(self,
  176. with_line_details=True,
  177. with_partners=True
  178. ):
  179. self.ensure_one()
  180. # Compute report data
  181. self._inject_account_values()
  182. if with_partners:
  183. self._inject_partner_values()
  184. if not self.filter_partner_ids:
  185. self._inject_partner_values(only_empty_partner=True)
  186. # Add unaffected earnings account
  187. if (not self.filter_account_ids or
  188. self.unaffected_earnings_account.id in
  189. self.filter_account_ids.ids):
  190. self._inject_unaffected_earnings_account_values()
  191. # Call this function even if we don't want line details because,
  192. # we need to compute
  193. # at least the values for unaffected earnings account
  194. # In this case, only unaffected earnings account values are computed
  195. only_unaffected_earnings_account = not with_line_details
  196. self._inject_line_not_centralized_values(
  197. only_unaffected_earnings_account=only_unaffected_earnings_account
  198. )
  199. if with_line_details:
  200. self._inject_line_not_centralized_values(
  201. is_account_line=False,
  202. is_partner_line=True)
  203. self._inject_line_not_centralized_values(
  204. is_account_line=False,
  205. is_partner_line=True,
  206. only_empty_partner_line=True)
  207. if self.centralize:
  208. self._inject_line_centralized_values()
  209. # Complete unaffected earnings account
  210. if (not self.filter_account_ids or
  211. self.unaffected_earnings_account.id in
  212. self.filter_account_ids.ids):
  213. self._complete_unaffected_earnings_account_values()
  214. if with_line_details:
  215. # Compute display flag
  216. self._compute_has_second_currency()
  217. # Refresh cache because all data are computed with SQL requests
  218. self.refresh()
  219. def _get_account_sub_subquery_sum_amounts(self, include_initial_balance):
  220. """ Return subquery used to compute sum amounts on accounts """
  221. sub_subquery_sum_amounts = """
  222. SELECT
  223. a.id AS account_id,
  224. SUM(ml.debit) AS debit,
  225. SUM(ml.credit) AS credit,
  226. SUM(ml.balance) AS balance
  227. FROM
  228. accounts a
  229. INNER JOIN
  230. account_account_type at ON a.user_type_id = at.id
  231. INNER JOIN
  232. account_move_line ml
  233. ON a.id = ml.account_id
  234. AND ml.date <= %s
  235. """
  236. if not include_initial_balance:
  237. sub_subquery_sum_amounts += """
  238. AND at.include_initial_balance != TRUE AND ml.date >= %s
  239. """
  240. else:
  241. sub_subquery_sum_amounts += """
  242. AND at.include_initial_balance = TRUE
  243. """
  244. if self.only_posted_moves:
  245. sub_subquery_sum_amounts += """
  246. INNER JOIN
  247. account_move m ON ml.move_id = m.id AND m.state = 'posted'
  248. """
  249. if self.filter_cost_center_ids:
  250. sub_subquery_sum_amounts += """
  251. INNER JOIN
  252. account_analytic_account aa
  253. ON
  254. ml.analytic_account_id = aa.id
  255. AND aa.id IN %s
  256. """
  257. sub_subquery_sum_amounts += """
  258. GROUP BY
  259. a.id
  260. """
  261. return sub_subquery_sum_amounts
  262. def _inject_account_values(self):
  263. """Inject report values for report_general_ledger_qweb_account."""
  264. subquery_sum_amounts = """
  265. SELECT
  266. sub.account_id AS account_id,
  267. SUM(COALESCE(sub.debit, 0.0)) AS debit,
  268. SUM(COALESCE(sub.credit, 0.0)) AS credit,
  269. SUM(COALESCE(sub.balance, 0.0)) AS balance
  270. FROM
  271. (
  272. """
  273. subquery_sum_amounts += self._get_account_sub_subquery_sum_amounts(
  274. include_initial_balance=False
  275. )
  276. subquery_sum_amounts += """
  277. UNION
  278. """
  279. subquery_sum_amounts += self._get_account_sub_subquery_sum_amounts(
  280. include_initial_balance=True
  281. )
  282. subquery_sum_amounts += """
  283. ) sub
  284. GROUP BY
  285. sub.account_id
  286. """
  287. query_inject_account = """
  288. WITH
  289. accounts AS
  290. (
  291. SELECT
  292. a.id,
  293. a.code,
  294. a.name,
  295. a.internal_type IN ('payable', 'receivable')
  296. AS is_partner_account,
  297. a.user_type_id
  298. FROM
  299. account_account a
  300. """
  301. if self.filter_partner_ids or self.filter_cost_center_ids:
  302. query_inject_account += """
  303. INNER JOIN
  304. account_move_line ml ON a.id = ml.account_id
  305. """
  306. if self.filter_partner_ids:
  307. query_inject_account += """
  308. INNER JOIN
  309. res_partner p ON ml.partner_id = p.id
  310. """
  311. if self.filter_cost_center_ids:
  312. query_inject_account += """
  313. INNER JOIN
  314. account_analytic_account aa
  315. ON
  316. ml.analytic_account_id = aa.id
  317. AND aa.id IN %s
  318. """
  319. query_inject_account += """
  320. WHERE
  321. a.company_id = %s
  322. AND a.id != %s
  323. """
  324. if self.filter_account_ids:
  325. query_inject_account += """
  326. AND
  327. a.id IN %s
  328. """
  329. if self.filter_partner_ids:
  330. query_inject_account += """
  331. AND
  332. p.id IN %s
  333. """
  334. if self.filter_partner_ids or self.filter_cost_center_ids:
  335. query_inject_account += """
  336. GROUP BY
  337. a.id
  338. """
  339. query_inject_account += """
  340. ),
  341. initial_sum_amounts AS ( """ + subquery_sum_amounts + """ ),
  342. final_sum_amounts AS ( """ + subquery_sum_amounts + """ )
  343. INSERT INTO
  344. report_general_ledger_qweb_account
  345. (
  346. report_id,
  347. create_uid,
  348. create_date,
  349. account_id,
  350. code,
  351. name,
  352. initial_debit,
  353. initial_credit,
  354. initial_balance,
  355. final_debit,
  356. final_credit,
  357. final_balance,
  358. is_partner_account
  359. )
  360. SELECT
  361. %s AS report_id,
  362. %s AS create_uid,
  363. NOW() AS create_date,
  364. a.id AS account_id,
  365. a.code,
  366. a.name,
  367. COALESCE(i.debit, 0.0) AS initial_debit,
  368. COALESCE(i.credit, 0.0) AS initial_credit,
  369. COALESCE(i.balance, 0.0) AS initial_balance,
  370. COALESCE(f.debit, 0.0) AS final_debit,
  371. COALESCE(f.credit, 0.0) AS final_credit,
  372. COALESCE(f.balance, 0.0) AS final_balance,
  373. a.is_partner_account
  374. FROM
  375. accounts a
  376. LEFT JOIN
  377. initial_sum_amounts i ON a.id = i.account_id
  378. LEFT JOIN
  379. final_sum_amounts f ON a.id = f.account_id
  380. WHERE
  381. (
  382. i.debit IS NOT NULL AND i.debit != 0
  383. OR i.credit IS NOT NULL AND i.credit != 0
  384. OR i.balance IS NOT NULL AND i.balance != 0
  385. OR f.debit IS NOT NULL AND f.debit != 0
  386. OR f.credit IS NOT NULL AND f.credit != 0
  387. OR f.balance IS NOT NULL AND f.balance != 0
  388. )
  389. """
  390. if self.hide_account_balance_at_0:
  391. query_inject_account += """
  392. AND
  393. f.balance IS NOT NULL AND f.balance != 0
  394. """
  395. query_inject_account_params = ()
  396. if self.filter_cost_center_ids:
  397. query_inject_account_params += (
  398. tuple(self.filter_cost_center_ids.ids),
  399. )
  400. query_inject_account_params += (
  401. self.company_id.id,
  402. self.unaffected_earnings_account.id,
  403. )
  404. if self.filter_account_ids:
  405. query_inject_account_params += (
  406. tuple(self.filter_account_ids.ids),
  407. )
  408. if self.filter_partner_ids:
  409. query_inject_account_params += (
  410. tuple(self.filter_partner_ids.ids),
  411. )
  412. query_inject_account_params += (
  413. self.date_from,
  414. self.fy_start_date,
  415. )
  416. if self.filter_cost_center_ids:
  417. query_inject_account_params += (
  418. tuple(self.filter_cost_center_ids.ids),
  419. )
  420. query_inject_account_params += (
  421. self.date_from,
  422. )
  423. if self.filter_cost_center_ids:
  424. query_inject_account_params += (
  425. tuple(self.filter_cost_center_ids.ids),
  426. )
  427. query_inject_account_params += (
  428. self.date_to,
  429. self.fy_start_date,
  430. )
  431. if self.filter_cost_center_ids:
  432. query_inject_account_params += (
  433. tuple(self.filter_cost_center_ids.ids),
  434. )
  435. query_inject_account_params += (
  436. self.date_to,
  437. )
  438. if self.filter_cost_center_ids:
  439. query_inject_account_params += (
  440. tuple(self.filter_cost_center_ids.ids),
  441. )
  442. query_inject_account_params += (
  443. self.id,
  444. self.env.uid,
  445. )
  446. self.env.cr.execute(query_inject_account, query_inject_account_params)
  447. def _get_partner_sub_subquery_sum_amounts(
  448. self, only_empty_partner, include_initial_balance
  449. ):
  450. """ Return subquery used to compute sum amounts on partners """
  451. sub_subquery_sum_amounts = """
  452. SELECT
  453. ap.account_id AS account_id,
  454. ap.partner_id AS partner_id,
  455. SUM(ml.debit) AS debit,
  456. SUM(ml.credit) AS credit,
  457. SUM(ml.balance) AS balance
  458. FROM
  459. accounts_partners ap
  460. INNER JOIN
  461. account_move_line ml
  462. ON ap.account_id = ml.account_id
  463. AND ml.date <= %s
  464. """
  465. if not only_empty_partner:
  466. sub_subquery_sum_amounts += """
  467. AND ap.partner_id = ml.partner_id
  468. """
  469. else:
  470. sub_subquery_sum_amounts += """
  471. AND ap.partner_id IS NULL AND ml.partner_id IS NULL
  472. """
  473. if not include_initial_balance:
  474. sub_subquery_sum_amounts += """
  475. AND ap.include_initial_balance != TRUE AND ml.date >= %s
  476. """
  477. else:
  478. sub_subquery_sum_amounts += """
  479. AND ap.include_initial_balance = TRUE
  480. """
  481. if self.only_posted_moves:
  482. sub_subquery_sum_amounts += """
  483. INNER JOIN
  484. account_move m ON ml.move_id = m.id AND m.state = 'posted'
  485. """
  486. if self.filter_cost_center_ids:
  487. sub_subquery_sum_amounts += """
  488. INNER JOIN
  489. account_analytic_account aa
  490. ON
  491. ml.analytic_account_id = aa.id
  492. AND aa.id IN %s
  493. """
  494. sub_subquery_sum_amounts += """
  495. GROUP BY
  496. ap.account_id, ap.partner_id
  497. """
  498. return sub_subquery_sum_amounts
  499. def _inject_partner_values(self, only_empty_partner=False):
  500. """ Inject report values for report_general_ledger_qweb_partner.
  501. Only for "partner" accounts (payable and receivable).
  502. """
  503. subquery_sum_amounts = """
  504. SELECT
  505. sub.account_id AS account_id,
  506. sub.partner_id AS partner_id,
  507. SUM(COALESCE(sub.debit, 0.0)) AS debit,
  508. SUM(COALESCE(sub.credit, 0.0)) AS credit,
  509. SUM(COALESCE(sub.balance, 0.0)) AS balance
  510. FROM
  511. (
  512. """
  513. subquery_sum_amounts += self._get_partner_sub_subquery_sum_amounts(
  514. only_empty_partner,
  515. include_initial_balance=False
  516. )
  517. subquery_sum_amounts += """
  518. UNION
  519. """
  520. subquery_sum_amounts += self._get_partner_sub_subquery_sum_amounts(
  521. only_empty_partner,
  522. include_initial_balance=True
  523. )
  524. subquery_sum_amounts += """
  525. ) sub
  526. GROUP BY
  527. sub.account_id, sub.partner_id
  528. """
  529. query_inject_partner = """
  530. WITH
  531. accounts_partners AS
  532. (
  533. SELECT
  534. ra.id AS report_account_id,
  535. a.id AS account_id,
  536. at.include_initial_balance AS include_initial_balance,
  537. p.id AS partner_id,
  538. COALESCE(
  539. CASE
  540. WHEN
  541. NULLIF(p.name, '') IS NOT NULL
  542. AND NULLIF(p.ref, '') IS NOT NULL
  543. THEN p.name || ' (' || p.ref || ')'
  544. ELSE p.name
  545. END,
  546. '""" + _('No partner allocated') + """'
  547. ) AS partner_name
  548. FROM
  549. report_general_ledger_qweb_account ra
  550. INNER JOIN
  551. account_account a ON ra.account_id = a.id
  552. INNER JOIN
  553. account_account_type at ON a.user_type_id = at.id
  554. INNER JOIN
  555. account_move_line ml ON a.id = ml.account_id
  556. LEFT JOIN
  557. res_partner p ON ml.partner_id = p.id
  558. """
  559. if self.filter_cost_center_ids:
  560. query_inject_partner += """
  561. INNER JOIN
  562. account_analytic_account aa
  563. ON
  564. ml.analytic_account_id = aa.id
  565. AND aa.id IN %s
  566. """
  567. query_inject_partner += """
  568. WHERE
  569. ra.report_id = %s
  570. AND
  571. ra.is_partner_account = TRUE
  572. """
  573. if not only_empty_partner:
  574. query_inject_partner += """
  575. AND
  576. p.id IS NOT NULL
  577. """
  578. else:
  579. query_inject_partner += """
  580. AND
  581. p.id IS NULL
  582. """
  583. query_inject_partner += """
  584. """
  585. if self.centralize:
  586. query_inject_partner += """
  587. AND (a.centralized IS NULL OR a.centralized != TRUE)
  588. """
  589. if self.filter_partner_ids:
  590. query_inject_partner += """
  591. AND
  592. p.id IN %s
  593. """
  594. query_inject_partner += """
  595. GROUP BY
  596. ra.id,
  597. a.id,
  598. p.id,
  599. at.include_initial_balance
  600. ),
  601. initial_sum_amounts AS ( """ + subquery_sum_amounts + """ ),
  602. final_sum_amounts AS ( """ + subquery_sum_amounts + """ )
  603. INSERT INTO
  604. report_general_ledger_qweb_partner
  605. (
  606. report_account_id,
  607. create_uid,
  608. create_date,
  609. partner_id,
  610. name,
  611. initial_debit,
  612. initial_credit,
  613. initial_balance,
  614. final_debit,
  615. final_credit,
  616. final_balance
  617. )
  618. SELECT
  619. ap.report_account_id,
  620. %s AS create_uid,
  621. NOW() AS create_date,
  622. ap.partner_id,
  623. ap.partner_name,
  624. COALESCE(i.debit, 0.0) AS initial_debit,
  625. COALESCE(i.credit, 0.0) AS initial_credit,
  626. COALESCE(i.balance, 0.0) AS initial_balance,
  627. COALESCE(f.debit, 0.0) AS final_debit,
  628. COALESCE(f.credit, 0.0) AS final_credit,
  629. COALESCE(f.balance, 0.0) AS final_balance
  630. FROM
  631. accounts_partners ap
  632. LEFT JOIN
  633. initial_sum_amounts i
  634. ON
  635. (
  636. """
  637. if not only_empty_partner:
  638. query_inject_partner += """
  639. ap.partner_id = i.partner_id
  640. """
  641. else:
  642. query_inject_partner += """
  643. ap.partner_id IS NULL AND i.partner_id IS NULL
  644. """
  645. query_inject_partner += """
  646. )
  647. AND ap.account_id = i.account_id
  648. LEFT JOIN
  649. final_sum_amounts f
  650. ON
  651. (
  652. """
  653. if not only_empty_partner:
  654. query_inject_partner += """
  655. ap.partner_id = f.partner_id
  656. """
  657. else:
  658. query_inject_partner += """
  659. ap.partner_id IS NULL AND f.partner_id IS NULL
  660. """
  661. query_inject_partner += """
  662. )
  663. AND ap.account_id = f.account_id
  664. WHERE
  665. (
  666. i.debit IS NOT NULL AND i.debit != 0
  667. OR i.credit IS NOT NULL AND i.credit != 0
  668. OR i.balance IS NOT NULL AND i.balance != 0
  669. OR f.debit IS NOT NULL AND f.debit != 0
  670. OR f.credit IS NOT NULL AND f.credit != 0
  671. OR f.balance IS NOT NULL AND f.balance != 0
  672. )
  673. """
  674. if self.hide_account_balance_at_0:
  675. query_inject_partner += """
  676. AND
  677. f.balance IS NOT NULL AND f.balance != 0
  678. """
  679. query_inject_partner_params = ()
  680. if self.filter_cost_center_ids:
  681. query_inject_partner_params += (
  682. tuple(self.filter_cost_center_ids.ids),
  683. )
  684. query_inject_partner_params += (
  685. self.id,
  686. )
  687. if self.filter_partner_ids:
  688. query_inject_partner_params += (
  689. tuple(self.filter_partner_ids.ids),
  690. )
  691. query_inject_partner_params += (
  692. self.date_from,
  693. self.fy_start_date,
  694. )
  695. if self.filter_cost_center_ids:
  696. query_inject_partner_params += (
  697. tuple(self.filter_cost_center_ids.ids),
  698. )
  699. query_inject_partner_params += (
  700. self.date_from,
  701. )
  702. if self.filter_cost_center_ids:
  703. query_inject_partner_params += (
  704. tuple(self.filter_cost_center_ids.ids),
  705. )
  706. query_inject_partner_params += (
  707. self.date_to,
  708. self.fy_start_date,
  709. )
  710. if self.filter_cost_center_ids:
  711. query_inject_partner_params += (
  712. tuple(self.filter_cost_center_ids.ids),
  713. )
  714. query_inject_partner_params += (
  715. self.date_to,
  716. )
  717. if self.filter_cost_center_ids:
  718. query_inject_partner_params += (
  719. tuple(self.filter_cost_center_ids.ids),
  720. )
  721. query_inject_partner_params += (
  722. self.env.uid,
  723. )
  724. self.env.cr.execute(query_inject_partner, query_inject_partner_params)
  725. def _inject_line_not_centralized_values(
  726. self,
  727. is_account_line=True,
  728. is_partner_line=False,
  729. only_empty_partner_line=False,
  730. only_unaffected_earnings_account=False):
  731. """ Inject report values for report_general_ledger_qweb_move_line.
  732. If centralized option have been chosen,
  733. only non centralized accounts are computed.
  734. In function of `is_account_line` and `is_partner_line` values,
  735. the move_line link is made either with account or either with partner.
  736. The "only_empty_partner_line" value is used
  737. to compute data without partner.
  738. """
  739. query_inject_move_line = """
  740. INSERT INTO
  741. report_general_ledger_qweb_move_line
  742. (
  743. """
  744. if is_account_line:
  745. query_inject_move_line += """
  746. report_account_id,
  747. """
  748. elif is_partner_line:
  749. query_inject_move_line += """
  750. report_partner_id,
  751. """
  752. query_inject_move_line += """
  753. create_uid,
  754. create_date,
  755. move_line_id,
  756. date,
  757. entry,
  758. journal,
  759. account,
  760. partner,
  761. label,
  762. cost_center,
  763. matching_number,
  764. debit,
  765. credit,
  766. cumul_balance,
  767. currency_name,
  768. amount_currency
  769. )
  770. SELECT
  771. """
  772. if is_account_line:
  773. query_inject_move_line += """
  774. ra.id AS report_account_id,
  775. """
  776. elif is_partner_line:
  777. query_inject_move_line += """
  778. rp.id AS report_partner_id,
  779. """
  780. query_inject_move_line += """
  781. %s AS create_uid,
  782. NOW() AS create_date,
  783. ml.id AS move_line_id,
  784. ml.date,
  785. m.name AS entry,
  786. j.code AS journal,
  787. a.code AS account,
  788. """
  789. if not only_empty_partner_line:
  790. query_inject_move_line += """
  791. CASE
  792. WHEN
  793. NULLIF(p.name, '') IS NOT NULL
  794. AND NULLIF(p.ref, '') IS NOT NULL
  795. THEN p.name || ' (' || p.ref || ')'
  796. ELSE p.name
  797. END AS partner,
  798. """
  799. elif only_empty_partner_line:
  800. query_inject_move_line += """
  801. '""" + _('No partner allocated') + """' AS partner,
  802. """
  803. query_inject_move_line += """
  804. CONCAT_WS(' - ', NULLIF(ml.ref, ''), NULLIF(ml.name, '')) AS label,
  805. aa.name AS cost_center,
  806. fr.name AS matching_number,
  807. ml.debit,
  808. ml.credit,
  809. """
  810. if is_account_line:
  811. query_inject_move_line += """
  812. ra.initial_balance + (
  813. SUM(ml.balance)
  814. OVER (PARTITION BY a.code
  815. ORDER BY a.code, ml.date, ml.id)
  816. ) AS cumul_balance,
  817. """
  818. elif is_partner_line and not only_empty_partner_line:
  819. query_inject_move_line += """
  820. rp.initial_balance + (
  821. SUM(ml.balance)
  822. OVER (PARTITION BY a.code, p.name
  823. ORDER BY a.code, p.name, ml.date, ml.id)
  824. ) AS cumul_balance,
  825. """
  826. elif is_partner_line and only_empty_partner_line:
  827. query_inject_move_line += """
  828. rp.initial_balance + (
  829. SUM(ml.balance)
  830. OVER (PARTITION BY a.code
  831. ORDER BY a.code, ml.date, ml.id)
  832. ) AS cumul_balance,
  833. """
  834. query_inject_move_line += """
  835. c.name AS currency_name,
  836. ml.amount_currency
  837. FROM
  838. """
  839. if is_account_line:
  840. query_inject_move_line += """
  841. report_general_ledger_qweb_account ra
  842. """
  843. elif is_partner_line:
  844. query_inject_move_line += """
  845. report_general_ledger_qweb_partner rp
  846. INNER JOIN
  847. report_general_ledger_qweb_account ra ON rp.report_account_id = ra.id
  848. """
  849. query_inject_move_line += """
  850. INNER JOIN
  851. account_move_line ml ON ra.account_id = ml.account_id
  852. INNER JOIN
  853. account_move m ON ml.move_id = m.id
  854. INNER JOIN
  855. account_journal j ON ml.journal_id = j.id
  856. INNER JOIN
  857. account_account a ON ml.account_id = a.id
  858. """
  859. if is_account_line:
  860. query_inject_move_line += """
  861. LEFT JOIN
  862. res_partner p ON ml.partner_id = p.id
  863. """
  864. elif is_partner_line and not only_empty_partner_line:
  865. query_inject_move_line += """
  866. INNER JOIN
  867. res_partner p
  868. ON ml.partner_id = p.id AND rp.partner_id = p.id
  869. """
  870. query_inject_move_line += """
  871. LEFT JOIN
  872. account_full_reconcile fr ON ml.full_reconcile_id = fr.id
  873. LEFT JOIN
  874. res_currency c ON a.currency_id = c.id
  875. """
  876. if self.filter_cost_center_ids:
  877. query_inject_move_line += """
  878. INNER JOIN
  879. account_analytic_account aa
  880. ON
  881. ml.analytic_account_id = aa.id
  882. AND aa.id IN %s
  883. """
  884. else:
  885. query_inject_move_line += """
  886. LEFT JOIN
  887. account_analytic_account aa ON ml.analytic_account_id = aa.id
  888. """
  889. query_inject_move_line += """
  890. WHERE
  891. ra.report_id = %s
  892. AND
  893. """
  894. if only_unaffected_earnings_account:
  895. query_inject_move_line += """
  896. a.id = %s
  897. AND
  898. """
  899. if is_account_line:
  900. query_inject_move_line += """
  901. (ra.is_partner_account IS NULL OR ra.is_partner_account != TRUE)
  902. """
  903. elif is_partner_line:
  904. query_inject_move_line += """
  905. ra.is_partner_account = TRUE
  906. """
  907. if self.centralize:
  908. query_inject_move_line += """
  909. AND
  910. (a.centralized IS NULL OR a.centralized != TRUE)
  911. """
  912. query_inject_move_line += """
  913. AND
  914. ml.date BETWEEN %s AND %s
  915. """
  916. if self.only_posted_moves:
  917. query_inject_move_line += """
  918. AND
  919. m.state = 'posted'
  920. """
  921. if only_empty_partner_line:
  922. query_inject_move_line += """
  923. AND
  924. ml.partner_id IS NULL
  925. AND
  926. rp.partner_id IS NULL
  927. """
  928. if is_account_line:
  929. query_inject_move_line += """
  930. ORDER BY
  931. a.code, ml.date, ml.id
  932. """
  933. elif is_partner_line and not only_empty_partner_line:
  934. query_inject_move_line += """
  935. ORDER BY
  936. a.code, p.name, ml.date, ml.id
  937. """
  938. elif is_partner_line and only_empty_partner_line:
  939. query_inject_move_line += """
  940. ORDER BY
  941. a.code, ml.date, ml.id
  942. """
  943. query_inject_move_line_params = (
  944. self.env.uid,
  945. )
  946. if self.filter_cost_center_ids:
  947. query_inject_move_line_params += (
  948. tuple(self.filter_cost_center_ids.ids),
  949. )
  950. query_inject_move_line_params += (
  951. self.id,
  952. )
  953. if only_unaffected_earnings_account:
  954. query_inject_move_line_params += (
  955. self.unaffected_earnings_account.id,
  956. )
  957. query_inject_move_line_params += (
  958. self.date_from,
  959. self.date_to,
  960. )
  961. self.env.cr.execute(
  962. query_inject_move_line,
  963. query_inject_move_line_params
  964. )
  965. def _inject_line_centralized_values(self):
  966. """ Inject report values for report_general_ledger_qweb_move_line.
  967. Only centralized accounts are computed.
  968. """
  969. query_inject_move_line_centralized = """
  970. WITH
  971. move_lines AS
  972. (
  973. SELECT
  974. ml.account_id,
  975. (
  976. DATE_TRUNC('month', ml.date) + interval '1 month'
  977. - interval '1 day'
  978. )::date AS date,
  979. SUM(ml.debit) AS debit,
  980. SUM(ml.credit) AS credit,
  981. SUM(ml.balance) AS balance
  982. FROM
  983. report_general_ledger_qweb_account ra
  984. INNER JOIN
  985. account_move_line ml ON ra.account_id = ml.account_id
  986. INNER JOIN
  987. account_move m ON ml.move_id = m.id
  988. INNER JOIN
  989. account_account a ON ml.account_id = a.id
  990. """
  991. if self.filter_cost_center_ids:
  992. query_inject_move_line_centralized += """
  993. INNER JOIN
  994. account_analytic_account aa
  995. ON
  996. ml.analytic_account_id = aa.id
  997. AND aa.id IN %s
  998. """
  999. query_inject_move_line_centralized += """
  1000. WHERE
  1001. ra.report_id = %s
  1002. AND
  1003. a.centralized = TRUE
  1004. AND
  1005. ml.date BETWEEN %s AND %s
  1006. """
  1007. if self.only_posted_moves:
  1008. query_inject_move_line_centralized += """
  1009. AND
  1010. m.state = 'posted'
  1011. """
  1012. query_inject_move_line_centralized += """
  1013. GROUP BY
  1014. ra.id, ml.account_id, a.code, 2
  1015. )
  1016. INSERT INTO
  1017. report_general_ledger_qweb_move_line
  1018. (
  1019. report_account_id,
  1020. create_uid,
  1021. create_date,
  1022. date,
  1023. account,
  1024. label,
  1025. debit,
  1026. credit,
  1027. cumul_balance
  1028. )
  1029. SELECT
  1030. ra.id AS report_account_id,
  1031. %s AS create_uid,
  1032. NOW() AS create_date,
  1033. ml.date,
  1034. a.code AS account,
  1035. '""" + _('Centralized Entries') + """' AS label,
  1036. ml.debit AS debit,
  1037. ml.credit AS credit,
  1038. ra.initial_balance + (
  1039. SUM(ml.balance)
  1040. OVER (PARTITION BY a.code ORDER BY ml.date)
  1041. ) AS cumul_balance
  1042. FROM
  1043. report_general_ledger_qweb_account ra
  1044. INNER JOIN
  1045. move_lines ml ON ra.account_id = ml.account_id
  1046. INNER JOIN
  1047. account_account a ON ml.account_id = a.id
  1048. LEFT JOIN
  1049. res_currency c ON a.currency_id = c.id
  1050. WHERE
  1051. ra.report_id = %s
  1052. AND
  1053. (a.centralized IS NOT NULL AND a.centralized = TRUE)
  1054. ORDER BY
  1055. a.code, ml.date
  1056. """
  1057. query_inject_move_line_centralized_params = ()
  1058. if self.filter_cost_center_ids:
  1059. query_inject_move_line_centralized_params += (
  1060. tuple(self.filter_cost_center_ids.ids),
  1061. )
  1062. query_inject_move_line_centralized_params += (
  1063. self.id,
  1064. self.date_from,
  1065. self.date_to,
  1066. self.env.uid,
  1067. self.id,
  1068. )
  1069. self.env.cr.execute(
  1070. query_inject_move_line_centralized,
  1071. query_inject_move_line_centralized_params
  1072. )
  1073. def _compute_has_second_currency(self):
  1074. """ Compute "has_second_currency" flag which will used for display."""
  1075. query_update_has_second_currency = """
  1076. UPDATE
  1077. report_general_ledger_qweb
  1078. SET
  1079. has_second_currency =
  1080. (
  1081. SELECT
  1082. TRUE
  1083. FROM
  1084. report_general_ledger_qweb_move_line l
  1085. INNER JOIN
  1086. report_general_ledger_qweb_account a
  1087. ON l.report_account_id = a.id
  1088. WHERE
  1089. a.report_id = %s
  1090. AND l.currency_name IS NOT NULL
  1091. LIMIT 1
  1092. )
  1093. OR
  1094. (
  1095. SELECT
  1096. TRUE
  1097. FROM
  1098. report_general_ledger_qweb_move_line l
  1099. INNER JOIN
  1100. report_general_ledger_qweb_partner p
  1101. ON l.report_partner_id = p.id
  1102. INNER JOIN
  1103. report_general_ledger_qweb_account a
  1104. ON p.report_account_id = a.id
  1105. WHERE
  1106. a.report_id = %s
  1107. AND l.currency_name IS NOT NULL
  1108. LIMIT 1
  1109. )
  1110. WHERE id = %s
  1111. """
  1112. params = (self.id,) * 3
  1113. self.env.cr.execute(query_update_has_second_currency, params)
  1114. def _get_unaffected_earnings_account_sub_subquery_sum_amounts(
  1115. self, include_initial_balance
  1116. ):
  1117. """ Return subquery used to compute sum amounts on
  1118. unaffected earnings accounts """
  1119. sub_subquery_sum_amounts = """
  1120. SELECT
  1121. SUM(ml.balance) AS balance
  1122. FROM
  1123. account_account a
  1124. INNER JOIN
  1125. account_account_type at ON a.user_type_id = at.id
  1126. INNER JOIN
  1127. account_move_line ml
  1128. ON a.id = ml.account_id
  1129. AND ml.date <= %s
  1130. """
  1131. if not include_initial_balance:
  1132. sub_subquery_sum_amounts += """
  1133. AND NOT(at.include_initial_balance != TRUE AND ml.date >= %s)
  1134. """
  1135. else:
  1136. sub_subquery_sum_amounts += """
  1137. AND at.include_initial_balance = FALSE
  1138. """
  1139. if self.only_posted_moves:
  1140. sub_subquery_sum_amounts += """
  1141. INNER JOIN
  1142. account_move m ON ml.move_id = m.id AND m.state = 'posted'
  1143. """
  1144. if self.filter_cost_center_ids:
  1145. sub_subquery_sum_amounts += """
  1146. INNER JOIN
  1147. account_analytic_account aa
  1148. ON
  1149. ml.analytic_account_id = aa.id
  1150. AND aa.id IN %s
  1151. """
  1152. sub_subquery_sum_amounts += """
  1153. WHERE
  1154. a.company_id =%s
  1155. AND a.id != %s
  1156. """
  1157. return sub_subquery_sum_amounts
  1158. def _inject_unaffected_earnings_account_values(self):
  1159. """Inject the report values of the unaffected earnings account
  1160. for report_general_ledger_qweb_account."""
  1161. subquery_sum_amounts = """
  1162. SELECT
  1163. SUM(COALESCE(sub.balance, 0.0)) AS balance
  1164. FROM
  1165. (
  1166. """
  1167. subquery_sum_amounts += \
  1168. self._get_unaffected_earnings_account_sub_subquery_sum_amounts(
  1169. include_initial_balance=False
  1170. )
  1171. subquery_sum_amounts += """
  1172. UNION
  1173. """
  1174. subquery_sum_amounts += \
  1175. self._get_unaffected_earnings_account_sub_subquery_sum_amounts(
  1176. include_initial_balance=True
  1177. )
  1178. subquery_sum_amounts += """
  1179. ) sub
  1180. """
  1181. query_inject_account = """
  1182. WITH
  1183. initial_sum_amounts AS ( """ + subquery_sum_amounts + """ )
  1184. INSERT INTO
  1185. report_general_ledger_qweb_account
  1186. (
  1187. report_id,
  1188. create_uid,
  1189. create_date,
  1190. account_id,
  1191. code,
  1192. name,
  1193. is_partner_account,
  1194. initial_balance
  1195. )
  1196. SELECT
  1197. %s AS report_id,
  1198. %s AS create_uid,
  1199. NOW() AS create_date,
  1200. a.id AS account_id,
  1201. a.code,
  1202. a.name,
  1203. False AS is_partner_account,
  1204. COALESCE(i.balance, 0.0) AS initial_balance
  1205. FROM
  1206. account_account a,
  1207. initial_sum_amounts i
  1208. WHERE
  1209. a.company_id = %s
  1210. AND a.id = %s
  1211. """
  1212. query_inject_account_params = (
  1213. self.date_from,
  1214. self.fy_start_date,
  1215. )
  1216. if self.filter_cost_center_ids:
  1217. query_inject_account_params += (
  1218. tuple(self.filter_cost_center_ids.ids),
  1219. )
  1220. query_inject_account_params += (
  1221. self.company_id.id,
  1222. self.unaffected_earnings_account.id,
  1223. )
  1224. query_inject_account_params += (
  1225. self.date_from,
  1226. )
  1227. if self.filter_cost_center_ids:
  1228. query_inject_account_params += (
  1229. tuple(self.filter_cost_center_ids.ids),
  1230. )
  1231. query_inject_account_params += (
  1232. self.company_id.id,
  1233. self.unaffected_earnings_account.id,
  1234. )
  1235. query_inject_account_params += (
  1236. self.id,
  1237. self.env.uid,
  1238. self.company_id.id,
  1239. self.unaffected_earnings_account.id,
  1240. )
  1241. self.env.cr.execute(query_inject_account,
  1242. query_inject_account_params)
  1243. def _complete_unaffected_earnings_account_values(self):
  1244. """Complete the report values of the unaffected earnings account
  1245. for report_general_ledger_qweb_account."""
  1246. query_update_unaffected_earnings_account_values = """
  1247. WITH
  1248. sum_amounts AS
  1249. (
  1250. SELECT
  1251. SUM(COALESCE(rml.debit, 0.0)) AS debit,
  1252. SUM(COALESCE(rml.credit, 0.0)) AS credit,
  1253. SUM(
  1254. COALESCE(rml.debit, 0.0) -
  1255. COALESCE(rml.credit, 0.0)
  1256. ) + ra.initial_balance AS balance
  1257. FROM
  1258. report_general_ledger_qweb_account ra
  1259. LEFT JOIN
  1260. report_general_ledger_qweb_move_line rml
  1261. ON ra.id = rml.report_account_id
  1262. WHERE
  1263. ra.report_id = %s
  1264. AND ra.account_id = %s
  1265. GROUP BY
  1266. ra.id
  1267. )
  1268. UPDATE
  1269. report_general_ledger_qweb_account ra
  1270. SET
  1271. initial_debit = 0.0,
  1272. initial_credit = 0.0,
  1273. final_debit = sum_amounts.debit,
  1274. final_credit = sum_amounts.credit,
  1275. final_balance = sum_amounts.balance
  1276. FROM
  1277. sum_amounts
  1278. WHERE
  1279. ra.report_id = %s
  1280. AND ra.account_id = %s
  1281. """
  1282. params = (
  1283. self.id,
  1284. self.unaffected_earnings_account.id,
  1285. self.id,
  1286. self.unaffected_earnings_account.id,
  1287. )
  1288. self.env.cr.execute(
  1289. query_update_unaffected_earnings_account_values,
  1290. params
  1291. )