From 7337d9ef110152067c359393d4a4e059f61b40af Mon Sep 17 00:00:00 2001 From: "Thomas Lefebvre (thle)" Date: Thu, 26 Jan 2023 10:04:11 +0000 Subject: [PATCH] [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 Signed-off-by: Lefebvre Thomas (thle) --- .../hr_holidays_attendance/models/hr_leave.py | 45 ++++++++++--------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/addons/hr_holidays_attendance/models/hr_leave.py b/addons/hr_holidays_attendance/models/hr_leave.py index 6468917212a..26c67d0aa20 100644 --- a/addons/hr_holidays_attendance/models/hr_leave.py +++ b/addons/hr_holidays_attendance/models/hr_leave.py @@ -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')