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

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