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.

126 lines
4.1 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 api, 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. user_type_id = fields.Many2one(
  46. "account.account.type", auto_join=True, readonly=True, index=True,
  47. )
  48. @api.model_cr
  49. def init(self):
  50. account_type_receivable = self.env.ref("account.data_account_type_receivable")
  51. query = (
  52. """
  53. SELECT
  54. -- we use negative id to avoid duplicates and we don't use
  55. -- ROW_NUMBER() because the performance was very poor
  56. -aml.id as id,
  57. CAST('move_line' AS varchar) as line_type,
  58. aml.id as move_line_id,
  59. aml.account_id as account_id,
  60. CASE
  61. WHEN aml.amount_residual > 0
  62. THEN aml.amount_residual
  63. ELSE 0.0
  64. END AS debit,
  65. CASE
  66. WHEN aml.amount_residual < 0
  67. THEN -aml.amount_residual
  68. ELSE 0.0
  69. END AS credit,
  70. aml.reconciled as reconciled,
  71. aml.full_reconcile_id as full_reconcile_id,
  72. aml.partner_id as partner_id,
  73. aml.company_id as company_id,
  74. aml.user_type_id as user_type_id,
  75. aml.name as name,
  76. aml.date_maturity as date
  77. FROM account_move_line as aml
  78. UNION ALL
  79. SELECT
  80. fl.id as id,
  81. CAST('forecast_line' AS varchar) as line_type,
  82. Null as move_line_id,
  83. fl.account_id as account_id,
  84. CASE
  85. WHEN fl.balance > 0
  86. THEN fl.balance
  87. ELSE 0.0
  88. END AS debit,
  89. CASE
  90. WHEN fl.balance < 0
  91. THEN -fl.balance
  92. ELSE 0.0
  93. END AS credit,
  94. Null as reconciled,
  95. Null as full_reconcile_id,
  96. fl.partner_id as partner_id,
  97. fl.company_id as company_id,
  98. %i as user_type_id,
  99. fl.name as name,
  100. fl.date as date
  101. FROM mis_cash_flow_forecast_line as fl
  102. """
  103. % account_type_receivable.id
  104. )
  105. tools.drop_view_if_exists(self.env.cr, self._table)
  106. self._cr.execute(
  107. "CREATE OR REPLACE VIEW %s AS %s", (AsIs(self._table), AsIs(query))
  108. )
  109. @api.multi
  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. )