diff --git a/addons/hr_holidays/models/hr_leave.py b/addons/hr_holidays/models/hr_leave.py index 4b89a1f2ab5d4..d2848401664bc 100644 --- a/addons/hr_holidays/models/hr_leave.py +++ b/addons/hr_holidays/models/hr_leave.py @@ -105,13 +105,22 @@ def _default_get_request_dates(self, values): # Instead of overwriting all the javascript methods to use # request_date_{from,to} instead of date_{from,to}, we just convert # date_{from,to} to request_date_{from,to} here. + + # Request dates are determined during an onchange scenario. + # To ensure that the values are correct in the client context (UI), + # the timezone must be applied (because no processing is carried out + # when these dates are received on the frontend). + # Note: + # Without the application of the timezone, days based on UTC datetimes + # will be returned (and will therefore not be correct for the client). + client_tz = timezone(self._context.get('tz') or self.env.user.tz or 'UTC') if values.get('date_from'): if not values.get('request_date_from'): - values['request_date_from'] = values['date_from'] + values['request_date_from'] = pytz.utc.localize(values['date_from']).astimezone(client_tz) del values['date_from'] if values.get('date_to'): if not values.get('request_date_to'): - values['request_date_to'] = values['date_to'] + values['request_date_to'] = pytz.utc.localize(values['date_to']).astimezone(client_tz) del values['date_to'] return values diff --git a/addons/hr_holidays/tests/test_leave_requests.py b/addons/hr_holidays/tests/test_leave_requests.py index 1389926a44e36..52ecd50673d99 100644 --- a/addons/hr_holidays/tests/test_leave_requests.py +++ b/addons/hr_holidays/tests/test_leave_requests.py @@ -1152,3 +1152,19 @@ def test_undefined_working_hours(self): }) holiday_status = self.holidays_type_4.with_user(self.user_employee_id) self._check_holidays_status(holiday_status, employee, 20.0, 0.0, 20.0, 16.0) + + def test_default_request_date_timezone(self): + """ + The purpose is to test whether the timezone is + taken into account when requesting a leave. + """ + self.user_employee.tz = 'Hongkong' # UTC +08:00 + context = { + # `date_from/to` in UTC to simulate client values + 'default_date_from': '2024-03-27 23:00:00', + 'default_date_to': '2024-03-28 08:00:00', + } + leave_form = Form(self.env['hr.leave'].with_user(self.user_employee).with_context(context)) + leave_form.holiday_status_id = self.holidays_type_2 + leave = leave_form.save() + self.assertEqual(leave.number_of_days, 1.0)