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.

921 lines
28 KiB

5 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. 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_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. # pylint: disable=sql-injection
  599. where_condition_partner_by_account = """
  600. WHERE
  601. id IN
  602. (
  603. SELECT
  604. rp.id
  605. FROM
  606. report_open_items_account ra
  607. INNER JOIN
  608. report_open_items_partner rp
  609. ON ra.id = rp.report_account_id
  610. WHERE
  611. ra.report_id = %s
  612. )"""
  613. query_computer_partner_residual_cumul = """
  614. UPDATE
  615. report_open_items_partner
  616. SET
  617. final_amount_residual =
  618. (
  619. SELECT
  620. SUM(rml.amount_residual) AS final_amount_residual
  621. FROM
  622. report_open_items_move_line rml
  623. WHERE
  624. rml.report_partner_id = report_open_items_partner.id
  625. )
  626. """ + where_condition_partner_by_account
  627. params_compute_partners_residual_cumul = (self.id,)
  628. self.env.cr.execute(query_computer_partner_residual_cumul,
  629. params_compute_partners_residual_cumul)
  630. query_compute_partners_due_cumul = """
  631. UPDATE
  632. report_open_items_partner
  633. SET
  634. final_amount_total_due =
  635. (
  636. SELECT
  637. SUM(rml.amount_total_due) AS final_amount_total_due
  638. FROM
  639. report_open_items_move_line rml
  640. WHERE
  641. rml.report_partner_id = report_open_items_partner.id
  642. )
  643. """ + where_condition_partner_by_account
  644. params_compute_partner_due_cumul = (self.id,)
  645. self.env.cr.execute(query_compute_partners_due_cumul,
  646. params_compute_partner_due_cumul)
  647. # Manage currency in partner
  648. where_condition_partner_by_account_cur = """
  649. WHERE
  650. id IN
  651. (
  652. SELECT
  653. rp.id
  654. FROM
  655. report_open_items_account ra
  656. INNER JOIN
  657. report_open_items_partner rp
  658. ON ra.id = rp.report_account_id
  659. WHERE
  660. ra.report_id = %s AND ra.currency_id IS NOT NULL
  661. )
  662. """
  663. query_compute_partners_cur_id_cumul = """
  664. UPDATE
  665. report_open_items_partner
  666. SET
  667. currency_id =
  668. (
  669. SELECT
  670. MAX(currency_id) as currency_id
  671. FROM
  672. report_open_items_move_line rml
  673. WHERE
  674. rml.report_partner_id = report_open_items_partner.id
  675. )
  676. """ + where_condition_partner_by_account_cur
  677. params_compute_partners_cur_id_cumul = (self.id,)
  678. self.env.cr.execute(query_compute_partners_cur_id_cumul,
  679. params_compute_partners_cur_id_cumul)
  680. query_compute_partners_cur_residual_cumul = """
  681. UPDATE
  682. report_open_items_partner
  683. SET
  684. final_amount_residual_currency =
  685. (
  686. SELECT
  687. SUM(rml.amount_residual_currency)
  688. AS final_amount_residual_currency
  689. FROM
  690. report_open_items_move_line rml
  691. WHERE
  692. rml.report_partner_id = report_open_items_partner.id
  693. )
  694. """ + where_condition_partner_by_account_cur
  695. params_compute_partners_cur_residual_cumul = (self.id,)
  696. self.env.cr.execute(query_compute_partners_cur_residual_cumul,
  697. params_compute_partners_cur_residual_cumul)
  698. query_compute_partners_cur_due_cumul = """
  699. UPDATE
  700. report_open_items_partner
  701. SET
  702. final_amount_total_due_currency =
  703. (
  704. SELECT
  705. SUM(rml.amount_total_due_currency)
  706. AS final_amount_total_due_currency
  707. FROM
  708. report_open_items_move_line rml
  709. WHERE
  710. rml.report_partner_id = report_open_items_partner.id
  711. )
  712. """ + where_condition_partner_by_account_cur
  713. params_compute_partners_cur_due_cumul = (self.id,)
  714. self.env.cr.execute(query_compute_partners_cur_due_cumul,
  715. params_compute_partners_cur_due_cumul)
  716. def _compute_account_cumul(self):
  717. query_compute_accounts_residual_cumul = """
  718. UPDATE
  719. report_open_items_account
  720. SET
  721. final_amount_residual =
  722. (
  723. SELECT
  724. SUM(rp.final_amount_residual) AS final_amount_residual
  725. FROM
  726. report_open_items_partner rp
  727. WHERE
  728. rp.report_account_id = report_open_items_account.id
  729. )
  730. WHERE
  731. report_id = %s
  732. """
  733. params_compute_accounts_residual_cumul = (self.id,)
  734. self.env.cr.execute(query_compute_accounts_residual_cumul,
  735. params_compute_accounts_residual_cumul)
  736. query_compute_accounts_cur_residual_cumul = """
  737. UPDATE
  738. report_open_items_account
  739. SET
  740. final_amount_residual_currency =
  741. (
  742. SELECT
  743. SUM(rp.final_amount_residual_currency)
  744. AS final_amount_residual_currency
  745. FROM
  746. report_open_items_partner rp
  747. WHERE
  748. rp.report_account_id = report_open_items_account.id
  749. )
  750. WHERE
  751. report_id = %s
  752. """
  753. params_compute_accounts_cur_residual_cumul = (self.id,)
  754. self.env.cr.execute(query_compute_accounts_cur_residual_cumul,
  755. params_compute_accounts_cur_residual_cumul)
  756. query_compute_accounts_due_cumul = """
  757. UPDATE
  758. report_open_items_account
  759. SET
  760. final_amount_total_due =
  761. (
  762. SELECT
  763. SUM(rp.final_amount_total_due) AS final_amount_total_due
  764. FROM
  765. report_open_items_partner rp
  766. WHERE
  767. rp.report_account_id = report_open_items_account.id
  768. )
  769. WHERE
  770. report_id = %s
  771. """
  772. params_compute_accounts_due_cumul = (self.id,)
  773. self.env.cr.execute(query_compute_accounts_due_cumul,
  774. params_compute_accounts_due_cumul)
  775. query_compute_accounts_cur_due_cumul = """
  776. UPDATE
  777. report_open_items_account
  778. SET
  779. final_amount_total_due_currency =
  780. (
  781. SELECT
  782. SUM(rp.final_amount_total_due_currency)
  783. AS final_amount_total_due_currency
  784. FROM
  785. report_open_items_partner rp
  786. WHERE
  787. rp.report_account_id = report_open_items_account.id
  788. )
  789. WHERE
  790. report_id = %s
  791. """
  792. params_compute_accounts_cur_due_cumul = (self.id,)
  793. self.env.cr.execute(query_compute_accounts_cur_due_cumul,
  794. params_compute_accounts_cur_due_cumul)
  795. def _clean_partners_and_accounts(self,
  796. only_delete_account_balance_at_0=False):
  797. """ Delete empty data for
  798. report_open_items_partner and report_open_items_account.
  799. The "only_delete_account_balance_at_0" value is used
  800. to delete also the data with cumulative amounts at 0.
  801. """
  802. query_clean_partners = """
  803. DELETE FROM
  804. report_open_items_partner
  805. WHERE
  806. id IN
  807. (
  808. SELECT
  809. DISTINCT rp.id
  810. FROM
  811. report_open_items_account ra
  812. INNER JOIN
  813. report_open_items_partner rp
  814. ON ra.id = rp.report_account_id
  815. LEFT JOIN
  816. report_open_items_move_line rml
  817. ON rp.id = rml.report_partner_id
  818. WHERE
  819. ra.report_id = %s
  820. """
  821. if not only_delete_account_balance_at_0:
  822. query_clean_partners += """
  823. AND rml.id IS NULL
  824. """
  825. elif only_delete_account_balance_at_0:
  826. query_clean_partners += """
  827. AND (
  828. rp.final_amount_residual IS NULL
  829. OR rp.final_amount_residual = 0
  830. )
  831. """
  832. query_clean_partners += """
  833. )
  834. """
  835. params_clean_partners = (self.id,)
  836. self.env.cr.execute(query_clean_partners, params_clean_partners)
  837. query_clean_accounts = """
  838. DELETE FROM
  839. report_open_items_account
  840. WHERE
  841. id IN
  842. (
  843. SELECT
  844. DISTINCT ra.id
  845. FROM
  846. report_open_items_account ra
  847. LEFT JOIN
  848. report_open_items_partner rp
  849. ON ra.id = rp.report_account_id
  850. WHERE
  851. ra.report_id = %s
  852. """
  853. if not only_delete_account_balance_at_0:
  854. query_clean_accounts += """
  855. AND rp.id IS NULL
  856. """
  857. elif only_delete_account_balance_at_0:
  858. query_clean_accounts += """
  859. AND (
  860. ra.final_amount_residual IS NULL
  861. OR ra.final_amount_residual = 0
  862. )
  863. """
  864. query_clean_accounts += """
  865. )
  866. """
  867. params_clean_accounts = (self.id,)
  868. self.env.cr.execute(query_clean_accounts, params_clean_accounts)