Skip to content

Commit

Permalink
[FIX] analytic: check current company applicability
Browse files Browse the repository at this point in the history
Have company A and company B
With company B:
- Create an analytic plan with default applicability 'Mandatory'
- Add an applicability: domain 'Expense', applicability 'Mandatory'
With company A:
- Open the same analytic plan
- Set default applicability 'Optional', no assigned applicability
- Add an analytc account for the plan
- Create an expense, add amount
- Create report > Submit to Mananger > Approve

Issue: Action will be blocked by Warning message
"One or more lines require a 100% analytic distribution."

This occurs because the system checks all applicabilities of the plan
without taking into account the company

opw-3709374

closes odoo#156815

X-original-commit: 60f053b
Signed-off-by: William André (wan) <wan@odoo.com>
Signed-off-by: Andrea Grazioso (agr) <agr@odoo.com>
  • Loading branch information
agr-odoo authored and MohammedBasioni committed Mar 8, 2024
1 parent f753528 commit ede3523
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion addons/analytic/models/analytic_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def create(self, vals_list):

def _validate_distribution(self, **kwargs):
if self.env.context.get('validate_analytic', False):
mandatory_plans_ids = [plan['id'] for plan in self.env['account.analytic.plan'].sudo().get_relevant_plans(**kwargs) if plan['applicability'] == 'mandatory']
mandatory_plans_ids = [plan['id'] for plan in self.env['account.analytic.plan'].sudo().with_company(self.company_id).get_relevant_plans(**kwargs) if plan['applicability'] == 'mandatory']
if not mandatory_plans_ids:
return
decimal_precision = self.env['decimal.precision'].precision_get('Percentage Analytic')
Expand Down
2 changes: 1 addition & 1 deletion addons/analytic/models/analytic_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def _get_applicability(self, **kwargs):
else:
score = 0
applicability = self.default_applicability
for applicability_rule in self.applicability_ids:
for applicability_rule in self.applicability_ids.filtered(lambda rule: rule.company_id == self.env.company):
score_rule = applicability_rule._get_score(**kwargs)
if score_rule > score:
applicability = applicability_rule.applicability
Expand Down
25 changes: 25 additions & 0 deletions addons/analytic/tests/test_plan_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,28 @@ def test_validate_deleted_account(self):
test_account.unlink()
plan.unlink()
distribution_model._validate_distribution()

def test_validate_company_plans(self):
company_2 = self.env['res.company'].create({
'name': 'company_2',
})
mandatory_plan = self.env['account.analytic.plan'].create([{
'name': 'Mandatory Plan',
'default_applicability': 'optional',
}])
mandatory_plan.with_company(company_2).write({'default_applicability': 'mandatory'})
self.env['account.analytic.applicability'].create({
'business_domain': 'general',
'analytic_plan_id': mandatory_plan.id,
'applicability': 'mandatory',
'company_id': company_2.id,
})
self.env['account.analytic.account'].create([{
'name': 'Mandatory Account',
'code': 'manda',
'plan_id': mandatory_plan.id,
}])
distribution_model = self.env['account.analytic.distribution.model'].create({}).with_context(validate_analytic=True)

# only mandatory applicability is in company_2, should not raise
distribution_model._validate_distribution(business_domain='general')

0 comments on commit ede3523

Please sign in to comment.