2023-01-26 14:11:14 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Business logic around trainings
|
|
|
|
module Trainings; end
|
|
|
|
|
|
|
|
# Automatically cancel trainings without enough reservation
|
|
|
|
class Trainings::AutoCancelService
|
|
|
|
class << self
|
|
|
|
# @param training [Training]
|
|
|
|
def auto_cancel_reservations(training)
|
|
|
|
return unless training.auto_cancel
|
|
|
|
|
|
|
|
training.availabilities
|
|
|
|
.includes(slots: :slots_reservations)
|
2023-02-03 11:54:57 +01:00
|
|
|
.where(availabilities: { lock: false })
|
|
|
|
.where('availabilities.start_at >= ? AND availabilities.start_at <= ?',
|
2023-02-17 11:44:04 +01:00
|
|
|
Time.current,
|
|
|
|
Time.current + training.auto_cancel_deadline.hours)
|
2023-01-26 14:11:14 +01:00
|
|
|
.find_each do |availability|
|
|
|
|
next if availability.reservations.count >= training.auto_cancel_threshold
|
|
|
|
|
|
|
|
auto_refund = Setting.get('wallet_module')
|
|
|
|
|
|
|
|
NotificationCenter.call type: 'notify_admin_training_auto_cancelled',
|
|
|
|
receiver: User.admins_and_managers,
|
|
|
|
attached_object: availability,
|
|
|
|
meta_data: { auto_refund: auto_refund }
|
|
|
|
|
2023-01-27 17:31:16 +01:00
|
|
|
availability.update(lock: true)
|
2023-01-26 14:11:14 +01:00
|
|
|
availability.slots_reservations.find_each do |sr|
|
|
|
|
NotificationCenter.call type: 'notify_member_training_auto_cancelled',
|
|
|
|
receiver: sr.reservation.user,
|
|
|
|
attached_object: sr,
|
|
|
|
meta_data: { auto_refund: auto_refund }
|
|
|
|
|
2023-02-17 11:44:04 +01:00
|
|
|
sr.update(canceled_at: Time.current)
|
2023-01-26 14:11:14 +01:00
|
|
|
refund_after_cancel(sr.reservation) if auto_refund
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# update the given training, depending on the provided settings
|
|
|
|
# @param training [Training]
|
|
|
|
# @param auto_cancel [Setting,NilClass]
|
|
|
|
# @param threshold [Setting,NilClass]
|
|
|
|
# @param deadline [Setting,NilClass]
|
|
|
|
def update_auto_cancel(training, auto_cancel, threshold, deadline)
|
|
|
|
previous_auto_cancel = auto_cancel.nil? ? Setting.find_by(name: 'trainings_auto_cancel').value : auto_cancel.previous_value
|
|
|
|
previous_threshold = threshold.nil? ? Setting.find_by(name: 'trainings_auto_cancel_threshold').value : threshold.previous_value
|
|
|
|
previous_deadline = deadline.nil? ? Setting.find_by(name: 'trainings_auto_cancel_deadline').value : deadline.previous_value
|
2023-01-27 17:31:16 +01:00
|
|
|
is_default = training.auto_cancel.to_s == previous_auto_cancel.to_s &&
|
|
|
|
training.auto_cancel_threshold.to_s == previous_threshold.to_s &&
|
|
|
|
training.auto_cancel_deadline.to_s == previous_deadline.to_s
|
2023-01-26 14:11:14 +01:00
|
|
|
|
|
|
|
return unless is_default
|
|
|
|
|
|
|
|
# update parameters if the given training is default
|
|
|
|
params = {}
|
|
|
|
params[:auto_cancel] = auto_cancel.value unless auto_cancel.nil?
|
|
|
|
params[:auto_cancel_threshold] = threshold.value unless threshold.nil?
|
|
|
|
params[:auto_cancel_deadline] = deadline.value unless deadline.nil?
|
|
|
|
training.update(params)
|
|
|
|
end
|
|
|
|
|
2023-01-30 17:08:38 +01:00
|
|
|
# @param training [Training]
|
|
|
|
# @return [Boolean]
|
|
|
|
def override_settings?(training)
|
2023-01-31 09:42:45 +01:00
|
|
|
training.auto_cancel.to_s != Setting.find_by(name: 'trainings_auto_cancel')&.value.to_s ||
|
|
|
|
training.auto_cancel_threshold.to_s != Setting.find_by(name: 'trainings_auto_cancel_threshold')&.value.to_s ||
|
|
|
|
training.auto_cancel_deadline.to_s != Setting.find_by(name: 'trainings_auto_cancel_deadline')&.value.to_s
|
2023-01-30 17:08:38 +01:00
|
|
|
end
|
|
|
|
|
2023-01-26 14:11:14 +01:00
|
|
|
private
|
|
|
|
|
|
|
|
# @param reservation [Reservation]
|
|
|
|
def refund_after_cancel(reservation)
|
|
|
|
invoice_item = reservation.invoice_items.joins(:invoice).where(invoices: { type: nil }).first
|
2023-02-03 11:54:57 +01:00
|
|
|
amount = (invoice_item&.amount_after_coupon || 0) / 100.00
|
|
|
|
return if amount.zero?
|
|
|
|
|
2023-01-26 14:11:14 +01:00
|
|
|
service = WalletService.new(user: reservation.user, wallet: reservation.user.wallet)
|
2023-02-03 11:54:57 +01:00
|
|
|
transaction = service.credit(amount)
|
2023-03-16 17:17:00 +01:00
|
|
|
service.create_avoir(transaction, I18n.t('trainings.refund_for_auto_cancel')) if transaction
|
2023-01-26 14:11:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|