Inheritance: extends Controller
Example #1
0
 public function index()
 {
     $panel = ['center' => ['width' => 10], 'left' => ['width' => 2, 'class' => 'home-no-padding']];
     $helperProd = new productsHelper();
     $carousel = $helperProd->suggest('carousel');
     $viewed = $helperProd->suggest('viewed', 8);
     $categories = $helperProd->suggest('categories');
     $purchased = $helperProd->suggest('purchased');
     $suggestion = ['carousel' => $carousel, 'viewed' => $viewed, 'categories' => $categories, 'purchased' => $purchased];
     $helperProd->resetHaystack();
     //reseting session id validator
     $events = [];
     if (config('app.offering_free_products')) {
         $events = FreeProduct::getNextEvents(['id', 'description', 'min_participants', 'max_participants', 'participation_cost', 'start_date', 'end_date'], 4, date('Y-m-d'));
     }
     $tagsCloud = ProductsController::getTopRated(0, 20, true);
     $allWishes = '';
     $user = \Auth::user();
     if ($user) {
         $allWishes = Order::ofType('wishlist')->where('user_id', $user->id)->where('description', '<>', '')->get();
     }
     $i = 0;
     //carousel implementation
     $jumbotronClasses = ['jumbotron-box-left', 'jumbotron-box-right'];
     //carousel implementation
     $banner = ['/img/banner/01.png', '/img/banner/02.png', '/img/banner/03.png', '/img/banner/04.png'];
     // $this->createTags();
     return view('home', compact('panel', 'suggestion', 'allWishes', 'events', 'tagsCloud', 'jumbotronClasses', 'i', 'banner'));
 }
Example #2
0
 /**
  * [searchAll description]
  * @param  Request $request [description]
  * @return [type]           [description]
  */
 public function searchAll(Request $request)
 {
     $crit = $request->get('crit');
     $response['products'] = array('results' => null, 'suggestions' => null);
     if ($crit != '') {
         $response['products']['results'] = Product::where('status', 1)->search($crit)->Free()->take(5)->get();
     }
     $response['products']['suggestions'] = ProductsController::getSuggestions(['user_id' => \Auth::id(), 'preferences_key' => 'my_searches', 'limit' => 3]);
     if ($request->wantsJson()) {
         return json_encode($response);
     }
 }
Example #3
0
 /**
  * Show the contents of the user Cart.
  *
  * @return view for orders.cart
  */
 public function showCart()
 {
     $user = \Auth::user();
     /**
      * $suggest-listed keeps tracking listed products to control the suggestion view
      */
     Session::forget('suggest-listed');
     /**
      * $totalAmount saves the shopping cart total amount
      * @var decimal
      */
     $totalAmount = 0;
     /**
      * $totalItems saves the shopping cart total items
      * @var integer
      */
     $totalItems = 0;
     if ($user) {
         /**
          * $cart has all the shopping cart information, which comes from an type of order called "cart"
          * @var [type]
          */
         $cart = Order::ofType('cart')->where('user_id', $user->id)->with('details')->first();
         /**
          * $laterCart has all the shopping cart (saved for later) information, which comes from an type of order called "later"
          * @var [type]
          */
         $laterCart = Order::ofType('later')->where('user_id', $user->id)->with('details')->first();
         /**
          * $validation_message keeps the message for those items that has a different stock since they were added to a shopping cart
          * @var array
          */
         $validation_message = [];
         if ($cart) {
             foreach ($cart->details as $detail) {
                 $totalItems += $detail->quantity;
                 $totalAmount += $detail->quantity * $detail->price;
                 if ($detail->quantity > $detail->product->stock) {
                     $detail->quantity = $detail->product->stock;
                     $detail->save();
                     $validation_message[] = trans('store.cart_view.item_changed_stock1') . ' ' . $detail->product->name . ' ' . trans('store.cart_view.item_changed_stock2');
                 }
                 //saving the product listed to not show it on suggestion view
                 Session::push('suggest-listed', $detail->product_id);
             }
             //saving the changes made to suggest-listed session var
             Session::save();
         }
         //if there are validation messages to show, they'll be saved in message session var
         if (count($validation_message) > 0) {
             Session::push('message', $validation_message);
         }
     } else {
         /**
          * $session_cart keeps saved all the items added to the shopping cart befor the user ins logged
          * @var [array]
          */
         $session_cart = Session::get('user.cart');
         if (is_array($session_cart)) {
             $session_details = Session::get('user.cart_content');
             $cart_details = [];
             $validation_message = [];
             foreach ($session_details as $id => $quantity) {
                 $product = Product::find($id);
                 $totalAmount += $product->price;
                 if ($quantity > $product->stock) {
                     $quantity = $product->stock;
                     $validation_message[] = trans('store.cart_view.item_changed_stock1') . ' ' . $product->name . ' ' . trans('store.cart_view.item_changed_stock2');
                 }
                 $cart_details[] = ['id' => 0, 'order_id' => 0, 'product_id' => $product->id, 'price' => $product->price, 'quantity' => $quantity, 'product' => ['id' => $product->id, 'name' => $product->name, 'description' => $product->description, 'price' => $product->price, 'stock' => $product->stock, 'type' => $product->type, 'features' => ['images' => [$product->FirstImage]]]];
                 Session::push('suggest-listed', $product->id);
             }
             if (count($validation_message) > 0) {
                 Session::push('message', $validation_message);
             }
             $cart = ['id' => 0, 'user_id' => 0, 'details' => $cart_details];
             $totalItems = count($cart_details);
         } else {
             $cart = ['id' => 0, 'user_id' => 0, 'details' => []];
         }
         $laterCart = [];
     }
     $panel = array('center' => ['width' => '12']);
     //suggestions based on cart content
     $suggestions = ProductsController::getSuggestions(['preferences_key' => Session::get('suggest-listed'), 'limit' => 4]);
     Session::forget('suggest-listed');
     return view('orders.cart', compact('cart', 'user', 'panel', 'laterCart', 'suggestions', 'totalItems', 'totalAmount'));
 }
