Skip to content
Snippets Groups Projects
Commit 25f6b68d authored by Thomas Lefebvre (thle)'s avatar Thomas Lefebvre (thle)
Browse files

[FIX] hr_holidays_attendance: check overtime deductible during write


Steps to reproduce:
- install Time Off and Attendances apps;
- making sure an employee doesn't have extra hours;
- create a time off type which is "Deduct Extra Hours" (A);
- create a time off type which is not "Deduct Extra Hours" (B);
- create a time off with the type (A);
- save;
- edit the time off with the type (B);
- save.

Issue:
The time off is modified and it is possible to approve it.

Solution:
Check that the employee has enough extra hours
if the type of time off used is "Deduct Extra Hours".

opw-3141108

closes odoo/odoo#111158

X-original-commit: 2a1db0d8
Signed-off-by: default avatarKevin Baptiste <kba@odoo.com>
Signed-off-by: default avatarLefebvre Thomas (thle) <thle@odoo.com>
parent a602924c
No related branches found
No related tags found
No related merge requests found
......@@ -24,30 +24,16 @@ class HRLeave(models.Model):
@api.model_create_multi
def create(self, vals_list):
res = super().create(vals_list)
today = fields.Date.today()
for leave in res:
if not leave.overtime_deductible:
continue
employee = leave.employee_id.sudo()
duration = leave.number_of_hours_display
if duration > employee.total_overtime:
if employee.user_id == self.env.user:
raise ValidationError(_('You do not have enough extra hours to request this leave'))
raise ValidationError(_('The employee does not have enough extra hours to request this leave.'))
if not leave.overtime_id:
leave.sudo().overtime_id = self.env['hr.attendance.overtime'].sudo().create({
'employee_id': employee.id,
'date': today,
'adjustment': True,
'duration': -1 * duration,
})
self._check_overtime_deductible(res)
return res
def write(self, vals):
res = super().write(vals)
fields_to_check = {'number_of_days', 'date_from', 'date_to', 'state', 'employee_id'}
fields_to_check = {'number_of_days', 'date_from', 'date_to', 'state', 'employee_id', 'holiday_status_id'}
if not any(field for field in fields_to_check if field in vals):
return res
if vals.get('holiday_status_id'):
self._check_overtime_deductible(self)
#User may not have access to overtime_id field
for leave in self.sudo().filtered('overtime_id'):
employee = leave.employee_id
......@@ -59,6 +45,25 @@ class HRLeave(models.Model):
leave.overtime_id.sudo().duration = -1 * duration
return res
def _check_overtime_deductible(self, leaves):
# If the type of leave is overtime deductible, we have to check that the employee has enough extra hours
for leave in leaves:
if not leave.overtime_deductible:
continue
employee = leave.employee_id.sudo()
duration = leave.number_of_hours_display
if duration > employee.total_overtime:
if employee.user_id == self.env.user:
raise ValidationError(_('You do not have enough extra hours to request this leave'))
raise ValidationError(_('The employee does not have enough extra hours to request this leave.'))
if not leave.overtime_id:
leave.sudo().overtime_id = self.env['hr.attendance.overtime'].sudo().create({
'employee_id': employee.id,
'date': fields.Date.today(),
'adjustment': True,
'duration': -1 * duration,
})
def action_draft(self):
overtime_leaves = self.filtered('overtime_deductible')
if any([l.employee_overtime < float_round(l.number_of_hours_display, 2) for l in overtime_leaves]):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment