diff --git a/addons/project_timesheet_holidays/__init__.py b/addons/project_timesheet_holidays/__init__.py
index dc5e6b693d1..cb09489a1e7 100644
--- a/addons/project_timesheet_holidays/__init__.py
+++ b/addons/project_timesheet_holidays/__init__.py
@@ -2,3 +2,19 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import models
+
+
+def post_init(cr, registry):
+ """ Set the timesheet project and task on existing leave type. Do it in post_init to
+ be sure the internal project/task of res.company are set. (Since timesheet_generate field
+ is true by default, those 2 fields are required on the leave type).
+ """
+ from odoo import api, SUPERUSER_ID
+
+ env = api.Environment(cr, SUPERUSER_ID, {})
+ for leave_type in env['hr.holidays.status'].search([('timesheet_generate', '=', True), ('timesheet_project_id', '=', False)]):
+ company = leave_type.company_id or env.user.company_id
+ leave_type.write({
+ 'timesheet_project_id': company.leave_timesheet_project_id.id,
+ 'timesheet_task_id': company.leave_timesheet_task_id.id,
+ })
diff --git a/addons/project_timesheet_holidays/__manifest__.py b/addons/project_timesheet_holidays/__manifest__.py
index 39f549e5b0c..d98cef28b51 100644
--- a/addons/project_timesheet_holidays/__manifest__.py
+++ b/addons/project_timesheet_holidays/__manifest__.py
@@ -17,8 +17,11 @@ on leaves. Project and task can be configured company-wide.
'data': [
'views/res_config_settings_views.xml',
'views/hr_holidays_views.xml',
+ 'security/ir.model.access.csv',
+
],
'demo': [],
'installable': True,
'auto_install': True,
+ 'post_init_hook': 'post_init',
}
diff --git a/addons/project_timesheet_holidays/models/hr_holidays.py b/addons/project_timesheet_holidays/models/hr_holidays.py
index a2d2457f573..d71ece9631b 100644
--- a/addons/project_timesheet_holidays/models/hr_holidays.py
+++ b/addons/project_timesheet_holidays/models/hr_holidays.py
@@ -1,13 +1,14 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
-from odoo import api, fields, models
+from odoo import api, fields, models, _
+from odoo.exceptions import ValidationError
class HolidaysType(models.Model):
_inherit = "hr.holidays.status"
- timesheet_generate = fields.Boolean('Generate Timesheet', help="If checked, when validating a leave, timesheet will be generated in the Vacation Project of the company.")
+ timesheet_generate = fields.Boolean('Generate Timesheet', default=True, help="If checked, when validating a leave, timesheet will be generated in the Vacation Project of the company.")
timesheet_project_id = fields.Many2one('project.project', string="Internal Project", help="The project will contain the timesheet generated when a leave is validated.")
timesheet_task_id = fields.Many2one('project.task', string="Internal Task for timesheet", domain="[('project_id', '=', timesheet_project_id)]")
@@ -21,6 +22,13 @@ class HolidaysType(models.Model):
self.timesheet_project_id = False
self.timesheet_task_id = False
+ @api.constrains('timesheet_generate')
+ def _check_timesheet_generate(self):
+ for holiday_status in self:
+ if holiday_status.timesheet_generate:
+ if not holiday_status.timesheet_project_id or not holiday_status.timesheet_task_id:
+ raise ValidationError(_('For the leaves to generate timesheet, the internal project and task are requried.'))
+
class Holidays(models.Model):
_inherit = "hr.holidays"
diff --git a/addons/project_timesheet_holidays/security/ir.model.access.csv b/addons/project_timesheet_holidays/security/ir.model.access.csv
new file mode 100644
index 00000000000..03c142ccb21
--- /dev/null
+++ b/addons/project_timesheet_holidays/security/ir.model.access.csv
@@ -0,0 +1,2 @@
+id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
+access_account_analytic_account_leaves_manager,account.analytic.account,analytic.model_account_analytic_account,hr_holidays.group_hr_holidays_manager,1,0,0,0