2019-05-07 12:24:51 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-04 16:55:02 +02:00
|
|
|
# A Time range, slicing an Availability.
|
|
|
|
# Slots duration are defined globally by Setting.get('slot_duration') but can be
|
|
|
|
# overridden per availability.
|
2020-03-25 10:16:47 +01:00
|
|
|
class Slot < ApplicationRecord
|
2016-03-23 18:39:41 +01:00
|
|
|
include NotifyWith::NotificationAttachedObject
|
|
|
|
|
2017-02-28 13:23:31 +01:00
|
|
|
has_many :slots_reservations, dependent: :destroy
|
|
|
|
has_many :reservations, through: :slots_reservations
|
2016-03-23 18:39:41 +01:00
|
|
|
belongs_to :availability
|
|
|
|
|
2022-12-28 17:51:27 +01:00
|
|
|
has_many :cart_item_reservation_slots, class_name: 'CartItem::ReservationSlot', dependent: :destroy
|
|
|
|
|
2023-01-11 17:32:16 +01:00
|
|
|
attr_accessor :is_reserved, :machine, :space, :title, :can_modify, :current_user_slots_reservations_ids, :current_user_pending_reservations_ids
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2022-07-20 11:22:00 +02:00
|
|
|
def full?(reservable = nil)
|
|
|
|
availability_places = availability.available_places_per_slot(reservable)
|
2022-07-13 16:28:43 +02:00
|
|
|
return false if availability_places.nil?
|
|
|
|
|
2022-07-25 16:41:48 +02:00
|
|
|
if reservable.nil?
|
|
|
|
slots_reservations.where(canceled_at: nil).count >= availability_places
|
|
|
|
else
|
|
|
|
slots_reservations.includes(:reservation).where(canceled_at: nil).where('reservations.reservable': reservable).count >= availability_places
|
|
|
|
end
|
2022-07-13 16:28:43 +02:00
|
|
|
end
|
|
|
|
|
2023-01-02 19:20:02 +01:00
|
|
|
def empty?(reservable = nil)
|
|
|
|
if reservable.nil?
|
|
|
|
slots_reservations.where(canceled_at: nil).count.zero?
|
|
|
|
else
|
|
|
|
slots_reservations.includes(:reservation).where(canceled_at: nil).where('reservations.reservable': reservable).count.zero?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-13 16:28:43 +02:00
|
|
|
def duration
|
|
|
|
(end_at - start_at).seconds
|
2017-02-27 16:15:27 +01:00
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|