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.

150 lines
6.3 KiB

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