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.

127 lines
5.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_q1(self, partners, date_end, account_type):
  10. partners = tuple(partners)
  11. return str(self._cr.mogrify("""
  12. SELECT m.name AS move_id, l.partner_id, l.date, l.name,
  13. l.ref, l.blocked, l.currency_id, l.company_id,
  14. CASE WHEN (l.currency_id is not null AND l.amount_currency > 0.0)
  15. THEN avg(l.amount_currency)
  16. ELSE avg(l.debit)
  17. END as debit,
  18. CASE WHEN (l.currency_id is not null AND l.amount_currency < 0.0)
  19. THEN avg(l.amount_currency * (-1))
  20. ELSE avg(l.credit)
  21. END as credit,
  22. CASE WHEN l.balance > 0.0
  23. THEN l.balance - sum(coalesce(pd.amount, 0.0))
  24. ELSE l.balance + sum(coalesce(pc.amount, 0.0))
  25. END AS open_amount,
  26. CASE WHEN l.balance > 0.0
  27. THEN l.amount_currency - sum(coalesce(pd.amount_currency, 0.0))
  28. ELSE l.amount_currency + sum(coalesce(pc.amount_currency, 0.0))
  29. END AS open_amount_currency,
  30. CASE WHEN l.date_maturity is null
  31. THEN l.date
  32. ELSE l.date_maturity
  33. END as date_maturity
  34. FROM account_move_line l
  35. JOIN account_account_type at ON (at.id = l.user_type_id)
  36. JOIN account_move m ON (l.move_id = m.id)
  37. LEFT JOIN (SELECT pr.*
  38. FROM account_partial_reconcile pr
  39. INNER JOIN account_move_line l2
  40. ON pr.credit_move_id = l2.id
  41. WHERE l2.date <= %(date_end)s
  42. ) as pd ON pd.debit_move_id = l.id
  43. LEFT JOIN (SELECT pr.*
  44. FROM account_partial_reconcile pr
  45. INNER JOIN account_move_line l2
  46. ON pr.debit_move_id = l2.id
  47. WHERE l2.date <= %(date_end)s
  48. ) as pc ON pc.credit_move_id = l.id
  49. WHERE l.partner_id IN %(partners)s AND at.type = %(account_type)s
  50. AND (
  51. (pd.id IS NOT NULL AND
  52. pd.max_date <= %(date_end)s) OR
  53. (pc.id IS NOT NULL AND
  54. pc.max_date <= %(date_end)s) OR
  55. (pd.id IS NULL AND pc.id IS NULL)
  56. ) AND l.date <= %(date_end)s AND m.state IN ('posted')
  57. GROUP BY l.partner_id, m.name, l.date, l.date_maturity, l.name,
  58. l.ref, l.blocked, l.currency_id,
  59. l.balance, l.amount_currency, l.company_id
  60. """, locals()), "utf-8"
  61. )
  62. def _display_lines_sql_q2(self):
  63. return str(
  64. self._cr.mogrify("""
  65. SELECT Q1.partner_id, Q1.currency_id, Q1.move_id,
  66. Q1.date, Q1.date_maturity, Q1.debit, Q1.credit,
  67. Q1.name, Q1.ref, Q1.blocked, Q1.company_id,
  68. CASE WHEN Q1.currency_id is not null
  69. THEN Q1.open_amount_currency
  70. ELSE Q1.open_amount
  71. END as open_amount
  72. FROM Q1
  73. """, locals()
  74. ),
  75. "utf-8"
  76. )
  77. def _display_lines_sql_q3(self, company_id):
  78. return str(self._cr.mogrify("""
  79. SELECT Q2.partner_id, Q2.move_id, Q2.date, Q2.date_maturity,
  80. Q2.name, Q2.ref, Q2.debit, Q2.credit,
  81. Q2.debit-Q2.credit AS amount, blocked,
  82. COALESCE(Q2.currency_id, c.currency_id) AS currency_id,
  83. Q2.open_amount
  84. FROM Q2
  85. JOIN res_company c ON (c.id = Q2.company_id)
  86. WHERE c.id = %(company_id)s AND Q2.open_amount != 0.0
  87. """, locals()), "utf-8"
  88. )
  89. def _get_account_display_lines(self, company_id, partner_ids, date_start,
  90. date_end, account_type):
  91. res = dict(map(lambda x: (x, []), partner_ids))
  92. partners = tuple(partner_ids)
  93. # pylint: disable=E8103
  94. self.env.cr.execute("""
  95. WITH Q1 as (%s),
  96. Q2 AS (%s),
  97. Q3 AS (%s)
  98. SELECT partner_id, currency_id, move_id, date, date_maturity, debit,
  99. credit, amount, open_amount, name, ref, blocked
  100. FROM Q3
  101. ORDER BY date, date_maturity, move_id""" % (
  102. self._display_lines_sql_q1(partners, date_end, account_type),
  103. self._display_lines_sql_q2(),
  104. self._display_lines_sql_q3(company_id)))
  105. for row in self.env.cr.dictfetchall():
  106. res[row.pop('partner_id')].append(row)
  107. return res
  108. @api.multi
  109. def _get_report_values(self, docids, data):
  110. if not data:
  111. data = {}
  112. if 'company_id' not in data:
  113. wiz = self.env["outstanding.statement.wizard"].with_context(
  114. active_ids=docids, model="res.partner"
  115. )
  116. data.update(wiz.create({})._prepare_statement())
  117. data['amount_field'] = 'open_amount'
  118. return super()._get_report_values(docids, data)