2021-06-21 17:39:48 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Provides methods for PrepaidPack
|
|
|
|
class PrepaidPackService
|
2021-06-25 17:24:34 +02:00
|
|
|
class << self
|
2023-01-20 13:57:36 +01:00
|
|
|
# @param filters [Hash{Symbol=>Integer,String}]
|
|
|
|
# @option filters [Integer] :group_id
|
|
|
|
# @option filters [Integer] :priceable_id
|
|
|
|
# @option filters [String] :priceable_type 'Machine' | 'Space'
|
2021-06-25 17:24:34 +02:00
|
|
|
def list(filters)
|
|
|
|
packs = PrepaidPack.where(nil)
|
2021-06-21 17:39:48 +02:00
|
|
|
|
2021-06-25 17:24:34 +02:00
|
|
|
packs = packs.where(group_id: filters[:group_id]) if filters[:group_id].present?
|
|
|
|
packs = packs.where(priceable_id: filters[:priceable_id]) if filters[:priceable_id].present?
|
|
|
|
packs = packs.where(priceable_type: filters[:priceable_type]) if filters[:priceable_type].present?
|
2021-06-21 17:39:48 +02:00
|
|
|
|
2021-06-25 17:24:34 +02:00
|
|
|
if filters[:disabled].present?
|
|
|
|
state = filters[:disabled] == 'false' ? [nil, false] : true
|
|
|
|
packs = packs.where(disabled: state)
|
|
|
|
end
|
|
|
|
|
|
|
|
packs
|
|
|
|
end
|
|
|
|
|
2021-06-29 11:14:36 +02:00
|
|
|
# return the not expired packs for the given item bought by the given user
|
2023-01-20 13:57:36 +01:00
|
|
|
# @param user [User]
|
|
|
|
# @param priceable [Machine,Space,NilClass]
|
|
|
|
def user_packs(user, priceable = nil)
|
|
|
|
sppp = StatisticProfilePrepaidPack.includes(:prepaid_pack)
|
|
|
|
.references(:prepaid_packs)
|
|
|
|
.where(statistic_profile_id: user.statistic_profile.id)
|
|
|
|
.where('expires_at > ? OR expires_at IS NULL', Time.current)
|
|
|
|
.where('minutes_used < prepaid_packs.minutes')
|
|
|
|
unless priceable.nil?
|
|
|
|
sppp = sppp.where(prepaid_packs: { priceable_id: priceable.id })
|
|
|
|
.where(prepaid_packs: { priceable_type: priceable.class.name })
|
|
|
|
end
|
|
|
|
sppp
|
2021-06-25 17:24:34 +02:00
|
|
|
end
|
2021-06-30 10:53:05 +02:00
|
|
|
|
|
|
|
# subtract the number of used prepaid minutes from the user's count
|
2023-01-20 13:57:36 +01:00
|
|
|
# @param user [User]
|
|
|
|
# @param reservation [Reservation]
|
2021-06-30 10:53:05 +02:00
|
|
|
def update_user_minutes(user, reservation)
|
|
|
|
# total number of minutes available in user's packs
|
|
|
|
available_minutes = minutes_available(user, reservation.reservable)
|
|
|
|
return if available_minutes.zero?
|
|
|
|
|
|
|
|
# total number of minutes in the reservation's slots
|
|
|
|
slots_minutes = reservation.slots.map do |slot|
|
2022-10-19 15:19:28 +02:00
|
|
|
(slot.end_at.to_time - slot.start_at.to_time) / 60.0
|
2021-06-30 10:53:05 +02:00
|
|
|
end
|
|
|
|
reservation_minutes = slots_minutes.reduce(:+) || 0
|
|
|
|
|
|
|
|
# total number of prepaid minutes used by this reservation
|
|
|
|
consumed = reservation_minutes
|
|
|
|
consumed = available_minutes if reservation_minutes > available_minutes
|
|
|
|
|
|
|
|
# subtract the consumed minutes for user's current packs
|
|
|
|
packs = user_packs(user, reservation.reservable).order(minutes_used: :desc)
|
|
|
|
packs.each do |pack|
|
|
|
|
pack_available = pack.prepaid_pack.minutes - pack.minutes_used
|
|
|
|
remaining = pack_available - consumed
|
|
|
|
remaining = 0 if remaining.negative?
|
|
|
|
pack_consumed = pack.prepaid_pack.minutes - remaining
|
2023-02-14 13:10:58 +01:00
|
|
|
pack.update(minutes_used: pack_consumed)
|
2021-06-30 10:53:05 +02:00
|
|
|
|
|
|
|
consumed -= pack_consumed
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-20 13:57:36 +01:00
|
|
|
# Total number of prepaid minutes available
|
|
|
|
# @param user [User]
|
|
|
|
# @param priceable [Machine,Space]
|
2021-06-30 10:53:05 +02:00
|
|
|
def minutes_available(user, priceable)
|
2021-12-20 17:08:14 +01:00
|
|
|
return 0 if Setting.get('pack_only_for_subscription') && !user.subscribed_plan
|
2021-09-20 19:43:05 +02:00
|
|
|
|
2021-06-30 10:53:05 +02:00
|
|
|
user_packs = user_packs(user, priceable)
|
|
|
|
total_available = user_packs.map { |up| up.prepaid_pack.minutes }.reduce(:+) || 0
|
|
|
|
total_used = user_packs.map(&:minutes_used).reduce(:+) || 0
|
|
|
|
|
|
|
|
total_available - total_used
|
|
|
|
end
|
2021-06-21 17:39:48 +02:00
|
|
|
end
|
|
|
|
end
|