Example #4
0
 /**
  * Start the checkout process for any type of order
  *
  * @param  int  $type_order Type of order to be processed
  * @return Response
  */
 public static function placeOrders($type_order)
 {
     $cart = Order::ofType($type_order)->auth()->whereStatus('open')->orderBy('id', 'desc')->first();
     $show_order_route = $type_order == 'freeproduct' ? 'freeproducts.show' : 'orders.show_cart';
     $cartDetail = OrderDetail::where('order_id', $cart->id)->get();
     $address_id = 0;
     //When address is invalid, it is because it comes from the creation of a free product. You must have a user direction (Default)
     if (is_null($cart->address_id)) {
         $useraddress = UserAddress::auth()->orderBy('default', 'DESC')->first();
         if ($useraddress) {
             $address_id = $useraddress->address_id;
         } else {
             return trans('address.no_registered');
         }
     } else {
         $address_id = $cart->address_id;
     }
     $address = Address::where('id', $address_id)->first();
     //Checks if the user has points for the cart price and the store has stock
     //and set the order prices to the current ones if different
     //Creates the lists or sellers to send mail to
     $total_points = 0;
     $seller_email = array();
     foreach ($cartDetail as $orderDetail) {
         $product = Product::find($orderDetail->product_id);
         $seller = User::find($product->user_id);
         if (!in_array($seller->email, $seller_email)) {
             $seller_email[] = $seller->email;
         }
         $total_points += $orderDetail->quantity * $product->price;
         if ($orderDetail->price != $product->price) {
             $orderDetail->price = $product->price;
             $orderDetail->save();
         }
         if ($product->type != 'item') {
             $virtual = VirtualProduct::where('product_id', $orderDetail->product_id)->get();
             $first = $virtual->first();
             //$first=null;
             //foreach ($virtual as $row){
             //$first=$row;
             //break;
             //}
             switch ($product->type) {
                 case 'key':
                 case 'software_key':
                     $virtualOrder = VirtualProductOrder::where('virtual_product_id', $first->id)->where('order_id', $orderDetail->order_id)->where('status', 1)->get();
                     if (count($virtual) - 1 < count($virtualOrder)) {
                         return trans('store.insufficientStock');
                     }
                     break;
                 default:
                     break;
             }
         } elseif ($product->stock < $orderDetail->quantity) {
             return trans('store.insufficientStock');
         }
     }
     //Checks if the user has points for the cart price
     $user = \Auth::user();
     if ($user->current_points < $total_points && config('app.payment_method') == 'Points') {
         return trans('store.cart_view.insufficient_funds');
     }
     if (config('app.payment_method') == 'Points') {
         $negativeTotal = -1 * $total_points;
         //7 is the action type id for order checkout
         $pointsModified = $user->modifyPoints($negativeTotal, 7, $cart->id);
     } else {
         $pointsModified = true;
     }
     if ($pointsModified) {
         //Separate the order for each seller
         //Looks for all the different sellers in the cart
         $sellers = [];
         foreach ($cartDetail as $orderDetail) {
             if (!in_array($orderDetail->product->user_id, $sellers)) {
                 $sellers[] = $orderDetail->product->user_id;
             }
         }
         foreach ($sellers as $seller) {
             //Creates a new order and address for each seller
             $newOrder = new Order();
             $newOrder->user_id = $user->id;
             $newOrder->address_id = $address->id;
             $newOrder->status = $type_order == 'freeproduct' ? 'paid' : 'open';
             $newOrder->type = $type_order == 'freeproduct' ? 'freeproduct' : 'order';
             $newOrder->seller_id = $seller;
             $newOrder->save();
             $newOrder->sendNotice();
             //moves the details to the new orders
             foreach ($cartDetail as $orderDetail) {
                 if ($orderDetail->product->user_id == $seller) {
                     $orderDetail->order_id = $newOrder->id;
                     $orderDetail->save();
                 }
                 //Increasing product counters.
                 ProductsController::setCounters($orderDetail->product, ['sale_counts' => trans('globals.product_value_counters.sale')], 'orders');
                 //saving tags in users preferences
                 if (trim($orderDetail->product->tags) != '') {
                     UserController::setPreferences('product_purchased', explode(',', $orderDetail->product->tags));
                 }
             }
         }
         //virtual products
         //Changes the stock of each product in the order
         foreach ($cartDetail as $orderDetail) {
             $product = Product::find($orderDetail->product_id);
             $product->stock = $product->stock - $orderDetail->quantity;
             $product->save();
             if ($product->type != 'item') {
                 $virtual = VirtualProduct::where('product_id', $orderDetail->product_id)->where('status', 'open')->get();
                 switch ($product->type) {
                     case 'key':
                         $first = VirtualProduct::where('product_id', $orderDetail->product_id)->where('status', 'cancelled')->first();
                         foreach ($virtual as $row) {
                             $virtualOrder = VirtualProductOrder::where('order_id', $cart->id)->where('virtual_product_id', $first->id)->where('status', 1)->first();
                             if ($virtualOrder) {
                                 $virtualOrder->virtual_product_id = $row->id;
                                 $virtualOrder->order_id = $orderDetail->order_id;
                                 $virtualOrder->status = 2;
                                 $virtualOrder->save();
                                 $row->status = 'paid';
                                 $row->save();
                             } else {
                                 break;
                             }
                         }
                         break;
                     default:
                         break;
                 }
             }
         }
         foreach ($seller_email as $email) {
             $mailed_order = Order::where('id', $newOrder->id)->with('details')->get()->first();
             //Send a mail to the user: Order has been placed
             $data = ['orderId' => $newOrder->id, 'order' => $mailed_order];
             //dd($data['order']->details,$newOrder->id);
             $title = trans('email.new_order_for_user.subject') . " (#{$newOrder->id})";
             Mail::queue('emails.neworder', compact('data', 'title'), function ($message) use($user) {
                 $message->to($user->email)->subject(trans('email.new_order_for_user.subject'));
             });
             //Send a mail to the seller: Order has been placed
             $title = trans('email.new_order_for_seller.subject') . " (#{$newOrder->id})";
             Mail::queue('emails.sellerorder', compact('data', 'title'), function ($message) use($email) {
                 $message->to($email)->subject(trans('email.new_order_for_seller.subject'));
             });
         }
         return;
     } else {
         return trans('store.insufficientFunds');
     }
 }
