Skip to content

Commit

Permalink
fixup! [IMP] resource_booking: Allow a booking to span more than one …
Browse files Browse the repository at this point in the history
…calendar day
  • Loading branch information
carmenbianca committed Jan 31, 2023
1 parent 6cc9e4c commit 87755a8
Showing 1 changed file with 28 additions and 29 deletions.
57 changes: 28 additions & 29 deletions resource_booking/models/resource_booking.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@
from odoo.addons.resource.models.resource import Intervals


def _availability_is_fitting(available_intervals, start_dt, end_dt, combination=None):
start_date = start_dt.date()
end_date = end_dt.date()
# Booking is uninterrupted on the same calendar day.
if len(available_intervals) == 1 and available_intervals._items[0][:2] == (
start_dt,
end_dt,
):
return True
# Booking spans more than one calendar day, e.g. from 23:00 to 1:00
# the next day.
elif (
available_intervals
and start_date != end_date
and (end_date - start_date).days == (len(available_intervals) - 1)
):
return True
return False


class ResourceBooking(models.Model):
_name = "resource.booking"
_inherit = ["mail.thread", "mail.activity.mixin", "portal.mixin"]
Expand Down Expand Up @@ -353,26 +373,10 @@ def _check_scheduling(self):
if already_happened:
unfitting_bookings -= booking
continue
meeting_dates = tuple(
fields.Datetime.context_timestamp(self, booking[field])
for field in ("start", "stop")
)
start_date = meeting_dates[0].date()
end_date = meeting_dates[1].date()
available_intervals = booking._get_intervals(*meeting_dates)
# Booking is uninterrupted on the same calendar day.
if (
len(available_intervals) == 1
and available_intervals._items[0][:2] == meeting_dates
):
unfitting_bookings -= booking
# Booking spans more than one calendar day, e.g. from 23:00 to 1:00
# the next day.
elif (
available_intervals
and start_date != end_date
and (end_date - start_date).days == (len(available_intervals) - 1)
):
start_dt = fields.Datetime.context_timestamp(self, booking["start"])
end_dt = fields.Datetime.context_timestamp(self, booking["stop"])
available_intervals = booking._get_intervals(start_dt, end_dt)
if _availability_is_fitting(available_intervals, start_dt, end_dt):
unfitting_bookings -= booking
# Explain which bookings failed validation
if unfitting_bookings:
Expand Down Expand Up @@ -434,17 +438,12 @@ def _get_best_combination(self):
sorted_combinations = self.combination_id + (
self.type_id._get_combinations_priorized() - self.combination_id
)
desired_interval = tuple(
fields.Datetime.context_timestamp(self, dt)
for dt in (self.start, self.stop)
)
start_dt = fields.Datetime.context_timestamp(self, self.start)
end_dt = fields.Datetime.context_timestamp(self, self.stop)
# Get 1st combination available in the desired interval
for combination in sorted_combinations:
availability = self._get_intervals(*desired_interval, combination)
if (
len(availability) == 1
and availability._items[0][:2] == desired_interval
):
available_intervals = self._get_intervals(start_dt, end_dt, combination)
if _availability_is_fitting(available_intervals, start_dt, end_dt):
return combination
# Tell portal user there's no combination available
if self.env.context.get("using_portal"):
Expand Down

0 comments on commit 87755a8

Please sign in to comment.