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.

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