Example #5
0
 /**
  * manage the home section suggestions
  * @param  [string] $type, which is the reference point to build the suggest
  * @return [json] $suggest, that contain the products list to be displayed on home page
  */
 public static function suggest($type, $limit = 4)
 {
     $data = [];
     switch ($type) {
         case 'purchased':
             $data['preferences_key'] = 'product_purchased';
             $data['limit'] = $limit;
             break;
         case 'categories':
             $data['preferences_key'] = 'product_categories';
             $data['limit'] = $limit;
             $usr_prefe = UserController::getPreferences('', $data['preferences_key']);
             //look up for user preferences
             if (count($usr_prefe['tags']) == 0) {
                 $data['category'] = ProductsController::getRandCategoryId();
                 //if there is not info, we get a rand category id
             } else {
                 $data['category'] = $usr_prefe['tags'][mt_rand(0, count($usr_prefe['tags']) - 1)];
                 //if so, we get a rand user preferences category
             }
             break;
         case 'viewed':
             $data['preferences_key'] = 'product_viewed';
             $data['limit'] = $limit;
             break;
         case 'carousel':
             return ProductsController::getTopRated(0, $limit, false);
             break;
         default:
             $data['limit'] = $limit;
             $data['preferences_key'] = '';
             break;
     }
     $suggest = ProductsController::getSuggestions($data);
     //suggestion array
     return $suggest;
 }
