app/Http/Controllers/BookingController.php
/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
Store a newly created resource in storage.
Store a newly created resource in storage .
public function store(StoreBookingRequest $request)
    {
        $ride = Ride::findOrFail($request->ride_id);
        $start = $request->start_terminal_id;
        $end = $request->end_terminal_id;
        $travelDate = $request->travel_date;
        $occupiedSeats = $this->bookingService->getOccupiedSeats($ride, $start, $end, $travelDate);
        $availableSeats = $ride->bus->bus_seat - $occupiedSeats;
        if ($request->pax > $availableSeats) {
            return redirect()->back()->withErrors('You cannot book more than available');
        }
        if ($ride->auto_confirm) {
            $status = "confirmed";
        }
        $booking = Booking::create([
            'passenger_id' => 17, 
            'ride_id' => $ride->id,
            'start_terminal_id' => $start,
            'end_terminal_id' => $end,
            'travel_date' => $travelDate,
            'pax' => $request->pax,
            'status' => $status ?? 'new',
        ]);
        $booking->sale()->create([
            'rate' => $ride->bus->busClass->rate,
            'payment' => $ride->getTotalPayment($start, $end) * $request->pax,
        ]);
        return redirect()->route('bookings.my.bookings');
    }
public function store ( StoreBookingRequest $ request ) { $ ride = Ride :: findOrFail ( $ request -> ride_id ) ; $ start = $ request -> start_terminal_id ; $ end = $ request -> end_terminal_id ; $ travelDate = $ request -> travel_date ; $ occupiedSeats = $ this -> bookingService -> getOccupiedSeats ( $ ride , $ start , $ end , $ travelDate ) ; $ availableSeats = $ ride -> bus -> bus_seat - $ occupiedSeats ; if ( $ request -> pax > $ availableSeats ) { return redirect ( ) -> back ( ) -> withErrors ( 'You cannot book more than available' ) ; } if ( $ ride -> auto_confirm ) { $ status = " confirmed " ; } $ booking = Booking :: create ( [ 'passenger_id' => 17 , 'ride_id' => $ ride -> id , 'start_terminal_id' => $ start , 'end_terminal_id' => $ end , 'travel_date' => $ travelDate , 'pax' => $ request -> pax , 'status' => $ status ?? 'new' , ] ) ; $ booking -> sale ( ) -> create ( [ 'rate' => $ ride -> bus -> busClass -> rate , 'payment' => $ ride -> getTotalPayment ( $ start , $ end ) * $ request -> pax , ] ) ; return redirect ( ) -> route ( 'bookings.my.bookings' ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .
//Check if seats is still available //Check auto_confirm //store to database //Change to passenger_id from api