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.

157 lines
5.4 KiB

  1. # Copyright 2019 Creu Blanca
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from datetime import timedelta
  4. from odoo.exceptions import ValidationError
  5. from odoo.fields import Date
  6. from odoo.tests.common import TransactionCase, tagged
  7. from odoo.tools import mute_logger
  8. @tagged("post_install", "-at_install")
  9. class TestCashFlow(TransactionCase):
  10. def setUp(self):
  11. super().setUp()
  12. self.company = self.env["res.company"].create({"name": "TEST"})
  13. self.report = self.browse_ref("mis_builder_cash_flow.mis_instance_cash_flow")
  14. self.report.company_id = self.company
  15. self.bank_account = self.env["account.account"].create(
  16. {
  17. "company_id": self.company.id,
  18. "code": "TEST1",
  19. "name": "Bank account 01",
  20. "user_type_id": self.browse_ref(
  21. "account.data_account_type_liquidity"
  22. ).id,
  23. }
  24. )
  25. self.bank_account_hide = self.env["account.account"].create(
  26. {
  27. "company_id": self.company.id,
  28. "code": "TEST2",
  29. "name": "Bank account 02",
  30. "user_type_id": self.browse_ref(
  31. "account.data_account_type_liquidity"
  32. ).id,
  33. "hide_in_cash_flow": True,
  34. }
  35. )
  36. self.account = self.env["account.account"].create(
  37. {
  38. "company_id": self.company.id,
  39. "code": "TEST3",
  40. "name": "Account",
  41. "user_type_id": self.browse_ref(
  42. "account.data_account_type_receivable"
  43. ).id,
  44. "reconcile": True,
  45. }
  46. )
  47. self.journal = self.env["account.journal"].create(
  48. {
  49. "name": "Journal",
  50. "code": "JOURNAL",
  51. "company_id": self.company.id,
  52. "type": "general",
  53. }
  54. )
  55. def test_company_constrain(self):
  56. with self.assertRaises(ValidationError):
  57. self.env["mis.cash_flow.forecast_line"].create(
  58. {"account_id": self.account.id, "date": Date.today(), "balance": 1000}
  59. )
  60. def test_report_instance(self):
  61. self.check_matrix()
  62. move = self.env["account.move"].create(
  63. {
  64. "name": "Move",
  65. "journal_id": self.journal.id,
  66. "company_id": self.company.id,
  67. "type": "entry",
  68. "line_ids": [
  69. (
  70. 0,
  71. 0,
  72. {
  73. "account_id": self.bank_account.id,
  74. "debit": 1500,
  75. "credit": 0,
  76. "company_id": self.company.id,
  77. },
  78. ),
  79. (
  80. 0,
  81. 0,
  82. {
  83. "account_id": self.bank_account_hide.id,
  84. "debit": 500,
  85. "credit": 0,
  86. "company_id": self.company.id,
  87. },
  88. ),
  89. (
  90. 0,
  91. 0,
  92. {
  93. "account_id": self.account.id,
  94. "debit": 0,
  95. "credit": 2000,
  96. "company_id": self.company.id,
  97. },
  98. ),
  99. ],
  100. }
  101. )
  102. move.post()
  103. self.check_matrix(
  104. args=[
  105. ("liquidity", "Current", 1500),
  106. ("balance", "Current", 1500),
  107. ("in_receivable", "Current", -2000),
  108. ],
  109. ignore_rows=["balance", "period_balance", "in_total"],
  110. )
  111. date = Date.today() + timedelta(weeks=8)
  112. self.env["mis.cash_flow.forecast_line"].create(
  113. {
  114. "account_id": self.account.id,
  115. "date": date,
  116. "balance": 1000,
  117. "company_id": self.company.id,
  118. }
  119. )
  120. self.check_matrix(
  121. [
  122. ("liquidity", "Current", 1500),
  123. ("balance", "Current", 1500),
  124. ("in_receivable", "Current", -2000),
  125. ("in_forecast", "+8w", 1000),
  126. ],
  127. ignore_rows=["balance", "period_balance", "in_total"],
  128. )
  129. def check_matrix(self, args=None, ignore_rows=None):
  130. if not args:
  131. args = []
  132. if not ignore_rows:
  133. ignore_rows = []
  134. with mute_logger("odoo.addons.mis_builder.models.kpimatrix"):
  135. matrix = self.report._compute_matrix()
  136. for row in matrix.iter_rows():
  137. if row.kpi.name in ignore_rows:
  138. continue
  139. for cell in row.iter_cells():
  140. if not cell:
  141. continue
  142. found = False
  143. label = cell.subcol.col.label
  144. for exp in args:
  145. if exp[0] == row.kpi.name and exp[1] == label:
  146. found = True
  147. self.assertEqual(cell.val, exp[2])
  148. break
  149. if not found:
  150. self.assertEqual(cell.val, 0)