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.

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