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.

118 lines
3.9 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. def init(self):
  49. query = """
  50. SELECT
  51. -- we use negative id to avoid duplicates and we don't use
  52. -- ROW_NUMBER() because the performance was very poor
  53. -aml.id as id,
  54. CAST('move_line' AS varchar) as line_type,
  55. aml.id as move_line_id,
  56. aml.account_id as account_id,
  57. CASE
  58. WHEN aml.amount_residual > 0
  59. THEN aml.amount_residual
  60. ELSE 0.0
  61. END AS debit,
  62. CASE
  63. WHEN aml.amount_residual < 0
  64. THEN -aml.amount_residual
  65. ELSE 0.0
  66. END AS credit,
  67. aml.reconciled as reconciled,
  68. aml.full_reconcile_id as full_reconcile_id,
  69. aml.partner_id as partner_id,
  70. aml.company_id as company_id,
  71. aml.name as name,
  72. COALESCE(aml.date_maturity, aml.date) as date
  73. FROM account_move_line as aml
  74. UNION ALL
  75. SELECT
  76. fl.id as id,
  77. CAST('forecast_line' AS varchar) as line_type,
  78. NULL as move_line_id,
  79. fl.account_id as account_id,
  80. CASE
  81. WHEN fl.balance > 0
  82. THEN fl.balance
  83. ELSE 0.0
  84. END AS debit,
  85. CASE
  86. WHEN fl.balance < 0
  87. THEN -fl.balance
  88. ELSE 0.0
  89. END AS credit,
  90. NULL as reconciled,
  91. NULL as full_reconcile_id,
  92. fl.partner_id as partner_id,
  93. fl.company_id as company_id,
  94. fl.name as name,
  95. fl.date as date
  96. FROM mis_cash_flow_forecast_line as fl
  97. """
  98. tools.drop_view_if_exists(self.env.cr, self._table)
  99. self._cr.execute(
  100. "CREATE OR REPLACE VIEW %s AS (%s)", (AsIs(self._table), AsIs(query))
  101. )
  102. def action_open_related_line(self):
  103. self.ensure_one()
  104. if self.line_type == "move_line":
  105. return self.move_line_id.get_formview_action()
  106. else:
  107. return (
  108. self.env["mis.cash_flow.forecast_line"]
  109. .browse(self.id)
  110. .get_formview_action()
  111. )