[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 <timezone_name>`
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) <sgv@odoo.com>
This commit is contained in:
Thomas Lefebvre (thle)
2023-07-10 21:25:51 +02:00
parent 9ac7694b9f
commit e937d480d0
@@ -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