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.

936 lines
29 KiB

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