public function checkout_step3() { // Processing Cart $cart_id = $this->cart_id; if (!$cart_id) { $cart_id = Input::get('cart_id'); } $address_id = Input::get('address_id'); $payment_id = Input::get('payment_id'); $cart_products = DB::table('cart_products')->where('cart_id', $cart_id)->leftJoin('products', 'cart_products.product_id', '=', 'products.id')->leftJoin('users', 'cart_products.added_by', '=', 'users.id')->select('products.*', 'cart_products.quantity as cart_quantity', 'users.first_name as added_by')->orderBy('store_id')->get(); $cart_users = DB::table('cart_users')->leftJoin('users', 'cart_users.user_id', '=', 'users.id')->select('users.*')->get(); $response_array['cart'] = Cart::find($cart_id)->toArray(); $response_array['cart']['total_amount'] = 0; $response_array['cart']['total_quantity'] = 0; $response_array['cart']['users'] = array(); $response_array['cart']['stores'] = array(); $store_id = 0; if ($cart_products) { $response_array['cart']['is_minimum_reached'] = 1; foreach ($cart_products as $cart_product) { if ($store_id != $cart_product->store_id) { $store_id = $cart_product->store_id; // Retriving Store $store = Store::find($store_id)->toArray(); // Retriving Delivery Slot $time = date("Y-m-d H:i:s"); $slot = Slot::where('store_id', $store_id)->where('start_time', '>', $time)->where('is_available', 1)->orderBy('start_time')->first(); if ($slot) { $next_slot_start = date("D, jS M, H a", strtotime($slot->start_time)); $next_slot_end = date("h a", strtotime($slot->end_time)); $next_slot = $next_slot_start . " - " . $next_slot_end; } else { $next_slot = "No Slots available"; } $store['slot'] = $next_slot; $store['count'] = 1; $store['sub_total'] = 0; $response_array['cart']['stores'][$store_id] = $store; $response_array['cart']['stores'][$store_id]['products'] = array(); } else { $response_array['cart']['stores'][$store_id]['count'] += 1; } $product = json_decode(json_encode($cart_product), true); $product['sub_total'] = $product['cart_quantity'] * $product['price']; $response_array['cart']['stores'][$store_id]['sub_total'] += $product['sub_total']; $response_array['cart']['total_amount'] += $product['sub_total']; $response_array['cart']['total_quantity']++; array_push($response_array['cart']['stores'][$store_id]['products'], $product); } } if ($cart_users) { foreach ($cart_users as $cart_user) { $product = json_decode(json_encode($cart_user), true); array_push($response_array['cart']['users'], $cart_user); } } foreach ($response_array['cart']['stores'] as $st) { if ($st['sub_total'] < $st['minimum_order_amount']) { $response_array['cart']['is_minimum_reached'] = 0; } } if (isset($response_array['cart']['is_minimum_reached']) && $response_array['cart']['is_minimum_reached'] == 1) { $order = new Order(); $order->user_id = $this->user_id; $order->date = date("Y-m-d H:i:s"); $order->total_products = $response_array['cart']['total_quantity']; $order->total_amount = $response_array['cart']['total_amount']; $order->payment_id = $payment_id; $order->address_id = $address_id; $payment = Payment::find($payment_id); $order->payment_customer_id = $payment->customer_id; $address = UserAddress::find($address_id); $order->address = $address->address . " " . $address->zipcode; $order->status = "Initiated"; $order->save(); foreach ($cart_products as $cart_product) { $order_product = new OrderProduct(); $order_product->order_id = $order->id; $product = Product::find($cart_product->id); $product->total_sale += $cart_product->cart_quantity; $product->save(); $order_product->price = $cart_product->price; $order_product->quantity = $cart_product->cart_quantity; $order_product->fulfilment_status = "Pending"; $order_product->type = "Product"; $order_product->parent_id = 0; $order_product->product_name = $product->name; $order_product->product_image_url = $product->image_url; $order_product->product_quantity = $product->quantity; $order_product->product_unit = $product->unit; $order_product->store_id = $product->store_id; $order_product->save(); } // removing products from cart CartProduct::where('cart_id', $cart_id)->delete(); // Order Placement Mail $user = User::find($this->user_id); Mail::send('emails.order_success', array('user' => $user, 'order' => $order), function ($message) use($user) { $message->to($user->email, $user->first_name)->subject('Thanks for placing the order!'); }); // Order Placement Mail to Admin $admin_email = Config::get('app.admin_email'); Mail::send('emails.admin_order_success', array('user' => $user, 'order' => $order), function ($message) use($admin_email) { $message->to($admin_email, "Admin")->subject('New Order!'); }); } else { $message = "Minimum order amount not reached"; if (Request::format() == 'html') { return View::make('checkout_step3')->with('store', $this->store)->with('departments', $this->departments)->with('zipcode', $this->zipcode)->with('city', $this->city)->with('stores1', $this->stores)->with('message', $message); } else { $response_array = array('success' => false, 'error_code' => 411, 'error' => $message); $response_code = 200; $response = Response::json($response_array, $response_code); return $response; } } if (Request::format() == 'html') { return View::make('checkout_step3')->with('store', $this->store)->with('departments', $this->departments)->with('zipcode', $this->zipcode)->with('city', $this->city)->with('stores1', $this->stores)->with('message', 'Thanks for placing your Order!!'); } else { $response_array = array('success' => true); $response_code = 200; $response = Response::json($response_array, $response_code); return $response; } }
public function order_shoppers() { $id = Input::get('order_id'); $order = Order::find($id); $address = UserAddress::find($order->address_id); $shoppers = Shopper::where('zipcode', $address->zipcode)->get()->toArray(); $response = Response::json($shoppers); return $response; }