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.

184 lines
6.5 KiB

6 years ago
  1. # Copyright 2018 ForgeFlow, S.L. (https://www.forgeflow.com)
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  3. from collections import defaultdict
  4. from odoo import api, models
  5. class ActivityStatement(models.AbstractModel):
  6. """Model of Activity Statement"""
  7. _inherit = "statement.common"
  8. _name = "report.partner_statement.activity_statement"
  9. _description = "Partner Activity Statement"
  10. def _initial_balance_sql_q1(self, partners, date_start, account_type):
  11. return str(
  12. self._cr.mogrify(
  13. """
  14. SELECT l.partner_id, l.currency_id, l.company_id,
  15. CASE WHEN l.currency_id is not null AND l.amount_currency > 0.0
  16. THEN sum(l.amount_currency)
  17. ELSE sum(l.debit)
  18. END as debit,
  19. CASE WHEN l.currency_id is not null AND l.amount_currency < 0.0
  20. THEN sum(l.amount_currency * (-1))
  21. ELSE sum(l.credit)
  22. END as credit
  23. FROM account_move_line l
  24. JOIN account_move m ON (l.move_id = m.id)
  25. WHERE l.partner_id IN %(partners)s
  26. AND l.account_internal_type = %(account_type)s
  27. AND l.date < %(date_start)s AND not l.blocked
  28. AND m.state IN ('posted')
  29. GROUP BY l.partner_id, l.currency_id, l.amount_currency,
  30. l.company_id
  31. """,
  32. locals(),
  33. ),
  34. "utf-8",
  35. )
  36. def _initial_balance_sql_q2(self, company_id):
  37. return str(
  38. self._cr.mogrify(
  39. """
  40. SELECT Q1.partner_id, debit-credit AS balance,
  41. COALESCE(Q1.currency_id, c.currency_id) AS currency_id
  42. FROM Q1
  43. JOIN res_company c ON (c.id = Q1.company_id)
  44. WHERE c.id = %(company_id)s
  45. """,
  46. locals(),
  47. ),
  48. "utf-8",
  49. )
  50. def _get_account_initial_balance(
  51. self, company_id, partner_ids, date_start, account_type
  52. ):
  53. balance_start = defaultdict(list)
  54. partners = tuple(partner_ids)
  55. # pylint: disable=E8103
  56. self.env.cr.execute(
  57. """WITH Q1 AS (%s), Q2 AS (%s)
  58. SELECT partner_id, currency_id, balance
  59. FROM Q2"""
  60. % (
  61. self._initial_balance_sql_q1(partners, date_start, account_type),
  62. self._initial_balance_sql_q2(company_id),
  63. )
  64. )
  65. for row in self.env.cr.dictfetchall():
  66. balance_start[row.pop("partner_id")].append(row)
  67. return balance_start
  68. def _display_lines_sql_q1(self, partners, date_start, date_end, account_type):
  69. return str(
  70. self._cr.mogrify(
  71. """
  72. SELECT m.name AS move_id, l.partner_id, l.date,
  73. CASE WHEN (aj.type IN ('sale', 'purchase'))
  74. THEN l.name
  75. ELSE '/'
  76. END as name,
  77. CASE WHEN (aj.type IN ('sale', 'purchase'))
  78. THEN l.ref
  79. WHEN (aj.type in ('bank', 'cash'))
  80. THEN 'Payment'
  81. ELSE ''
  82. END as ref,
  83. l.blocked, l.currency_id, l.company_id,
  84. CASE WHEN (l.currency_id is not null AND l.amount_currency > 0.0)
  85. THEN sum(l.amount_currency)
  86. ELSE sum(l.debit)
  87. END as debit,
  88. CASE WHEN (l.currency_id is not null AND l.amount_currency < 0.0)
  89. THEN sum(l.amount_currency * (-1))
  90. ELSE sum(l.credit)
  91. END as credit,
  92. CASE WHEN l.date_maturity is null
  93. THEN l.date
  94. ELSE l.date_maturity
  95. END as date_maturity
  96. FROM account_move_line l
  97. JOIN account_move m ON (l.move_id = m.id)
  98. JOIN account_journal aj ON (l.journal_id = aj.id)
  99. WHERE l.partner_id IN %(partners)s
  100. AND l.account_internal_type = %(account_type)s
  101. AND %(date_start)s <= l.date
  102. AND l.date <= %(date_end)s
  103. AND m.state IN ('posted')
  104. GROUP BY l.partner_id, m.name, l.date, l.date_maturity,
  105. CASE WHEN (aj.type IN ('sale', 'purchase'))
  106. THEN l.name
  107. ELSE '/'
  108. END,
  109. CASE WHEN (aj.type IN ('sale', 'purchase'))
  110. THEN l.ref
  111. WHEN (aj.type in ('bank', 'cash'))
  112. THEN 'Payment'
  113. ELSE ''
  114. END,
  115. l.blocked, l.currency_id, l.amount_currency, l.company_id
  116. """,
  117. locals(),
  118. ),
  119. "utf-8",
  120. )
  121. def _display_lines_sql_q2(self, company_id):
  122. return str(
  123. self._cr.mogrify(
  124. """
  125. SELECT Q1.partner_id, Q1.move_id, Q1.date, Q1.date_maturity,
  126. Q1.name, Q1.ref, Q1.debit, Q1.credit,
  127. Q1.debit-Q1.credit as amount, Q1.blocked,
  128. COALESCE(Q1.currency_id, c.currency_id) AS currency_id
  129. FROM Q1
  130. JOIN res_company c ON (c.id = Q1.company_id)
  131. WHERE c.id = %(company_id)s
  132. """,
  133. locals(),
  134. ),
  135. "utf-8",
  136. )
  137. def _get_account_display_lines(
  138. self, company_id, partner_ids, date_start, date_end, account_type
  139. ):
  140. res = dict(map(lambda x: (x, []), partner_ids))
  141. partners = tuple(partner_ids)
  142. # pylint: disable=E8103
  143. self.env.cr.execute(
  144. """
  145. WITH Q1 AS (%s),
  146. Q2 AS (%s)
  147. SELECT partner_id, move_id, date, date_maturity, name, ref, debit,
  148. credit, amount, blocked, currency_id
  149. FROM Q2
  150. ORDER BY date, date_maturity, move_id"""
  151. % (
  152. self._display_lines_sql_q1(
  153. partners, date_start, date_end, account_type
  154. ),
  155. self._display_lines_sql_q2(company_id),
  156. )
  157. )
  158. for row in self.env.cr.dictfetchall():
  159. res[row.pop("partner_id")].append(row)
  160. return res
  161. @api.model
  162. def _get_report_values(self, docids, data=None):
  163. if not data:
  164. data = {}
  165. if "company_id" not in data:
  166. wiz = self.env["activity.statement.wizard"].with_context(
  167. active_ids=docids, model="res.partner"
  168. )
  169. data.update(wiz.create({})._prepare_statement())
  170. data["amount_field"] = "amount"
  171. return super()._get_report_values(docids, data)