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.

914 lines
28 KiB

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