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.

190 lines
7.0 KiB

  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
  78. WHEN (aj.type IN ('sale', 'purchase')) AND l.name IS NOT NULL
  79. THEN l.ref
  80. WHEN aj.type IN ('sale', 'purchase') AND l.name IS NULL
  81. THEN m.ref
  82. WHEN (aj.type in ('bank', 'cash'))
  83. THEN 'Payment'
  84. ELSE ''
  85. END as ref,
  86. l.blocked, l.currency_id, l.company_id,
  87. CASE WHEN (l.currency_id is not null AND l.amount_currency > 0.0)
  88. THEN sum(l.amount_currency)
  89. ELSE sum(l.debit)
  90. END as debit,
  91. CASE WHEN (l.currency_id is not null AND l.amount_currency < 0.0)
  92. THEN sum(l.amount_currency * (-1))
  93. ELSE sum(l.credit)
  94. END as credit,
  95. CASE WHEN l.date_maturity is null
  96. THEN l.date
  97. ELSE l.date_maturity
  98. END as date_maturity
  99. FROM account_move_line l
  100. JOIN account_move m ON (l.move_id = m.id)
  101. JOIN account_journal aj ON (l.journal_id = aj.id)
  102. WHERE l.partner_id IN %(partners)s
  103. AND l.account_internal_type = %(account_type)s
  104. AND %(date_start)s <= l.date
  105. AND l.date <= %(date_end)s
  106. AND m.state IN ('posted')
  107. GROUP BY l.partner_id, m.name, l.date, l.date_maturity,
  108. CASE WHEN (aj.type IN ('sale', 'purchase'))
  109. THEN l.name
  110. ELSE '/'
  111. END,
  112. CASE
  113. WHEN (aj.type IN ('sale', 'purchase')) AND l.name IS NOT NULL
  114. THEN l.ref
  115. WHEN aj.type IN ('sale', 'purchase') AND l.name IS NULL
  116. THEN m.ref
  117. WHEN (aj.type in ('bank', 'cash'))
  118. THEN 'Payment'
  119. ELSE ''
  120. END,
  121. l.blocked, l.currency_id, l.amount_currency, l.company_id
  122. """,
  123. locals(),
  124. ),
  125. "utf-8",
  126. )
  127. def _display_lines_sql_q2(self, company_id):
  128. return str(
  129. self._cr.mogrify(
  130. """
  131. SELECT Q1.partner_id, Q1.move_id, Q1.date, Q1.date_maturity,
  132. Q1.name, Q1.ref, Q1.debit, Q1.credit,
  133. Q1.debit-Q1.credit as amount, Q1.blocked,
  134. COALESCE(Q1.currency_id, c.currency_id) AS currency_id
  135. FROM Q1
  136. JOIN res_company c ON (c.id = Q1.company_id)
  137. WHERE c.id = %(company_id)s
  138. """,
  139. locals(),
  140. ),
  141. "utf-8",
  142. )
  143. def _get_account_display_lines(
  144. self, company_id, partner_ids, date_start, date_end, account_type
  145. ):
  146. res = dict(map(lambda x: (x, []), partner_ids))
  147. partners = tuple(partner_ids)
  148. # pylint: disable=E8103
  149. self.env.cr.execute(
  150. """
  151. WITH Q1 AS (%s),
  152. Q2 AS (%s)
  153. SELECT partner_id, move_id, date, date_maturity, name, ref, debit,
  154. credit, amount, blocked, currency_id
  155. FROM Q2
  156. ORDER BY date, date_maturity, move_id"""
  157. % (
  158. self._display_lines_sql_q1(
  159. partners, date_start, date_end, account_type
  160. ),
  161. self._display_lines_sql_q2(company_id),
  162. )
  163. )
  164. for row in self.env.cr.dictfetchall():
  165. res[row.pop("partner_id")].append(row)
  166. return res
  167. @api.model
  168. def _get_report_values(self, docids, data=None):
  169. if not data:
  170. data = {}
  171. if "company_id" not in data:
  172. wiz = self.env["activity.statement.wizard"].with_context(
  173. active_ids=docids, model="res.partner"
  174. )
  175. data.update(wiz.create({})._prepare_statement())
  176. data["amount_field"] = "amount"
  177. return super()._get_report_values(docids, data)