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.

133 lines
4.0 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. move_line_id = fields.Many2one(
  25. comodel_name='account.move.line',
  26. string='Journal Item',
  27. auto_join=True,
  28. readonly=True,
  29. )
  30. company_id = fields.Many2one(
  31. comodel_name='res.company',
  32. string='Company',
  33. auto_join=True,
  34. readonly=True,
  35. index=True,
  36. )
  37. credit = fields.Float(
  38. readonly=True,
  39. )
  40. debit = fields.Float(
  41. readonly=True,
  42. )
  43. date = fields.Date(
  44. readonly=True,
  45. index=True,
  46. )
  47. reconciled = fields.Boolean(
  48. readonly=True,
  49. )
  50. full_reconcile_id = fields.Many2one(
  51. 'account.full.reconcile',
  52. string="Matching Number",
  53. readonly=True,
  54. index=True,
  55. )
  56. user_type_id = fields.Many2one(
  57. 'account.account.type',
  58. auto_join=True,
  59. readonly=True,
  60. index=True,
  61. )
  62. @api.model_cr
  63. def init(self):
  64. account_type_receivable = self.env.ref(
  65. 'account.data_account_type_receivable')
  66. query = """
  67. SELECT
  68. -- we use negative id to avoid duplicates and we don't use
  69. -- ROW_NUMBER() because the performance was very poor
  70. -aml.id as id,
  71. CAST('move_line' AS varchar) as line_type,
  72. aml.id as move_line_id,
  73. aml.account_id as account_id,
  74. CASE
  75. WHEN aml.amount_residual > 0
  76. THEN aml.amount_residual
  77. ELSE 0.0
  78. END AS debit,
  79. CASE
  80. WHEN aml.amount_residual < 0
  81. THEN -aml.amount_residual
  82. ELSE 0.0
  83. END AS credit,
  84. aml.reconciled as reconciled,
  85. aml.full_reconcile_id as full_reconcile_id,
  86. aml.company_id as company_id,
  87. aml.user_type_id as user_type_id,
  88. aml.name as name,
  89. aml.date_maturity as date
  90. FROM account_move_line as aml
  91. UNION ALL
  92. SELECT
  93. fl.id as id,
  94. CAST('forecast_line' AS varchar) as line_type,
  95. Null as move_line_id,
  96. fl.account_id as account_id,
  97. CASE
  98. WHEN fl.balance > 0
  99. THEN fl.balance
  100. ELSE 0.0
  101. END AS debit,
  102. CASE
  103. WHEN fl.balance < 0
  104. THEN -fl.balance
  105. ELSE 0.0
  106. END AS credit,
  107. Null as reconciled,
  108. Null as full_reconcile_id,
  109. fl.company_id as company_id,
  110. %i as user_type_id,
  111. fl.name as name,
  112. fl.date as date
  113. FROM mis_cash_flow_forecast_line as fl
  114. """ % account_type_receivable.id
  115. tools.drop_view_if_exists(self.env.cr, self._table)
  116. self._cr.execute(
  117. 'CREATE OR REPLACE VIEW %s AS %s',
  118. (AsIs(self._table), AsIs(query))
  119. )
  120. @api.multi
  121. def action_open_related_line(self):
  122. self.ensure_one()
  123. if self.line_type == 'move_line':
  124. return self.move_line_id.get_formview_action()
  125. else:
  126. return self.env['mis.cash_flow.forecast_line'].browse(
  127. self.id).get_formview_action()