[FIX] project: set default analytic plan in settings
Current behaviour: You cannot set the Default Analytic Plan for projects in the settings. Expected behaviour: You should be able to set the default plan, like any other settings. Steps to reproduce: - Install Accounting, Project, (Timesheets if you want to test project creation with an auto-created analytic account) - Settings > Check Analytic Accounts - Try to change the Default Plan in Settings > Project - Upon saving, the change is lost Reason for the problem: The `analytic_plan_id` in the project's `res.config.settings` is related to the same field on the `res.company` model. The `analytic_plan_id` that is set on the company upon installing the project module is a non-stored computed field, which makes a call to a `_get_default` -> the field cannot be set. It will always return the first default plan in the sequence. Fix: Since the field isn't stored (which would have worked if it was), we use `ir.config_parameter` to write and read to it, per company. In master we should just add a `store=True` and write a migration for it? Affected versions: - 16.0 - saas-16.1 - master opw-3127638 closes odoo/odoo#111635 X-original-commit: 11b0e2556ce824048bb4c760893b05e98537d171 Signed-off-by: Xavier Bol (xbo) <xbo@odoo.com>
This commit is contained in:
@@ -16,4 +16,13 @@ class ResCompany(models.Model):
|
||||
|
||||
def _compute_analytic_plan_id(self):
|
||||
for company in self:
|
||||
company.analytic_plan_id = self.env['account.analytic.plan'].with_company(company)._get_default()
|
||||
default_plan = self.env['ir.config_parameter'].with_company(company).sudo().get_param("default_analytic_plan_id_%s" % company.id)
|
||||
company.analytic_plan_id = int(default_plan) if default_plan else False
|
||||
if not company.analytic_plan_id:
|
||||
company.analytic_plan_id = self.env['account.analytic.plan'].with_company(company)._get_default()
|
||||
|
||||
def write(self, values):
|
||||
for company in self:
|
||||
if 'analytic_plan_id' in values:
|
||||
self.env['ir.config_parameter'].sudo().set_param("default_analytic_plan_id_%s" % company.id, values['analytic_plan_id'])
|
||||
return super().write(values)
|
||||
|
||||
@@ -20,6 +20,7 @@ from . import test_project_update_ui
|
||||
from . import test_portal
|
||||
from . import test_multicompany
|
||||
from . import test_personal_stages
|
||||
from . import test_res_config_settings
|
||||
from . import test_task_dependencies
|
||||
from . import test_task_follow
|
||||
from . import test_task_tracking
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo.tests import tagged, Form, TransactionCase
|
||||
|
||||
|
||||
@tagged('post_install', '-at_install')
|
||||
class TestResConfigSettings(TransactionCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super().setUpClass()
|
||||
cls.analytic_plan_1, cls.analytic_plan_2 = cls.env['account.analytic.plan'].create([
|
||||
{
|
||||
'name': 'Plan 1',
|
||||
'default_applicability': 'optional',
|
||||
'company_id': False,
|
||||
}, {
|
||||
'name': 'Plan 2',
|
||||
'default_applicability': 'optional',
|
||||
'company_id': False,
|
||||
},
|
||||
])
|
||||
cls.company_1, cls.company_2 = cls.env['res.company'].create([
|
||||
{'name': 'Test Company 1'},
|
||||
{'name': 'Test Company 2'},
|
||||
])
|
||||
(cls.analytic_plan_1 + cls.analytic_plan_2).write({
|
||||
'company_id': cls.company_1.id,
|
||||
})
|
||||
|
||||
def test_set_default_analytic_plan(self):
|
||||
"""
|
||||
Test that we can set the default analytic plan in the settings per company.
|
||||
When there are no analytic plans for the company, a new one named "Default" should be created.
|
||||
"""
|
||||
self.env.user.groups_id += self.env.ref('analytic.group_analytic_accounting')
|
||||
settings_company_1 = self.env['res.config.settings'].with_company(self.company_1).create({})
|
||||
with Form(settings_company_1) as form:
|
||||
self.assertEqual(form.analytic_plan_id, self.analytic_plan_1)
|
||||
form.analytic_plan_id = self.analytic_plan_2
|
||||
form.save()
|
||||
self.assertEqual(settings_company_1.analytic_plan_id, self.analytic_plan_2)
|
||||
settings_company_2 = self.env['res.config.settings'].with_company(self.company_2).create({})
|
||||
with Form(settings_company_2) as form:
|
||||
self.assertNotEqual(form.analytic_plan_id, self.analytic_plan_1)
|
||||
self.assertNotEqual(form.analytic_plan_id, self.analytic_plan_2)
|
||||
self.assertEqual(form.analytic_plan_id.name, "Default")
|
||||
Reference in New Issue
Block a user