Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions hr_timesheet_sheet_attendance/models/hr_timesheet_sheet.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from datetime import datetime

import pytz

from odoo import _, api, fields, models
from odoo.exceptions import UserError

Expand Down Expand Up @@ -90,17 +92,29 @@ def check_employee_attendance_state(self):
@api.model
def create(self, vals):
res = super(HrTimesheetSheet, self).create(vals)
tz_name = res.employee_id.user_id.partner_id.tz or "UTC"
employee_tz = pytz.timezone(tz_name)
date_start_utc = (
employee_tz.localize(datetime.combine(res.date_start, datetime.min.time()))
.astimezone(pytz.utc)
.replace(tzinfo=None)
)
date_end_utc = (
employee_tz.localize(datetime.combine(res.date_end, datetime.max.time()))
.astimezone(pytz.utc)
.replace(tzinfo=None)
)
attendances = self.env["hr.attendance"].search(
[
("employee_id", "=", res.employee_id.id),
("sheet_id", "=", False),
("check_in", ">=", res.date_start),
("check_in", "<=", res.date_end),
("check_in", ">=", date_start_utc),
("check_in", "<=", date_end_utc),
"|",
("check_out", "=", False),
"&",
("check_out", ">=", res.date_start),
("check_out", "<=", res.date_end),
("check_out", ">=", date_start_utc),
("check_out", "<=", date_end_utc),
]
)
attendances._compute_sheet_id()
Expand Down
18 changes: 18 additions & 0 deletions hr_timesheet_sheet_attendance/tests/test_hr_timesheet_sheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,24 @@ def test_01_compute_total_time_and_difference(self):
\nMethod: action_timesheet_confirm",
)

def test_02_check_timesheet_compute_old_attendance_timezone(self):
"""sheet_id should be set for attendances created before the sheet
when check_in UTC falls before date_start midnight due to timezone offset"""
self.user_id.tz = "Europe/Brussels"
# 2019-01-14 23:30 UTC = 2019-01-15 00:30 Europe/Brussels (UTC+1)
attendance = self._create_attendance(
employee=self.employee,
checkIn=datetime.datetime(2019, 1, 14, 23, 30, 0),
)
time_sheet = self._create_timesheet_sheet(
self.employee, datetime.date(2019, 1, 15)
)
self.assertEqual(
attendance.sheet_id,
time_sheet,
"Attendance with UTC check_in before date_start midnight should be linked",
)

def test_03_sighin_sighout(self):
"""test Check In/Check Out button on timesheet-sheet"""
time_sheet = self._create_timesheet_sheet(self.employee)
Expand Down
Loading