public function checkout()
 {
     //Check if user is logged in
     if (!Backend::validateUser()) {
         return redirect('/');
     }
     $aRequest = \Request::all();
     //This is a temprory array that will be used to create a temp item to be used in saved and any processing on item before saving it.
     $tempItem = array();
     if (!isset($aRequest['items'])) {
         return;
     }
     /*
      * Total price will be saved here,
      * this also will be used to indicate the last price that will be required on each restaurant
      */
     foreach ($aRequest['items'] as $item) {
         $dTotalPrice = 0;
         $oItem = DB::table('food')->where(array('id_item' => $item['id_item']))->get(array("id_restaurant", "price"))[0];
         $tempItem['id_item'] = $item['id_item'];
         $tempItem['qty'] = $item['qty'];
         $tempItem['spicy'] = $item['spicy'];
         //$tempItem['note']=$item['note'];
         $tempItem['total_price'] = $oItem->price * $item['qty'];
         $dTotalPrice += $tempItem['total_price'];
         $aRestaurants[$oItem->id_restaurant]['total_price'] = $dTotalPrice;
         $aRestaurants[$oItem->id_restaurant]['items'][] = $tempItem;
         $tempItem = array();
     }
     $sNote = $aRequest['note'];
     $sLocation = json_encode($aRequest['location']);
     foreach ($aRestaurants as $id_restaurant => $aRestaurant) {
         $id_user = \Session::all()['id_user'];
         $sOrderDetails = json_encode($aRestaurant);
         $aOrder = array('id_user' => $id_user, 'id_restaurant' => $id_restaurant, 'order_details' => $sOrderDetails, 'note' => $sNote, 'location' => $sLocation, 'date_inserted' => Date('Y-m-d h:i:s'), 'status' => 'not_approved_yet');
         DB::table('orders')->insert($aOrder);
     }
     \Session::forget('cart');
     return redirect('/');
 }
 public static function LoginData()
 {
     $aSession = \Session::all();
     $aData = array();
     $aData['items_count'] = isset($aSession['cart']['items']) ? count($aSession['cart']['items']) : 0;
     if (isset($aSession['loggedin']) && $aSession['loggedin'] == true) {
         $aData['username'] = $aSession['username'];
         $aData['id_user'] = $aSession['id_user'];
         $aData['logged'] = 'yes';
         $aData['user_type'] = $aSession['user_type'];
     } else {
         //Not logged in logic
         $aData['logged'] = 'no';
     }
     return $aData;
 }
 public function showRestaurantsOrders()
 {
     if (!Backend::validateUser()) {
         return redirect('/');
     }
     $aSession = \Session::all();
     $aOrdersList = DB::table('orders')->where(array('id_restaurant' => $aSession['id_user']))->WhereNotIn('status', array('DONE'))->orderBy('date_inserted', 'desc')->get(array('*'));
     $aTmpOrder = array();
     $dTotalPrice = 0;
     $aOrders = array();
     foreach ($aOrdersList as $oOrder) {
         $aTmpOrder['customer'] = DB::table('users')->where(array('id_user' => $oOrder->id_user))->get(array('first_name', 'last_name', 'user_mobile', 'username', 'email'))[0];
         $oOrderDetails = json_decode($oOrder->order_details);
         $oOrder->order_details = $this->parseOrderDetails($oOrderDetails);
         $oOrder->status = str_replace('_', ' ', $oOrder->status);
         $oLocation = json_decode($oOrder->location);
         if (!empty($oLocation->lat)) {
             $aTmpOrder['location'] = "http://maps.google.com/maps?q={$oLocation->lat},{$oLocation->long}";
         }
         $aTmpOrder['info'] = $oOrder;
         $dTotalPrice += $oOrderDetails->total_price;
         $oOrder->date_inserted = Backend::ago(new \DateTime($oOrder->date_inserted));
         $aOrders[] = $aTmpOrder;
         $aTmpOrder = [];
     }
     $data = Backend::LoginData();
     $data['isRestaurant'] = true;
     $data['status'] = array('preparing', 'canceled', 'not approved yet', 'on the way');
     return view('pages.orders')->with(array('orders' => $aOrders, 'data' => $data));
 }
 public function addNewOffer()
 {
     $aSession = \Session::all();
     $this->validateUser($aSession);
     $aItemsList = DB::table('food')->where(array('id_restaurant' => $aSession['id_user']))->get(array("*"));
     $aData = array('user_type' => $aSession['user_type'], 'id_user' => $aSession['id_user'], 'page_type' => 'offer');
     return view('/pages.addNewMenu')->with(array('data' => $aData, 'items' => $aItemsList));
 }
 function back_button()
 {
     if (\Session::has('entry_date')) {
         \Session::forget('entry_date');
     }
     $active = $this->check();
     if ($active) {
         $Session = \Session::all();
         //return view('Dashboard', compact('Session'));
         return \Redirect::to('dashboard');
     } else {
         return \Redirect::to('login');
     }
 }
 public function submitReview()
 {
     //Get the request from the submitted page
     $aRequest = \Request::all();
     //Get the Session
     $aSession = \Session::all();
     //GET RESTAURANT ID
     $aRestaurant = DB::table('restaurants')->where(array('username' => $aRequest['username']))->get(array("id_restaurant"));
     $oRestaurant = $aRestaurant[0];
     $aReview['id_restaurant'] = $oRestaurant->id_restaurant;
     $aReview['id_user'] = $aSession['id_user'];
     $aReview['body'] = $aRequest['review'];
     $aReview['rating'] = $aRequest['rating'];
     $oImage = $aRequest['review-picture'];
     //Uplaod the pociture of the review
     $FullImagePath = Backend::uploadPhotos($oImage, 'review-picture');
     $aReview['review_image'] = $FullImagePath;
     $aReview['date_created'] = Date('Y-m-d h:i:s');
     //Insert the data into Reviews Table and get it's id
     $dReviewId = DB::table('reviews')->insertGetId($aReview);
     //Add Activity to it's table
     //save activity into the DB:
     Backend::addActivity($aSession['id_user'], 'review', $dReviewId);
     //Redirect the user to the restaurant's page.
     return redirect('/p/' . $aRequest['username']);
 }