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.

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