test_analytic_account.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. # -*- coding: utf-8 -*-
  2. from odoo.tests import tagged
  3. from odoo.tests.common import TransactionCase
  4. from odoo import Command
  5. @tagged('post_install', '-at_install')
  6. class TestAnalyticAccount(TransactionCase):
  7. @classmethod
  8. def setUpClass(cls):
  9. super().setUpClass()
  10. cls.analytic_plan_1 = cls.env['account.analytic.plan'].create({
  11. 'name': 'Plan 1',
  12. 'default_applicability': 'unavailable',
  13. 'company_id': False,
  14. })
  15. cls.analytic_plan_child = cls.env['account.analytic.plan'].create({
  16. 'name': 'Plan Child',
  17. 'parent_id': cls.analytic_plan_1.id,
  18. 'company_id': False,
  19. })
  20. cls.analytic_plan_2 = cls.env['account.analytic.plan'].create({
  21. 'name': 'Plan 2',
  22. 'company_id': False,
  23. })
  24. # Create new user to avoid demo data.
  25. user = cls.env['res.users'].create({
  26. 'name': 'The anal(ytic) expert!',
  27. 'login': 'analytic',
  28. 'password': 'analytic',
  29. 'groups_id': [
  30. (6, 0, cls.env.user.groups_id.ids),
  31. (4, cls.env.ref('analytic.group_analytic_accounting').id),
  32. ],
  33. })
  34. user.partner_id.email = 'analyticman@test.com'
  35. # Shadow the current environment/cursor with one having the report user.
  36. # This is mandatory to test access rights.
  37. cls.env = cls.env(user=user)
  38. cls.cr = cls.env.cr
  39. cls.company_data = cls.env['res.company'].create({
  40. 'name': 'company_data',
  41. })
  42. cls.env.user.company_ids |= cls.company_data
  43. user.write({
  44. 'company_ids': [(6, 0, cls.company_data.ids)],
  45. 'company_id': cls.company_data.id,
  46. })
  47. cls.partner_a = cls.env['res.partner'].create({'name': 'partner_a', 'company_id': False})
  48. cls.partner_b = cls.env['res.partner'].create({'name': 'partner_b', 'company_id': False})
  49. cls.analytic_account_1 = cls.env['account.analytic.account'].create({'name': 'Account 1', 'plan_id': cls.analytic_plan_1.id})
  50. cls.analytic_account_2 = cls.env['account.analytic.account'].create({'name': 'Account 2', 'plan_id': cls.analytic_plan_child.id})
  51. cls.analytic_account_3 = cls.env['account.analytic.account'].create({'name': 'Account 3', 'plan_id': cls.analytic_plan_2.id})
  52. cls.distribution_1 = cls.env['account.analytic.distribution.model'].create({
  53. 'partner_id': cls.partner_a.id,
  54. 'analytic_distribution': {cls.analytic_account_3.id: 100}
  55. })
  56. cls.distribution_2 = cls.env['account.analytic.distribution.model'].create({
  57. 'partner_id': cls.partner_b.id,
  58. 'analytic_distribution': {cls.analytic_account_2.id: 100}
  59. })
  60. def test_get_plans_without_options(self):
  61. """ Test that the plans with the good appliability are returned without if no options are given """
  62. kwargs = {}
  63. plans_json = self.env['account.analytic.plan'].get_relevant_plans(**kwargs)
  64. self.assertEqual(1, len(plans_json), "Only the Default plan should be available")
  65. self.analytic_plan_1.write({'default_applicability': 'mandatory'})
  66. plans_json = self.env['account.analytic.plan'].get_relevant_plans(**kwargs)
  67. self.assertEqual(2, len(plans_json), "All root plans should be available")
  68. def test_get_plans_with_option(self):
  69. """ Test the plans returned with applicability rules and options """
  70. kwargs = {'business_domain': 'general'}
  71. plans_json = self.env['account.analytic.plan'].get_relevant_plans(**kwargs)
  72. self.assertEqual(1, len(plans_json), "Only the Default plan should be available")
  73. applicability = self.env['account.analytic.applicability'].create({
  74. 'business_domain': 'general',
  75. 'analytic_plan_id': self.analytic_plan_1.id,
  76. 'applicability': 'mandatory'
  77. })
  78. plans_json = self.env['account.analytic.plan'].get_relevant_plans(**kwargs)
  79. self.assertEqual(2, len(plans_json), "All root plans should be available")
  80. self.analytic_plan_1.write({'default_applicability': 'mandatory'})
  81. applicability.write({'applicability': 'unavailable'})
  82. plans_json = self.env['account.analytic.plan'].get_relevant_plans(**kwargs)
  83. self.assertEqual(1, len(plans_json), "Plan 1 should be unavailable")
  84. kwargs = {'business_domain': 'purchase_order'}
  85. plans_json = self.env['account.analytic.plan'].get_relevant_plans(**kwargs)
  86. self.assertEqual(2, len(plans_json), "Both plans should be available")
  87. kwargs = {'applicability': 'optional'}
  88. plans_json = self.env['account.analytic.plan'].get_relevant_plans(**kwargs)
  89. self.assertEqual(2, len(plans_json), "All root plans should be available")
  90. def test_analytic_distribution_model(self):
  91. """ Test the distribution returned from the distribution model """
  92. distribution_json = self.env['account.analytic.distribution.model']._get_distribution({})
  93. self.assertEqual(distribution_json, {}, "No distribution should be given")
  94. distribution_json = self.env['account.analytic.distribution.model']._get_distribution({
  95. "partner_id": self.partner_a.id,
  96. "company_id": self.company_data.id,
  97. })
  98. self.assertEqual(distribution_json, {str(self.analytic_account_3.id): 100}, "Distribution 1 should be given")
  99. distribution_json = self.env['account.analytic.distribution.model']._get_distribution({
  100. "partner_id": self.partner_b.id,
  101. "company_id": self.company_data.id,
  102. })
  103. self.assertEqual(distribution_json, {str(self.analytic_account_2.id): 100}, "Distribution 2 should be given")
  104. def test_order_analytic_distribution_model(self):
  105. """ Test the distribution returned with company field"""
  106. distribution_3 = self.env['account.analytic.distribution.model'].create({
  107. 'partner_id': self.partner_a.id,
  108. 'analytic_distribution': {self.analytic_account_1.id: 100},
  109. 'company_id': self.company_data.id,
  110. })
  111. distribution_json = self.env['account.analytic.distribution.model']._get_distribution({})
  112. self.assertEqual(distribution_json, {}, "No distribution should be given")
  113. distribution_json = self.env['account.analytic.distribution.model']._get_distribution({
  114. "partner_id": self.partner_a.id,
  115. "company_id": self.company_data.id,
  116. })
  117. self.assertEqual(distribution_json, distribution_3.analytic_distribution,
  118. "Distribution 3 should be given, as the company is specified in the model")
  119. distribution_json = self.env['account.analytic.distribution.model']._get_distribution({
  120. "partner_id": self.partner_b.id,
  121. "company_id": self.company_data.id,
  122. })
  123. self.assertEqual(distribution_json, {str(self.analytic_account_2.id): 100},
  124. "Distribution 2 should be given, for the partner")
  125. partner_category = self.env['res.partner.category'].create({'name': 'partner_categ'})
  126. self.partner_a.write({
  127. 'category_id': [Command.set([partner_category.id])]
  128. })
  129. distribution_4 = self.env['account.analytic.distribution.model'].create({
  130. 'partner_id': self.partner_a.id,
  131. 'analytic_distribution': {self.analytic_account_1.id: 100, self.analytic_account_2.id: 100},
  132. 'partner_category_id': partner_category.id,
  133. })
  134. distribution_json = self.env['account.analytic.distribution.model']._get_distribution({
  135. "partner_id": self.partner_a.id,
  136. "company_id": self.company_data.id,
  137. "partner_category_id": partner_category.ids,
  138. })
  139. self.assertEqual(distribution_json, distribution_4.analytic_distribution,
  140. "Distribution 4 should be given, as the partner_category_id is better than the company_id")
  141. def test_analytic_plan_account_child(self):
  142. """
  143. Check that when an analytic account is set to the third (or more) child,
  144. the root plan is correctly retrieved.
  145. """
  146. self.analytic_plan = self.env['account.analytic.plan'].create({
  147. 'name': 'Parent Plan',
  148. 'company_id': False,
  149. })
  150. self.analytic_sub_plan = self.env['account.analytic.plan'].create({
  151. 'name': 'Sub Plan',
  152. 'parent_id': self.analytic_plan.id,
  153. 'company_id': False,
  154. })
  155. self.analytic_sub_sub_plan = self.env['account.analytic.plan'].create({
  156. 'name': 'Sub Sub Plan',
  157. 'parent_id': self.analytic_sub_plan.id,
  158. 'company_id': False,
  159. })
  160. self.analytic_account_1 = self.env['account.analytic.account'].create({'name': 'Child Account', 'plan_id': self.analytic_sub_sub_plan.id})
  161. plans_json = self.env['account.analytic.plan'].get_relevant_plans()
  162. self.assertEqual(2, len(plans_json),
  163. "The parent plan should be available even if the analytic account is set on child of third generation")