1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-11 22:24:21 +01:00
fab-manager/app/models/cart_item/event_reservation.rb

75 lines
2.3 KiB
Ruby
Raw Normal View History

# 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
end
def price
2022-12-28 17:51:27 +01:00
amount = reservable.amount * normal_tickets
is_privileged = operator.privileged? && operator.id != customer.id
2022-12-28 17:51:27 +01:00
cart_item_event_reservation_tickets.each do |ticket|
amount += ticket.booked * ticket.event_price_category.amount
end
elements = { slots: [] }
total = 0
2022-12-28 17:51:27 +01:00
cart_item_reservation_slots.each do |sr|
total += get_slot_price(amount,
2022-12-28 17:51:27 +01:00
sr,
is_privileged,
elements: elements,
is_division: false)
end
{ elements: elements, amount: total }
end
def to_object
::Reservation.new(
2022-12-28 17:51:27 +01:00
reservable_id: reservable.id,
reservable_type: Event.name,
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
)
end
def name
2022-12-28 17:51:27 +01:00
reservable.title
end
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
end