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.

924 lines
28 KiB

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