Class: SpreeCmCommissioner::TripSeatLayoutQuery
- Inherits:
-
Object
- Object
- SpreeCmCommissioner::TripSeatLayoutQuery
- Defined in:
- app/queries/spree_cm_commissioner/trip_seat_layout_query.rb
Instance Attribute Summary collapse
-
#date ⇒ Object
readonly
Returns the value of attribute date.
-
#route ⇒ Object
readonly
Returns the value of attribute route.
-
#trip_id ⇒ Object
readonly
Returns the value of attribute trip_id.
-
#vehicle ⇒ Object
readonly
Returns the value of attribute vehicle.
-
#vehicle_type ⇒ Object
readonly
Returns the value of attribute vehicle_type.
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(trip_id:, date:) ⇒ TripSeatLayoutQuery
constructor
A new instance of TripSeatLayoutQuery.
- #ordered_seats ⇒ Object
- #process_seat_selection(allow_seat_selection) ⇒ Object
- #seats ⇒ Object
Constructor Details
#initialize(trip_id:, date:) ⇒ TripSeatLayoutQuery
Returns a new instance of TripSeatLayoutQuery.
5 6 7 8 9 10 11 |
# File 'app/queries/spree_cm_commissioner/trip_seat_layout_query.rb', line 5 def initialize(trip_id:, date:) @trip_id = trip_id @date = date @route = Spree::Variant.find_by(id: trip_id).product @vehicle = SpreeCmCommissioner::Vehicle.find_by(id: route.trip.vehicle_id) @vehicle_type = SpreeCmCommissioner::VehicleType.find_by(id: vehicle.vehicle_type_id) end |
Instance Attribute Details
#date ⇒ Object (readonly)
Returns the value of attribute date.
3 4 5 |
# File 'app/queries/spree_cm_commissioner/trip_seat_layout_query.rb', line 3 def date @date end |
#route ⇒ Object (readonly)
Returns the value of attribute route.
3 4 5 |
# File 'app/queries/spree_cm_commissioner/trip_seat_layout_query.rb', line 3 def route @route end |
#trip_id ⇒ Object (readonly)
Returns the value of attribute trip_id.
3 4 5 |
# File 'app/queries/spree_cm_commissioner/trip_seat_layout_query.rb', line 3 def trip_id @trip_id end |
#vehicle ⇒ Object (readonly)
Returns the value of attribute vehicle.
3 4 5 |
# File 'app/queries/spree_cm_commissioner/trip_seat_layout_query.rb', line 3 def vehicle @vehicle end |
#vehicle_type ⇒ Object (readonly)
Returns the value of attribute vehicle_type.
3 4 5 |
# File 'app/queries/spree_cm_commissioner/trip_seat_layout_query.rb', line 3 def vehicle_type @vehicle_type end |
Instance Method Details
#call ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'app/queries/spree_cm_commissioner/trip_seat_layout_query.rb', line 13 def call allow_seat_selection = @route.trip.allow_seat_selection total_sold, remaining_seats, layout = process_seat_selection(allow_seat_selection) SpreeCmCommissioner::TripSeatLayoutResult.new({ trip_id: trip_id, total_sold: total_sold, total_seats: vehicle.number_of_seats, remaining_seats: remaining_seats, layout: layout, allow_seat_selection: allow_seat_selection } ) end |
#ordered_seats ⇒ Object
68 69 70 71 72 73 74 |
# File 'app/queries/spree_cm_commissioner/trip_seat_layout_query.rb', line 68 def ordered_seats SpreeCmCommissioner::LineItemSeat.select('cm_line_item_seats.seat_id') .joins('INNER JOIN spree_line_items ON cm_line_item_seats.line_item_id = spree_line_items.id') .joins('INNER JOIN spree_orders ON spree_orders.id = spree_line_items.order_id') .where('spree_orders.state = ? ', 'complete') .where('cm_line_item_seats.variant_id = ? AND cm_line_item_seats.date = ?', trip_id, date) end |
#process_seat_selection(allow_seat_selection) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'app/queries/spree_cm_commissioner/trip_seat_layout_query.rb', line 26 def process_seat_selection(allow_seat_selection) # layout_structure # {"First_Layer" => {"Row_1" => [{seat1}, {seat2}, {seat3}, {seat4}], # {"Row_2" => [{seat5}, {seat6}, {seat7}, {seat8}], # } if allow_seat_selection vehicle_seats = seats.to_a total_sold = vehicle_seats.count { |s| s.seat_id.present? } remaining_seats = vehicle.number_of_seats - total_sold layout = vehicle_seats.group_by(&:layer).transform_values do |s| s.group_by(&:row).transform_values do |r| r.sort_by(&:column).map do |seat| { row: seat.row, column: seat.column, label: seat.label, layer: seat.layer, seat_type: seat.seat_type, created_at: seat.created_at, seat_id: seat.seat_id, vehicle_type_id: seat.vehicle_type_id } end end end else total_sold = Spree::LineItem.joins(:order) .where(variant_id: trip_id, date: date, spree_orders: { state: 'complete' }) .sum(:quantity) remaining_seats = vehicle.number_of_seats - total_sold layout = nil end [total_sold, remaining_seats, layout] end |
#seats ⇒ Object
62 63 64 65 66 |
# File 'app/queries/spree_cm_commissioner/trip_seat_layout_query.rb', line 62 def seats SpreeCmCommissioner::VehicleSeat.select('cm_vehicle_seats.*, os.seat_id as seat_id') .joins("LEFT JOIN (#{ordered_seats.to_sql}) os ON cm_vehicle_seats.id = os.seat_id") .where('cm_vehicle_seats.vehicle_type_id = ? ', vehicle_type.id) end |