Skip to content

Commit 7f20097

Browse files
committed
[FIX] hr_work_entry: use relevant tz for work entry generation
**Steps to reproduce** 1. Have a company and employee using a calendar in UTC+8, with attendances in the calendar starting at 7am. 2. Create a global time off generating a work entry. 3. Generate work entries for the employee. Issue: the work entry generated by the global time off is 1 day earlier than expected. **Cause** The date used was in UTC, ignoring the tz defined on the version/ employee/company. https://github.com/odoo/odoo/blob/192c2d98f7bf871c4f221aaa36c1cb2a541d3945/addons/hr_work_entry/models/hr_version.py#L503-L510 opw-5106975 closes odoo#228586 Signed-off-by: Yannick Tivisse (yti) <[email protected]>
1 parent e69a723 commit 7f20097

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

addons/hr_work_entry/models/hr_version.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,8 @@ def _get_tz(version_id):
561561
version = self.env['hr.version'].browse(vals['version_id'])
562562
calendar = version.resource_calendar_id
563563
employee = version.employee_id
564-
vals['date'] = date_start.date()
564+
tz = _get_tz(vals['version_id'])
565+
vals['date'] = date_start.astimezone(tz).date()
565566
vals['duration'] = mapped_version_data[date_start, date_stop][calendar][employee.id]['hours'] if calendar else 0.0
566567
vals.pop('date_start', False)
567568
vals.pop('date_stop', False)

addons/hr_work_entry/tests/test_work_entry.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,20 @@ def test_work_entry_timezone(self):
9797
'contract_date_end': False,
9898
'wage': 1000,
9999
})
100+
self.env['resource.calendar.leaves'].create({
101+
'date_from': pytz.timezone('Asia/Hong_Kong').localize(datetime(2023, 8, 2, 0, 0, 0)).astimezone(pytz.utc).replace(tzinfo=None),
102+
'date_to': pytz.timezone('Asia/Hong_Kong').localize(datetime(2023, 8, 2, 23, 59, 59)).astimezone(pytz.utc).replace(tzinfo=None),
103+
'calendar_id': hk_resource_calendar_id.id,
104+
'work_entry_type_id': self.work_entry_type_leave.id,
105+
})
100106
self.env.company.resource_calendar_id = hk_resource_calendar_id
101-
hk_employee.generate_work_entries(datetime(2023, 8, 1), datetime(2023, 8, 1))
107+
hk_employee.generate_work_entries(datetime(2023, 8, 1), datetime(2023, 8, 2))
102108
work_entries = self.env['hr.work.entry'].search([('employee_id', '=', hk_employee.id)])
109+
self.assertEqual(len(work_entries), 2)
103110
self.assertEqual(work_entries[0].date, date(2023, 8, 1))
104111
self.assertEqual(work_entries[0].duration, 8)
112+
self.assertEqual(work_entries[1].date, date(2023, 8, 2))
113+
self.assertEqual(work_entries[1].duration, 8)
105114

106115
def test_separate_overlapping_work_entries_by_type(self):
107116
calendar = self.env['resource.calendar'].create({'name': 'Calendar', 'tz': 'Europe/Brussels'})

0 commit comments

Comments
 (0)