Skip to content

Commit

Permalink
[FIX] hr_holidays: keep duration display updated
Browse files Browse the repository at this point in the history
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#158296

X-original-commit: 1327b80
Signed-off-by: Bertrand Dossogne (bedo) <bedo@odoo.com>
Signed-off-by: Levi Siuzdak <sile@odoo.com>
  • Loading branch information
lvsz committed Mar 21, 2024
1 parent 31cb1a5 commit 056e5b3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion addons/hr_holidays/models/resource.py
Original file line number Diff line number Diff line change
@@ -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, _
Expand Down Expand Up @@ -64,6 +63,7 @@ def _reevaluate_leaves(self, time_domain_dict):
'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
Expand Down
48 changes: 47 additions & 1 deletion addons/hr_holidays/tests/test_leave_requests.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -1083,6 +1082,53 @@ def run_validation_flow(leave_validation_type):
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. """
Expand Down

0 comments on commit 056e5b3

Please sign in to comment.