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.

146 lines
6.1 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,
  54. CASE WHEN (aj.type IN ('sale', 'purchase'))
  55. THEN l.name
  56. ELSE '/'
  57. END as name,
  58. CASE WHEN (aj.type IN ('sale', 'purchase'))
  59. THEN l.ref
  60. WHEN (aj.type in ('bank', 'cash'))
  61. THEN 'Payment'
  62. ELSE ''
  63. END as ref,
  64. l.blocked, l.currency_id, l.company_id,
  65. CASE WHEN (l.currency_id is not null AND l.amount_currency > 0.0)
  66. THEN sum(l.amount_currency)
  67. ELSE sum(l.debit)
  68. END as debit,
  69. CASE WHEN (l.currency_id is not null AND l.amount_currency < 0.0)
  70. THEN sum(l.amount_currency * (-1))
  71. ELSE sum(l.credit)
  72. END as credit,
  73. CASE WHEN l.date_maturity is null
  74. THEN l.date
  75. ELSE l.date_maturity
  76. END as date_maturity
  77. FROM account_move_line l
  78. JOIN account_account_type at ON (at.id = l.user_type_id)
  79. JOIN account_move m ON (l.move_id = m.id)
  80. JOIN account_journal aj ON (l.journal_id = aj.id)
  81. WHERE l.partner_id IN %(partners)s
  82. AND at.type = %(account_type)s
  83. AND %(date_start)s <= l.date
  84. AND l.date <= %(date_end)s
  85. GROUP BY l.partner_id, m.name, l.date, l.date_maturity,
  86. CASE WHEN (aj.type IN ('sale', 'purchase'))
  87. THEN l.name
  88. ELSE '/'
  89. END,
  90. CASE WHEN (aj.type IN ('sale', 'purchase'))
  91. THEN l.ref
  92. WHEN (aj.type in ('bank', 'cash'))
  93. THEN 'Payment'
  94. ELSE ''
  95. END,
  96. l.blocked, l.currency_id, l.amount_currency, l.company_id
  97. """, locals()), "utf-8")
  98. def _display_lines_sql_q2(self, company_id):
  99. return str(self._cr.mogrify("""
  100. SELECT Q1.partner_id, Q1.move_id, Q1.date, Q1.date_maturity,
  101. Q1.name, Q1.ref, Q1.debit, Q1.credit,
  102. Q1.debit-Q1.credit as amount, Q1.blocked,
  103. COALESCE(Q1.currency_id, c.currency_id) AS currency_id
  104. FROM Q1
  105. JOIN res_company c ON (c.id = Q1.company_id)
  106. WHERE c.id = %(company_id)s
  107. """, locals()), "utf-8")
  108. def _get_account_display_lines(self, company_id, partner_ids, date_start,
  109. date_end, account_type):
  110. res = dict(map(lambda x: (x, []), partner_ids))
  111. partners = tuple(partner_ids)
  112. # pylint: disable=E8103
  113. self.env.cr.execute("""
  114. WITH Q1 AS (%s),
  115. Q2 AS (%s)
  116. SELECT partner_id, move_id, date, date_maturity, name, ref, debit,
  117. credit, amount, blocked, currency_id
  118. FROM Q2
  119. ORDER BY date, date_maturity, move_id""" % (
  120. self._display_lines_sql_q1(partners, date_start, date_end,
  121. account_type),
  122. self._display_lines_sql_q2(company_id)))
  123. for row in self.env.cr.dictfetchall():
  124. res[row.pop('partner_id')].append(row)
  125. return res
  126. @api.multi
  127. def _get_report_values(self, docids, data):
  128. if not data:
  129. wiz = self.env["activity.statement.wizard"].with_context(
  130. active_ids=docids, model="res.partner"
  131. )
  132. data = wiz.create({})._prepare_statement()
  133. data['amount_field'] = 'amount'
  134. return super()._get_report_values(docids, data)