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
4.2 KiB

  1. # Copyright 2019 ADHOC SA
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import api, fields, models, tools
  4. from psycopg2.extensions import AsIs
  5. class MisCashFlow(models.Model):
  6. _name = 'mis.cash_flow'
  7. _description = 'MIS Cash Flow'
  8. _auto = False
  9. line_type = fields.Selection(
  10. [('forecast_line', 'Forecast Line'), ('move_line', 'Journal Item')],
  11. index=True,
  12. readonly=True,
  13. )
  14. name = fields.Char(
  15. readonly=True,
  16. )
  17. account_id = fields.Many2one(
  18. comodel_name='account.account',
  19. string='Account',
  20. auto_join=True,
  21. index=True,
  22. readonly=True,
  23. )
  24. partner_id = fields.Many2one(
  25. comodel_name='res.partner',
  26. string='Partner',
  27. readonly=True,
  28. )
  29. move_line_id = fields.Many2one(
  30. comodel_name='account.move.line',
  31. string='Journal Item',
  32. auto_join=True,
  33. readonly=True,
  34. )
  35. company_id = fields.Many2one(
  36. comodel_name='res.company',
  37. string='Company',
  38. auto_join=True,
  39. readonly=True,
  40. index=True,
  41. )
  42. credit = fields.Float(
  43. readonly=True,
  44. )
  45. debit = fields.Float(
  46. readonly=True,
  47. )
  48. date = fields.Date(
  49. readonly=True,
  50. index=True,
  51. )
  52. reconciled = fields.Boolean(
  53. readonly=True,
  54. )
  55. full_reconcile_id = fields.Many2one(
  56. 'account.full.reconcile',
  57. string="Matching Number",
  58. readonly=True,
  59. index=True,
  60. )
  61. user_type_id = fields.Many2one(
  62. 'account.account.type',
  63. auto_join=True,
  64. readonly=True,
  65. index=True,
  66. )
  67. @api.model_cr
  68. def init(self):
  69. account_type_receivable = self.env.ref(
  70. 'account.data_account_type_receivable')
  71. query = """
  72. SELECT
  73. -- we use negative id to avoid duplicates and we don't use
  74. -- ROW_NUMBER() because the performance was very poor
  75. -aml.id as id,
  76. CAST('move_line' AS varchar) as line_type,
  77. aml.id as move_line_id,
  78. aml.account_id as account_id,
  79. CASE
  80. WHEN aml.amount_residual > 0
  81. THEN aml.amount_residual
  82. ELSE 0.0
  83. END AS debit,
  84. CASE
  85. WHEN aml.amount_residual < 0
  86. THEN -aml.amount_residual
  87. ELSE 0.0
  88. END AS credit,
  89. aml.reconciled as reconciled,
  90. aml.full_reconcile_id as full_reconcile_id,
  91. aml.partner_id as partner_id,
  92. aml.company_id as company_id,
  93. aml.user_type_id as user_type_id,
  94. aml.name as name,
  95. aml.date_maturity as date
  96. FROM account_move_line as aml
  97. UNION ALL
  98. SELECT
  99. fl.id as id,
  100. CAST('forecast_line' AS varchar) as line_type,
  101. Null as move_line_id,
  102. fl.account_id as account_id,
  103. CASE
  104. WHEN fl.balance > 0
  105. THEN fl.balance
  106. ELSE 0.0
  107. END AS debit,
  108. CASE
  109. WHEN fl.balance < 0
  110. THEN -fl.balance
  111. ELSE 0.0
  112. END AS credit,
  113. Null as reconciled,
  114. Null as full_reconcile_id,
  115. fl.partner_id as partner_id,
  116. fl.company_id as company_id,
  117. %i as user_type_id,
  118. fl.name as name,
  119. fl.date as date
  120. FROM mis_cash_flow_forecast_line as fl
  121. """ % account_type_receivable.id
  122. tools.drop_view_if_exists(self.env.cr, self._table)
  123. self._cr.execute(
  124. 'CREATE OR REPLACE VIEW %s AS %s',
  125. (AsIs(self._table), AsIs(query))
  126. )
  127. @api.multi
  128. def action_open_related_line(self):
  129. self.ensure_one()
  130. if self.line_type == 'move_line':
  131. return self.move_line_id.get_formview_action()
  132. else:
  133. return self.env['mis.cash_flow.forecast_line'].browse(
  134. self.id).get_formview_action()