diff --git a/addons_extensions/hr_timeoff_extended/models/hr_timeoff.py b/addons_extensions/hr_timeoff_extended/models/hr_timeoff.py index a1cb5b2d3..d39fa5741 100644 --- a/addons_extensions/hr_timeoff_extended/models/hr_timeoff.py +++ b/addons_extensions/hr_timeoff_extended/models/hr_timeoff.py @@ -305,8 +305,8 @@ class HRLeave(models.Model): def _check_validity(self): for rec in self: if rec.holiday_status_id.limit_leave_requests: - if rec.holiday_status_id.limit_request_count and rec.holiday_status_id.limit_request_type and rec.holiday_status_id.limit_emp_type and rec.holiday_status_id.limit_request_count >= 0: - if rec.employee_id.id in rec.holiday_status_id.limit_emp_type.ids: + if rec.holiday_status_id.limit_request_type and rec.holiday_status_id.limit_emp_type and rec.holiday_status_id.limit_request_count >= 0: + if rec.employee_id.sudo().emp_type and rec.employee_id.sudo().emp_type.id in rec.holiday_status_id.limit_emp_type.ids: time_frame = { 'week': timedelta(weeks=1), 'month': timedelta(days=30), @@ -316,7 +316,7 @@ class HRLeave(models.Model): restriction_start_date = datetime.now() - time_frame # Count the leave requests made by the employee within the restriction period - leave_count = self.env['hr.leave'].search_count([ + leave_count = self.env['hr.leave'].sudo().search_count([ ('employee_id', '=', rec.employee_id.id), ('state', 'not in', ['cancel', 'refuse', 'draft']), # Adjust states if needed ('holiday_status_id', '=', rec.holiday_status_id.id), @@ -324,15 +324,18 @@ class HRLeave(models.Model): ('id','!=',rec.id) ]) if leave_count >= rec.holiday_status_id.limit_request_count: - raise ValidationError(_( - "You have exceeded the maximum allowed leave requests (%s) for the selected period (%s)." - ) % (rec.holiday_status_id.limit_request_count, rec.holiday_status_id.limit_request_type)) + if rec.holiday_status_id.limit_request_count == 0: + raise ValidationError(_("This time off type is not applicable for %s employee. Please contact your admin if required"%(rec.employee_id.sudo().emp_type.name))) + else: + raise ValidationError(_( + "You have exceeded the maximum allowed leave requests (%s) for the selected period (%s)." + ) % (rec.holiday_status_id.limit_request_count, rec.holiday_status_id.limit_request_type)) return super(HRLeave, self)._check_validity() def action_draft(self): for rec in self: - if rec.employee_id.user_id.id != self.env.user.id: + if rec.sudo().employee_id.user_id.id != self.env.user.id: raise ValidationError(_("Only employee can submit his own leave")) rec.submitted_date = fields.Datetime.now() self._check_validity()