public function deleteCart()
 {
     $query = Cart::find(Input::get('id'));
     if ($query->delete()) {
         return Redirect::back()->with('event', '<p class="alert alert-success"><span class="glyphicon glyphicon-ok"></span> Cart deleted</p>');
     }
     return Redirect::back()->with('event', '<p class="alert alert-danger"><span class="glyphicon glyphicon-remove"></span> Error occured.</p>');
 }
 function isCartEmpty()
 {
     $result = $this->Cart->find('first', array('conditions' => array('Cart.ct_session_id' => $this->sid)));
     if (empty($result)) {
         return true;
     } else {
         return false;
     }
 }
 public function postDelete()
 {
     $query = Cart::find(Input::get('id'));
     $user = User::where('id', Input::get('user_id'))->get();
     // Check field
     $validator = Validator::make(Input::all(), ['id' => 'required', 'user_id' => 'required', 'message' => 'required']);
     if ($validator->fails()) {
         return Redirect::back()->withErrors($validator)->withInput();
     }
     $email = $user->first()->email;
     $name = $user->first()->name;
     // Send mail
     Mail::send('emails.auth.denyOrder', ['name' => $name, 'message' => Input::get('message')], function ($message) use($email, $name) {
         $message->from("*****@*****.**", "Nexus IT Zone");
         $message->to($email, $name)->subject('Order denied');
     });
     // Delete the order
     if ($query->delete()) {
         return Redirect::back()->with('event', '<p class="alert alert-success"><span class="glyphicon glyphicon-warning"></span> Order deleted</p>');
     }
     return Redirect::back()->with('event', '<p class="alert alert-danger"><span class="glyphicon glyphicon-remove"></span> Error occured</p>');
 }
 public static function create_from_signup($signup, $note = "")
 {
     $hash = self::hash_items($signup);
     $cart = Cart::find("carts.hash = '{$hash}' AND carts.hash IS NOT NULL");
     if ($cart) {
         return $cart;
     }
     $cart = new Cart();
     $cart->user_id = $signup->user->id;
     $cart->note = $note;
     $cart->hash = $hash;
     if ($cart->save()) {
         // Add Signup
         $added = false;
         if (!$signup->paid) {
             $cart->add_item($signup);
             $added = true;
         }
         foreach ($signup->event_services() as $service) {
             if (!$service->paid) {
                 $cart->add_item($service);
                 $added = true;
             }
         }
         $cart = Cart::find_by_id($cart->id);
         if ($added) {
             return $cart;
         } else {
             $cart->destroy();
             return false;
         }
     }
     return false;
 }
 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;
     }
 }
 /**
  * Show the form for editing the specified cart.
  *
  * @param  int  $id
  * @return Response
  */
 public function edit($id)
 {
     $cart = Cart::find($id);
     return View::make('carts.edit', compact('cart'));
 }
 public function complete()
 {
     if (isset($_GET['merchant_return_link'])) {
         $id = $_GET['id'];
         Redirect("payments/{$id}/complete");
     }
     $id = mysql_real_escape_string($_GET['id']);
     $user_id = mysql_real_escape_string(Site::CurrentUser()->id);
     $cart = Cart::find("carts.user_id = '{$user_id}' AND carts.id = '{$id}'");
     if ($cart) {
         // Validate Cart
         foreach ($cart->items() as $item) {
             if ($item->object == null) {
                 Error404();
             }
         }
         $signups = $cart->get_signups();
         if (count($signups) == 1) {
             $this->assign("signup", current($signups));
         }
         $redemptions = array();
         if ($cart->full_cart_discount()) {
             $id = mysql_real_escape_string($cart->id);
             $redemptions = DiscountRedemption::find_all("discount_redemptions.cart_id = '{$id}' and discount_redemptions.cart_item_id is null");
         }
         $this->assign("cart", $cart);
         $this->assign("cart_discounts", $redemptions);
         $this->title = "Payment Complete";
         $this->render("paymenttransaction/complete.tpl");
     } else {
         Error404();
     }
 }
 public function user_index()
 {
     $user_id = mysql_real_escape_string(Site::CurrentUser()->id);
     // Code from Cart->create()			// First check CRSF
     if ($this->post) {
         // From the post data, build the cart items
         $raw_items = array();
         if (isset($_POST['items']['signups'])) {
             foreach ($_POST['items']['signups'] as $id => $value) {
                 $id = mysql_real_escape_string($id);
                 $signup = EventSignup::find("event_signups.user_id = '{$user_id}' AND event_signups.id = '{$id}'");
                 if ($signup && !$signup->paid && !$signup->is_soldout()) {
                     $raw_items[$signup->id]['signup'] = $signup;
                 }
             }
         }
         if (isset($_POST['items']['services'])) {
             $service_count = array();
             foreach ($_POST['items']['services'] as $id => $value) {
                 $id = mysql_real_escape_string($id);
                 $service = EventService::find("event_signups.user_id = '{$user_id}' AND event_services.id = '{$id}'");
                 if ($service && !$service->paid && $service->service->available()) {
                     if (isset($service_count[$service->service->id])) {
                         $service_count[$service->service->id]++;
                     } else {
                         $service_count[$service->service->id] = 1;
                     }
                     if ($service->service->available() == -1 || $service_count[$service->service->id] <= $service->service->available()) {
                         if ($service->event_signup->paid || isset($raw_items[$service->event_signup->id]['signup'])) {
                             $raw_items[$service->event_signup->id]['services'][] = $service;
                         }
                     }
                 }
             }
         }
         // Sort properly
         $items = array();
         foreach ($raw_items as $id => $parts) {
             if (isset($parts['signup'])) {
                 $items[] = $parts['signup'];
             }
             if (isset($parts['services'])) {
                 $items = array_merge($items, $parts['services']);
             }
         }
         if (count($items) == 0) {
             Site::Flash("error", "None of the items you selected could be paid for.");
             Redirect("bookings");
         }
         $hash = Cart::hash_items($items);
         $cart = Cart::find("carts.hash = '{$hash}' AND carts.hash IS NOT NULL");
         if (!$cart) {
             $cart = new Cart();
             $cart->user_id = $user_id;
             $cart->hash = $hash;
             if ($cart->save()) {
                 foreach ($items as $item) {
                     $cart->add_item($item);
                 }
             } else {
                 Site::Flash("error", "Unable to create cart.");
                 Redirect("bookings");
             }
         }
         Redirect("bookings/pay/{$cart->id}");
     } elseif ($this->post) {
         global $site;
         $site['flash']['error'] = "Invalid form submission";
     }
     // Fetch all signups in event order and iterate through them
     $items = array();
     $signups = EventSignup::find_all("event_signups.user_id = '{$user_id}' AND events.enddate >= NOW()", "events.startdate DESC");
     foreach ($signups as $signup) {
         if (!$signup->paid) {
             $items[] = $signup;
         } else {
             foreach ($signup->event_services() as $service) {
                 if (!$service->paid) {
                     $items[] = $signup;
                     break;
                 }
             }
         }
     }
     $this->assign("items", $items);
     // Traditional My Bookings Page
     $unpaid = EventSignup::find_all("event_signups.user_id = '{$user_id}' AND event_signups.paid = false", "events.startdate DESC");
     $paid = EventSignup::find_all("event_signups.user_id = '{$user_id}' AND event_signups.paid = true AND voucher = false", "events.startdate DESC");
     $vouchers = EventSignup::find_all("event_signups.user_id = '{$user_id}' AND event_signups.paid = true AND voucher = true", "events.startdate DESC");
     $this->assign("unpaid", $unpaid);
     $this->assign("paid", $paid);
     $this->assign("vouchers", $vouchers);
     $this->title = "My Bookings";
     $this->render("event_signup/user_index.tpl");
 }
 public function removeFromCart($id)
 {
     $cartItem = Cart::find($id);
     $cartItem->remove();
     return Redirect::back()->with('message', 'Item removed from cart.');
 }
 public function getDelete($id)
 {
     $cart = Cart::find($id)->delete();
     return Redirect::route('cart');
 }
 public function delCart($id)
 {
     $token = Input::get('token', '');
     $u_id = Input::get('u_id', 0);
     try {
         $user = User::chkUserByToken($token, $u_id);
         $cart = Cart::find($id);
         if (empty($cart->c_id)) {
             throw new Exception("无法获取到购物车", 7001);
         }
         if ($cart->c_status != 1 || $cart->u_id != $u_id) {
             throw new Exception("该购物车已经失效", 7001);
         }
         $cart->removeCart();
         $re = Tools::reTrue('删除购物车成功');
     } catch (Exception $e) {
         $re = Tools::reFalse($e->getCode(), '删除购物车失败:' . $e->getMessage());
     }
     return Response::json($re);
 }
 protected function getCart($id = null)
 {
     if (!$id) {
         $id = $this->GetData('id');
     }
     $id = mysql_real_escape_string($id);
     $user_id = mysql_real_escape_string(Site::CurrentUser()->id);
     $cart = Cart::find("carts.id = '{$id}' and carts.user_id = '{$user_id}'");
     if (!$cart) {
         throw new Error404('Unable to find cart');
     }
     return $cart;
 }