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.

953 lines
28 KiB

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