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.

126 lines
5.5 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. from collections import defaultdict
  6. class ActivityStatement(models.AbstractModel):
  7. """Model of Activity Statement"""
  8. _inherit = 'statement.common'
  9. _name = 'report.partner_statement.activity_statement'
  10. def _initial_balance_sql_q1(self, partners, date_start, account_type):
  11. return str(self._cr.mogrify("""
  12. SELECT l.partner_id, l.currency_id, l.company_id,
  13. CASE WHEN l.currency_id is not null AND l.amount_currency > 0.0
  14. THEN sum(l.amount_currency)
  15. ELSE sum(l.debit)
  16. END as debit,
  17. CASE WHEN l.currency_id is not null AND l.amount_currency < 0.0
  18. THEN sum(l.amount_currency * (-1))
  19. ELSE sum(l.credit)
  20. END as credit
  21. FROM account_move_line l
  22. JOIN account_account_type at ON (at.id = l.user_type_id)
  23. JOIN account_move m ON (l.move_id = m.id)
  24. WHERE l.partner_id IN %(partners)s AND at.type = %(account_type)s
  25. AND l.date < %(date_start)s AND not l.blocked
  26. GROUP BY l.partner_id, l.currency_id, l.amount_currency,
  27. l.company_id
  28. """, locals()), "utf-8")
  29. def _initial_balance_sql_q2(self, company_id):
  30. return str(self._cr.mogrify("""
  31. SELECT Q1.partner_id, debit-credit AS balance,
  32. COALESCE(Q1.currency_id, c.currency_id) AS currency_id
  33. FROM Q1
  34. JOIN res_company c ON (c.id = Q1.company_id)
  35. WHERE c.id = %(company_id)s
  36. """, locals()), "utf-8")
  37. def _get_account_initial_balance(self, company_id, partner_ids,
  38. date_start, account_type):
  39. balance_start = defaultdict(list)
  40. partners = tuple(partner_ids)
  41. # pylint: disable=E8103
  42. self.env.cr.execute("""WITH Q1 AS (%s), Q2 AS (%s)
  43. SELECT partner_id, currency_id, balance
  44. FROM Q2""" % (self._initial_balance_sql_q1(partners, date_start,
  45. account_type),
  46. self._initial_balance_sql_q2(company_id)))
  47. for row in self.env.cr.dictfetchall():
  48. balance_start[row.pop('partner_id')].append(row)
  49. return balance_start
  50. def _display_lines_sql_q1(self, partners, date_start, date_end,
  51. account_type):
  52. return str(self._cr.mogrify("""
  53. SELECT m.name AS move_id, l.partner_id, l.date, l.name,
  54. l.ref, l.blocked, l.currency_id, l.company_id,
  55. CASE WHEN (l.currency_id is not null AND l.amount_currency > 0.0)
  56. THEN sum(l.amount_currency)
  57. ELSE sum(l.debit)
  58. END as debit,
  59. CASE WHEN (l.currency_id is not null AND l.amount_currency < 0.0)
  60. THEN sum(l.amount_currency * (-1))
  61. ELSE sum(l.credit)
  62. END as credit,
  63. CASE WHEN l.date_maturity is null
  64. THEN l.date
  65. ELSE l.date_maturity
  66. END as date_maturity
  67. FROM account_move_line l
  68. JOIN account_account_type at ON (at.id = l.user_type_id)
  69. JOIN account_move m ON (l.move_id = m.id)
  70. WHERE l.partner_id IN %(partners)s
  71. AND at.type = %(account_type)s
  72. AND %(date_start)s <= l.date
  73. AND l.date <= %(date_end)s
  74. GROUP BY l.partner_id, m.name, l.date, l.date_maturity, l.name,
  75. l.ref, l.blocked, l.currency_id,
  76. l.amount_currency, l.company_id
  77. """, locals()), "utf-8")
  78. def _display_lines_sql_q2(self, company_id):
  79. return str(self._cr.mogrify("""
  80. SELECT Q1.partner_id, Q1.move_id, Q1.date, Q1.date_maturity,
  81. Q1.name, Q1.ref, Q1.debit, Q1.credit,
  82. Q1.debit-Q1.credit as amount, Q1.blocked,
  83. COALESCE(Q1.currency_id, c.currency_id) AS currency_id
  84. FROM Q1
  85. JOIN res_company c ON (c.id = Q1.company_id)
  86. WHERE c.id = %(company_id)s
  87. """, locals()), "utf-8")
  88. def _get_account_display_lines(self, company_id, partner_ids, date_start,
  89. date_end, account_type):
  90. res = dict(map(lambda x: (x, []), partner_ids))
  91. partners = tuple(partner_ids)
  92. # pylint: disable=E8103
  93. self.env.cr.execute("""
  94. WITH Q1 AS (%s),
  95. Q2 AS (%s)
  96. SELECT partner_id, move_id, date, date_maturity, name, ref, debit,
  97. credit, amount, blocked, currency_id
  98. FROM Q2
  99. ORDER BY date, date_maturity, move_id""" % (
  100. self._display_lines_sql_q1(partners, date_start, date_end,
  101. account_type),
  102. self._display_lines_sql_q2(company_id)))
  103. for row in self.env.cr.dictfetchall():
  104. res[row.pop('partner_id')].append(row)
  105. return res
  106. @api.multi
  107. def _get_report_values(self, docids, data):
  108. if not data:
  109. wiz = self.env["activity.statement.wizard"].with_context(
  110. active_ids=docids, model="res.partner"
  111. )
  112. data = wiz.create({})._prepare_statement()
  113. data['amount_field'] = 'amount'
  114. return super()._get_report_values(docids, data)