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.

132 lines
3.9 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. query = """
  65. SELECT
  66. -- we use negative id to avoid duplicates and we don't use
  67. -- ROW_NUMBER() because the performance was very poor
  68. -aml.id as id,
  69. CAST('move_line' AS varchar) as line_type,
  70. aml.id as move_line_id,
  71. aml.account_id as account_id,
  72. CASE
  73. WHEN aml.amount_residual > 0
  74. THEN aml.amount_residual
  75. ELSE 0.0
  76. END AS debit,
  77. CASE
  78. WHEN aml.amount_residual < 0
  79. THEN -aml.amount_residual
  80. ELSE 0.0
  81. END AS credit,
  82. aml.reconciled as reconciled,
  83. aml.full_reconcile_id as full_reconcile_id,
  84. aml.company_id as company_id,
  85. aml.user_type_id as user_type_id,
  86. aml.name as name,
  87. aml.date_maturity as date
  88. FROM account_move_line as aml
  89. UNION ALL
  90. SELECT
  91. fl.id as id,
  92. CAST('forecast_line' AS varchar) as line_type,
  93. Null as move_line_id,
  94. fl.account_id as account_id,
  95. CASE
  96. WHEN fl.balance > 0
  97. THEN fl.balance
  98. ELSE 0.0
  99. END AS debit,
  100. CASE
  101. WHEN fl.balance < 0
  102. THEN -fl.balance
  103. ELSE 0.0
  104. END AS credit,
  105. Null as reconciled,
  106. Null as full_reconcile_id,
  107. fl.company_id as company_id,
  108. -- we dont need this field on forecast lines
  109. Null as user_type_id,
  110. fl.name as name,
  111. fl.date as date
  112. FROM mis_cash_flow_forecast_line as fl
  113. """
  114. tools.drop_view_if_exists(self.env.cr, self._table)
  115. self._cr.execute(
  116. 'CREATE OR REPLACE VIEW %s AS %s',
  117. (AsIs(self._table), AsIs(query))
  118. )
  119. @api.multi
  120. def action_open_related_line(self):
  121. self.ensure_one()
  122. if self.line_type == 'move_line':
  123. return self.move_line_id.get_formview_action()
  124. else:
  125. return self.env['mis.cash_flow.forecast_line'].browse(
  126. self.id).get_formview_action()