public function index(Request $request)
 {
     $data = $request->json()->all('bags');
     $payment = Payment::findOrFail($data['payment_id']);
     $logistics = Logistics::findOrFail($data['logistics_id']);
     $bags = Bag::with('stock.product')->find($data['bags']);
     $order = new Order(array_except($data, 'bags'));
     \DB::transaction(function () use($bags, $logistics, $payment, $order, $data, $request) {
         $order_products = [];
         foreach ($bags as $bag) {
             $order_products[] = $bag->stock->pick($bag->quantity);
             $bag->stock->save();
         }
         $order->user_id = $request->user->id;
         $order->calc($order_products);
         $order->save();
         $order->products()->saveMany($order_products);
         Bag::destroy($data['bags']);
     });
     return response()->created($order);
 }
Exemple #2
0
 public function checkout()
 {
     $cartContent = Cart::instance('shopping')->content();
     // dd($cart);
     $cartTotal = Cart::instance('shopping')->total();
     $addrId = Session::get('addressId');
     $address = Address::where('id', '=', $addrId)->first();
     //$aa = json_encode(Input::get('prod'));
     // $bb = json_encode($cart);
     $order_total = Input::get('order_total');
     $payment_method = Input::get('payment_method');
     $order = new Order();
     $order->user_id = Session::get('loggedinUserId');
     $order->order_amt = $cartTotal;
     $order->pay_amt = $cartTotal;
     $order->cod_charges = '0';
     $order->payment_method = $payment_method;
     $order->payment_status = '1';
     $order->cart = json_encode($cartContent);
     $order->first_name = $address->firstname;
     $order->last_name = $address->lastname;
     $order->location = $address->address;
     $order->city = $address->city;
     $order->postcode = $address->postcode;
     $order->country_id = $address->country_id;
     $order->state_id = $address->zone_id;
     $order->order_status = '1';
     $order->save();
     //echo "Thank You";
     $cart_ids = [];
     foreach ($cartContent as $cart) {
         $cart_ids[$cart->rowid] = ["prod_id" => $cart->id, "qty" => $cart->qty, "uprice" => $cart->price, "price" => $cart->subtotal, "created_at" => date('Y-m-d H:i:s')];
         //  dd($cart_ids[$cart->rowid]);
         if ($cart->options->has('sub_prod')) {
             $cart_ids[$cart->rowid]["sub_prod_id"] = $cart->options->sub_prod;
             $cart_ids[$cart->rowid]["price_saved"] = $cart->options->ysave;
         } else {
             if ($cart->options->has('combos')) {
                 $sub_prd_ids = [];
                 foreach ($cart->options->combos as $key => $val) {
                     if (isset($val['sub_prod'])) {
                         array_push($sub_prd_ids, (string) $val['sub_prod']);
                     }
                 }
                 $cart_ids[$cart->id]["sub_prod_id"] = json_encode($sub_prd_ids);
             }
         }
         if (!empty($cart_ids)) {
             $order->products()->sync($cart_ids);
         }
     }
     return view(Config('constants.frontendCheckoutView') . '.response');
 }