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.

957 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_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)],
  131. limit=1).report_action(self, config=False)
  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.report_open_items').render(
  141. 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_account."""
  164. query_inject_account = """
  165. WITH
  166. accounts AS
  167. (
  168. SELECT
  169. a.id,
  170. a.code,
  171. a.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. res_currency c ON a.currency_id = c.id
  180. """
  181. if self.filter_partner_ids:
  182. query_inject_account += """
  183. INNER JOIN
  184. res_partner p ON ml.partner_id = p.id
  185. """
  186. if self.only_posted_moves:
  187. query_inject_account += """
  188. INNER JOIN
  189. account_move m ON ml.move_id = m.id AND m.state = 'posted'
  190. """
  191. query_inject_account += """
  192. WHERE
  193. a.company_id = %s
  194. AND a.reconcile IS true
  195. """
  196. if self.filter_account_ids:
  197. query_inject_account += """
  198. AND
  199. a.id IN %s
  200. """
  201. if self.filter_partner_ids:
  202. query_inject_account += """
  203. AND
  204. p.id IN %s
  205. """
  206. query_inject_account += """
  207. GROUP BY
  208. a.id, c.id
  209. )
  210. INSERT INTO
  211. report_open_items_account
  212. (
  213. report_id,
  214. create_uid,
  215. create_date,
  216. account_id,
  217. currency_id,
  218. code,
  219. name
  220. )
  221. SELECT
  222. %s AS report_id,
  223. %s AS create_uid,
  224. NOW() AS create_date,
  225. a.id AS account_id,
  226. a.currency_id,
  227. a.code,
  228. a.name
  229. FROM
  230. accounts a
  231. """
  232. query_inject_account_params = (
  233. self.date_at,
  234. self.company_id.id,
  235. )
  236. if self.filter_account_ids:
  237. query_inject_account_params += (
  238. tuple(self.filter_account_ids.ids),
  239. )
  240. if self.filter_partner_ids:
  241. query_inject_account_params += (
  242. tuple(self.filter_partner_ids.ids),
  243. )
  244. query_inject_account_params += (
  245. self.id,
  246. self.env.uid,
  247. )
  248. self.env.cr.execute(query_inject_account, query_inject_account_params)
  249. def _inject_partner_values(self):
  250. """ Inject report values for report_open_items_partner. """
  251. # pylint: disable=sql-injection
  252. query_inject_partner = """
  253. WITH
  254. accounts_partners AS
  255. (
  256. SELECT
  257. ra.id AS report_account_id,
  258. a.id AS account_id,
  259. at.include_initial_balance AS include_initial_balance,
  260. p.id AS partner_id,
  261. COALESCE(
  262. CASE
  263. WHEN
  264. NULLIF(p.name, '') IS NOT NULL
  265. AND NULLIF(p.ref, '') IS NOT NULL
  266. THEN p.name || ' (' || p.ref || ')'
  267. ELSE p.name
  268. END,
  269. '""" + _('No partner allocated') + """'
  270. ) AS partner_name
  271. FROM
  272. report_open_items_account ra
  273. INNER JOIN
  274. account_account a ON ra.account_id = a.id
  275. INNER JOIN
  276. account_account_type at ON a.user_type_id = at.id
  277. INNER JOIN
  278. account_move_line ml ON a.id = ml.account_id AND ml.date <= %s
  279. """
  280. if self.only_posted_moves:
  281. query_inject_partner += """
  282. INNER JOIN
  283. account_move m ON ml.move_id = m.id AND m.state = 'posted'
  284. """
  285. query_inject_partner += """
  286. LEFT JOIN
  287. res_partner p ON ml.partner_id = p.id
  288. WHERE
  289. ra.report_id = %s
  290. """
  291. if self.filter_partner_ids:
  292. query_inject_partner += """
  293. AND
  294. p.id IN %s
  295. """
  296. query_inject_partner += """
  297. GROUP BY
  298. ra.id,
  299. a.id,
  300. p.id,
  301. at.include_initial_balance
  302. )
  303. INSERT INTO
  304. report_open_items_partner
  305. (
  306. report_account_id,
  307. create_uid,
  308. create_date,
  309. partner_id,
  310. name
  311. )
  312. SELECT
  313. ap.report_account_id,
  314. %s AS create_uid,
  315. NOW() AS create_date,
  316. ap.partner_id,
  317. ap.partner_name
  318. FROM
  319. accounts_partners ap
  320. """
  321. query_inject_partner_params = (
  322. self.date_at,
  323. self.id,
  324. )
  325. if self.filter_partner_ids:
  326. query_inject_partner_params += (
  327. tuple(self.filter_partner_ids.ids),
  328. )
  329. query_inject_partner_params += (
  330. self.env.uid,
  331. )
  332. self.env.cr.execute(query_inject_partner, query_inject_partner_params)
  333. def _get_line_sub_query_move_lines(self,
  334. only_empty_partner_line=False,
  335. positive_balance=True):
  336. """ Return subquery used to compute sum amounts on lines """
  337. sub_query = """
  338. SELECT
  339. ml.id,
  340. ml.balance,
  341. SUM(
  342. CASE
  343. WHEN ml_past.id IS NOT NULL
  344. THEN pr.amount
  345. ELSE NULL
  346. END
  347. ) AS partial_amount,
  348. ml.amount_currency,
  349. SUM(
  350. CASE
  351. WHEN ml_past.id IS NOT NULL
  352. THEN pr.amount_currency
  353. ELSE NULL
  354. END
  355. ) AS partial_amount_currency,
  356. ml.currency_id
  357. FROM
  358. report_open_items_partner rp
  359. INNER JOIN
  360. report_open_items_account ra
  361. ON rp.report_account_id = ra.id
  362. INNER JOIN
  363. account_move_line ml
  364. ON ra.account_id = ml.account_id
  365. """
  366. if not only_empty_partner_line:
  367. sub_query += """
  368. AND rp.partner_id = ml.partner_id
  369. """
  370. elif only_empty_partner_line:
  371. sub_query += """
  372. AND ml.partner_id IS NULL
  373. """
  374. if not positive_balance:
  375. sub_query += """
  376. LEFT JOIN
  377. account_partial_reconcile pr
  378. ON ml.balance < 0 AND pr.credit_move_id = ml.id
  379. LEFT JOIN
  380. account_move_line ml_future
  381. ON ml.balance < 0 AND pr.debit_move_id = ml_future.id
  382. AND ml_future.date > %s
  383. LEFT JOIN
  384. account_move_line ml_past
  385. ON ml.balance < 0 AND pr.debit_move_id = ml_past.id
  386. AND ml_past.date <= %s
  387. """
  388. else:
  389. sub_query += """
  390. LEFT JOIN
  391. account_partial_reconcile pr
  392. ON ml.balance > 0 AND pr.debit_move_id = ml.id
  393. LEFT JOIN
  394. account_move_line ml_future
  395. ON ml.balance > 0 AND pr.credit_move_id = ml_future.id
  396. AND ml_future.date > %s
  397. LEFT JOIN
  398. account_move_line ml_past
  399. ON ml.balance > 0 AND pr.credit_move_id = ml_past.id
  400. AND ml_past.date <= %s
  401. """
  402. sub_query += """
  403. WHERE
  404. ra.report_id = %s
  405. GROUP BY
  406. ml.id,
  407. ml.balance,
  408. ml.amount_currency
  409. HAVING
  410. (
  411. ml.full_reconcile_id IS NULL
  412. OR MAX(ml_future.id) IS NOT NULL
  413. )
  414. """
  415. return sub_query
  416. def _inject_line_values(self, only_empty_partner_line=False):
  417. """ Inject report values for report_open_items_move_line.
  418. The "only_empty_partner_line" value is used
  419. to compute data without partner.
  420. """
  421. query_inject_move_line = """
  422. WITH
  423. move_lines_amount AS
  424. (
  425. """
  426. query_inject_move_line += self._get_line_sub_query_move_lines(
  427. only_empty_partner_line=only_empty_partner_line,
  428. positive_balance=True
  429. )
  430. query_inject_move_line += """
  431. UNION
  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=False
  436. )
  437. query_inject_move_line += """
  438. ),
  439. move_lines AS
  440. (
  441. SELECT
  442. id,
  443. CASE
  444. WHEN SUM(partial_amount) > 0
  445. THEN
  446. CASE
  447. WHEN balance > 0
  448. THEN balance - SUM(partial_amount)
  449. ELSE balance + SUM(partial_amount)
  450. END
  451. ELSE balance
  452. END AS amount_residual,
  453. CASE
  454. WHEN SUM(partial_amount_currency) > 0
  455. THEN
  456. CASE
  457. WHEN amount_currency > 0
  458. THEN amount_currency - SUM(partial_amount_currency)
  459. ELSE amount_currency + SUM(partial_amount_currency)
  460. END
  461. ELSE amount_currency
  462. END AS amount_residual_currency,
  463. currency_id
  464. FROM
  465. move_lines_amount
  466. GROUP BY
  467. id,
  468. balance,
  469. amount_currency,
  470. currency_id
  471. )
  472. INSERT INTO
  473. report_open_items_move_line
  474. (
  475. report_partner_id,
  476. create_uid,
  477. create_date,
  478. move_line_id,
  479. date,
  480. date_due,
  481. entry,
  482. journal,
  483. account,
  484. partner,
  485. label,
  486. amount_total_due,
  487. amount_residual,
  488. currency_id,
  489. amount_total_due_currency,
  490. amount_residual_currency
  491. )
  492. SELECT
  493. rp.id AS report_partner_id,
  494. %s AS create_uid,
  495. NOW() AS create_date,
  496. ml.id AS move_line_id,
  497. ml.date,
  498. ml.date_maturity,
  499. m.name AS entry,
  500. j.code AS journal,
  501. a.code AS account,
  502. """
  503. if not only_empty_partner_line:
  504. query_inject_move_line += """
  505. CASE
  506. WHEN
  507. NULLIF(p.name, '') IS NOT NULL
  508. AND NULLIF(p.ref, '') IS NOT NULL
  509. THEN p.name || ' (' || p.ref || ')'
  510. ELSE p.name
  511. END AS partner,
  512. """
  513. elif only_empty_partner_line:
  514. query_inject_move_line += """
  515. '""" + _('No partner allocated') + """' AS partner,
  516. """
  517. query_inject_move_line += """
  518. CONCAT_WS(' - ', NULLIF(ml.ref, ''), NULLIF(ml.name, '')) AS label,
  519. ml.balance,
  520. ml2.amount_residual,
  521. c.id AS currency_id,
  522. ml.amount_currency,
  523. ml2.amount_residual_currency
  524. FROM
  525. report_open_items_partner rp
  526. INNER JOIN
  527. report_open_items_account ra ON rp.report_account_id = ra.id
  528. INNER JOIN
  529. account_move_line ml ON ra.account_id = ml.account_id
  530. INNER JOIN
  531. move_lines ml2
  532. ON ml.id = ml2.id
  533. AND ml2.amount_residual IS NOT NULL
  534. AND ml2.amount_residual != 0
  535. INNER JOIN
  536. account_move m ON ml.move_id = m.id
  537. INNER JOIN
  538. account_journal j ON ml.journal_id = j.id
  539. INNER JOIN
  540. account_account a ON ml.account_id = a.id
  541. """
  542. if not only_empty_partner_line:
  543. query_inject_move_line += """
  544. INNER JOIN
  545. res_partner p
  546. ON ml.partner_id = p.id AND rp.partner_id = p.id
  547. """
  548. query_inject_move_line += """
  549. LEFT JOIN
  550. account_full_reconcile fr ON ml.full_reconcile_id = fr.id
  551. LEFT JOIN
  552. res_currency c ON ml2.currency_id = c.id
  553. WHERE
  554. ra.report_id = %s
  555. AND
  556. ml.date <= %s
  557. """
  558. if self.only_posted_moves:
  559. query_inject_move_line += """
  560. AND
  561. m.state = 'posted'
  562. """
  563. if only_empty_partner_line:
  564. query_inject_move_line += """
  565. AND
  566. ml.partner_id IS NULL
  567. AND
  568. rp.partner_id IS NULL
  569. """
  570. if not only_empty_partner_line:
  571. query_inject_move_line += """
  572. ORDER BY
  573. a.code, p.name, ml.date, ml.id
  574. """
  575. elif only_empty_partner_line:
  576. query_inject_move_line += """
  577. ORDER BY
  578. a.code, ml.date, ml.id
  579. """
  580. self.env.cr.execute(
  581. query_inject_move_line,
  582. (self.date_at,
  583. self.date_at,
  584. self.id,
  585. self.date_at,
  586. self.date_at,
  587. self.id,
  588. self.env.uid,
  589. self.id,
  590. self.date_at,)
  591. )
  592. def _compute_partners_and_accounts_cumul(self):
  593. """ Compute cumulative amount for
  594. report_open_items_partner and report_open_items_account.
  595. """
  596. self._compute_partner_cumul()
  597. self._compute_account_cumul()
  598. def _compute_partner_cumul(self):
  599. query_computer_partner_residual_cumul = """
  600. UPDATE
  601. report_open_items_partner
  602. SET
  603. final_amount_residual =
  604. (
  605. SELECT
  606. SUM(rml.amount_residual) AS final_amount_residual
  607. FROM
  608. report_open_items_move_line rml
  609. WHERE
  610. rml.report_partner_id = report_open_items_partner.id
  611. )
  612. WHERE
  613. id IN
  614. (
  615. SELECT
  616. rp.id
  617. FROM
  618. report_open_items_account ra
  619. INNER JOIN
  620. report_open_items_partner rp
  621. ON ra.id = rp.report_account_id
  622. WHERE
  623. ra.report_id = %s
  624. )
  625. """
  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
  643. id IN
  644. (
  645. SELECT
  646. rp.id
  647. FROM
  648. report_open_items_account ra
  649. INNER JOIN
  650. report_open_items_partner rp
  651. ON ra.id = rp.report_account_id
  652. WHERE
  653. ra.report_id = %s
  654. )
  655. """
  656. params_compute_partner_due_cumul = (self.id,)
  657. self.env.cr.execute(query_compute_partners_due_cumul,
  658. params_compute_partner_due_cumul)
  659. # Manage currency in partner
  660. query_compute_partners_cur_id_cumul = """
  661. UPDATE
  662. report_open_items_partner
  663. SET
  664. currency_id =
  665. (
  666. SELECT
  667. MAX(currency_id) as currency_id
  668. FROM
  669. report_open_items_move_line rml
  670. WHERE
  671. rml.report_partner_id = report_open_items_partner.id
  672. )
  673. WHERE
  674. id IN
  675. (
  676. SELECT
  677. rp.id
  678. FROM
  679. report_open_items_account ra
  680. INNER JOIN
  681. report_open_items_partner rp
  682. ON ra.id = rp.report_account_id
  683. WHERE
  684. ra.report_id = %s AND ra.currency_id IS NOT NULL
  685. )
  686. """
  687. params_compute_partners_cur_id_cumul = (self.id,)
  688. self.env.cr.execute(query_compute_partners_cur_id_cumul,
  689. params_compute_partners_cur_id_cumul)
  690. query_compute_partners_cur_residual_cumul = """
  691. UPDATE
  692. report_open_items_partner
  693. SET
  694. final_amount_residual_currency =
  695. (
  696. SELECT
  697. SUM(rml.amount_residual_currency)
  698. AS final_amount_residual_currency
  699. FROM
  700. report_open_items_move_line rml
  701. WHERE
  702. rml.report_partner_id = report_open_items_partner.id
  703. )
  704. WHERE
  705. id IN
  706. (
  707. SELECT
  708. rp.id
  709. FROM
  710. report_open_items_account ra
  711. INNER JOIN
  712. report_open_items_partner rp
  713. ON ra.id = rp.report_account_id
  714. WHERE
  715. ra.report_id = %s AND ra.currency_id IS NOT NULL
  716. )
  717. """
  718. params_compute_partners_cur_residual_cumul = (self.id,)
  719. self.env.cr.execute(query_compute_partners_cur_residual_cumul,
  720. params_compute_partners_cur_residual_cumul)
  721. query_compute_partners_cur_due_cumul = """
  722. UPDATE
  723. report_open_items_partner
  724. SET
  725. final_amount_total_due_currency =
  726. (
  727. SELECT
  728. SUM(rml.amount_total_due_currency)
  729. AS final_amount_total_due_currency
  730. FROM
  731. report_open_items_move_line rml
  732. WHERE
  733. rml.report_partner_id = report_open_items_partner.id
  734. )
  735. WHERE
  736. id IN
  737. (
  738. SELECT
  739. rp.id
  740. FROM
  741. report_open_items_account ra
  742. INNER JOIN
  743. report_open_items_partner rp
  744. ON ra.id = rp.report_account_id
  745. WHERE
  746. ra.report_id = %s AND ra.currency_id IS NOT NULL
  747. )
  748. """
  749. params_compute_partners_cur_due_cumul = (self.id,)
  750. self.env.cr.execute(query_compute_partners_cur_due_cumul,
  751. params_compute_partners_cur_due_cumul)
  752. def _compute_account_cumul(self):
  753. query_compute_accounts_residual_cumul = """
  754. UPDATE
  755. report_open_items_account
  756. SET
  757. final_amount_residual =
  758. (
  759. SELECT
  760. SUM(rp.final_amount_residual) AS final_amount_residual
  761. FROM
  762. report_open_items_partner rp
  763. WHERE
  764. rp.report_account_id = report_open_items_account.id
  765. )
  766. WHERE
  767. report_id = %s
  768. """
  769. params_compute_accounts_residual_cumul = (self.id,)
  770. self.env.cr.execute(query_compute_accounts_residual_cumul,
  771. params_compute_accounts_residual_cumul)
  772. query_compute_accounts_cur_residual_cumul = """
  773. UPDATE
  774. report_open_items_account
  775. SET
  776. final_amount_residual_currency =
  777. (
  778. SELECT
  779. SUM(rp.final_amount_residual_currency)
  780. AS final_amount_residual_currency
  781. FROM
  782. report_open_items_partner rp
  783. WHERE
  784. rp.report_account_id = report_open_items_account.id
  785. )
  786. WHERE
  787. report_id = %s
  788. """
  789. params_compute_accounts_cur_residual_cumul = (self.id,)
  790. self.env.cr.execute(query_compute_accounts_cur_residual_cumul,
  791. params_compute_accounts_cur_residual_cumul)
  792. query_compute_accounts_due_cumul = """
  793. UPDATE
  794. report_open_items_account
  795. SET
  796. final_amount_total_due =
  797. (
  798. SELECT
  799. SUM(rp.final_amount_total_due) AS final_amount_total_due
  800. FROM
  801. report_open_items_partner rp
  802. WHERE
  803. rp.report_account_id = report_open_items_account.id
  804. )
  805. WHERE
  806. report_id = %s
  807. """
  808. params_compute_accounts_due_cumul = (self.id,)
  809. self.env.cr.execute(query_compute_accounts_due_cumul,
  810. params_compute_accounts_due_cumul)
  811. query_compute_accounts_cur_due_cumul = """
  812. UPDATE
  813. report_open_items_account
  814. SET
  815. final_amount_total_due_currency =
  816. (
  817. SELECT
  818. SUM(rp.final_amount_total_due_currency)
  819. AS final_amount_total_due_currency
  820. FROM
  821. report_open_items_partner rp
  822. WHERE
  823. rp.report_account_id = report_open_items_account.id
  824. )
  825. WHERE
  826. report_id = %s
  827. """
  828. params_compute_accounts_cur_due_cumul = (self.id,)
  829. self.env.cr.execute(query_compute_accounts_cur_due_cumul,
  830. params_compute_accounts_cur_due_cumul)
  831. def _clean_partners_and_accounts(self,
  832. only_delete_account_balance_at_0=False):
  833. """ Delete empty data for
  834. report_open_items_partner and report_open_items_account.
  835. The "only_delete_account_balance_at_0" value is used
  836. to delete also the data with cumulative amounts at 0.
  837. """
  838. query_clean_partners = """
  839. DELETE FROM
  840. report_open_items_partner
  841. WHERE
  842. id IN
  843. (
  844. SELECT
  845. DISTINCT rp.id
  846. FROM
  847. report_open_items_account ra
  848. INNER JOIN
  849. report_open_items_partner rp
  850. ON ra.id = rp.report_account_id
  851. LEFT JOIN
  852. report_open_items_move_line rml
  853. ON rp.id = rml.report_partner_id
  854. WHERE
  855. ra.report_id = %s
  856. """
  857. if not only_delete_account_balance_at_0:
  858. query_clean_partners += """
  859. AND rml.id IS NULL
  860. """
  861. elif only_delete_account_balance_at_0:
  862. query_clean_partners += """
  863. AND (
  864. rp.final_amount_residual IS NULL
  865. OR rp.final_amount_residual = 0
  866. )
  867. """
  868. query_clean_partners += """
  869. )
  870. """
  871. params_clean_partners = (self.id,)
  872. self.env.cr.execute(query_clean_partners, params_clean_partners)
  873. query_clean_accounts = """
  874. DELETE FROM
  875. report_open_items_account
  876. WHERE
  877. id IN
  878. (
  879. SELECT
  880. DISTINCT ra.id
  881. FROM
  882. report_open_items_account ra
  883. LEFT JOIN
  884. report_open_items_partner rp
  885. ON ra.id = rp.report_account_id
  886. WHERE
  887. ra.report_id = %s
  888. """
  889. if not only_delete_account_balance_at_0:
  890. query_clean_accounts += """
  891. AND rp.id IS NULL
  892. """
  893. elif only_delete_account_balance_at_0:
  894. query_clean_accounts += """
  895. AND (
  896. ra.final_amount_residual IS NULL
  897. OR ra.final_amount_residual = 0
  898. )
  899. """
  900. query_clean_accounts += """
  901. )
  902. """
  903. params_clean_accounts = (self.id,)
  904. self.env.cr.execute(query_clean_accounts, params_clean_accounts)