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(Request $request)
    {
        $date = new DateTime();
        $date->setTimezone(new DateTimeZone('Asia/Bangkok'));
        $rules = array(
            'products' => 'required',
            'date' => 'required',
            'productName' => 'required',
            'totalPrice' => 'required|integer',
            'number' => 'required|integer',
        );
        $validator = Validator::make($request->all(), $rules);
        if ($validator->fails()) return response()->json(array('result' => false, 'message' => 'invalid input field.'));
        $user = \Auth::user();
        if ($user == null) return response()->json(array('result' => false, 'message' => 'user is not define.'));
        $booking_code = date('YmdHms') . '-' . $user->id;
        $booking = Booking::create([
            'sale_at' => $request->input('date'),
            'userID' => $user->id,
            'userCode' => $user->code,
            'quantity' => $request->input('number'),
            'totalPrice' => $request->input('totalPrice'),
            'code' => $booking_code,
            'productName' => $request->input('productName'),
            'status' => 'BK',
            'type' => $request->input('type'),
            'payment_type' => 1
        ]);
        if ($booking == null) return response()->json(array('result' => false, 'message' => 'booking create fails.'));
        $products_arr = $request->input('products');
        $count = 0;
        $detail_result = [];
        foreach ($products_arr as $key => $product_arr) {
            $product = (object)$product_arr;
            $calendar = Calendar::where('opened_at', $request->input('date'))->where('code', $product->code)->get()->first();
            $detail = BookingDetail::create([
                'code' => $date->format('Ymd-His') . '-' . $user->id . '-' . ($count + 1),
                'bookingID' => $booking->id,
                'bookingCode' => $booking->code,
                'zoneID' => $calendar->zoneID,
                'zoneCode' => $calendar->code,
                'zoneNumber' => $product->name,
                'price' => $product->price,
                'status' => 'BK',
                'sale_at' => $request->input('date')
            ]);
            if ($detail != null) {
                array_push($detail_result, $detail->id);
                $count++;
            }
        }
        if ($count == count($products_arr)) {
            return response()->json(array('result' => true, 'message' => 'success'
            , 'bookingCode' => $booking->code, 'detail' => $detail_result
            ));
        }
        return response()->json(array('result' => false, 'message' => 'booking create fails.'));
    }
public function store ( Request $ request ) { $ date = new DateTime ( ) ; $ date -> setTimezone ( new DateTimeZone ( 'Asia/Bangkok' ) ) ; $ rules = array ( 'products' => 'required' , 'date' => 'required' , 'productName' => 'required' , 'totalPrice' => 'required|integer' , 'number' => 'required|integer' , ) ; $ validator = Validator :: make ( $ request -> all ( ) , $ rules ) ; if ( $ validator -> fails ( ) ) return response ( ) -> json ( array ( 'result' => false , 'message' => 'invalid input field.' ) ) ; $ user = \ Auth :: user ( ) ; if ( $ user == null ) return response ( ) -> json ( array ( 'result' => false , 'message' => 'user is not define.' ) ) ; $ booking_code = date ( 'YmdHms' ) . '-' . $ user -> id ; $ booking = Booking :: create ( [ 'sale_at' => $ request -> input ( 'date' ) , 'userID' => $ user -> id , 'userCode' => $ user -> code , 'quantity' => $ request -> input ( 'number' ) , 'totalPrice' => $ request -> input ( 'totalPrice' ) , 'code' => $ booking_code , 'productName' => $ request -> input ( 'productName' ) , 'status' => 'BK' , 'type' => $ request -> input ( 'type' ) , 'payment_type' => 1 ] ) ; if ( $ booking == null ) return response ( ) -> json ( array ( 'result' => false , 'message' => 'booking create fails.' ) ) ; $ products_arr = $ request -> input ( 'products' ) ; $ count = 0 ; $ detail_result = [ ] ; foreach ( $ products_arr as $ key => $ product_arr ) { $ product = ( object ) $ product_arr ; $ calendar = Calendar :: where ( 'opened_at' , $ request -> input ( 'date' ) ) -> where ( 'code' , $ product -> code ) -> get ( ) -> first ( ) ; $ detail = BookingDetail :: create ( [ 'code' => $ date -> format ( 'Ymd-His' ) . '-' . $ user -> id . '-' . ( $ count + 1 ) , 'bookingID' => $ booking -> id , 'bookingCode' => $ booking -> code , 'zoneID' => $ calendar -> zoneID , 'zoneCode' => $ calendar -> code , 'zoneNumber' => $ product -> name , 'price' => $ product -> price , 'status' => 'BK' , 'sale_at' => $ request -> input ( 'date' ) ] ) ; if ( $ detail != null ) { array_push ( $ detail_result , $ detail -> id ) ; $ count ++ ; } } if ( $ count == count ( $ products_arr ) ) { return response ( ) -> json ( array ( 'result' => true , 'message' => 'success' , 'bookingCode' => $ booking -> code , 'detail' => $ detail_result ) ) ; } return response ( ) -> json ( array ( 'result' => false , 'message' => 'booking create fails.' ) ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .
//$open = (object) $arr[$i]; # code... //end foreach