[FIX] hr_holidays: keep duration display updated

Versions
--------
- 16.0+

Steps
-----
1. Create a leave spanning multiple days;
2. create a public holiday that falls inside that leave;
3. check leave in list view & form view.

Issue
-----
The leave's duration no longer matches between the two views. In form
view, the duration was updated, in list view, it remained unchanged.

Cause
-----
There's nothing to trigger a recomputation of duration_display when
adding/removing leaves.

Solution
--------
The `number_of_days` field is kept up to date by the usage of
`add_to_compute` when modifying leaves. The same can be done for the
`duration_display` field.

opw-3642500

closes odoo/odoo#158296

X-original-commit: 1327b8014df79a488b1f7c1b909944ac8faff426
Signed-off-by: Bertrand Dossogne (bedo) <bedo@odoo.com>
Signed-off-by: Levi Siuzdak <sile@odoo.com>
This commit is contained in:
Levi Siuzdak (sile)
2024-03-21 15:29:44 +00:00
parent 31cb1a5260
commit 056e5b3c1c
2 changed files with 48 additions and 2 deletions
+1 -1
View File
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import fields, models, api, _
@@ -64,6 +63,7 @@ class CalendarLeaves(models.Model):
'state': 'draft',
})
self.env.add_to_compute(self.env['hr.leave']._fields['number_of_days'], leaves)
self.env.add_to_compute(self.env['hr.leave']._fields['duration_display'], leaves)
sick_time_status = self.env.ref('hr_holidays.holiday_status_sl')
for previous_duration, leave, state in zip(previous_durations, leaves, previous_states):
duration_difference = previous_duration - leave.number_of_days
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from datetime import datetime, date, timedelta
@@ -1083,6 +1082,53 @@ class TestLeaveRequests(TestHrHolidaysCommon):
with self.assertRaises(RuntimeError), self.env.cr.savepoint():
run_validation_flow(leave_validation_type)
@freeze_time('2019-11-01')
def test_duration_display_global_leave(self):
""" Ensure duration_display stays in sync with leave duration. """
employee = self.employee_emp
calendar = employee.resource_calendar_id
sick_leave_type = self.env['hr.leave.type'].create({
'name': 'Sick Leave (days)',
'request_unit': 'day',
'leave_validation_type': 'hr',
})
sick_leave = self.env['hr.leave'].create({
'name': 'Sick 3 days',
'employee_id': employee.id,
'holiday_status_id': sick_leave_type.id,
'request_date_from': '2019-12-23',
'request_date_to': '2019-12-25',
})
comp_leave_type = self.env['hr.leave.type'].create({
'name': 'OT Compensation (hours)',
'request_unit': 'hour',
'leave_validation_type': 'manager',
})
comp_leave = self.env['hr.leave'].create({
'name': 'OT Comp (4 hours)',
'employee_id': employee.id,
'holiday_status_id': comp_leave_type.id,
'request_unit_hours': True,
'request_date_from': '2019-12-26',
'request_date_to': '2019-12-26',
'request_hour_from': '8',
'request_hour_to': '12',
})
self.assertEqual(sick_leave.duration_display, '3 days')
self.assertEqual(comp_leave.duration_display, '4 hours')
calendar.global_leave_ids = [(0, 0, {
'name': 'Winter Holidays',
'date_from': '2019-12-25 00:00:00',
'date_to': '2019-12-26 23:59:59',
'time_type': 'leave',
})]
msg = "hr_holidays: duration_display should update after adding an overlapping holiday"
self.assertEqual(sick_leave.duration_display, '2 days', msg)
self.assertEqual(comp_leave.duration_display, '0 hours', msg)
@freeze_time('2024-01-18')
def test_undefined_working_hours(self):
""" Ensure time-off can also be allocated without ResourceCalendar. """