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
|
2023-01-09 17:36:11 +01:00
|
|
|
self.table_name = 'cart_item_event_reservations'
|
|
|
|
|
2022-12-28 17:51:27 +01:00
|
|
|
has_many :cart_item_event_reservation_tickets, class_name: 'CartItem::EventReservationTicket', dependent: :destroy,
|
|
|
|
inverse_of: :cart_item_event_reservation,
|
|
|
|
foreign_key: 'cart_item_event_reservation_id'
|
|
|
|
accepts_nested_attributes_for :cart_item_event_reservation_tickets
|
|
|
|
|
|
|
|
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 :event
|
|
|
|
|
|
|
|
def reservable
|
|
|
|
event
|
2021-04-22 19:24:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def price
|
2022-12-28 17:51:27 +01:00
|
|
|
amount = reservable.amount * normal_tickets
|
|
|
|
is_privileged = operator.privileged? && operator.id != customer.id
|
2021-04-22 19:24:08 +02:00
|
|
|
|
2022-12-28 17:51:27 +01:00
|
|
|
cart_item_event_reservation_tickets.each do |ticket|
|
|
|
|
amount += ticket.booked * ticket.event_price_category.amount
|
2021-04-22 19:24:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
elements = { slots: [] }
|
2021-04-26 16:38:00 +02:00
|
|
|
total = 0
|
2021-04-22 19:24:08 +02:00
|
|
|
|
2022-12-28 17:51:27 +01:00
|
|
|
cart_item_reservation_slots.each do |sr|
|
2021-04-26 16:38:00 +02:00
|
|
|
total += get_slot_price(amount,
|
2022-12-28 17:51:27 +01:00
|
|
|
sr,
|
2021-04-26 17:42:03 +02:00
|
|
|
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(
|
2022-12-28 17:51:27 +01:00
|
|
|
reservable_id: reservable.id,
|
2021-05-19 18:12:52 +02:00
|
|
|
reservable_type: Event.name,
|
2022-07-13 16:28:43 +02:00
|
|
|
slots_reservations_attributes: slots_params,
|
2022-12-28 17:51:27 +01:00
|
|
|
tickets_attributes: cart_item_event_reservation_tickets.map do |t|
|
|
|
|
{
|
|
|
|
event_price_category_id: t.event_price_category_id,
|
|
|
|
booked: t.booked
|
|
|
|
}
|
|
|
|
end,
|
|
|
|
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
|
2022-12-28 17:51:27 +01:00
|
|
|
reservable.title
|
2021-04-23 12:52:06 +02:00
|
|
|
end
|
2021-05-19 18:12:52 +02:00
|
|
|
|
2022-03-18 19:44:30 +01:00
|
|
|
def type
|
|
|
|
'event'
|
|
|
|
end
|
2023-01-12 17:48:52 +01:00
|
|
|
|
|
|
|
def total_tickets
|
|
|
|
(normal_tickets || 0) + (cart_item_event_reservation_tickets.map(&:booked).reduce(:+) || 0)
|
|
|
|
end
|
2021-04-22 19:24:08 +02:00
|
|
|
end
|