[FIX] project, hr_timesheet: use correct analytic plan setting

Currently, the `plan_id` used to create analytic accounts is not the one set in the settings.

Steps to reproduce:
-------------------
* Go to the **Settings**
* Enable developper mode
* Select **User & Companies** > **Groups**
* Select `Technical/Analytic Accounting`
* Add user
* Go to the **Project** app
* Select **Configuration** > **Settings**
* Under **Time Management**, enable Timesheets
* Under **Analytics** > **Analytic Plan**, select Projects
* Create a new project
* Go into the settings of the project
* Under the **Settings** tab, select the internal link for the **Analytic Account**
  > **Observation**: The Plan is set to Projects
* Go to **Conffiguration** > **Settings**
* Under **Analytics** > **Analytic Plan**, change Projects to Departments
* Create a new project
* Go into the settings of the project
* Under the **Settings** tab, select the internal link for the **Analytic Account**
  > **Observation**: The Plan is still set to Projects

Why the fix:
------------
When creating, an analytic account, the plan is computed with `_get_all_plans()`.
https://github.com/odoo/odoo/blob/e365e22485dc45f1cbe87ae93395b022a4724a3c/addons/project/models/project_project.py#L894-L903

Inside `__get_all_plans()` the plan is computed as follows:
https://github.com/odoo/odoo/blob/e365e22485dc45f1cbe87ae93395b022a4724a3c/addons/analytic/models/analytic_plan.py#L106-L07

However, the setting that the user changes in the frontend corresponds to `analytic.analytic_plan_projects`.
https://github.com/odoo/odoo/blob/e365e22485dc45f1cbe87ae93395b022a4724a3c/addons/project/models/res_config_settings.py#L18-L22

This seeting is not company-related. It can be used on projects even if they have a `company_id` set to false. We fallback on `_get_all_plans()` if the user did not specifically choose a plan in the settings.

opw-3751661

closes odoo/odoo#157247

Signed-off-by: Xavier Bol (xbo) <xbo@odoo.com>
This commit is contained in:
Sarah Bellefroid
2024-03-18 16:22:38 +00:00
parent a39ffcaf5f
commit 9dde5a64e9
2 changed files with 27 additions and 10 deletions
@@ -703,3 +703,20 @@ class TestTimesheet(TestCommonTimesheet):
},
])
self.assertEqual(self.task1.progress, 100, 'The percentage of allocated hours should be 100%.')
def test_analytic_plan_setting(self):
self.env['ir.config_parameter'].set_param('analytic.analytic_plan_projects', 1)
project_1 = self.env['project.project'].create({
'name': "Project with plan setting 1",
'allow_timesheets': True,
'partner_id': self.partner.id,
})
self.assertEqual(project_1.analytic_account_id.plan_id.id, 1)
self.env['ir.config_parameter'].set_param('analytic.analytic_plan_projects', 2)
project_2 = self.env['project.project'].create({
'name': "Project with plan setting 2",
'allow_timesheets': True,
'partner_id': self.partner.id,
})
self.assertEqual(project_2.analytic_account_id.plan_id.id, 2)
+10 -10
View File
@@ -893,27 +893,27 @@ class Project(models.Model):
@api.model
def _create_analytic_account_from_values(self, values):
company = self.env['res.company'].browse(values.get('company_id', False))
project_plan, _other_plans = self.env['account.analytic.plan']._get_all_plans()
project_plan_id = int(self.env['ir.config_parameter'].sudo().get_param('analytic.analytic_plan_projects'))
if not project_plan_id:
project_plan, _other_plans = self.env['account.analytic.plan']._get_all_plans()
project_plan_id = project_plan.id
analytic_account = self.env['account.analytic.account'].create({
'name': values.get('name', _('Unknown Analytic Account')),
'company_id': company.id,
'partner_id': values.get('partner_id'),
'plan_id': project_plan.id,
'plan_id': project_plan_id,
})
return analytic_account
def _create_analytic_account(self):
for project in self:
company_id = project.company_id.id
project_plan, _other_plans = self.env['account.analytic.plan']._get_all_plans()
analytic_account = self.env['account.analytic.account'].create({
project.analytic_account_id = self._create_analytic_account_from_values({
'company_id': project.company_id.id,
'name': project.name,
'company_id': company_id,
'partner_id': project.partner_id.id,
'plan_id': project_plan.id,
'active': True,
'partner_id': project.partner_id.id
})
project.write({'analytic_account_id': analytic_account.id})
def _get_projects_to_make_billable_domain(self):
return [('partner_id', '!=', False)]