2021-04-22 19:24:08 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# A machine reservation added to the shopping cart
|
|
|
|
class CartItem::MachineReservation < CartItem::Reservation
|
2022-12-28 17:51:27 +01:00
|
|
|
has_many :cart_item_reservation_slots, class_name: 'CartItem::ReservationSlot', dependent: :destroy, inverse_of: :cart_item,
|
2023-02-24 17:26:55 +01:00
|
|
|
foreign_type: 'cart_item_type', as: :cart_item
|
2022-12-28 17:51:27 +01:00
|
|
|
accepts_nested_attributes_for :cart_item_reservation_slots
|
|
|
|
|
|
|
|
belongs_to :operator_profile, class_name: 'InvoicingProfile'
|
|
|
|
belongs_to :customer_profile, class_name: 'InvoicingProfile'
|
|
|
|
|
|
|
|
belongs_to :reservable, polymorphic: true
|
|
|
|
|
|
|
|
belongs_to :plan
|
2021-05-19 18:12:52 +02:00
|
|
|
|
2022-03-18 19:44:30 +01:00
|
|
|
def type
|
|
|
|
'machine'
|
|
|
|
end
|
|
|
|
|
2022-12-30 15:29:52 +01:00
|
|
|
def valid?(all_items = [])
|
2022-12-28 17:51:27 +01:00
|
|
|
cart_item_reservation_slots.each do |slot|
|
2022-07-20 17:46:09 +02:00
|
|
|
same_hour_slots = SlotsReservation.joins(:reservation).where(
|
2022-12-28 17:51:27 +01:00
|
|
|
reservations: { reservable: reservable },
|
2022-07-20 17:46:09 +02:00
|
|
|
slot_id: slot[:slot_id],
|
|
|
|
canceled_at: nil
|
|
|
|
).count
|
|
|
|
if same_hour_slots.positive?
|
2022-12-28 17:51:27 +01:00
|
|
|
errors.add(:slot, I18n.t('cart_item_validation.reserved'))
|
2023-01-04 11:12:02 +01:00
|
|
|
return false
|
|
|
|
end
|
2022-12-28 17:51:27 +01:00
|
|
|
if reservable.disabled
|
|
|
|
errors.add(:reservable, I18n.t('cart_item_validation.machine'))
|
2023-01-04 11:12:02 +01:00
|
|
|
return false
|
|
|
|
end
|
2022-12-28 17:51:27 +01:00
|
|
|
unless reservable.reservable
|
|
|
|
errors.add(:reservable, I18n.t('cart_item_validation.reservable'))
|
2022-07-20 17:46:09 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2021-04-22 19:24:08 +02:00
|
|
|
protected
|
|
|
|
|
|
|
|
def credits
|
2022-12-28 17:51:27 +01:00
|
|
|
return 0 if plan.nil?
|
2021-04-22 19:24:08 +02:00
|
|
|
|
2022-12-28 17:51:27 +01:00
|
|
|
machine_credit = plan.machine_credits.find { |credit| credit.creditable_id == reservable.id }
|
|
|
|
credits_hours(machine_credit, new_plan_being_bought: new_subscription)
|
2021-04-22 19:24:08 +02:00
|
|
|
end
|
2023-03-01 21:44:01 +01:00
|
|
|
|
|
|
|
def reservation_deadline_minutes
|
2023-03-20 11:44:04 +01:00
|
|
|
Setting.get('machine_reservation_deadline').to_i
|
2023-03-01 21:44:01 +01:00
|
|
|
end
|
2021-04-22 19:24:08 +02:00
|
|
|
end
|