2021-04-22 19:24:08 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# A generic reservation added to the shopping cart
|
|
|
|
class CartItem::Reservation < CartItem::BaseItem
|
2023-01-12 17:48:52 +01:00
|
|
|
self.table_name = 'cart_item_reservations'
|
|
|
|
|
2023-01-09 17:36:11 +01:00
|
|
|
MINUTES_PER_HOUR = 60.0
|
|
|
|
SECONDS_PER_MINUTE = 60.0
|
|
|
|
|
|
|
|
GET_SLOT_PRICE_DEFAULT_OPTS = { has_credits: false, elements: nil, is_division: true, prepaid: { minutes: 0 }, custom_duration: nil }.freeze
|
2022-12-28 17:51:27 +01:00
|
|
|
|
|
|
|
def reservable
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2023-03-10 16:14:38 +01:00
|
|
|
# @return [Plan,NilClass]
|
2022-12-28 17:51:27 +01:00
|
|
|
def plan
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2023-03-10 16:14:38 +01:00
|
|
|
# @return [User]
|
2022-12-28 17:51:27 +01:00
|
|
|
def operator
|
|
|
|
operator_profile.user
|
|
|
|
end
|
|
|
|
|
2023-03-10 16:14:38 +01:00
|
|
|
# @return [User]
|
2022-12-28 17:51:27 +01:00
|
|
|
def customer
|
|
|
|
customer_profile.user
|
2021-04-22 19:24:08 +02:00
|
|
|
end
|
|
|
|
|
2023-03-10 16:14:38 +01:00
|
|
|
# @return [Hash{Symbol=>Integer,Hash{Symbol=>Array<Hash{Symbol=>Integer,Float,Boolean,Time}>}}]
|
2021-04-22 19:24:08 +02:00
|
|
|
def price
|
2022-12-28 17:51:27 +01:00
|
|
|
is_privileged = operator.privileged? && operator.id != customer.id
|
|
|
|
prepaid = { minutes: PrepaidPackService.minutes_available(customer, reservable) }
|
2021-04-22 19:24:08 +02:00
|
|
|
|
2023-01-04 12:08:37 +01:00
|
|
|
raise InvalidGroupError, I18n.t('cart_items.group_subscription_mismatch') if !@plan.nil? && @customer.group_id != @plan.group_id
|
|
|
|
|
2021-04-22 19:24:08 +02:00
|
|
|
elements = { slots: [] }
|
|
|
|
amount = 0
|
|
|
|
|
|
|
|
hours_available = credits
|
2022-10-26 17:35:02 +02:00
|
|
|
grouped_slots.each_value do |slots|
|
2021-12-29 22:07:28 +01:00
|
|
|
prices = applicable_prices(slots)
|
|
|
|
slots.each_with_index do |slot, index|
|
|
|
|
amount += get_slot_price_from_prices(prices, slot, is_privileged,
|
|
|
|
elements: elements,
|
|
|
|
has_credits: (index < hours_available),
|
|
|
|
prepaid: prepaid)
|
|
|
|
end
|
2021-04-22 19:24:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
{ elements: elements, amount: amount }
|
|
|
|
end
|
|
|
|
|
2023-03-10 16:14:38 +01:00
|
|
|
# @return [String,NilClass]
|
2021-04-23 12:52:06 +02:00
|
|
|
def name
|
2022-12-28 17:51:27 +01:00
|
|
|
reservable&.name
|
2021-04-23 12:52:06 +02:00
|
|
|
end
|
|
|
|
|
2023-03-10 16:14:38 +01:00
|
|
|
# @param all_items [Array<CartItem::BaseItem>]
|
|
|
|
# @return [Boolean]
|
2022-12-30 15:29:52 +01:00
|
|
|
def valid?(all_items = [])
|
2021-05-31 15:39:56 +02:00
|
|
|
pending_subscription = all_items.find { |i| i.is_a?(CartItem::Subscription) }
|
2023-03-13 17:29:06 +01:00
|
|
|
plan = pending_subscription&.plan || customer&.subscribed_plan
|
2022-12-22 17:18:34 +01:00
|
|
|
|
2023-03-13 17:29:06 +01:00
|
|
|
unless ReservationLimitService.authorized?(plan, customer, self, all_items)
|
2023-02-24 17:26:55 +01:00
|
|
|
errors.add(:reservation, I18n.t('cart_item_validation.limit_reached', **{
|
2023-03-16 12:02:08 +01:00
|
|
|
HOURS: ReservationLimitService.limit(plan, reservable).limit,
|
2023-03-14 11:54:41 +01:00
|
|
|
RESERVABLE: reservable.name
|
|
|
|
}))
|
2021-05-31 15:39:56 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2023-03-13 17:29:06 +01:00
|
|
|
cart_item_reservation_slots.each do |sr|
|
2023-03-15 20:26:45 +01:00
|
|
|
return false unless validate_slot_reservation(sr, pending_subscription, errors)
|
2023-03-13 17:29:06 +01:00
|
|
|
end
|
|
|
|
|
2021-05-31 15:39:56 +02:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2023-03-10 16:14:38 +01:00
|
|
|
# @return [Reservation]
|
2022-12-28 17:51:27 +01:00
|
|
|
def to_object
|
|
|
|
::Reservation.new(
|
|
|
|
reservable_id: reservable_id,
|
|
|
|
reservable_type: reservable_type,
|
|
|
|
slots_reservations_attributes: slots_params,
|
|
|
|
statistic_profile_id: StatisticProfile.find_by(user: customer).id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-04-22 19:24:08 +02:00
|
|
|
protected
|
|
|
|
|
|
|
|
def credits
|
|
|
|
0
|
|
|
|
end
|
|
|
|
|
2021-12-29 22:07:28 +01:00
|
|
|
# Group the slots by date, if the extended_prices_in_same_day option is set to true
|
2023-03-10 16:14:38 +01:00
|
|
|
# @return [Hash{Symbol => Array<CartItem::ReservationSlot>}]
|
2021-12-29 22:07:28 +01:00
|
|
|
def grouped_slots
|
2022-12-28 17:51:27 +01:00
|
|
|
return { all: cart_item_reservation_slots } unless Setting.get('extended_prices_in_same_day')
|
2022-07-13 16:28:43 +02:00
|
|
|
|
2022-12-28 17:51:27 +01:00
|
|
|
cart_item_reservation_slots.group_by { |slot| slot.slot[:start_at].to_date }
|
2021-12-29 22:07:28 +01:00
|
|
|
end
|
|
|
|
|
2021-12-21 15:30:08 +01:00
|
|
|
# Compute the price of a single slot, according to the list of applicable prices.
|
2023-01-12 17:48:52 +01:00
|
|
|
# @param prices [Hash{Symbol => Array<Hash{Symbol => Number,Price}>}] list of prices to use with the current reservation,
|
|
|
|
# as returned by #get_slot_price
|
|
|
|
# @option prices [Array<Hash{Symbol => Number,Price}>}] :prices => {price: Price, duration: number}
|
|
|
|
# @param slot_reservation [CartItem::ReservationSlot]
|
|
|
|
# @param is_privileged [Boolean]
|
|
|
|
# @param options [Hash] optional parameters, allowing the following options:
|
|
|
|
# @option options [Array] :elements if provided the resulting price will be append into elements.slots
|
|
|
|
# @option options [Boolean] :has_credits true if the user still has credits for the given slot, false if not provided
|
|
|
|
# @option options [Boolean] :is_division false if the slot covers a full availability, true if it is a subdivision (default)
|
|
|
|
# @option options [Number] :prepaid_minutes number of remaining prepaid minutes for the customer
|
2023-03-10 16:14:38 +01:00
|
|
|
# @return [Float,Integer]
|
2022-12-28 17:51:27 +01:00
|
|
|
def get_slot_price_from_prices(prices, slot_reservation, is_privileged, options = {})
|
2021-12-21 15:30:08 +01:00
|
|
|
options = GET_SLOT_PRICE_DEFAULT_OPTS.merge(options)
|
|
|
|
|
2022-12-28 17:51:27 +01:00
|
|
|
slot_minutes = (slot_reservation.slot[:end_at].to_time - slot_reservation.slot[:start_at].to_time) / SECONDS_PER_MINUTE
|
2021-12-21 15:30:08 +01:00
|
|
|
price = prices[:prices].find { |p| p[:duration] <= slot_minutes && p[:duration].positive? }
|
|
|
|
price = prices[:prices].first if price.nil?
|
2022-10-26 17:35:02 +02:00
|
|
|
hourly_rate = ((Rational(price[:price].amount.to_f) / Rational(price[:price].duration)) * Rational(MINUTES_PER_HOUR)).to_f
|
2021-12-21 15:30:08 +01:00
|
|
|
|
|
|
|
# apply the base price to the real slot duration
|
2022-12-28 17:51:27 +01:00
|
|
|
real_price = get_slot_price(hourly_rate, slot_reservation, is_privileged, options)
|
2021-12-21 15:30:08 +01:00
|
|
|
|
|
|
|
price[:duration] -= slot_minutes
|
|
|
|
|
|
|
|
real_price
|
|
|
|
end
|
|
|
|
|
2021-04-22 19:24:08 +02:00
|
|
|
# Compute the price of a single slot, according to the base price and the ability for an admin
|
|
|
|
# to offer the slot.
|
2023-01-12 17:48:52 +01:00
|
|
|
# @param hourly_rate [Float] base price of a slot
|
|
|
|
# @param slot_reservation [CartItem::ReservationSlot]
|
|
|
|
# @param is_privileged [Boolean] true if the current user has a privileged role (admin or manager)
|
|
|
|
# @param options [Hash] optional parameters, allowing the following options:
|
|
|
|
# @option options [Array] :elements if provided the resulting price will be append into elements.slots
|
|
|
|
# @option options [Boolean] :has_credits true if the user still has credits for the given slot, false if not provided
|
|
|
|
# @option options [Boolean] :is_division false if the slot covers a full availability, true if it is a subdivision (default)
|
|
|
|
# @option options [Number] :prepaid_minutes number of remaining prepaid minutes for the customer
|
2023-03-10 16:14:38 +01:00
|
|
|
# @return [Float,Integer] price of the slot
|
2022-12-28 17:51:27 +01:00
|
|
|
def get_slot_price(hourly_rate, slot_reservation, is_privileged, options = {})
|
2021-04-22 19:24:08 +02:00
|
|
|
options = GET_SLOT_PRICE_DEFAULT_OPTS.merge(options)
|
|
|
|
|
2022-12-28 17:51:27 +01:00
|
|
|
slot_rate = options[:has_credits] || (slot_reservation[:offered] && is_privileged) ? 0 : hourly_rate
|
|
|
|
slot_minutes = (slot_reservation.slot[:end_at].to_time - slot_reservation.slot[:start_at].to_time) / SECONDS_PER_MINUTE
|
2021-06-30 10:53:05 +02:00
|
|
|
# apply the base price to the real slot duration
|
2021-04-22 19:24:08 +02:00
|
|
|
real_price = if options[:is_division]
|
2022-10-26 17:35:02 +02:00
|
|
|
((Rational(slot_rate) / Rational(MINUTES_PER_HOUR)) * Rational(slot_minutes)).to_f
|
2021-04-22 19:24:08 +02:00
|
|
|
else
|
|
|
|
slot_rate
|
|
|
|
end
|
2023-01-12 17:48:52 +01:00
|
|
|
|
|
|
|
real_price = handle_prepaid_pack_price(real_price, slot_minutes, slot_rate, options)
|
2021-04-22 19:24:08 +02:00
|
|
|
|
|
|
|
unless options[:elements].nil?
|
|
|
|
options[:elements][:slots].push(
|
2023-01-12 17:48:52 +01:00
|
|
|
slot_id: slot_reservation.slot_id,
|
2022-12-28 17:51:27 +01:00
|
|
|
start_at: slot_reservation.slot[:start_at],
|
2021-04-22 19:24:08 +02:00
|
|
|
price: real_price,
|
|
|
|
promo: (slot_rate != hourly_rate)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
real_price
|
|
|
|
end
|
|
|
|
|
2023-01-12 17:48:52 +01:00
|
|
|
# @param price [Float, Integer]
|
|
|
|
# @param slot_minutes [Number]
|
|
|
|
# @param options [Hash] optional parameters, allowing the following options:
|
|
|
|
# @option options [Array] :elements if provided the resulting price will be append into elements.slots
|
|
|
|
# @option options [Boolean] :has_credits true if the user still has credits for the given slot, false if not provided
|
|
|
|
# @option options [Boolean] :is_division false if the slot covers a full availability, true if it is a subdivision (default)
|
|
|
|
# @option options [Number] :prepaid_minutes number of remaining prepaid minutes for the customer
|
|
|
|
# @return [Float, Integer]
|
|
|
|
def handle_prepaid_pack_price(price, slot_minutes, slot_rate, options)
|
|
|
|
return price unless price.positive? && options[:prepaid][:minutes]&.positive?
|
|
|
|
|
|
|
|
# subtract free minutes from prepaid packs
|
|
|
|
consumed = slot_minutes
|
|
|
|
consumed = options[:prepaid][:minutes] if slot_minutes > options[:prepaid][:minutes]
|
|
|
|
options[:prepaid][:minutes] -= consumed
|
|
|
|
(Rational(slot_minutes - consumed) * (Rational(slot_rate) / Rational(MINUTES_PER_HOUR))).to_f
|
|
|
|
end
|
|
|
|
|
2021-12-21 15:30:08 +01:00
|
|
|
# We determine the list of prices applicable to current reservation
|
|
|
|
# The longest available price is always used in priority.
|
2021-12-20 17:08:14 +01:00
|
|
|
# Eg. If the reservation is for 12 hours, and there are prices for 3 hours, 7 hours,
|
2021-12-21 15:30:08 +01:00
|
|
|
# and the base price (1 hours), we use the 7 hours price, then 3 hours price, and finally the base price twice (7+3+1+1 = 12).
|
|
|
|
# All these prices are returned to be applied to the reservation.
|
2023-01-12 17:48:52 +01:00
|
|
|
# @param slots_reservations [Array<CartItem::ReservationSlot>]
|
|
|
|
# @return [Hash{Symbol => Array<Hash{Symbol => Number, Price}>}]
|
2022-12-28 17:51:27 +01:00
|
|
|
def applicable_prices(slots_reservations)
|
|
|
|
total_duration = slots_reservations.map do |slot|
|
|
|
|
(slot.slot[:end_at].to_time - slot.slot[:start_at].to_time) / SECONDS_PER_MINUTE
|
2023-01-12 17:48:52 +01:00
|
|
|
end.reduce(:+) || 0
|
2021-12-21 15:30:08 +01:00
|
|
|
rates = { prices: [] }
|
2021-12-20 17:08:14 +01:00
|
|
|
|
|
|
|
remaining_duration = total_duration
|
2021-12-21 11:14:09 +01:00
|
|
|
while remaining_duration.positive?
|
2022-12-28 17:51:27 +01:00
|
|
|
max_duration = reservable&.prices&.where(group_id: customer.group_id, plan_id: plan.try(:id))
|
|
|
|
&.where(Price.arel_table[:duration].lteq(remaining_duration))
|
|
|
|
&.maximum(:duration)
|
2021-12-29 22:07:28 +01:00
|
|
|
max_duration = 60 if max_duration.nil?
|
2022-12-28 17:51:27 +01:00
|
|
|
max_duration_price = reservable&.prices&.find_by(group_id: customer.group_id, plan_id: plan.try(:id), duration: max_duration)
|
2021-12-20 17:08:14 +01:00
|
|
|
|
2021-12-21 11:14:09 +01:00
|
|
|
current_duration = [remaining_duration, max_duration].min
|
2021-12-21 15:30:08 +01:00
|
|
|
rates[:prices].push(price: max_duration_price, duration: current_duration)
|
|
|
|
|
|
|
|
remaining_duration -= current_duration
|
2021-12-20 17:08:14 +01:00
|
|
|
end
|
|
|
|
|
2021-12-21 15:30:08 +01:00
|
|
|
rates[:prices].sort! { |a, b| b[:duration] <=> a[:duration] }
|
|
|
|
rates
|
2021-12-20 17:08:14 +01:00
|
|
|
end
|
|
|
|
|
2021-04-22 19:24:08 +02:00
|
|
|
##
|
|
|
|
# Compute the number of remaining hours in the users current credits (for machine or space)
|
|
|
|
##
|
2022-10-26 17:35:02 +02:00
|
|
|
def credits_hours(credits, new_plan_being_bought: false)
|
2021-04-23 17:54:59 +02:00
|
|
|
return 0 unless credits
|
|
|
|
|
2021-04-22 19:24:08 +02:00
|
|
|
hours_available = credits.hours
|
|
|
|
unless new_plan_being_bought
|
2022-12-28 17:51:27 +01:00
|
|
|
user_credit = customer.users_credits.find_by(credit_id: credits.id)
|
2021-04-22 19:24:08 +02:00
|
|
|
hours_available = credits.hours - user_credit.hours_used if user_credit
|
|
|
|
end
|
|
|
|
hours_available
|
|
|
|
end
|
2021-05-19 18:12:52 +02:00
|
|
|
|
|
|
|
def slots_params
|
2022-12-28 17:51:27 +01:00
|
|
|
cart_item_reservation_slots.map { |sr| { id: sr.slots_reservation_id, slot_id: sr.slot_id, offered: sr.offered } }
|
2021-05-19 18:12:52 +02:00
|
|
|
end
|
2022-07-20 17:46:09 +02:00
|
|
|
|
|
|
|
# Check if the given availability requires a valid subscription. If so, check if the current customer
|
|
|
|
# has the required susbcription, otherwise, check if the operator is privileged
|
2023-03-10 16:14:38 +01:00
|
|
|
# @param availability [Availability]
|
|
|
|
# @param pending_subscription [CartItem::Subscription, NilClass]
|
2022-07-20 17:46:09 +02:00
|
|
|
def required_subscription?(availability, pending_subscription)
|
2022-12-28 17:51:27 +01:00
|
|
|
(customer.subscribed_plan && availability.plan_ids.include?(customer.subscribed_plan.id)) ||
|
2022-07-20 17:46:09 +02:00
|
|
|
(pending_subscription && availability.plan_ids.include?(pending_subscription.plan.id)) ||
|
2022-12-28 17:51:27 +01:00
|
|
|
(operator.manager? && customer.id != operator.id) ||
|
|
|
|
operator.admin?
|
2022-07-20 17:46:09 +02:00
|
|
|
end
|
2023-03-13 17:29:06 +01:00
|
|
|
|
2023-03-01 21:44:01 +01:00
|
|
|
# Gets the deadline in minutes for slots in this reservation
|
2023-03-20 11:44:04 +01:00
|
|
|
# @return [Integer]
|
2023-03-01 21:44:01 +01:00
|
|
|
def reservation_deadline_minutes
|
2023-03-20 11:44:04 +01:00
|
|
|
0
|
2023-03-15 20:47:46 +01:00
|
|
|
end
|
2023-03-15 20:26:45 +01:00
|
|
|
|
2023-03-13 17:29:06 +01:00
|
|
|
# @param reservation_slot [CartItem::ReservationSlot]
|
|
|
|
# @param pending_subscription [CartItem::Subscription, NilClass]
|
|
|
|
# @param errors [ActiveModel::Errors]
|
|
|
|
# @return [Boolean]
|
2023-03-15 20:26:45 +01:00
|
|
|
def validate_slot_reservation(reservation_slot, pending_subscription, errors)
|
2023-03-13 17:29:06 +01:00
|
|
|
slot = reservation_slot.slot
|
|
|
|
if slot.nil?
|
|
|
|
errors.add(:slot, I18n.t('cart_item_validation.slot'))
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
availability = slot.availability
|
|
|
|
if availability.nil?
|
|
|
|
errors.add(:availability, I18n.t('cart_item_validation.availability'))
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
if slot.full?
|
|
|
|
errors.add(:slot, I18n.t('cart_item_validation.full'))
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2023-03-15 20:26:45 +01:00
|
|
|
if slot.start_at < reservation_deadline_minutes.minutes.since && !operator.privileged?
|
2023-02-24 17:26:55 +01:00
|
|
|
errors.add(:slot, I18n.t('cart_item_validation.deadline', **{ MINUTES: reservation_deadline_minutes }))
|
2023-03-13 17:29:06 +01:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2023-03-15 14:17:06 +01:00
|
|
|
if availability.plan_ids.any? && !required_subscription?(availability, pending_subscription)
|
2023-03-13 17:29:06 +01:00
|
|
|
errors.add(:availability, I18n.t('cart_item_validation.restricted'))
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2023-07-28 16:16:44 +02:00
|
|
|
unless operator.privileged?
|
|
|
|
if reservable_type == "Space"
|
|
|
|
space = reservable
|
|
|
|
slot = reservation_slot.slot
|
|
|
|
if Slots::InterblockingService.new.blocked_slots_for_spaces([space], [slot]).any?
|
|
|
|
errors.add(:slot, I18n.t('cart_item_validation.blocked_by_another_reservation'))
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if reservable_type == "Machine"
|
|
|
|
machine = reservable
|
|
|
|
slot = reservation_slot.slot
|
|
|
|
if Slots::InterblockingService.new.blocked_slots_for_machines([machine], [slot]).any?
|
|
|
|
errors.add(:slot, I18n.t('cart_item_validation.blocked_by_another_reservation'))
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-13 17:29:06 +01:00
|
|
|
true
|
|
|
|
end
|
2021-04-22 19:24:08 +02:00
|
|
|
end
|