diff --git a/hr_employee_calendar_planning/models/hr_employee.py b/hr_employee_calendar_planning/models/hr_employee.py index e005af1adb02..9a9ff5fc257a 100644 --- a/hr_employee_calendar_planning/models/hr_employee.py +++ b/hr_employee_calendar_planning/models/hr_employee.py @@ -141,13 +141,30 @@ def _sync_user(self, user): @api.model_create_multi def create(self, vals_list): + # Set calendars if created from an hr job to avoid error + if ( + self.env.context.get("active_model", "hr.job") + and self.env.context.get("active_id") + and not self.env.context.get("skip_employee_calendars_required") + ): + for vals in vals_list: + if not vals.get("calendar_ids"): + vals["calendar_ids"] = [ + ( + 0, + 0, + {"calendar_id": self.env.company.resource_calendar_id.id}, + ), + ] 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