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.

144 lines
5.6 KiB

  1. # Copyright 2018 ForgeFlow, S.L. (https://www.forgeflow.com)
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  3. from odoo import api, models
  4. class OutstandingStatement(models.AbstractModel):
  5. """Model of Outstanding Statement"""
  6. _inherit = "statement.common"
  7. _name = "report.partner_statement.outstanding_statement"
  8. _description = "Partner Outstanding Statement"
  9. def _display_lines_sql_q1(self, partners, date_end, account_type):
  10. partners = tuple(partners)
  11. return str(
  12. self._cr.mogrify(
  13. """
  14. SELECT m.name AS move_id, l.partner_id, l.date, l.name,
  15. l.ref, l.blocked, l.currency_id, l.company_id,
  16. CASE WHEN (l.currency_id is not null AND l.amount_currency > 0.0)
  17. THEN avg(l.amount_currency)
  18. ELSE avg(l.debit)
  19. END as debit,
  20. CASE WHEN (l.currency_id is not null AND l.amount_currency < 0.0)
  21. THEN avg(l.amount_currency * (-1))
  22. ELSE avg(l.credit)
  23. END as credit,
  24. CASE WHEN l.balance > 0.0
  25. THEN l.balance - sum(coalesce(pd.amount, 0.0))
  26. ELSE l.balance + sum(coalesce(pc.amount, 0.0))
  27. END AS open_amount,
  28. CASE WHEN l.balance > 0.0
  29. THEN l.amount_currency - sum(coalesce(pd.amount_currency, 0.0))
  30. ELSE l.amount_currency + sum(coalesce(pc.amount_currency, 0.0))
  31. END AS open_amount_currency,
  32. CASE WHEN l.date_maturity is null
  33. THEN l.date
  34. ELSE l.date_maturity
  35. END as date_maturity
  36. FROM account_move_line l
  37. JOIN account_move m ON (l.move_id = m.id)
  38. LEFT JOIN (SELECT pr.*
  39. FROM account_partial_reconcile pr
  40. INNER JOIN account_move_line l2
  41. ON pr.credit_move_id = l2.id
  42. WHERE l2.date <= %(date_end)s
  43. ) as pd ON pd.debit_move_id = l.id
  44. LEFT JOIN (SELECT pr.*
  45. FROM account_partial_reconcile pr
  46. INNER JOIN account_move_line l2
  47. ON pr.debit_move_id = l2.id
  48. WHERE l2.date <= %(date_end)s
  49. ) as pc ON pc.credit_move_id = l.id
  50. WHERE l.partner_id IN %(partners)s
  51. AND l.account_internal_type = %(account_type)s
  52. AND (
  53. (pd.id IS NOT NULL AND
  54. pd.max_date <= %(date_end)s) OR
  55. (pc.id IS NOT NULL AND
  56. pc.max_date <= %(date_end)s) OR
  57. (pd.id IS NULL AND pc.id IS NULL)
  58. ) AND l.date <= %(date_end)s AND m.state IN ('posted')
  59. GROUP BY l.partner_id, m.name, l.date, l.date_maturity, l.name,
  60. l.ref, l.blocked, l.currency_id,
  61. l.balance, l.amount_currency, l.company_id
  62. """,
  63. locals(),
  64. ),
  65. "utf-8",
  66. )
  67. def _display_lines_sql_q2(self):
  68. return str(
  69. self._cr.mogrify(
  70. """
  71. SELECT Q1.partner_id, Q1.currency_id, Q1.move_id,
  72. Q1.date, Q1.date_maturity, Q1.debit, Q1.credit,
  73. Q1.name, Q1.ref, Q1.blocked, Q1.company_id,
  74. CASE WHEN Q1.currency_id is not null
  75. THEN Q1.open_amount_currency
  76. ELSE Q1.open_amount
  77. END as open_amount
  78. FROM Q1
  79. """,
  80. locals(),
  81. ),
  82. "utf-8",
  83. )
  84. def _display_lines_sql_q3(self, company_id):
  85. return str(
  86. self._cr.mogrify(
  87. """
  88. SELECT Q2.partner_id, Q2.move_id, Q2.date, Q2.date_maturity,
  89. Q2.name, Q2.ref, Q2.debit, Q2.credit,
  90. Q2.debit-Q2.credit AS amount, blocked,
  91. COALESCE(Q2.currency_id, c.currency_id) AS currency_id,
  92. Q2.open_amount
  93. FROM Q2
  94. JOIN res_company c ON (c.id = Q2.company_id)
  95. WHERE c.id = %(company_id)s AND Q2.open_amount != 0.0
  96. """,
  97. locals(),
  98. ),
  99. "utf-8",
  100. )
  101. def _get_account_display_lines(
  102. self, company_id, partner_ids, date_start, date_end, account_type
  103. ):
  104. res = dict(map(lambda x: (x, []), partner_ids))
  105. partners = tuple(partner_ids)
  106. # pylint: disable=E8103
  107. self.env.cr.execute(
  108. """
  109. WITH Q1 as (%s),
  110. Q2 AS (%s),
  111. Q3 AS (%s)
  112. SELECT partner_id, currency_id, move_id, date, date_maturity, debit,
  113. credit, amount, open_amount, name, ref, blocked
  114. FROM Q3
  115. ORDER BY date, date_maturity, move_id"""
  116. % (
  117. self._display_lines_sql_q1(partners, date_end, account_type),
  118. self._display_lines_sql_q2(),
  119. self._display_lines_sql_q3(company_id),
  120. )
  121. )
  122. for row in self.env.cr.dictfetchall():
  123. res[row.pop("partner_id")].append(row)
  124. return res
  125. @api.model
  126. def _get_report_values(self, docids, data=None):
  127. if not data:
  128. data = {}
  129. if "company_id" not in data:
  130. wiz = self.env["outstanding.statement.wizard"].with_context(
  131. active_ids=docids, model="res.partner"
  132. )
  133. data.update(wiz.create({})._prepare_statement())
  134. data["amount_field"] = "open_amount"
  135. return super()._get_report_values(docids, data)