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.

153 lines
6.4 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 Q1.partner_id, Q1.currency_id, Q1.move_id,
  91. Q1.date, Q1.date_maturity, Q1.debit, Q1.credit,
  92. Q1.name, Q1.ref, Q1.blocked, Q1.company_id,
  93. CASE WHEN Q1.currency_id is not null
  94. THEN open_amount_currency
  95. ELSE open_amount
  96. END as open_amount
  97. FROM Q1
  98. """, locals()
  99. ),
  100. "utf-8"
  101. )
  102. def _display_lines_sql_q3(self, company_id):
  103. return str(self._cr.mogrify("""
  104. SELECT Q2.partner_id, Q2.move_id, Q2.date, Q2.date_maturity,
  105. Q2.name, Q2.ref, Q2.debit, Q2.credit,
  106. Q2.debit-Q2.credit AS amount, blocked,
  107. COALESCE(Q2.currency_id, c.currency_id) AS currency_id,
  108. Q2.open_amount
  109. FROM Q2
  110. JOIN res_company c ON (c.id = Q2.company_id)
  111. WHERE c.id = %(company_id)s
  112. """, locals()), "utf-8"
  113. )
  114. def _get_account_display_lines(self, company_id, partner_ids, date_start,
  115. date_end, account_type):
  116. res = dict(map(lambda x: (x, []), partner_ids))
  117. partners = tuple(partner_ids)
  118. # pylint: disable=E8103
  119. self.env.cr.execute("""
  120. WITH Q0 as (%s),
  121. Q1 AS (%s),
  122. Q2 AS (%s),
  123. Q3 AS (%s)
  124. SELECT partner_id, currency_id, move_id, date, date_maturity, debit,
  125. credit, amount, open_amount, name, ref, blocked
  126. FROM Q3
  127. ORDER BY date, date_maturity, move_id""" % (
  128. self._display_lines_sql_q0(date_end),
  129. self._display_lines_sql_q1(partners, date_end, account_type),
  130. self._display_lines_sql_q2(),
  131. self._display_lines_sql_q3(company_id)))
  132. for row in self.env.cr.dictfetchall():
  133. res[row.pop('partner_id')].append(row)
  134. return res
  135. @api.multi
  136. def _get_report_values(self, docids, data):
  137. if not data:
  138. wiz = self.env["outstanding.statement.wizard"].with_context(
  139. active_ids=docids, model="res.partner"
  140. )
  141. data = wiz.create({})._prepare_statement()
  142. data['amount_field'] = 'open_amount'
  143. return super()._get_report_values(docids, data)