[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#111166

X-original-commit: 2a1db0d803ea04a6d94f3d07ba3ea7ddcb24828e
Signed-off-by: Kevin Baptiste <kba@odoo.com>
Signed-off-by: Lefebvre Thomas (thle) <thle@odoo.com>
This commit is contained in:
Thomas Lefebvre (thle)
2023-01-27 08:17:52 +01:00
parent a3e6b5caae
commit 7337d9ef11
@@ -24,8 +24,30 @@ 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:
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', '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
duration = leave.number_of_hours_display
overtime_duration = leave.overtime_id.sudo().duration
if overtime_duration != duration:
if duration > employee.total_overtime - overtime_duration:
raise ValidationError(_('The employee does not have enough extra hours to extend this leave.'))
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()
@@ -37,27 +59,10 @@ class HRLeave(models.Model):
if not leave.overtime_id:
leave.sudo().overtime_id = self.env['hr.attendance.overtime'].sudo().create({
'employee_id': employee.id,
'date': today,
'date': fields.Date.today(),
'adjustment': True,
'duration': -1 * duration,
})
return res
def write(self, vals):
res = super().write(vals)
fields_to_check = {'number_of_days', 'date_from', 'date_to', 'state', 'employee_id'}
if not any(field for field in fields_to_check if field in vals):
return res
#User may not have access to overtime_id field
for leave in self.sudo().filtered('overtime_id'):
employee = leave.employee_id
duration = leave.number_of_hours_display
overtime_duration = leave.overtime_id.sudo().duration
if overtime_duration != duration:
if duration > employee.total_overtime - overtime_duration:
raise ValidationError(_('The employee does not have enough extra hours to extend this leave.'))
leave.overtime_id.sudo().duration = -1 * duration
return res
def action_draft(self):
overtime_leaves = self.filtered('overtime_deductible')