From 901294601ef03ef677d97d833045fb173819f1cf Mon Sep 17 00:00:00 2001 From: Yash Vaishnav Date: Fri, 21 Jul 2023 11:22:04 +0000 Subject: [PATCH] [IMP] project_timesheet_holidays: set project and task of the time-off type's company Before this commit, the project and task fields were not dependent on the company. The default project was set as the company if it existed; otherwise, it was retrieved from the environment, and the internal project was set as the default. The task was computed based on the project. In this commit, the project will only be set to its internal project if the company exists. If the company is false or not specified, both project and task will be set to false. task-3279188 closes odoo/odoo#129539 Signed-off-by: Xavier Bol (xbo) --- .../data/holiday_timesheets_demo.xml | 2 +- .../models/hr_holidays.py | 23 ++++++++----------- .../tests/test_timesheet_holidays.py | 5 ++-- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/addons/project_timesheet_holidays/data/holiday_timesheets_demo.xml b/addons/project_timesheet_holidays/data/holiday_timesheets_demo.xml index e7f81104751..ec045cdae11 100644 --- a/addons/project_timesheet_holidays/data/holiday_timesheets_demo.xml +++ b/addons/project_timesheet_holidays/data/holiday_timesheets_demo.xml @@ -4,7 +4,7 @@ - + diff --git a/addons/project_timesheet_holidays/models/hr_holidays.py b/addons/project_timesheet_holidays/models/hr_holidays.py index a2f68d56805..0d0ac1e9210 100644 --- a/addons/project_timesheet_holidays/models/hr_holidays.py +++ b/addons/project_timesheet_holidays/models/hr_holidays.py @@ -8,21 +8,14 @@ from odoo.exceptions import ValidationError class HolidaysType(models.Model): _inherit = "hr.leave.type" - def _default_project_id(self): - company = self.company_id if self.company_id else self.env.company - return company.internal_project_id.id - - def _default_task_id(self): - company = self.company_id if self.company_id else self.env.company - return company.leave_timesheet_task_id.id - timesheet_generate = fields.Boolean( 'Generate Timesheet', compute='_compute_timesheet_generate', store=True, readonly=False, help="If checked, when validating a time off, timesheet will be generated in the Vacation Project of the company.") - timesheet_project_id = fields.Many2one('project.project', string="Project", default=_default_project_id, domain="[('company_id', '=', company_id)]") + timesheet_project_id = fields.Many2one('project.project', string="Project", domain="[('company_id', '=', company_id)]", + compute="_compute_timesheet_project_id", store=True, readonly=False) timesheet_task_id = fields.Many2one( 'project.task', string="Task", compute='_compute_timesheet_task_id', - store=True, readonly=False, default=_default_task_id, + store=True, readonly=False, domain="[('project_id', '=', timesheet_project_id)," "('project_id', '!=', False)," "('company_id', '=', company_id)]") @@ -32,11 +25,15 @@ class HolidaysType(models.Model): for leave_type in self: leave_type.timesheet_generate = leave_type.timesheet_task_id and leave_type.timesheet_project_id + @api.depends('company_id') + def _compute_timesheet_project_id(self): + for leave in self: + leave.timesheet_project_id = leave.company_id.internal_project_id + @api.depends('timesheet_project_id') def _compute_timesheet_task_id(self): for leave_type in self: - company = leave_type.company_id if leave_type.company_id else self.env.company - default_task_id = company.leave_timesheet_task_id + default_task_id = leave_type.company_id.leave_timesheet_task_id if default_task_id and default_task_id.project_id == leave_type.timesheet_project_id: leave_type.timesheet_task_id = default_task_id @@ -46,7 +43,7 @@ class HolidaysType(models.Model): @api.constrains('timesheet_generate', 'timesheet_project_id', 'timesheet_task_id') def _check_timesheet_generate(self): for holiday_status in self: - if holiday_status.timesheet_generate: + if holiday_status.timesheet_generate and holiday_status.company_id: if not holiday_status.timesheet_project_id or not holiday_status.timesheet_task_id: raise ValidationError(_("Both the internal project and task are required to " "generate a timesheet for the time off %s. If you don't want a timesheet, you should " diff --git a/addons/project_timesheet_holidays/tests/test_timesheet_holidays.py b/addons/project_timesheet_holidays/tests/test_timesheet_holidays.py index 69c24e30bc3..40b2e0d405e 100644 --- a/addons/project_timesheet_holidays/tests/test_timesheet_holidays.py +++ b/addons/project_timesheet_holidays/tests/test_timesheet_holidays.py @@ -22,9 +22,8 @@ class TestTimesheetHolidaysCreate(common.TransactionCase): 'requires_allocation': 'no' }) - company = self.env.company - self.assertEqual(status.timesheet_project_id, company.internal_project_id, 'The default project linked to the status should be the same as the company') - self.assertEqual(status.timesheet_task_id, company.leave_timesheet_task_id, 'The default task linked to the status should be the same as the company') + self.assertEqual(status.timesheet_project_id, status.company_id.internal_project_id, 'The default project linked to the status should be the same as the company') + self.assertEqual(status.timesheet_task_id, status.company_id.leave_timesheet_task_id, 'The default task linked to the status should be the same as the company') def test_company_create(self): main_company = self.env.ref('base.main_company')