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.

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