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.

928 lines
28 KiB

5 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 odoo import models, fields, api, _
  5. class OpenItemsReport(models.TransientModel):
  6. """ Here, we just define class fields.
  7. For methods, go more bottom at this file.
  8. The class hierarchy is :
  9. * OpenItemsReport
  10. ** OpenItemsReportAccount
  11. *** OpenItemsReportPartner
  12. **** OpenItemsReportMoveLine
  13. """
  14. _name = 'report_open_items_qweb'
  15. _inherit = 'report_qweb_abstract'
  16. # Filters fields, used for data computation
  17. date_at = fields.Date()
  18. only_posted_moves = fields.Boolean()
  19. hide_account_at_0 = fields.Boolean()
  20. foreign_currency = fields.Boolean()
  21. company_id = fields.Many2one(comodel_name='res.company')
  22. filter_account_ids = fields.Many2many(comodel_name='account.account')
  23. filter_partner_ids = fields.Many2many(comodel_name='res.partner')
  24. # Data fields, used to browse report data
  25. account_ids = fields.One2many(
  26. comodel_name='report_open_items_qweb_account',
  27. inverse_name='report_id'
  28. )
  29. class OpenItemsReportAccount(models.TransientModel):
  30. _name = 'report_open_items_qweb_account'
  31. _inherit = 'report_qweb_abstract'
  32. _order = 'code ASC'
  33. report_id = fields.Many2one(
  34. comodel_name='report_open_items_qweb',
  35. ondelete='cascade',
  36. index=True
  37. )
  38. # Data fields, used to keep link with real object
  39. account_id = fields.Many2one(
  40. 'account.account',
  41. index=True
  42. )
  43. # Data fields, used for report display
  44. code = fields.Char()
  45. name = fields.Char()
  46. currency_id = fields.Many2one(comodel_name='res.currency')
  47. final_amount_residual = fields.Float(digits=(16, 2))
  48. final_amount_total_due = fields.Float(digits=(16, 2))
  49. final_amount_residual_currency = fields.Float(digits=(16, 2))
  50. final_amount_total_due_currency = fields.Float(digits=(16, 2))
  51. # Data fields, used to browse report data
  52. partner_ids = fields.One2many(
  53. comodel_name='report_open_items_qweb_partner',
  54. inverse_name='report_account_id'
  55. )
  56. class OpenItemsReportPartner(models.TransientModel):
  57. _name = 'report_open_items_qweb_partner'
  58. _inherit = 'report_qweb_abstract'
  59. report_account_id = fields.Many2one(
  60. comodel_name='report_open_items_qweb_account',
  61. ondelete='cascade',
  62. index=True
  63. )
  64. # Data fields, used to keep link with real object
  65. partner_id = fields.Many2one(
  66. 'res.partner',
  67. index=True
  68. )
  69. # Data fields, used for report display
  70. name = fields.Char()
  71. currency_id = fields.Many2one(comodel_name='res.currency')
  72. final_amount_residual = fields.Float(digits=(16, 2))
  73. final_amount_total_due = fields.Float(digits=(16, 2))
  74. final_amount_residual_currency = fields.Float(digits=(16, 2))
  75. final_amount_total_due_currency = fields.Float(digits=(16, 2))
  76. # Data fields, used to browse report data
  77. move_line_ids = fields.One2many(
  78. comodel_name='report_open_items_qweb_move_line',
  79. inverse_name='report_partner_id'
  80. )
  81. @api.model
  82. def _generate_order_by(self, order_spec, query):
  83. """Custom order to display "No partner allocated" at last position."""
  84. return """
  85. ORDER BY
  86. CASE
  87. WHEN "report_open_items_qweb_partner"."partner_id" IS NOT NULL
  88. THEN 0
  89. ELSE 1
  90. END,
  91. "report_open_items_qweb_partner"."name"
  92. """
  93. class OpenItemsReportMoveLine(models.TransientModel):
  94. _name = 'report_open_items_qweb_move_line'
  95. _inherit = 'report_qweb_abstract'
  96. report_partner_id = fields.Many2one(
  97. comodel_name='report_open_items_qweb_partner',
  98. ondelete='cascade',
  99. index=True
  100. )
  101. # Data fields, used to keep link with real object
  102. move_line_id = fields.Many2one('account.move.line')
  103. # Data fields, used for report display
  104. date = fields.Date()
  105. date_due = fields.Date()
  106. entry = fields.Char()
  107. journal = fields.Char()
  108. account = fields.Char()
  109. partner = fields.Char()
  110. label = fields.Char()
  111. amount_total_due = fields.Float(digits=(16, 2))
  112. amount_residual = fields.Float(digits=(16, 2))
  113. currency_id = fields.Many2one(comodel_name='res.currency')
  114. amount_total_due_currency = fields.Float(digits=(16, 2))
  115. amount_residual_currency = fields.Float(digits=(16, 2))
  116. class OpenItemsReportCompute(models.TransientModel):
  117. """ Here, we just define methods.
  118. For class fields, go more top at this file.
  119. """
  120. _inherit = 'report_open_items_qweb'
  121. @api.multi
  122. def print_report(self, report_type):
  123. self.ensure_one()
  124. if report_type == 'xlsx':
  125. report_name = 'account_financial_report_qweb.' \
  126. 'report_open_items_xlsx'
  127. else:
  128. report_name = 'account_financial_report_qweb.' \
  129. 'report_open_items_qweb'
  130. return self.env['report'].get_action(docids=self.ids,
  131. report_name=report_name)
  132. def _get_html(self):
  133. result = {}
  134. rcontext = {}
  135. context = dict(self.env.context)
  136. report = self.browse(context.get('active_id'))
  137. if report:
  138. rcontext['o'] = report
  139. result['html'] = self.env.ref(
  140. 'account_financial_report_qweb.'
  141. 'report_open_items_html').render(rcontext)
  142. return result
  143. @api.model
  144. def get_html(self, given_context=None):
  145. return self._get_html()
  146. @api.multi
  147. def compute_data_for_report(self):
  148. self.ensure_one()
  149. # Compute report data
  150. self._inject_account_values()
  151. self._inject_partner_values()
  152. self._inject_line_values()
  153. self._inject_line_values(only_empty_partner_line=True)
  154. self._clean_partners_and_accounts()
  155. self._compute_partners_and_accounts_cumul()
  156. if self.hide_account_at_0:
  157. self._clean_partners_and_accounts(
  158. only_delete_account_balance_at_0=True
  159. )
  160. # Refresh cache because all data are computed with SQL requests
  161. self.invalidate_cache()
  162. def _inject_account_values(self):
  163. """Inject report values for report_open_items_qweb_account."""
  164. query_inject_account = """
  165. WITH
  166. accounts AS
  167. (
  168. SELECT
  169. a.id,
  170. a.code,
  171. coalesce (t.value, a.name) AS name,
  172. a.user_type_id,
  173. c.id as currency_id
  174. FROM
  175. account_account a
  176. INNER JOIN
  177. account_move_line ml ON a.id = ml.account_id AND ml.date <= %s
  178. LEFT JOIN
  179. ir_translation t
  180. ON
  181. a.id = t.res_id
  182. AND t.name = 'account.account,name'
  183. AND t.lang = %s
  184. LEFT JOIN
  185. res_currency c ON a.currency_id = c.id
  186. """
  187. if self.filter_partner_ids:
  188. query_inject_account += """
  189. INNER JOIN
  190. res_partner p ON ml.partner_id = p.id
  191. """
  192. if self.only_posted_moves:
  193. query_inject_account += """
  194. INNER JOIN
  195. account_move m ON ml.move_id = m.id AND m.state = 'posted'
  196. """
  197. query_inject_account += """
  198. WHERE
  199. a.company_id = %s
  200. AND a.reconcile IS true
  201. """
  202. if self.filter_account_ids:
  203. query_inject_account += """
  204. AND
  205. a.id IN %s
  206. """
  207. if self.filter_partner_ids:
  208. query_inject_account += """
  209. AND
  210. p.id IN %s
  211. """
  212. query_inject_account += """
  213. GROUP BY
  214. a.id, c.id, t.value
  215. )
  216. INSERT INTO
  217. report_open_items_qweb_account
  218. (
  219. report_id,
  220. create_uid,
  221. create_date,
  222. account_id,
  223. currency_id,
  224. code,
  225. name
  226. )
  227. SELECT
  228. %s AS report_id,
  229. %s AS create_uid,
  230. NOW() AS create_date,
  231. a.id AS account_id,
  232. a.currency_id,
  233. a.code,
  234. a.name
  235. FROM
  236. accounts a
  237. """
  238. query_inject_account_params = (
  239. self.date_at,
  240. self.env.user.lang,
  241. self.company_id.id,
  242. )
  243. if self.filter_account_ids:
  244. query_inject_account_params += (
  245. tuple(self.filter_account_ids.ids),
  246. )
  247. if self.filter_partner_ids:
  248. query_inject_account_params += (
  249. tuple(self.filter_partner_ids.ids),
  250. )
  251. query_inject_account_params += (
  252. self.id,
  253. self.env.uid,
  254. )
  255. self.env.cr.execute(query_inject_account, query_inject_account_params)
  256. def _inject_partner_values(self):
  257. """ Inject report values for report_open_items_qweb_partner. """
  258. # pylint: disable=sql-injection
  259. query_inject_partner = """
  260. WITH
  261. accounts_partners AS
  262. (
  263. SELECT
  264. ra.id AS report_account_id,
  265. a.id AS account_id,
  266. at.include_initial_balance AS include_initial_balance,
  267. p.id AS partner_id,
  268. COALESCE(
  269. CASE
  270. WHEN
  271. NULLIF(p.name, '') IS NOT NULL
  272. AND NULLIF(p.ref, '') IS NOT NULL
  273. THEN p.name || ' (' || p.ref || ')'
  274. ELSE p.name
  275. END,
  276. '""" + _('No partner allocated') + """'
  277. ) AS partner_name
  278. FROM
  279. report_open_items_qweb_account ra
  280. INNER JOIN
  281. account_account a ON ra.account_id = a.id
  282. INNER JOIN
  283. account_account_type at ON a.user_type_id = at.id
  284. INNER JOIN
  285. account_move_line ml ON a.id = ml.account_id AND ml.date <= %s
  286. """
  287. if self.only_posted_moves:
  288. query_inject_partner += """
  289. INNER JOIN
  290. account_move m ON ml.move_id = m.id AND m.state = 'posted'
  291. """
  292. query_inject_partner += """
  293. LEFT JOIN
  294. res_partner p ON ml.partner_id = p.id
  295. WHERE
  296. ra.report_id = %s
  297. """
  298. if self.filter_partner_ids:
  299. query_inject_partner += """
  300. AND
  301. p.id IN %s
  302. """
  303. query_inject_partner += """
  304. GROUP BY
  305. ra.id,
  306. a.id,
  307. p.id,
  308. at.include_initial_balance
  309. )
  310. INSERT INTO
  311. report_open_items_qweb_partner
  312. (
  313. report_account_id,
  314. create_uid,
  315. create_date,
  316. partner_id,
  317. name
  318. )
  319. SELECT
  320. ap.report_account_id,
  321. %s AS create_uid,
  322. NOW() AS create_date,
  323. ap.partner_id,
  324. ap.partner_name
  325. FROM
  326. accounts_partners ap
  327. """
  328. query_inject_partner_params = (
  329. self.date_at,
  330. self.id,
  331. )
  332. if self.filter_partner_ids:
  333. query_inject_partner_params += (
  334. tuple(self.filter_partner_ids.ids),
  335. )
  336. query_inject_partner_params += (
  337. self.env.uid,
  338. )
  339. self.env.cr.execute(query_inject_partner, query_inject_partner_params)
  340. def _get_line_sub_query_move_lines(self,
  341. only_empty_partner_line=False,
  342. positive_balance=True):
  343. """ Return subquery used to compute sum amounts on lines """
  344. sub_query = """
  345. SELECT
  346. ml.id,
  347. ml.balance,
  348. SUM(
  349. CASE
  350. WHEN ml_past.id IS NOT NULL
  351. THEN pr.amount
  352. ELSE NULL
  353. END
  354. ) AS partial_amount,
  355. ml.amount_currency,
  356. SUM(
  357. CASE
  358. WHEN ml_past.id IS NOT NULL
  359. THEN pr.amount_currency
  360. ELSE NULL
  361. END
  362. ) AS partial_amount_currency,
  363. ml.currency_id
  364. FROM
  365. report_open_items_qweb_partner rp
  366. INNER JOIN
  367. report_open_items_qweb_account ra
  368. ON rp.report_account_id = ra.id
  369. INNER JOIN
  370. account_move_line ml
  371. ON ra.account_id = ml.account_id
  372. """
  373. if not only_empty_partner_line:
  374. sub_query += """
  375. AND rp.partner_id = ml.partner_id
  376. """
  377. elif only_empty_partner_line:
  378. sub_query += """
  379. AND ml.partner_id IS NULL
  380. """
  381. if not positive_balance:
  382. sub_query += """
  383. LEFT JOIN
  384. account_partial_reconcile pr
  385. ON ml.balance < 0 AND pr.credit_move_id = ml.id
  386. LEFT JOIN
  387. account_move_line ml_future
  388. ON ml.balance < 0 AND pr.debit_move_id = ml_future.id
  389. AND ml_future.date > %s
  390. LEFT JOIN
  391. account_move_line ml_past
  392. ON ml.balance < 0 AND pr.debit_move_id = ml_past.id
  393. AND ml_past.date <= %s
  394. """
  395. else:
  396. sub_query += """
  397. LEFT JOIN
  398. account_partial_reconcile pr
  399. ON ml.balance > 0 AND pr.debit_move_id = ml.id
  400. LEFT JOIN
  401. account_move_line ml_future
  402. ON ml.balance > 0 AND pr.credit_move_id = ml_future.id
  403. AND ml_future.date > %s
  404. LEFT JOIN
  405. account_move_line ml_past
  406. ON ml.balance > 0 AND pr.credit_move_id = ml_past.id
  407. AND ml_past.date <= %s
  408. """
  409. sub_query += """
  410. WHERE
  411. ra.report_id = %s
  412. GROUP BY
  413. ml.id,
  414. ml.balance,
  415. ml.amount_currency
  416. HAVING
  417. (
  418. ml.full_reconcile_id IS NULL
  419. OR MAX(ml_future.id) IS NOT NULL
  420. )
  421. """
  422. return sub_query
  423. def _inject_line_values(self, only_empty_partner_line=False):
  424. """ Inject report values for report_open_items_qweb_move_line.
  425. The "only_empty_partner_line" value is used
  426. to compute data without partner.
  427. """
  428. query_inject_move_line = """
  429. WITH
  430. move_lines_amount AS
  431. (
  432. """
  433. query_inject_move_line += self._get_line_sub_query_move_lines(
  434. only_empty_partner_line=only_empty_partner_line,
  435. positive_balance=True
  436. )
  437. query_inject_move_line += """
  438. UNION
  439. """
  440. query_inject_move_line += self._get_line_sub_query_move_lines(
  441. only_empty_partner_line=only_empty_partner_line,
  442. positive_balance=False
  443. )
  444. query_inject_move_line += """
  445. ),
  446. move_lines AS
  447. (
  448. SELECT
  449. id,
  450. CASE
  451. WHEN SUM(partial_amount) > 0
  452. THEN
  453. CASE
  454. WHEN balance > 0
  455. THEN balance - SUM(partial_amount)
  456. ELSE balance + SUM(partial_amount)
  457. END
  458. ELSE balance
  459. END AS amount_residual,
  460. CASE
  461. WHEN SUM(partial_amount_currency) > 0
  462. THEN
  463. CASE
  464. WHEN amount_currency > 0
  465. THEN amount_currency - SUM(partial_amount_currency)
  466. ELSE amount_currency + SUM(partial_amount_currency)
  467. END
  468. ELSE amount_currency
  469. END AS amount_residual_currency,
  470. currency_id
  471. FROM
  472. move_lines_amount
  473. GROUP BY
  474. id,
  475. balance,
  476. amount_currency,
  477. currency_id
  478. )
  479. INSERT INTO
  480. report_open_items_qweb_move_line
  481. (
  482. report_partner_id,
  483. create_uid,
  484. create_date,
  485. move_line_id,
  486. date,
  487. date_due,
  488. entry,
  489. journal,
  490. account,
  491. partner,
  492. label,
  493. amount_total_due,
  494. amount_residual,
  495. currency_id,
  496. amount_total_due_currency,
  497. amount_residual_currency
  498. )
  499. SELECT
  500. rp.id AS report_partner_id,
  501. %s AS create_uid,
  502. NOW() AS create_date,
  503. ml.id AS move_line_id,
  504. ml.date,
  505. ml.date_maturity,
  506. m.name AS entry,
  507. j.code AS journal,
  508. a.code AS account,
  509. """
  510. if not only_empty_partner_line:
  511. query_inject_move_line += """
  512. CASE
  513. WHEN
  514. NULLIF(p.name, '') IS NOT NULL
  515. AND NULLIF(p.ref, '') IS NOT NULL
  516. THEN p.name || ' (' || p.ref || ')'
  517. ELSE p.name
  518. END AS partner,
  519. """
  520. elif only_empty_partner_line:
  521. query_inject_move_line += """
  522. '""" + _('No partner allocated') + """' AS partner,
  523. """
  524. query_inject_move_line += """
  525. CONCAT_WS(' - ', NULLIF(ml.ref, ''), NULLIF(ml.name, '')) AS label,
  526. ml.balance,
  527. ml2.amount_residual,
  528. c.id AS currency_id,
  529. ml.amount_currency,
  530. ml2.amount_residual_currency
  531. FROM
  532. report_open_items_qweb_partner rp
  533. INNER JOIN
  534. report_open_items_qweb_account ra ON rp.report_account_id = ra.id
  535. INNER JOIN
  536. account_move_line ml ON ra.account_id = ml.account_id
  537. INNER JOIN
  538. move_lines ml2
  539. ON ml.id = ml2.id
  540. AND ml2.amount_residual IS NOT NULL
  541. AND ml2.amount_residual != 0
  542. INNER JOIN
  543. account_move m ON ml.move_id = m.id
  544. INNER JOIN
  545. account_journal j ON ml.journal_id = j.id
  546. INNER JOIN
  547. account_account a ON ml.account_id = a.id
  548. """
  549. if not only_empty_partner_line:
  550. query_inject_move_line += """
  551. INNER JOIN
  552. res_partner p
  553. ON ml.partner_id = p.id AND rp.partner_id = p.id
  554. """
  555. query_inject_move_line += """
  556. LEFT JOIN
  557. account_full_reconcile fr ON ml.full_reconcile_id = fr.id
  558. LEFT JOIN
  559. res_currency c ON ml2.currency_id = c.id
  560. WHERE
  561. ra.report_id = %s
  562. AND
  563. ml.date <= %s
  564. """
  565. if self.only_posted_moves:
  566. query_inject_move_line += """
  567. AND
  568. m.state = 'posted'
  569. """
  570. if only_empty_partner_line:
  571. query_inject_move_line += """
  572. AND
  573. ml.partner_id IS NULL
  574. AND
  575. rp.partner_id IS NULL
  576. """
  577. if not only_empty_partner_line:
  578. query_inject_move_line += """
  579. ORDER BY
  580. a.code, p.name, ml.date, ml.id
  581. """
  582. elif only_empty_partner_line:
  583. query_inject_move_line += """
  584. ORDER BY
  585. a.code, ml.date, ml.id
  586. """
  587. self.env.cr.execute(
  588. query_inject_move_line,
  589. (self.date_at,
  590. self.date_at,
  591. self.id,
  592. self.date_at,
  593. self.date_at,
  594. self.id,
  595. self.env.uid,
  596. self.id,
  597. self.date_at,)
  598. )
  599. def _compute_partners_and_accounts_cumul(self):
  600. """ Compute cumulative amount for
  601. report_open_items_qweb_partner and report_open_items_qweb_account.
  602. """
  603. self._compute_partner_cumul()
  604. self._compute_account_cumul()
  605. def _compute_partner_cumul(self):
  606. # pylint: disable=sql-injection
  607. where_condition_partner_by_account = """
  608. WHERE
  609. id IN
  610. (
  611. SELECT
  612. rp.id
  613. FROM
  614. report_open_items_qweb_account ra
  615. INNER JOIN
  616. report_open_items_qweb_partner rp
  617. ON ra.id = rp.report_account_id
  618. WHERE
  619. ra.report_id = %s
  620. )"""
  621. query_computer_partner_residual_cumul = """
  622. UPDATE
  623. report_open_items_qweb_partner
  624. SET
  625. final_amount_residual =
  626. (
  627. SELECT
  628. SUM(rml.amount_residual) AS final_amount_residual
  629. FROM
  630. report_open_items_qweb_move_line rml
  631. WHERE
  632. rml.report_partner_id = report_open_items_qweb_partner.id
  633. )
  634. """ + where_condition_partner_by_account
  635. params_compute_partners_residual_cumul = (self.id,)
  636. self.env.cr.execute(query_computer_partner_residual_cumul,
  637. params_compute_partners_residual_cumul)
  638. query_compute_partners_due_cumul = """
  639. UPDATE
  640. report_open_items_qweb_partner
  641. SET
  642. final_amount_total_due =
  643. (
  644. SELECT
  645. SUM(rml.amount_total_due) AS final_amount_total_due
  646. FROM
  647. report_open_items_qweb_move_line rml
  648. WHERE
  649. rml.report_partner_id = report_open_items_qweb_partner.id
  650. )
  651. """ + where_condition_partner_by_account
  652. params_compute_partner_due_cumul = (self.id,)
  653. self.env.cr.execute(query_compute_partners_due_cumul,
  654. params_compute_partner_due_cumul)
  655. # Manage currency in partner
  656. where_condition_partner_by_account_cur = """
  657. WHERE
  658. id IN
  659. (
  660. SELECT
  661. rp.id
  662. FROM
  663. report_open_items_qweb_account ra
  664. INNER JOIN
  665. report_open_items_qweb_partner rp
  666. ON ra.id = rp.report_account_id
  667. WHERE
  668. ra.report_id = %s AND ra.currency_id IS NOT NULL
  669. )
  670. """
  671. query_compute_partners_cur_id_cumul = """
  672. UPDATE
  673. report_open_items_qweb_partner
  674. SET
  675. currency_id =
  676. (
  677. SELECT
  678. MAX(currency_id) as currency_id
  679. FROM
  680. report_open_items_qweb_move_line rml
  681. WHERE
  682. rml.report_partner_id = report_open_items_qweb_partner.id
  683. )
  684. """ + where_condition_partner_by_account_cur
  685. params_compute_partners_cur_id_cumul = (self.id,)
  686. self.env.cr.execute(query_compute_partners_cur_id_cumul,
  687. params_compute_partners_cur_id_cumul)
  688. query_compute_partners_cur_residual_cumul = """
  689. UPDATE
  690. report_open_items_qweb_partner
  691. SET
  692. final_amount_residual_currency =
  693. (
  694. SELECT
  695. SUM(rml.amount_residual_currency)
  696. AS final_amount_residual_currency
  697. FROM
  698. report_open_items_qweb_move_line rml
  699. WHERE
  700. rml.report_partner_id = report_open_items_qweb_partner.id
  701. )
  702. """ + where_condition_partner_by_account_cur
  703. params_compute_partners_cur_residual_cumul = (self.id,)
  704. self.env.cr.execute(query_compute_partners_cur_residual_cumul,
  705. params_compute_partners_cur_residual_cumul)
  706. query_compute_partners_cur_due_cumul = """
  707. UPDATE
  708. report_open_items_qweb_partner
  709. SET
  710. final_amount_total_due_currency =
  711. (
  712. SELECT
  713. SUM(rml.amount_total_due_currency)
  714. AS final_amount_total_due_currency
  715. FROM
  716. report_open_items_qweb_move_line rml
  717. WHERE
  718. rml.report_partner_id = report_open_items_qweb_partner.id
  719. )
  720. """ + where_condition_partner_by_account_cur
  721. params_compute_partners_cur_due_cumul = (self.id,)
  722. self.env.cr.execute(query_compute_partners_cur_due_cumul,
  723. params_compute_partners_cur_due_cumul)
  724. def _compute_account_cumul(self):
  725. query_compute_accounts_residual_cumul = """
  726. UPDATE
  727. report_open_items_qweb_account
  728. SET
  729. final_amount_residual =
  730. (
  731. SELECT
  732. SUM(rp.final_amount_residual) AS final_amount_residual
  733. FROM
  734. report_open_items_qweb_partner rp
  735. WHERE
  736. rp.report_account_id = report_open_items_qweb_account.id
  737. )
  738. WHERE
  739. report_id = %s
  740. """
  741. params_compute_accounts_residual_cumul = (self.id,)
  742. self.env.cr.execute(query_compute_accounts_residual_cumul,
  743. params_compute_accounts_residual_cumul)
  744. query_compute_accounts_cur_residual_cumul = """
  745. UPDATE
  746. report_open_items_qweb_account
  747. SET
  748. final_amount_residual_currency =
  749. (
  750. SELECT
  751. SUM(rp.final_amount_residual_currency)
  752. AS final_amount_residual_currency
  753. FROM
  754. report_open_items_qweb_partner rp
  755. WHERE
  756. rp.report_account_id = report_open_items_qweb_account.id
  757. )
  758. WHERE
  759. report_id = %s
  760. """
  761. params_compute_accounts_cur_residual_cumul = (self.id,)
  762. self.env.cr.execute(query_compute_accounts_cur_residual_cumul,
  763. params_compute_accounts_cur_residual_cumul)
  764. query_compute_accounts_due_cumul = """
  765. UPDATE
  766. report_open_items_qweb_account
  767. SET
  768. final_amount_total_due =
  769. (
  770. SELECT
  771. SUM(rp.final_amount_total_due) AS final_amount_total_due
  772. FROM
  773. report_open_items_qweb_partner rp
  774. WHERE
  775. rp.report_account_id = report_open_items_qweb_account.id
  776. )
  777. WHERE
  778. report_id = %s
  779. """
  780. params_compute_accounts_due_cumul = (self.id,)
  781. self.env.cr.execute(query_compute_accounts_due_cumul,
  782. params_compute_accounts_due_cumul)
  783. query_compute_accounts_cur_due_cumul = """
  784. UPDATE
  785. report_open_items_qweb_account
  786. SET
  787. final_amount_total_due_currency =
  788. (
  789. SELECT
  790. SUM(rp.final_amount_total_due_currency)
  791. AS final_amount_total_due_currency
  792. FROM
  793. report_open_items_qweb_partner rp
  794. WHERE
  795. rp.report_account_id = report_open_items_qweb_account.id
  796. )
  797. WHERE
  798. report_id = %s
  799. """
  800. params_compute_accounts_cur_due_cumul = (self.id,)
  801. self.env.cr.execute(query_compute_accounts_cur_due_cumul,
  802. params_compute_accounts_cur_due_cumul)
  803. def _clean_partners_and_accounts(self,
  804. only_delete_account_balance_at_0=False):
  805. """ Delete empty data for
  806. report_open_items_qweb_partner and report_open_items_qweb_account.
  807. The "only_delete_account_balance_at_0" value is used
  808. to delete also the data with cumulative amounts at 0.
  809. """
  810. query_clean_partners = """
  811. DELETE FROM
  812. report_open_items_qweb_partner
  813. WHERE
  814. id IN
  815. (
  816. SELECT
  817. DISTINCT rp.id
  818. FROM
  819. report_open_items_qweb_account ra
  820. INNER JOIN
  821. report_open_items_qweb_partner rp
  822. ON ra.id = rp.report_account_id
  823. LEFT JOIN
  824. report_open_items_qweb_move_line rml
  825. ON rp.id = rml.report_partner_id
  826. WHERE
  827. ra.report_id = %s
  828. """
  829. if not only_delete_account_balance_at_0:
  830. query_clean_partners += """
  831. AND rml.id IS NULL
  832. """
  833. elif only_delete_account_balance_at_0:
  834. query_clean_partners += """
  835. AND (
  836. rp.final_amount_residual IS NULL
  837. OR rp.final_amount_residual = 0
  838. )
  839. """
  840. query_clean_partners += """
  841. )
  842. """
  843. params_clean_partners = (self.id,)
  844. self.env.cr.execute(query_clean_partners, params_clean_partners)
  845. query_clean_accounts = """
  846. DELETE FROM
  847. report_open_items_qweb_account
  848. WHERE
  849. id IN
  850. (
  851. SELECT
  852. DISTINCT ra.id
  853. FROM
  854. report_open_items_qweb_account ra
  855. LEFT JOIN
  856. report_open_items_qweb_partner rp
  857. ON ra.id = rp.report_account_id
  858. WHERE
  859. ra.report_id = %s
  860. """
  861. if not only_delete_account_balance_at_0:
  862. query_clean_accounts += """
  863. AND rp.id IS NULL
  864. """
  865. elif only_delete_account_balance_at_0:
  866. query_clean_accounts += """
  867. AND (
  868. ra.final_amount_residual IS NULL
  869. OR ra.final_amount_residual = 0
  870. )
  871. """
  872. query_clean_accounts += """
  873. )
  874. """
  875. params_clean_accounts = (self.id,)
  876. self.env.cr.execute(query_clean_accounts, params_clean_accounts)