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
25 changes: 16 additions & 9 deletions hr_shift/models/resource_calendar.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright 2024 Tecnativa - David Vidal
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from datetime import datetime
from datetime import datetime, timedelta

import pytz

Expand All @@ -15,16 +15,23 @@ class ResourceCalendar(models.Model):

@api.model
def _resource_shift_for_datetime_range(self, start_dt, end_dt, resources, tz=None):
min_time = datetime.combine(
start_dt, start_dt.min.time(), tzinfo=tz or pytz.UTC
"""Shifts starting on the local days of the range: a night shift
belongs to the day it starts on"""
tz = tz or pytz.UTC
# end_dt is exclusive, and start_dt == end_dt stands for that day
end_dt = max(start_dt, end_dt - timedelta(microseconds=1))
min_time = tz.localize(
datetime.combine(start_dt.astimezone(tz).date(), datetime.min.time())
)
max_time = tz.localize(
datetime.combine(end_dt.astimezone(tz).date(), datetime.max.time())
)
max_time = datetime.combine(end_dt, end_dt.max.time(), tzinfo=tz or pytz.UTC)
shifts = self.env["hr.shift.planning.line"].search(
[
("resource_id", "in", resources.ids),
("state", "=", "assigned"),
("start_time", ">=", min_time),
("end_time", "<=", max_time),
("start_time", "<=", max_time),
]
)
return shifts
Expand All @@ -47,19 +54,19 @@ def _attendance_intervals_batch(
intervals_to_remove = []
resource_intervals = res[resource.id]._items
for shift in shifts:
start_time = string_to_datetime(shift.start_time).astimezone(tz)
end_time = string_to_datetime(shift.end_time).astimezone(tz)
# Remove any other interval that belongs to any of the dates of the
# shift (no matter the hours, as if the shift is around midnight,
# there will be a mix of 2 dates)
intervals_to_remove += [
(start, end, resource_item)
for start, end, resource_item in resource_intervals
if (
shift.start_time.date() == start.date()
or shift.end_time.date() == end.date()
start_time.date() == start.date()
or end_time.date() == end.date()
)
]
start_time = string_to_datetime(shift.start_time).astimezone(tz)
end_time = string_to_datetime(shift.end_time).astimezone(tz)
intervals_to_add.append((start_time, end_time, shift))
res[resource.id]._items = [
x for x in resource_intervals if x not in intervals_to_remove
Expand Down
43 changes: 28 additions & 15 deletions hr_shift/models/shift_planning.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright 2024 Tecnativa - David Vidal
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from datetime import datetime
from datetime import datetime, timedelta

import pytz
from dateutil.relativedelta import relativedelta
Expand Down Expand Up @@ -429,6 +429,13 @@ def _compute_shift_time(self):
and shift.template_id._prepare_time()["end_time"]
or {"hour": 23, "minute": 59}
)
end_date = shift_date
if (
shift.template_id
and shift.template_id.end_time <= shift.template_id.start_time
):
# Overnight shift: the end time falls on the next day
end_date += timedelta(days=1)
tz = pytz.timezone(shift.template_id.tz or self.env.user.tz)
start_time = tz.localize(
datetime.combine(
Expand All @@ -440,7 +447,7 @@ def _compute_shift_time(self):
)
end_time = tz.localize(
datetime.combine(
shift_date,
end_date,
datetime.min.time().replace(
hour=end_time["hour"], minute=end_time["minute"]
),
Expand Down Expand Up @@ -477,25 +484,31 @@ def _is_on_leave(self):
if not (self.start_time and self.end_time and self.employee_id):
return False
local_tz = pytz.timezone(self.template_id.tz or self.env.user.tz)
start_time = fields.datetime.combine(
pytz.utc.localize(self.start_time).astimezone(local_tz),
self.start_time.min.time(),
# Local day the shift starts on, in UTC
local_start_day = pytz.utc.localize(self.start_time).astimezone(local_tz).date()
start_time = (
local_tz.localize(datetime.combine(local_start_day, datetime.min.time()))
.astimezone(pytz.utc)
.replace(tzinfo=None)
)
end_time = fields.datetime.combine(
pytz.utc.localize(self.end_time).astimezone(local_tz),
self.end_time.max.time(),
end_time = (
local_tz.localize(datetime.combine(local_start_day, datetime.max.time()))
.astimezone(pytz.utc)
.replace(tzinfo=None)
)
return bool(
self.env["resource.calendar.leaves"]
.sudo()
.search(
[
("resource_id", "=", self.employee_id.resource_id.id),
("date_from", "<=", end_time),
("date_to", ">=", start_time),
]
)
.search(self._get_is_on_leave_domain(start_time, end_time))
)

def _get_is_on_leave_domain(self, start_time, end_time):
"""Hook to extend the leave search domain"""
return [
("resource_id", "=", self.employee_id.resource_id.id),
("date_from", "<=", end_time),
("date_to", ">=", start_time),
]

def action_unassign_shift(self):
self.template_id = False
70 changes: 70 additions & 0 deletions hr_shift/tests/test_hr_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,73 @@ def test_hr_shift_current_shift_id_two_shifts_same_day_after_both(self):
}
)
self.assertFalse(self.employee_a.current_shift_id)

def _assign_night_shift(self):
template = self.env["hr.shift.template"].create(
{"name": "Night", "start_time": 22.0, "end_time": 6.0, "tz": "UTC"}
)
self.planning.generate_shifts()
line = self.planning.shift_ids.filtered(
lambda x: x.employee_id == self.employee_a
).line_ids.filtered(lambda x: x.day_number == "0")
line.template_id = template
return line

def test_night_shift(self):
line = self._assign_night_shift()
self.assertEqual(line.end_time, fields.Datetime.from_string("2025-01-14 06:00"))
resource = self.employee_a.resource_id
day = datetime(2025, 1, 13, tzinfo=pytz.utc)
shifts = self.calendar._resource_shift_for_datetime_range(day, day, resource)
self.assertIn(line, shifts)
next_day = datetime(2025, 1, 14, tzinfo=pytz.utc)
intervals = self.calendar._attendance_intervals_batch(
next_day, next_day, resources=resource
)[resource.id]
# The tail of the night shift doesn't belong to the next day
self.assertFalse([i for i in intervals if i[2] == line])

def test_night_shift_leave_next_day(self):
self.env["resource.calendar.leaves"].create(
{
"resource_id": self.employee_a.resource_id.id,
"date_from": "2025-01-14 08:00:00",
"date_to": "2025-01-14 17:00:00",
}
)
line = self._assign_night_shift()
self.assertEqual(line.state, "assigned")

def test_night_shift_attendance_intervals_local_day(self):
madrid = pytz.timezone("Europe/Madrid")
template = self.env["hr.shift.template"].create(
{
"name": "Night",
"start_time": 22.0,
"end_time": 6.0,
"tz": "Europe/Madrid",
}
)
self.planning.generate_shifts()
lines = self.planning.shift_ids.filtered(
lambda x: x.employee_id == self.employee_a
).line_ids.filtered(lambda x: x.day_number in ("0", "1"))
lines.template_id = template
line_1 = lines.filtered(lambda x: x.day_number == "1")
# A local day given in UTC: its night shift is whole, not cut at
# midnight, and the tail of the previous one isn't included
start_dt = madrid.localize(datetime(2025, 1, 14)).astimezone(pytz.utc)
end_dt = madrid.localize(datetime(2025, 1, 15)).astimezone(pytz.utc)
res = self.calendar._attendance_intervals_batch(
start_dt, end_dt, resources=self.employee_a.resource_id, tz=madrid
)[self.employee_a.resource_id.id]
self.assertEqual(
[i for i in res if i[2] in lines],
[
(
madrid.localize(datetime(2025, 1, 14, 22)),
madrid.localize(datetime(2025, 1, 15, 6)),
line_1,
)
],
)
Loading