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.

127 lines
4.2 KiB

  1. # Copyright 2019 ADHOC SA
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from psycopg2.extensions import AsIs
  4. from odoo import fields, models, tools
  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(readonly=True,)
  15. account_id = fields.Many2one(
  16. comodel_name="account.account",
  17. string="Account",
  18. auto_join=True,
  19. index=True,
  20. readonly=True,
  21. )
  22. partner_id = fields.Many2one(
  23. comodel_name="res.partner", string="Partner", readonly=True,
  24. )
  25. move_line_id = fields.Many2one(
  26. comodel_name="account.move.line",
  27. string="Journal Item",
  28. auto_join=True,
  29. readonly=True,
  30. )
  31. company_id = fields.Many2one(
  32. comodel_name="res.company",
  33. string="Company",
  34. auto_join=True,
  35. readonly=True,
  36. index=True,
  37. )
  38. credit = fields.Float(readonly=True,)
  39. debit = fields.Float(readonly=True,)
  40. date = fields.Date(readonly=True, index=True,)
  41. reconciled = fields.Boolean(readonly=True,)
  42. full_reconcile_id = fields.Many2one(
  43. "account.full.reconcile", string="Matching Number", readonly=True, index=True,
  44. )
  45. account_internal_type = fields.Selection(
  46. related="account_id.user_type_id.type", readonly=True
  47. )
  48. state = fields.Selection(selection="_selection_parent_state",)
  49. def _selection_parent_state(self):
  50. return self.env["account.move"].fields_get(allfields=["state"])["state"][
  51. "selection"
  52. ]
  53. def init(self):
  54. query = """
  55. SELECT
  56. -- we use negative id to avoid duplicates and we don't use
  57. -- ROW_NUMBER() because the performance was very poor
  58. -aml.id as id,
  59. 'move_line' as line_type,
  60. aml.id as move_line_id,
  61. aml.account_id as account_id,
  62. CASE
  63. WHEN aml.amount_residual > 0
  64. THEN aml.amount_residual
  65. ELSE 0.0
  66. END AS debit,
  67. CASE
  68. WHEN aml.amount_residual < 0
  69. THEN -aml.amount_residual
  70. ELSE 0.0
  71. END AS credit,
  72. aml.reconciled as reconciled,
  73. aml.full_reconcile_id as full_reconcile_id,
  74. aml.partner_id as partner_id,
  75. aml.company_id as company_id,
  76. aml.name as name,
  77. aml.parent_state as state,
  78. COALESCE(aml.date_maturity, aml.date) as date
  79. FROM account_move_line as aml
  80. WHERE aml.parent_state != 'cancel'
  81. UNION ALL
  82. SELECT
  83. fl.id as id,
  84. 'forecast_line' as line_type,
  85. NULL as move_line_id,
  86. fl.account_id as account_id,
  87. CASE
  88. WHEN fl.balance > 0
  89. THEN fl.balance
  90. ELSE 0.0
  91. END AS debit,
  92. CASE
  93. WHEN fl.balance < 0
  94. THEN -fl.balance
  95. ELSE 0.0
  96. END AS credit,
  97. NULL as reconciled,
  98. NULL as full_reconcile_id,
  99. fl.partner_id as partner_id,
  100. fl.company_id as company_id,
  101. fl.name as name,
  102. 'posted' as state,
  103. fl.date as date
  104. FROM mis_cash_flow_forecast_line as fl
  105. """
  106. tools.drop_view_if_exists(self.env.cr, self._table)
  107. self._cr.execute(
  108. "CREATE OR REPLACE VIEW %s AS (%s)", (AsIs(self._table), AsIs(query))
  109. )
  110. def action_open_related_line(self):
  111. self.ensure_one()
  112. if self.line_type == "move_line":
  113. return self.move_line_id.get_formview_action()
  114. else:
  115. return (
  116. self.env["mis.cash_flow.forecast_line"]
  117. .browse(self.id)
  118. .get_formview_action()
  119. )