1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00
fab-manager/app/models/cart_item/event_reservation.rb
Sylvain c7a59c8cb7 WIP: refactoring to singularize the booking process
We need to achieve only one process for all booking, not one for subscription, one for reservations, etc.
Moreover we must store one object per invoice_item/payment_schedule_object and stop using Invoice.invoiced or PaymentSchedule.scheduled
2021-05-21 18:25:18 +02:00

58 lines
1.7 KiB
Ruby

# 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: [])
raise TypeError unless event.is_a? Event
super(customer, operator, event, slots)
@normal_tickets = normal_tickets || 0
@other_tickets = other_tickets || []
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: [] }
total = 0
@slots.each do |slot|
total += get_slot_price(amount,
slot,
is_privileged,
elements: elements,
is_division: false)
end
{ elements: elements, amount: total }
end
def to_object
::Reservation.new(
reservable_id: @reservable.id,
reservable_type: Event.name,
slots_attributes: slots_params,
tickets_attributes: tickets_params,
nb_reserve_places: @normal_tickets,
statistic_profile_id: StatisticProfile.find_by(user: @customer).id
)
end
def name
@reservable.title
end
protected
def tickets_params
@other_tickets.map { |ticket| ticket.permit(:event_price_category_id, :booked) }
end
end