From e937d480d05f71432369002f280ee62ddd0121df Mon Sep 17 00:00:00 2001 From: "Thomas Lefebvre (thle)" Date: Mon, 3 Jul 2023 07:35:52 +0000 Subject: [PATCH] [FIX] hr_attendance: correct timezone in report Steps to reproduce: ------------------- - with timezone "Europe/Brussels" (UTC +02:00); - create an attendance that starts at 1:00 AM; - go to pivot view in Reporting tab. Issue: ------ Attendance appears to be a day early. Cause: ------ When converting a date to UTC for database storage, it's possible that the date may change (due to a large offset or hours close to 00:00). Therefore, when we use `CAST(check_in as DATE)`, we will keep the date that has potentially changed. Solution: --------- Use postgresql's `at time zone ` statement to convert the recorded attendance to UTC in the employee's timezone. Note: ----- On an employee's form view, the "Working Hours" field (`resource_calendar_id`) is required. The `resource_calendar_id` field is a Many to One to the `resource.calendar` model. The `resource.calendar` model contains the `tz` field, which is required. As a result, it will always be possible to find an employee's timezone via his calendar resource. Hypothesis: ----------- Attendances are created using the browser's timezone. It is normal to think that the employee's calendar timezone is the same as his browser timezone. opw-3370874 closes odoo/odoo#127989 X-original-commit: 8ac7e19ce94685438ab16f052c6550e86e692a75 Signed-off-by: Sofie Gvaladze (sgv) --- addons/hr_attendance/report/hr_attendance_report.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/addons/hr_attendance/report/hr_attendance_report.py b/addons/hr_attendance/report/hr_attendance_report.py index 227d57defe9..c725566bf78 100644 --- a/addons/hr_attendance/report/hr_attendance_report.py +++ b/addons/hr_attendance/report/hr_attendance_report.py @@ -37,7 +37,13 @@ class HRAttendanceReport(models.Model): id, row_number() over (partition by employee_id, CAST(check_in AS DATE)) as ot_check, employee_id, - CAST(check_in as DATE) as check_in, + CAST(check_in + at time zone 'utc' + at time zone + (SELECT calendar.tz FROM resource_calendar as calendar + INNER JOIN hr_employee as employee ON employee.id = employee_id + WHERE calendar.id = employee.resource_calendar_id) + as DATE) as check_in, worked_hours FROM hr_attendance