Example #6
0
 /**
  * Save the user preferences.
  *
  * @param [String] $index user preference key array
  * @param [Array]  $tags  products tags
  */
 public static function setPreferences($index = '', $tags = [])
 {
     $user = \Auth::user();
     if ($user) {
         $userHelper = new UserHelper();
         $categories = ProductsController::getTagsCategories($tags);
         $user->preferences = $userHelper->preferencesToJson($user->preferences, $index, $tags, $categories);
         $user->save();
     }
 }
 /**
  * [Search products in auto complete fields]
  * @param  Request $request [Request laravel]
  * @return [type]           [json array]
  */
 public function searchAll(Request $request)
 {
     $crit = $request->get('crit');
     $suggest = $request->get('suggest');
     $group = $request->get('group');
     $response['products'] = array('results' => null, 'suggestions' => null);
     $crit = str_replace(' ', '%', trim($crit));
     $crit = str_replace('%%', '%', $crit);
     if ($crit != '') {
         if ($suggest) {
             $response['products']['categories'] = Category::select('id', 'name')->search($crit, null, true)->actives()->where('type', 'store')->orderBy('name')->take(3)->get();
         }
         $response['products']['results'] = Product::where(function ($query) use($crit) {
             $query->where('name', 'like', '%' . $crit . '%')->orWhere('description', 'like', '%' . $crit . '%');
         })->select('id', 'name', 'products_group')->actives()->free()->orderBy('rate_val', 'desc');
         if ($group) {
             $response['products']['results']->where(function ($query) use($group) {
                 $query->where('products_group', '<>', $group)->orWhereNull('products_group');
             })->where('id', '<>', $group);
         }
         $response['products']['results'] = $response['products']['results']->take(5)->get();
         $deep = '';
         if ($suggest) {
             $crit = str_replace('%', '', $crit);
             for ($i = 0; $i < strlen($crit); $i++) {
                 $deep .= ' ' . $crit[$i];
             }
         }
         if (!$response['products']['results']->count() && strlen($crit) > 2) {
             $response['products']['results'] = Product::select('id', 'name', 'products_group')->search($deep, null, true)->actives()->free()->orderBy('rate_val', 'desc');
             if ($group) {
                 $response['products']['results']->where(function ($query) use($group) {
                     $query->where('products_group', '<>', $group)->orWhereNull('products_group');
                 })->where('id', '<>', $group);
             }
             $response['products']['results'] = $response['products']['results']->take(5)->get();
         }
         if ($suggest) {
             $response['products']['suggestions'] = ProductsController::getSuggestions(['user_id' => \Auth::id(), 'preferences_key' => 'my_searches', 'limit' => 3, 'select' => ['id', 'name', 'features']]);
             if (!$response['products']['categories']->count() && strlen($crit) > 2) {
                 $response['products']['categories'] = Category::select('id', 'name')->search($deep, null, true)->actives()->where('type', 'store')->orderBy('name')->take(3)->get();
             }
         }
     }
     $response['products']['categories_title'] = trans('globals.suggested_categories');
     $response['products']['suggestions_title'] = trans('globals.suggested_products');
     $response['products']['results_title'] = trans('globals.searchResults');
     if ($request->wantsJson()) {
         return json_encode($response);
     } else {
         if (env('APP_DEBUG', false)) {
             dd($response);
         }
     }
 }