Files
odoo_source/addons/analytic/tests/test_analytic_account.py
T
Yolann Sabaux 1892080d1b [FIX] analytic: enable fetching account_ids of children plans
Steps to reproduce:
- Create a parent analytic plan with no analytic account
- Create a subplan for this analytic plan with no analytic account.
- Create a subplan for the above subplan and create an analytic account for this subplan.
- create an invoice and try to put the created analytic account

Issue:
The analytic account is not availble (nor the subplan, nor the root
plan are displayed)

Cause:
We only fetch root plans (plans without parent_id) that have an
analytic account set. In this cas, the root plan is not retrieved
since the account_id is defined on the sub-sub-sub plan and not on the subplan nor the direct child of the root plan.

Solution:
Fetch all plans that have account_ids set and append the root plan  to the relevant plans

opw-3107652

closes odoo/odoo#112419

X-original-commit: d725c74336feb27bd02e0f05491b24cb51627374
Signed-off-by: William André (wan) <wan@odoo.com>
2023-02-10 15:44:27 +01:00

192 lines
9.0 KiB
Python

# -*- coding: utf-8 -*-
from odoo.tests import tagged
from odoo.tests.common import TransactionCase
from odoo import Command
@tagged('post_install', '-at_install')
class TestAnalyticAccount(TransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.analytic_plan_1 = cls.env['account.analytic.plan'].create({
'name': 'Plan 1',
'default_applicability': 'unavailable',
'company_id': False,
})
cls.analytic_plan_child = cls.env['account.analytic.plan'].create({
'name': 'Plan Child',
'parent_id': cls.analytic_plan_1.id,
'company_id': False,
})
cls.analytic_plan_2 = cls.env['account.analytic.plan'].create({
'name': 'Plan 2',
'company_id': False,
})
# Create new user to avoid demo data.
user = cls.env['res.users'].create({
'name': 'The anal(ytic) expert!',
'login': 'analytic',
'password': 'analytic',
'groups_id': [
(6, 0, cls.env.user.groups_id.ids),
(4, cls.env.ref('analytic.group_analytic_accounting').id),
],
})
user.partner_id.email = 'analyticman@test.com'
# Shadow the current environment/cursor with one having the report user.
# This is mandatory to test access rights.
cls.env = cls.env(user=user)
cls.cr = cls.env.cr
cls.company_data = cls.env['res.company'].create({
'name': 'company_data',
})
cls.env.user.company_ids |= cls.company_data
user.write({
'company_ids': [(6, 0, cls.company_data.ids)],
'company_id': cls.company_data.id,
})
cls.partner_a = cls.env['res.partner'].create({'name': 'partner_a', 'company_id': False})
cls.partner_b = cls.env['res.partner'].create({'name': 'partner_b', 'company_id': False})
cls.analytic_account_1 = cls.env['account.analytic.account'].create({'name': 'Account 1', 'plan_id': cls.analytic_plan_1.id})
cls.analytic_account_2 = cls.env['account.analytic.account'].create({'name': 'Account 2', 'plan_id': cls.analytic_plan_child.id})
cls.analytic_account_3 = cls.env['account.analytic.account'].create({'name': 'Account 3', 'plan_id': cls.analytic_plan_2.id})
cls.distribution_1 = cls.env['account.analytic.distribution.model'].create({
'partner_id': cls.partner_a.id,
'analytic_distribution': {cls.analytic_account_3.id: 100}
})
cls.distribution_2 = cls.env['account.analytic.distribution.model'].create({
'partner_id': cls.partner_b.id,
'analytic_distribution': {cls.analytic_account_2.id: 100}
})
def test_get_plans_without_options(self):
""" Test that the plans with the good appliability are returned without if no options are given """
kwargs = {}
plans_json = self.env['account.analytic.plan'].get_relevant_plans(**kwargs)
self.assertEqual(1, len(plans_json), "Only the Default plan should be available")
self.analytic_plan_1.write({'default_applicability': 'mandatory'})
plans_json = self.env['account.analytic.plan'].get_relevant_plans(**kwargs)
self.assertEqual(2, len(plans_json), "All root plans should be available")
def test_get_plans_with_option(self):
""" Test the plans returned with applicability rules and options """
kwargs = {'business_domain': 'general'}
plans_json = self.env['account.analytic.plan'].get_relevant_plans(**kwargs)
self.assertEqual(1, len(plans_json), "Only the Default plan should be available")
applicability = self.env['account.analytic.applicability'].create({
'business_domain': 'general',
'analytic_plan_id': self.analytic_plan_1.id,
'applicability': 'mandatory'
})
plans_json = self.env['account.analytic.plan'].get_relevant_plans(**kwargs)
self.assertEqual(2, len(plans_json), "All root plans should be available")
self.analytic_plan_1.write({'default_applicability': 'mandatory'})
applicability.write({'applicability': 'unavailable'})
plans_json = self.env['account.analytic.plan'].get_relevant_plans(**kwargs)
self.assertEqual(1, len(plans_json), "Plan 1 should be unavailable")
kwargs = {'business_domain': 'purchase_order'}
plans_json = self.env['account.analytic.plan'].get_relevant_plans(**kwargs)
self.assertEqual(2, len(plans_json), "Both plans should be available")
kwargs = {'applicability': 'optional'}
plans_json = self.env['account.analytic.plan'].get_relevant_plans(**kwargs)
self.assertEqual(2, len(plans_json), "All root plans should be available")
def test_analytic_distribution_model(self):
""" Test the distribution returned from the distribution model """
distribution_json = self.env['account.analytic.distribution.model']._get_distribution({})
self.assertEqual(distribution_json, {}, "No distribution should be given")
distribution_json = self.env['account.analytic.distribution.model']._get_distribution({
"partner_id": self.partner_a.id,
"company_id": self.company_data.id,
})
self.assertEqual(distribution_json, {str(self.analytic_account_3.id): 100}, "Distribution 1 should be given")
distribution_json = self.env['account.analytic.distribution.model']._get_distribution({
"partner_id": self.partner_b.id,
"company_id": self.company_data.id,
})
self.assertEqual(distribution_json, {str(self.analytic_account_2.id): 100}, "Distribution 2 should be given")
def test_order_analytic_distribution_model(self):
""" Test the distribution returned with company field"""
distribution_3 = self.env['account.analytic.distribution.model'].create({
'partner_id': self.partner_a.id,
'analytic_distribution': {self.analytic_account_1.id: 100},
'company_id': self.company_data.id,
})
distribution_json = self.env['account.analytic.distribution.model']._get_distribution({})
self.assertEqual(distribution_json, {}, "No distribution should be given")
distribution_json = self.env['account.analytic.distribution.model']._get_distribution({
"partner_id": self.partner_a.id,
"company_id": self.company_data.id,
})
self.assertEqual(distribution_json, distribution_3.analytic_distribution,
"Distribution 3 should be given, as the company is specified in the model")
distribution_json = self.env['account.analytic.distribution.model']._get_distribution({
"partner_id": self.partner_b.id,
"company_id": self.company_data.id,
})
self.assertEqual(distribution_json, {str(self.analytic_account_2.id): 100},
"Distribution 2 should be given, for the partner")
partner_category = self.env['res.partner.category'].create({'name': 'partner_categ'})
self.partner_a.write({
'category_id': [Command.set([partner_category.id])]
})
distribution_4 = self.env['account.analytic.distribution.model'].create({
'partner_id': self.partner_a.id,
'analytic_distribution': {self.analytic_account_1.id: 100, self.analytic_account_2.id: 100},
'partner_category_id': partner_category.id,
})
distribution_json = self.env['account.analytic.distribution.model']._get_distribution({
"partner_id": self.partner_a.id,
"company_id": self.company_data.id,
"partner_category_id": partner_category.ids,
})
self.assertEqual(distribution_json, distribution_4.analytic_distribution,
"Distribution 4 should be given, as the partner_category_id is better than the company_id")
def test_analytic_plan_account_child(self):
"""
Check that when an analytic account is set to the third (or more) child,
the root plan is correctly retrieved.
"""
self.analytic_plan = self.env['account.analytic.plan'].create({
'name': 'Parent Plan',
'company_id': False,
})
self.analytic_sub_plan = self.env['account.analytic.plan'].create({
'name': 'Sub Plan',
'parent_id': self.analytic_plan.id,
'company_id': False,
})
self.analytic_sub_sub_plan = self.env['account.analytic.plan'].create({
'name': 'Sub Sub Plan',
'parent_id': self.analytic_sub_plan.id,
'company_id': False,
})
self.analytic_account_1 = self.env['account.analytic.account'].create({'name': 'Child Account', 'plan_id': self.analytic_sub_sub_plan.id})
plans_json = self.env['account.analytic.plan'].get_relevant_plans()
self.assertEqual(2, len(plans_json),
"The parent plan should be available even if the analytic account is set on child of third generation")