From 372b3eb462f556fbbc7dc17c1e0c3343957e6d80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Mart=C3=ADnez?= Date: Thu, 29 Jun 2023 14:20:39 +0200 Subject: [PATCH 1/2] [IMP] hr_employee_calendar_planning: Set calendar_ids default value to cover all use cases TT44093 --- .../models/hr_employee.py | 29 ++++++++----------- .../test_hr_employee_calendar_planning.py | 4 +++ 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/hr_employee_calendar_planning/models/hr_employee.py b/hr_employee_calendar_planning/models/hr_employee.py index e005af1adb0..89470889ffc 100644 --- a/hr_employee_calendar_planning/models/hr_employee.py +++ b/hr_employee_calendar_planning/models/hr_employee.py @@ -48,6 +48,16 @@ class HrEmployee(models.Model): copy=True, ) + @api.model + def default_get(self, fields): + """Set calendar_ids default value to cover all use cases.""" + vals = super().default_get(fields) + if "calendar_ids" in fields and not vals.get("calendar_ids"): + vals["calendar_ids"] = [ + (0, 0, {"calendar_id": self.env.company.resource_calendar_id.id}), + ] + return vals + def _regenerate_calendar(self): self.ensure_one() vals_list = [] @@ -122,32 +132,17 @@ def copy(self, default=None): new.filtered("calendar_ids").regenerate_calendar() return new - def _sync_user(self, user): - res = super()._sync_user(user=user) - # set calendar_ids from Create employee button from user - if not self.calendar_ids: - res.update( - { - "calendar_ids": [ - ( - 0, - 0, - {"calendar_id": user.company_id.resource_calendar_id.id}, - ), - ] - } - ) - return res - @api.model_create_multi def create(self, vals_list): res = super().create(vals_list) + # Avoid creating an employee without calendars if ( not self.env.context.get("skip_employee_calendars_required") and not config["test_enable"] and res.filtered(lambda x: not x.calendar_ids) ): raise UserError(_("You can not create employees without any calendars.")) + # Regenerate calendar res.filtered("calendar_ids").regenerate_calendar() return res diff --git a/hr_employee_calendar_planning/tests/test_hr_employee_calendar_planning.py b/hr_employee_calendar_planning/tests/test_hr_employee_calendar_planning.py index aec73b7bf31..6a1c4beb2dc 100644 --- a/hr_employee_calendar_planning/tests/test_hr_employee_calendar_planning.py +++ b/hr_employee_calendar_planning/tests/test_hr_employee_calendar_planning.py @@ -19,6 +19,8 @@ def setUpClass(cls): mail_create_nosubscribe=True, mail_notrack=True, no_reset_password=True, + tracking_disable=True, + test_hr_employee_calendar_planning=True, ) ) resource_calendar = cls.env["resource.calendar"] @@ -73,6 +75,8 @@ def setUpClass(cls): "date_to": "2019-06-10", } ) + # By default a calendar_ids is set, we remove it to better clarify the tests. + cls.employee.write({"calendar_ids": [(2, cls.employee.calendar_ids.id)]}) def test_calendar_planning(self): self.employee.calendar_ids = [ From 045dde510f7c40088bde11826aebf02c1a37a183 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Mart=C3=ADnez?= Date: Tue, 4 Jul 2023 08:12:08 +0200 Subject: [PATCH 2/2] [FIX] hr_employee_calendar_planning: Add ondelete to employee_id and calendar_id fields to allow removal TT44093 --- hr_employee_calendar_planning/models/hr_employee.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hr_employee_calendar_planning/models/hr_employee.py b/hr_employee_calendar_planning/models/hr_employee.py index 89470889ffc..2a123902b37 100644 --- a/hr_employee_calendar_planning/models/hr_employee.py +++ b/hr_employee_calendar_planning/models/hr_employee.py @@ -155,7 +155,7 @@ class HrEmployeeCalendar(models.Model): date_start = fields.Date(string="Start Date",) date_end = fields.Date(string="End Date",) employee_id = fields.Many2one( - comodel_name="hr.employee", string="Employee", required=True, + comodel_name="hr.employee", string="Employee", required=True, ondelete="cascade" ) company_id = fields.Many2one(related="employee_id.company_id") calendar_id = fields.Many2one( @@ -163,6 +163,7 @@ class HrEmployeeCalendar(models.Model): string="Working Time", required=True, check_company=True, + ondelete="restrict", ) _sql_constraints = [