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.

1364 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. COALESCE(tr.value, a.name) AS 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. LEFT JOIN
  381. ir_translation tr ON a.id = tr.res_id
  382. AND tr.lang = %s
  383. AND tr.type = 'model'
  384. AND tr.name = 'account.account,name'
  385. WHERE
  386. (
  387. i.debit IS NOT NULL AND i.debit != 0
  388. OR i.credit IS NOT NULL AND i.credit != 0
  389. OR i.balance IS NOT NULL AND i.balance != 0
  390. OR f.debit IS NOT NULL AND f.debit != 0
  391. OR f.credit IS NOT NULL AND f.credit != 0
  392. OR f.balance IS NOT NULL AND f.balance != 0
  393. )
  394. """
  395. if self.hide_account_balance_at_0:
  396. query_inject_account += """
  397. AND
  398. f.balance IS NOT NULL AND f.balance != 0
  399. """
  400. query_inject_account_params = ()
  401. if self.filter_cost_center_ids:
  402. query_inject_account_params += (
  403. tuple(self.filter_cost_center_ids.ids),
  404. )
  405. query_inject_account_params += (
  406. self.company_id.id,
  407. self.unaffected_earnings_account.id,
  408. )
  409. if self.filter_account_ids:
  410. query_inject_account_params += (
  411. tuple(self.filter_account_ids.ids),
  412. )
  413. if self.filter_partner_ids:
  414. query_inject_account_params += (
  415. tuple(self.filter_partner_ids.ids),
  416. )
  417. query_inject_account_params += (
  418. self.date_from,
  419. self.fy_start_date,
  420. )
  421. if self.filter_cost_center_ids:
  422. query_inject_account_params += (
  423. tuple(self.filter_cost_center_ids.ids),
  424. )
  425. query_inject_account_params += (
  426. self.date_from,
  427. )
  428. if self.filter_cost_center_ids:
  429. query_inject_account_params += (
  430. tuple(self.filter_cost_center_ids.ids),
  431. )
  432. query_inject_account_params += (
  433. self.date_to,
  434. self.fy_start_date,
  435. )
  436. if self.filter_cost_center_ids:
  437. query_inject_account_params += (
  438. tuple(self.filter_cost_center_ids.ids),
  439. )
  440. query_inject_account_params += (
  441. self.date_to,
  442. )
  443. if self.filter_cost_center_ids:
  444. query_inject_account_params += (
  445. tuple(self.filter_cost_center_ids.ids),
  446. )
  447. query_inject_account_params += (
  448. self.id,
  449. self.env.uid,
  450. self.env.lang,
  451. )
  452. self.env.cr.execute(query_inject_account, query_inject_account_params)
  453. def _get_partner_sub_subquery_sum_amounts(
  454. self, only_empty_partner, include_initial_balance
  455. ):
  456. """ Return subquery used to compute sum amounts on partners """
  457. sub_subquery_sum_amounts = """
  458. SELECT
  459. ap.account_id AS account_id,
  460. ap.partner_id AS partner_id,
  461. SUM(ml.debit) AS debit,
  462. SUM(ml.credit) AS credit,
  463. SUM(ml.balance) AS balance
  464. FROM
  465. accounts_partners ap
  466. INNER JOIN
  467. account_move_line ml
  468. ON ap.account_id = ml.account_id
  469. AND ml.date <= %s
  470. """
  471. if not only_empty_partner:
  472. sub_subquery_sum_amounts += """
  473. AND ap.partner_id = ml.partner_id
  474. """
  475. else:
  476. sub_subquery_sum_amounts += """
  477. AND ap.partner_id IS NULL AND ml.partner_id IS NULL
  478. """
  479. if not include_initial_balance:
  480. sub_subquery_sum_amounts += """
  481. AND ap.include_initial_balance != TRUE AND ml.date >= %s
  482. """
  483. else:
  484. sub_subquery_sum_amounts += """
  485. AND ap.include_initial_balance = TRUE
  486. """
  487. if self.only_posted_moves:
  488. sub_subquery_sum_amounts += """
  489. INNER JOIN
  490. account_move m ON ml.move_id = m.id AND m.state = 'posted'
  491. """
  492. if self.filter_cost_center_ids:
  493. sub_subquery_sum_amounts += """
  494. INNER JOIN
  495. account_analytic_account aa
  496. ON
  497. ml.analytic_account_id = aa.id
  498. AND aa.id IN %s
  499. """
  500. sub_subquery_sum_amounts += """
  501. GROUP BY
  502. ap.account_id, ap.partner_id
  503. """
  504. return sub_subquery_sum_amounts
  505. def _inject_partner_values(self, only_empty_partner=False):
  506. """ Inject report values for report_general_ledger_qweb_partner.
  507. Only for "partner" accounts (payable and receivable).
  508. """
  509. subquery_sum_amounts = """
  510. SELECT
  511. sub.account_id AS account_id,
  512. sub.partner_id AS partner_id,
  513. SUM(COALESCE(sub.debit, 0.0)) AS debit,
  514. SUM(COALESCE(sub.credit, 0.0)) AS credit,
  515. SUM(COALESCE(sub.balance, 0.0)) AS balance
  516. FROM
  517. (
  518. """
  519. subquery_sum_amounts += self._get_partner_sub_subquery_sum_amounts(
  520. only_empty_partner,
  521. include_initial_balance=False
  522. )
  523. subquery_sum_amounts += """
  524. UNION
  525. """
  526. subquery_sum_amounts += self._get_partner_sub_subquery_sum_amounts(
  527. only_empty_partner,
  528. include_initial_balance=True
  529. )
  530. subquery_sum_amounts += """
  531. ) sub
  532. GROUP BY
  533. sub.account_id, sub.partner_id
  534. """
  535. query_inject_partner = """
  536. WITH
  537. accounts_partners AS
  538. (
  539. SELECT
  540. ra.id AS report_account_id,
  541. a.id AS account_id,
  542. at.include_initial_balance AS include_initial_balance,
  543. p.id AS partner_id,
  544. COALESCE(
  545. CASE
  546. WHEN
  547. NULLIF(p.name, '') IS NOT NULL
  548. AND NULLIF(p.ref, '') IS NOT NULL
  549. THEN p.name || ' (' || p.ref || ')'
  550. ELSE p.name
  551. END,
  552. '""" + _('No partner allocated') + """'
  553. ) AS partner_name
  554. FROM
  555. report_general_ledger_qweb_account ra
  556. INNER JOIN
  557. account_account a ON ra.account_id = a.id
  558. INNER JOIN
  559. account_account_type at ON a.user_type_id = at.id
  560. INNER JOIN
  561. account_move_line ml ON a.id = ml.account_id
  562. LEFT JOIN
  563. res_partner p ON ml.partner_id = p.id
  564. """
  565. if self.filter_cost_center_ids:
  566. query_inject_partner += """
  567. INNER JOIN
  568. account_analytic_account aa
  569. ON
  570. ml.analytic_account_id = aa.id
  571. AND aa.id IN %s
  572. """
  573. query_inject_partner += """
  574. WHERE
  575. ra.report_id = %s
  576. AND
  577. ra.is_partner_account = TRUE
  578. """
  579. if not only_empty_partner:
  580. query_inject_partner += """
  581. AND
  582. p.id IS NOT NULL
  583. """
  584. else:
  585. query_inject_partner += """
  586. AND
  587. p.id IS NULL
  588. """
  589. query_inject_partner += """
  590. """
  591. if self.centralize:
  592. query_inject_partner += """
  593. AND (a.centralized IS NULL OR a.centralized != TRUE)
  594. """
  595. if self.filter_partner_ids:
  596. query_inject_partner += """
  597. AND
  598. p.id IN %s
  599. """
  600. query_inject_partner += """
  601. GROUP BY
  602. ra.id,
  603. a.id,
  604. p.id,
  605. at.include_initial_balance
  606. ),
  607. initial_sum_amounts AS ( """ + subquery_sum_amounts + """ ),
  608. final_sum_amounts AS ( """ + subquery_sum_amounts + """ )
  609. INSERT INTO
  610. report_general_ledger_qweb_partner
  611. (
  612. report_account_id,
  613. create_uid,
  614. create_date,
  615. partner_id,
  616. name,
  617. initial_debit,
  618. initial_credit,
  619. initial_balance,
  620. final_debit,
  621. final_credit,
  622. final_balance
  623. )
  624. SELECT
  625. ap.report_account_id,
  626. %s AS create_uid,
  627. NOW() AS create_date,
  628. ap.partner_id,
  629. ap.partner_name,
  630. COALESCE(i.debit, 0.0) AS initial_debit,
  631. COALESCE(i.credit, 0.0) AS initial_credit,
  632. COALESCE(i.balance, 0.0) AS initial_balance,
  633. COALESCE(f.debit, 0.0) AS final_debit,
  634. COALESCE(f.credit, 0.0) AS final_credit,
  635. COALESCE(f.balance, 0.0) AS final_balance
  636. FROM
  637. accounts_partners ap
  638. LEFT JOIN
  639. initial_sum_amounts i
  640. ON
  641. (
  642. """
  643. if not only_empty_partner:
  644. query_inject_partner += """
  645. ap.partner_id = i.partner_id
  646. """
  647. else:
  648. query_inject_partner += """
  649. ap.partner_id IS NULL AND i.partner_id IS NULL
  650. """
  651. query_inject_partner += """
  652. )
  653. AND ap.account_id = i.account_id
  654. LEFT JOIN
  655. final_sum_amounts f
  656. ON
  657. (
  658. """
  659. if not only_empty_partner:
  660. query_inject_partner += """
  661. ap.partner_id = f.partner_id
  662. """
  663. else:
  664. query_inject_partner += """
  665. ap.partner_id IS NULL AND f.partner_id IS NULL
  666. """
  667. query_inject_partner += """
  668. )
  669. AND ap.account_id = f.account_id
  670. WHERE
  671. (
  672. i.debit IS NOT NULL AND i.debit != 0
  673. OR i.credit IS NOT NULL AND i.credit != 0
  674. OR i.balance IS NOT NULL AND i.balance != 0
  675. OR f.debit IS NOT NULL AND f.debit != 0
  676. OR f.credit IS NOT NULL AND f.credit != 0
  677. OR f.balance IS NOT NULL AND f.balance != 0
  678. )
  679. """
  680. if self.hide_account_balance_at_0:
  681. query_inject_partner += """
  682. AND
  683. f.balance IS NOT NULL AND f.balance != 0
  684. """
  685. query_inject_partner_params = ()
  686. if self.filter_cost_center_ids:
  687. query_inject_partner_params += (
  688. tuple(self.filter_cost_center_ids.ids),
  689. )
  690. query_inject_partner_params += (
  691. self.id,
  692. )
  693. if self.filter_partner_ids:
  694. query_inject_partner_params += (
  695. tuple(self.filter_partner_ids.ids),
  696. )
  697. query_inject_partner_params += (
  698. self.date_from,
  699. self.fy_start_date,
  700. )
  701. if self.filter_cost_center_ids:
  702. query_inject_partner_params += (
  703. tuple(self.filter_cost_center_ids.ids),
  704. )
  705. query_inject_partner_params += (
  706. self.date_from,
  707. )
  708. if self.filter_cost_center_ids:
  709. query_inject_partner_params += (
  710. tuple(self.filter_cost_center_ids.ids),
  711. )
  712. query_inject_partner_params += (
  713. self.date_to,
  714. self.fy_start_date,
  715. )
  716. if self.filter_cost_center_ids:
  717. query_inject_partner_params += (
  718. tuple(self.filter_cost_center_ids.ids),
  719. )
  720. query_inject_partner_params += (
  721. self.date_to,
  722. )
  723. if self.filter_cost_center_ids:
  724. query_inject_partner_params += (
  725. tuple(self.filter_cost_center_ids.ids),
  726. )
  727. query_inject_partner_params += (
  728. self.env.uid,
  729. )
  730. self.env.cr.execute(query_inject_partner, query_inject_partner_params)
  731. def _inject_line_not_centralized_values(
  732. self,
  733. is_account_line=True,
  734. is_partner_line=False,
  735. only_empty_partner_line=False,
  736. only_unaffected_earnings_account=False):
  737. """ Inject report values for report_general_ledger_qweb_move_line.
  738. If centralized option have been chosen,
  739. only non centralized accounts are computed.
  740. In function of `is_account_line` and `is_partner_line` values,
  741. the move_line link is made either with account or either with partner.
  742. The "only_empty_partner_line" value is used
  743. to compute data without partner.
  744. """
  745. query_inject_move_line = """
  746. INSERT INTO
  747. report_general_ledger_qweb_move_line
  748. (
  749. """
  750. if is_account_line:
  751. query_inject_move_line += """
  752. report_account_id,
  753. """
  754. elif is_partner_line:
  755. query_inject_move_line += """
  756. report_partner_id,
  757. """
  758. query_inject_move_line += """
  759. create_uid,
  760. create_date,
  761. move_line_id,
  762. date,
  763. entry,
  764. journal,
  765. account,
  766. partner,
  767. label,
  768. cost_center,
  769. matching_number,
  770. debit,
  771. credit,
  772. cumul_balance,
  773. currency_name,
  774. amount_currency
  775. )
  776. SELECT
  777. """
  778. if is_account_line:
  779. query_inject_move_line += """
  780. ra.id AS report_account_id,
  781. """
  782. elif is_partner_line:
  783. query_inject_move_line += """
  784. rp.id AS report_partner_id,
  785. """
  786. query_inject_move_line += """
  787. %s AS create_uid,
  788. NOW() AS create_date,
  789. ml.id AS move_line_id,
  790. ml.date,
  791. m.name AS entry,
  792. j.code AS journal,
  793. a.code AS account,
  794. """
  795. if not only_empty_partner_line:
  796. query_inject_move_line += """
  797. CASE
  798. WHEN
  799. NULLIF(p.name, '') IS NOT NULL
  800. AND NULLIF(p.ref, '') IS NOT NULL
  801. THEN p.name || ' (' || p.ref || ')'
  802. ELSE p.name
  803. END AS partner,
  804. """
  805. elif only_empty_partner_line:
  806. query_inject_move_line += """
  807. '""" + _('No partner allocated') + """' AS partner,
  808. """
  809. query_inject_move_line += """
  810. CONCAT_WS(' - ', NULLIF(ml.ref, ''), NULLIF(ml.name, '')) AS label,
  811. aa.name AS cost_center,
  812. fr.name AS matching_number,
  813. ml.debit,
  814. ml.credit,
  815. """
  816. if is_account_line:
  817. query_inject_move_line += """
  818. ra.initial_balance + (
  819. SUM(ml.balance)
  820. OVER (PARTITION BY a.code
  821. ORDER BY a.code, ml.date, ml.id)
  822. ) AS cumul_balance,
  823. """
  824. elif is_partner_line and not only_empty_partner_line:
  825. query_inject_move_line += """
  826. rp.initial_balance + (
  827. SUM(ml.balance)
  828. OVER (PARTITION BY a.code, p.name
  829. ORDER BY a.code, p.name, ml.date, ml.id)
  830. ) AS cumul_balance,
  831. """
  832. elif is_partner_line and only_empty_partner_line:
  833. query_inject_move_line += """
  834. rp.initial_balance + (
  835. SUM(ml.balance)
  836. OVER (PARTITION BY a.code
  837. ORDER BY a.code, ml.date, ml.id)
  838. ) AS cumul_balance,
  839. """
  840. query_inject_move_line += """
  841. c.name AS currency_name,
  842. ml.amount_currency
  843. FROM
  844. """
  845. if is_account_line:
  846. query_inject_move_line += """
  847. report_general_ledger_qweb_account ra
  848. """
  849. elif is_partner_line:
  850. query_inject_move_line += """
  851. report_general_ledger_qweb_partner rp
  852. INNER JOIN
  853. report_general_ledger_qweb_account ra ON rp.report_account_id = ra.id
  854. """
  855. query_inject_move_line += """
  856. INNER JOIN
  857. account_move_line ml ON ra.account_id = ml.account_id
  858. INNER JOIN
  859. account_move m ON ml.move_id = m.id
  860. INNER JOIN
  861. account_journal j ON ml.journal_id = j.id
  862. INNER JOIN
  863. account_account a ON ml.account_id = a.id
  864. """
  865. if is_account_line:
  866. query_inject_move_line += """
  867. LEFT JOIN
  868. res_partner p ON ml.partner_id = p.id
  869. """
  870. elif is_partner_line and not only_empty_partner_line:
  871. query_inject_move_line += """
  872. INNER JOIN
  873. res_partner p
  874. ON ml.partner_id = p.id AND rp.partner_id = p.id
  875. """
  876. query_inject_move_line += """
  877. LEFT JOIN
  878. account_full_reconcile fr ON ml.full_reconcile_id = fr.id
  879. LEFT JOIN
  880. res_currency c ON a.currency_id = c.id
  881. """
  882. if self.filter_cost_center_ids:
  883. query_inject_move_line += """
  884. INNER JOIN
  885. account_analytic_account aa
  886. ON
  887. ml.analytic_account_id = aa.id
  888. AND aa.id IN %s
  889. """
  890. else:
  891. query_inject_move_line += """
  892. LEFT JOIN
  893. account_analytic_account aa ON ml.analytic_account_id = aa.id
  894. """
  895. query_inject_move_line += """
  896. WHERE
  897. ra.report_id = %s
  898. AND
  899. """
  900. if only_unaffected_earnings_account:
  901. query_inject_move_line += """
  902. a.id = %s
  903. AND
  904. """
  905. if is_account_line:
  906. query_inject_move_line += """
  907. (ra.is_partner_account IS NULL OR ra.is_partner_account != TRUE)
  908. """
  909. elif is_partner_line:
  910. query_inject_move_line += """
  911. ra.is_partner_account = TRUE
  912. """
  913. if self.centralize:
  914. query_inject_move_line += """
  915. AND
  916. (a.centralized IS NULL OR a.centralized != TRUE)
  917. """
  918. query_inject_move_line += """
  919. AND
  920. ml.date BETWEEN %s AND %s
  921. """
  922. if self.only_posted_moves:
  923. query_inject_move_line += """
  924. AND
  925. m.state = 'posted'
  926. """
  927. if only_empty_partner_line:
  928. query_inject_move_line += """
  929. AND
  930. ml.partner_id IS NULL
  931. AND
  932. rp.partner_id IS NULL
  933. """
  934. if is_account_line:
  935. query_inject_move_line += """
  936. ORDER BY
  937. a.code, ml.date, ml.id
  938. """
  939. elif is_partner_line and not only_empty_partner_line:
  940. query_inject_move_line += """
  941. ORDER BY
  942. a.code, p.name, ml.date, ml.id
  943. """
  944. elif is_partner_line and only_empty_partner_line:
  945. query_inject_move_line += """
  946. ORDER BY
  947. a.code, ml.date, ml.id
  948. """
  949. query_inject_move_line_params = (
  950. self.env.uid,
  951. )
  952. if self.filter_cost_center_ids:
  953. query_inject_move_line_params += (
  954. tuple(self.filter_cost_center_ids.ids),
  955. )
  956. query_inject_move_line_params += (
  957. self.id,
  958. )
  959. if only_unaffected_earnings_account:
  960. query_inject_move_line_params += (
  961. self.unaffected_earnings_account.id,
  962. )
  963. query_inject_move_line_params += (
  964. self.date_from,
  965. self.date_to,
  966. )
  967. self.env.cr.execute(
  968. query_inject_move_line,
  969. query_inject_move_line_params
  970. )
  971. def _inject_line_centralized_values(self):
  972. """ Inject report values for report_general_ledger_qweb_move_line.
  973. Only centralized accounts are computed.
  974. """
  975. query_inject_move_line_centralized = """
  976. WITH
  977. move_lines AS
  978. (
  979. SELECT
  980. ml.account_id,
  981. (
  982. DATE_TRUNC('month', ml.date) + interval '1 month'
  983. - interval '1 day'
  984. )::date AS date,
  985. SUM(ml.debit) AS debit,
  986. SUM(ml.credit) AS credit,
  987. SUM(ml.balance) AS balance
  988. FROM
  989. report_general_ledger_qweb_account ra
  990. INNER JOIN
  991. account_move_line ml ON ra.account_id = ml.account_id
  992. INNER JOIN
  993. account_move m ON ml.move_id = m.id
  994. INNER JOIN
  995. account_account a ON ml.account_id = a.id
  996. """
  997. if self.filter_cost_center_ids:
  998. query_inject_move_line_centralized += """
  999. INNER JOIN
  1000. account_analytic_account aa
  1001. ON
  1002. ml.analytic_account_id = aa.id
  1003. AND aa.id IN %s
  1004. """
  1005. query_inject_move_line_centralized += """
  1006. WHERE
  1007. ra.report_id = %s
  1008. AND
  1009. a.centralized = TRUE
  1010. AND
  1011. ml.date BETWEEN %s AND %s
  1012. """
  1013. if self.only_posted_moves:
  1014. query_inject_move_line_centralized += """
  1015. AND
  1016. m.state = 'posted'
  1017. """
  1018. query_inject_move_line_centralized += """
  1019. GROUP BY
  1020. ra.id, ml.account_id, a.code, 2
  1021. )
  1022. INSERT INTO
  1023. report_general_ledger_qweb_move_line
  1024. (
  1025. report_account_id,
  1026. create_uid,
  1027. create_date,
  1028. date,
  1029. account,
  1030. label,
  1031. debit,
  1032. credit,
  1033. cumul_balance
  1034. )
  1035. SELECT
  1036. ra.id AS report_account_id,
  1037. %s AS create_uid,
  1038. NOW() AS create_date,
  1039. ml.date,
  1040. a.code AS account,
  1041. '""" + _('Centralized Entries') + """' AS label,
  1042. ml.debit AS debit,
  1043. ml.credit AS credit,
  1044. ra.initial_balance + (
  1045. SUM(ml.balance)
  1046. OVER (PARTITION BY a.code ORDER BY ml.date)
  1047. ) AS cumul_balance
  1048. FROM
  1049. report_general_ledger_qweb_account ra
  1050. INNER JOIN
  1051. move_lines ml ON ra.account_id = ml.account_id
  1052. INNER JOIN
  1053. account_account a ON ml.account_id = a.id
  1054. LEFT JOIN
  1055. res_currency c ON a.currency_id = c.id
  1056. WHERE
  1057. ra.report_id = %s
  1058. AND
  1059. (a.centralized IS NOT NULL AND a.centralized = TRUE)
  1060. ORDER BY
  1061. a.code, ml.date
  1062. """
  1063. query_inject_move_line_centralized_params = ()
  1064. if self.filter_cost_center_ids:
  1065. query_inject_move_line_centralized_params += (
  1066. tuple(self.filter_cost_center_ids.ids),
  1067. )
  1068. query_inject_move_line_centralized_params += (
  1069. self.id,
  1070. self.date_from,
  1071. self.date_to,
  1072. self.env.uid,
  1073. self.id,
  1074. )
  1075. self.env.cr.execute(
  1076. query_inject_move_line_centralized,
  1077. query_inject_move_line_centralized_params
  1078. )
  1079. def _compute_has_second_currency(self):
  1080. """ Compute "has_second_currency" flag which will used for display."""
  1081. query_update_has_second_currency = """
  1082. UPDATE
  1083. report_general_ledger_qweb
  1084. SET
  1085. has_second_currency =
  1086. (
  1087. SELECT
  1088. TRUE
  1089. FROM
  1090. report_general_ledger_qweb_move_line l
  1091. INNER JOIN
  1092. report_general_ledger_qweb_account a
  1093. ON l.report_account_id = a.id
  1094. WHERE
  1095. a.report_id = %s
  1096. AND l.currency_name IS NOT NULL
  1097. LIMIT 1
  1098. )
  1099. OR
  1100. (
  1101. SELECT
  1102. TRUE
  1103. FROM
  1104. report_general_ledger_qweb_move_line l
  1105. INNER JOIN
  1106. report_general_ledger_qweb_partner p
  1107. ON l.report_partner_id = p.id
  1108. INNER JOIN
  1109. report_general_ledger_qweb_account a
  1110. ON p.report_account_id = a.id
  1111. WHERE
  1112. a.report_id = %s
  1113. AND l.currency_name IS NOT NULL
  1114. LIMIT 1
  1115. )
  1116. WHERE id = %s
  1117. """
  1118. params = (self.id,) * 3
  1119. self.env.cr.execute(query_update_has_second_currency, params)
  1120. def _get_unaffected_earnings_account_sub_subquery_sum_amounts(
  1121. self, include_initial_balance
  1122. ):
  1123. """ Return subquery used to compute sum amounts on
  1124. unaffected earnings accounts """
  1125. sub_subquery_sum_amounts = """
  1126. SELECT
  1127. SUM(ml.balance) AS balance
  1128. FROM
  1129. account_account a
  1130. INNER JOIN
  1131. account_account_type at ON a.user_type_id = at.id
  1132. INNER JOIN
  1133. account_move_line ml
  1134. ON a.id = ml.account_id
  1135. AND ml.date <= %s
  1136. """
  1137. if not include_initial_balance:
  1138. sub_subquery_sum_amounts += """
  1139. AND NOT(at.include_initial_balance != TRUE AND ml.date >= %s)
  1140. """
  1141. else:
  1142. sub_subquery_sum_amounts += """
  1143. AND at.include_initial_balance = FALSE
  1144. """
  1145. if self.only_posted_moves:
  1146. sub_subquery_sum_amounts += """
  1147. INNER JOIN
  1148. account_move m ON ml.move_id = m.id AND m.state = 'posted'
  1149. """
  1150. if self.filter_cost_center_ids:
  1151. sub_subquery_sum_amounts += """
  1152. INNER JOIN
  1153. account_analytic_account aa
  1154. ON
  1155. ml.analytic_account_id = aa.id
  1156. AND aa.id IN %s
  1157. """
  1158. sub_subquery_sum_amounts += """
  1159. WHERE
  1160. a.company_id =%s
  1161. AND a.id != %s
  1162. """
  1163. return sub_subquery_sum_amounts
  1164. def _inject_unaffected_earnings_account_values(self):
  1165. """Inject the report values of the unaffected earnings account
  1166. for report_general_ledger_qweb_account."""
  1167. subquery_sum_amounts = """
  1168. SELECT
  1169. SUM(COALESCE(sub.balance, 0.0)) AS balance
  1170. FROM
  1171. (
  1172. """
  1173. subquery_sum_amounts += \
  1174. self._get_unaffected_earnings_account_sub_subquery_sum_amounts(
  1175. include_initial_balance=False
  1176. )
  1177. subquery_sum_amounts += """
  1178. UNION
  1179. """
  1180. subquery_sum_amounts += \
  1181. self._get_unaffected_earnings_account_sub_subquery_sum_amounts(
  1182. include_initial_balance=True
  1183. )
  1184. subquery_sum_amounts += """
  1185. ) sub
  1186. """
  1187. query_inject_account = """
  1188. WITH
  1189. initial_sum_amounts AS ( """ + subquery_sum_amounts + """ )
  1190. INSERT INTO
  1191. report_general_ledger_qweb_account
  1192. (
  1193. report_id,
  1194. create_uid,
  1195. create_date,
  1196. account_id,
  1197. code,
  1198. name,
  1199. is_partner_account,
  1200. initial_balance
  1201. )
  1202. SELECT
  1203. %s AS report_id,
  1204. %s AS create_uid,
  1205. NOW() AS create_date,
  1206. a.id AS account_id,
  1207. a.code,
  1208. a.name,
  1209. False AS is_partner_account,
  1210. COALESCE(i.balance, 0.0) AS initial_balance
  1211. FROM
  1212. account_account a,
  1213. initial_sum_amounts i
  1214. WHERE
  1215. a.company_id = %s
  1216. AND a.id = %s
  1217. """
  1218. query_inject_account_params = (
  1219. self.date_from,
  1220. self.fy_start_date,
  1221. )
  1222. if self.filter_cost_center_ids:
  1223. query_inject_account_params += (
  1224. tuple(self.filter_cost_center_ids.ids),
  1225. )
  1226. query_inject_account_params += (
  1227. self.company_id.id,
  1228. self.unaffected_earnings_account.id,
  1229. )
  1230. query_inject_account_params += (
  1231. self.date_from,
  1232. )
  1233. if self.filter_cost_center_ids:
  1234. query_inject_account_params += (
  1235. tuple(self.filter_cost_center_ids.ids),
  1236. )
  1237. query_inject_account_params += (
  1238. self.company_id.id,
  1239. self.unaffected_earnings_account.id,
  1240. )
  1241. query_inject_account_params += (
  1242. self.id,
  1243. self.env.uid,
  1244. self.company_id.id,
  1245. self.unaffected_earnings_account.id,
  1246. )
  1247. self.env.cr.execute(query_inject_account,
  1248. query_inject_account_params)
  1249. def _complete_unaffected_earnings_account_values(self):
  1250. """Complete the report values of the unaffected earnings account
  1251. for report_general_ledger_qweb_account."""
  1252. query_update_unaffected_earnings_account_values = """
  1253. WITH
  1254. sum_amounts AS
  1255. (
  1256. SELECT
  1257. SUM(COALESCE(rml.debit, 0.0)) AS debit,
  1258. SUM(COALESCE(rml.credit, 0.0)) AS credit,
  1259. SUM(
  1260. COALESCE(rml.debit, 0.0) -
  1261. COALESCE(rml.credit, 0.0)
  1262. ) + ra.initial_balance AS balance
  1263. FROM
  1264. report_general_ledger_qweb_account ra
  1265. LEFT JOIN
  1266. report_general_ledger_qweb_move_line rml
  1267. ON ra.id = rml.report_account_id
  1268. WHERE
  1269. ra.report_id = %s
  1270. AND ra.account_id = %s
  1271. GROUP BY
  1272. ra.id
  1273. )
  1274. UPDATE
  1275. report_general_ledger_qweb_account ra
  1276. SET
  1277. initial_debit = 0.0,
  1278. initial_credit = 0.0,
  1279. final_debit = sum_amounts.debit,
  1280. final_credit = sum_amounts.credit,
  1281. final_balance = sum_amounts.balance
  1282. FROM
  1283. sum_amounts
  1284. WHERE
  1285. ra.report_id = %s
  1286. AND ra.account_id = %s
  1287. """
  1288. params = (
  1289. self.id,
  1290. self.unaffected_earnings_account.id,
  1291. self.id,
  1292. self.unaffected_earnings_account.id,
  1293. )
  1294. self.env.cr.execute(
  1295. query_update_unaffected_earnings_account_values,
  1296. params
  1297. )