2021-04-22 19:24:08 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# A training reservation added to the shopping cart
|
|
|
|
class CartItem::TrainingReservation < CartItem::Reservation
|
2022-12-28 17:51:27 +01:00
|
|
|
self.table_name = 'cart_item_reservations'
|
|
|
|
|
|
|
|
has_many :cart_item_reservation_slots, class_name: 'CartItem::ReservationSlot', dependent: :destroy, inverse_of: :cart_item,
|
|
|
|
foreign_key: 'cart_item_id', foreign_type: 'cart_item_type'
|
|
|
|
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-04-22 19:24:08 +02:00
|
|
|
|
|
|
|
def price
|
2022-12-28 17:51:27 +01:00
|
|
|
base_amount = reservable&.amount_by_group(customer.group_id)&.amount
|
|
|
|
is_privileged = operator.admin? || (operator.manager? && operator.id != customer.id)
|
2021-04-22 19:24:08 +02:00
|
|
|
|
|
|
|
elements = { slots: [] }
|
|
|
|
amount = 0
|
|
|
|
|
|
|
|
hours_available = credits
|
2022-12-28 17:51:27 +01:00
|
|
|
cart_item_reservation_slots.each do |sr|
|
2021-04-22 19:24:08 +02:00
|
|
|
amount += get_slot_price(base_amount,
|
2022-12-28 17:51:27 +01:00
|
|
|
sr,
|
2021-04-22 19:24:08 +02:00
|
|
|
is_privileged,
|
|
|
|
elements: elements,
|
2022-12-28 17:51:27 +01:00
|
|
|
has_credits: (customer.training_credits.size < hours_available),
|
2021-04-22 19:24:08 +02:00
|
|
|
is_division: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
{ elements: elements, amount: amount }
|
|
|
|
end
|
|
|
|
|
2022-03-18 19:44:30 +01:00
|
|
|
def type
|
|
|
|
'training'
|
|
|
|
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
|
|
|
is_creditable = plan&.training_credits&.select { |credit| credit.creditable_id == reservable&.id }&.any?
|
|
|
|
is_creditable ? plan&.training_credit_nb : 0
|
2021-04-22 19:24:08 +02:00
|
|
|
end
|
|
|
|
end
|