2021-04-22 19:24:08 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# An event reservation added to the shopping cart
|
|
|
|
class CartItem::EventReservation < CartItem::Reservation
|
|
|
|
# @param normal_tickets {Number} number of tickets at the normal price
|
|
|
|
# @param other_tickets {Array<{booked: Number, event_price_category_id: Number}>}
|
|
|
|
def initialize(customer, operator, event, slots, normal_tickets: 0, other_tickets: [])
|
2021-04-26 11:40:26 +02:00
|
|
|
raise TypeError unless event.is_a? Event
|
2021-04-22 19:24:08 +02:00
|
|
|
|
|
|
|
super(customer, operator, event, slots)
|
2021-04-29 16:29:35 +02:00
|
|
|
@normal_tickets = normal_tickets || 0
|
2021-04-23 17:54:59 +02:00
|
|
|
@other_tickets = other_tickets || []
|
2021-04-22 19:24:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def price
|
|
|
|
amount = @reservable.amount * @normal_tickets
|
|
|
|
is_privileged = @operator.admin? || (@operator.manager? && @operator.id != @customer.id)
|
|
|
|
|
|
|
|
@other_tickets.each do |ticket|
|
|
|
|
amount += ticket[:booked] * EventPriceCategory.find(ticket[:event_price_category_id]).amount
|
|
|
|
end
|
|
|
|
|
|
|
|
elements = { slots: [] }
|
2021-04-26 16:38:00 +02:00
|
|
|
total = 0
|
2021-04-22 19:24:08 +02:00
|
|
|
|
|
|
|
@slots.each do |slot|
|
2021-04-26 16:38:00 +02:00
|
|
|
total += get_slot_price(amount,
|
2021-04-26 17:42:03 +02:00
|
|
|
slot,
|
|
|
|
is_privileged,
|
|
|
|
elements: elements,
|
|
|
|
is_division: false)
|
2021-04-22 19:24:08 +02:00
|
|
|
end
|
|
|
|
|
2021-04-26 16:38:00 +02:00
|
|
|
{ elements: elements, amount: total }
|
2021-04-22 19:24:08 +02:00
|
|
|
end
|
|
|
|
|
2021-05-21 18:25:18 +02:00
|
|
|
def to_object
|
2021-05-19 18:12:52 +02:00
|
|
|
::Reservation.new(
|
|
|
|
reservable_id: @reservable.id,
|
|
|
|
reservable_type: Event.name,
|
|
|
|
slots_attributes: slots_params,
|
|
|
|
tickets_attributes: tickets_params,
|
2021-05-21 18:25:18 +02:00
|
|
|
nb_reserve_places: @normal_tickets,
|
|
|
|
statistic_profile_id: StatisticProfile.find_by(user: @customer).id
|
2021-05-19 18:12:52 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-04-23 12:52:06 +02:00
|
|
|
def name
|
|
|
|
@reservable.title
|
|
|
|
end
|
2021-05-19 18:12:52 +02:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def tickets_params
|
|
|
|
@other_tickets.map { |ticket| ticket.permit(:event_price_category_id, :booked) }
|
|
|
|
end
|
2021-04-22 19:24:08 +02:00
|
|
|
end
|