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.

130 lines
4.5 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.tools import mute_logger
  7. from odoo.tests.common import TransactionCase, post_install, at_install
  8. @at_install(False)
  9. @post_install(True)
  10. class TestCashFlow(TransactionCase):
  11. def setUp(self):
  12. super().setUp()
  13. self.company = self.env['res.company'].create({
  14. 'name': 'TEST'
  15. })
  16. self.report = self.browse_ref(
  17. 'mis_builder_cash_flow.mis_instance_cash_flow')
  18. self.report.company_id = self.company
  19. self.bank_account = self.env['account.account'].create({
  20. "company_id": self.company.id,
  21. "code": "TEST1",
  22. "name": "Bank account 01",
  23. "user_type_id": self.browse_ref(
  24. "account.data_account_type_liquidity"
  25. ).id,
  26. })
  27. self.bank_account_hide = self.env['account.account'].create({
  28. "company_id": self.company.id,
  29. "code": "TEST2",
  30. "name": "Bank account 02",
  31. "user_type_id": self.browse_ref(
  32. "account.data_account_type_liquidity"
  33. ).id,
  34. "hide_in_cash_flow": True,
  35. })
  36. self.account = self.env['account.account'].create({
  37. "company_id": self.company.id,
  38. "code": "TEST3",
  39. "name": "Account",
  40. "user_type_id": self.browse_ref(
  41. "account.data_account_type_receivable"
  42. ).id,
  43. "reconcile": True,
  44. })
  45. self.journal = self.env["account.journal"].create({
  46. "name": "Journal",
  47. "code": "JOURNAL",
  48. "company_id": self.company.id,
  49. "type": "general",
  50. })
  51. def test_company_constrain(self):
  52. with self.assertRaises(ValidationError):
  53. self.env['mis.cash_flow.forecast_line'].create({
  54. 'account_id': self.account.id,
  55. 'date': Date.today(),
  56. 'balance': 1000,
  57. })
  58. def test_report_instance(self):
  59. self.check_matrix()
  60. move = self.env['account.move'].create({
  61. 'name': 'Move',
  62. 'journal_id': self.journal.id,
  63. 'company_id': self.company.id,
  64. 'line_ids': [(0, 0, {
  65. 'account_id': self.bank_account.id,
  66. 'debit': 1500,
  67. 'credit': 0,
  68. 'company_id': self.company.id,
  69. }), (0, 0, {
  70. 'account_id': self.bank_account_hide.id,
  71. 'debit': 500,
  72. 'credit': 0,
  73. 'company_id': self.company.id,
  74. }), (0, 0, {
  75. 'account_id': self.account.id,
  76. 'debit': 0,
  77. 'credit': 2000,
  78. 'company_id': self.company.id,
  79. })]
  80. })
  81. move.post()
  82. self.check_matrix(args=[
  83. ('liquidity', 'Current', 1500),
  84. ('balance', 'Current', 1500),
  85. ('in_receivable', 'Current', -2000),
  86. ], ignore_rows=['balance', 'period_balance', 'in_total'],
  87. )
  88. date = Date.from_string(Date.today()) + timedelta(weeks=8)
  89. self.env['mis.cash_flow.forecast_line'].create({
  90. 'account_id': self.account.id,
  91. 'date': Date.to_string(date),
  92. 'balance': 1000,
  93. 'company_id': self.company.id,
  94. })
  95. self.check_matrix([
  96. ('liquidity', 'Current', 1500),
  97. ('balance', 'Current', 1500),
  98. ('in_receivable', 'Current', -2000),
  99. ('in_forecast', '+8w', 1000),
  100. ], ignore_rows=['balance', 'period_balance', 'in_total'])
  101. def check_matrix(self, args=False, ignore_rows=False):
  102. if not args:
  103. args = []
  104. if not ignore_rows:
  105. ignore_rows = []
  106. with mute_logger('odoo.addons.mis_builder.models.kpimatrix'):
  107. matrix = self.report._compute_matrix()
  108. for row in matrix.iter_rows():
  109. if row.kpi.name in ignore_rows:
  110. continue
  111. for cell in row.iter_cells():
  112. if not cell:
  113. continue
  114. found = False
  115. label = cell.subcol.col.label
  116. for exp in args:
  117. if exp[0] == row.kpi.name and exp[1] == label:
  118. found = True
  119. self.assertEqual(cell.val, exp[2])
  120. break
  121. if not found:
  122. self.assertEqual(cell.val, 0)