/** * Display the specified resource. * * @param int $id * * @return Response */ public function show($id) { $user = \Auth::user(); $allWishes = ''; $panel = ['center' => ['width' => '12']]; if ($user) { $allWishes = Order::ofType('wishlist')->where('user_id', $user->id)->where('description', '<>', '')->orderBy('id', 'desc')->take(5)->get(); } $product = Product::select(['id', 'category_id', 'user_id', 'name', 'description', 'price', 'stock', 'features', 'condition', 'rate_val', 'rate_count', 'low_stock', 'status', 'type', 'tags', 'products_group', 'brand'])->with(['group' => function ($query) { $query->select(['id', 'products_group', 'features']); }])->with('categories')->find($id); if ($product) { //if there is a user in session, the admin menu will be shown if ($user && $user->id == $product->user_id) { $panel = ['left' => ['width' => '2'], 'center' => ['width' => '10']]; } //retrieving products features $features = ProductDetail::all()->toArray(); //increasing product counters, in order to have a suggestion orden $this->setCounters($product, ['view_counts' => trans('globals.product_value_counters.view')], 'viewed'); //saving the product tags into users preferences if (trim($product->tags) != '') { UserController::setPreferences('product_viewed', explode(',', $product->tags)); } //receiving products user reviews & comments $reviews = OrderDetail::where('product_id', $product->id)->whereNotNull('rate_comment')->select('rate', 'rate_comment', 'updated_at')->orderBy('updated_at', 'desc')->take(5)->get(); //If it is a free product, we got to retrieve its package information if ($product->type == 'freeproduct') { $order = OrderDetail::where('product_id', $product->id)->first(); $freeproduct = FreeProductOrder::where('order_id', $order->order_id)->first(); } $freeproductId = isset($freeproduct) ? $freeproduct->freeproduct_id : 0; //products suggestions control //saving product id into suggest-listed, in order to exclude products from suggestions type "view" Session::push('suggest-listed', $product->id); $suggestions = $this->getSuggestions(['preferences_key' => $product->id, 'limit' => 4]); Session::forget('suggest-listed'); //retrieving products groups of the product shown if (count($product->group)) { $featuresHelper = new featuresHelper(); $product->group = $featuresHelper->group($product->group); } return view('products.detailProd', compact('product', 'panel', 'allWishes', 'reviews', 'freeproductId', 'features', 'suggestions')); } else { return redirect(route('products')); } }
/** * Store a newly created resource in storage. * * @return Response */ public function store(Request $request) { try { $validator = Validator::make($request->all(), $this->form_rules); if ($validator->fails()) { return redirect()->route('freeproducts.create', [$request->input('order_id')])->withErrors($validator->errors())->withInput(); } //As is not defined that way is going to deliver products for every winner, I will confirm that the total winning is equal to total products in the cart $cart_detail = OrderDetail::where('order_id', $request->input('order_id'))->get(); if ($request->input('draw_number') > $cart_detail->count()) { return redirect()->route('freeproducts.create', [$request->input('order_id')])->withErrors(trans('freeproduct.drawnumber_exceed_total_products'))->withInput(); } else { //Process the order. The process is the same as with a shopping cart. The address is not requested //Direction is taken as the one with the user by default. Not having, it notifies the user to create a. $errors = Order::placeOrders('freeproduct'); if ($errors) { return redirect()->route('freeproducts.create', [$request->input('order_id')])->withErrors($errors)->withInput(); } else { $user = \Auth::user(); //Save Free Product $freeproduct = new FreeProduct(); $freeproduct->user_id = $user->id; $freeproduct->description = $request->input('description'); $freeproduct->start_date = $request->input('start_date'); $freeproduct->end_date = $request->input('end_date'); $freeproduct->participation_cost = $request->input('participation_cost'); $freeproduct->min_participants = $request->input('min_participants'); $freeproduct->max_participants = $request->input('max_participants'); $freeproduct->max_participations_per_user = $request->input('max_participations_per_user'); $freeproduct->draw_number = $request->input('draw_number'); $freeproduct->draw_date = $request->input('draw_date'); $freeproduct->save(); //Because the method placeOrders products generates orders for each vendor, you need to associate these orders to free product $orders = Order::ofType('freeproduct')->ofStatus('paid')->where('user_id', $user->id)->get(); if ($orders) { foreach ($orders as $order) { //Each order products are searched and a duplicate of the same is made, marking them as a free product. This will allow the product goes on the results of the advanced search $order_detail = OrderDetail::where('order_id', $order->id)->get(); if ($order_detail) { foreach ($order_detail as $detail) { $product = Product::find($detail->product_id); $productactual = $product->toArray(); unset($productactual['id']); unset($productactual['num_of_reviews']); $productactual['user_id'] = $user->id; $productactual['stock'] = $detail->quantity; $productactual['type'] = 'freeproduct'; $productactual['parent_id'] = $product->id; $newproduct = Product::create($productactual); } } if (!FreeProductOrder::where('order_id', $order->id)->first()) { //order registration as a free product $order_to_fp = new FreeProductOrder(); $order_to_fp->freeproduct_id = $freeproduct->id; $order_to_fp->order_id = $order->id; $order_to_fp->save(); } } } //Send message process Ok and redirect Session::flash('message', trans('freeproduct.saved_successfully')); return redirect()->route('freeproducts.show', [$freeproduct->id]); } } } catch (ModelNotFoundException $e) { Log::error($e); return redirect()->back()->withErrors(['induced_error' => [trans('freeproduct.error_exception')]])->withInput(); } }
/** * Display the specified resource. * * @param int $id * @return Response */ public function show($id) { $user = \Auth::user(); $allWishes = ''; $panel = array('center' => ['width' => '12']); if ($user) { $allWishes = Order::ofType('wishlist')->where('user_id', $user->id)->where('description', '<>', '')->orderBy('id', 'desc')->take(5)->get(); } $product = Product::select('id', 'category_id', 'user_id', 'name', 'description', 'price', 'stock', 'features', 'condition', 'rate_val', 'rate_count', 'low_stock', 'status', 'type', 'tags', 'products_group', 'brand')->with(['group' => function ($query) { $query->select('id', 'products_group', 'features'); }])->find($id); if ($product) { if ($user && $user->id == $product->user_id) { $panel = array('left' => ['width' => '2'], 'center' => ['width' => '10']); } $features = ProductDetail::all()->toArray(); //Increasing product counters. $this->setCounters($product, ['view_counts' => trans('globals.product_value_counters.view')], 'viewed'); //saving tags in users preferences if (trim($product->tags) != '') { UserController::setPreferences('product_viewed', explode(',', $product->tags)); } $details = OrderDetail::where('product_id', $product->id)->whereNotNull('rate_comment')->select('rate', 'rate_comment', 'updated_at')->orderBy('updated_at', 'desc')->take(5)->get(); $jsonDetails = json_encode($details->toArray()); //Si es un producto de tipo free, debemos buscar a que paquete freeproduct pertenece if ($product->type == 'freeproduct') { $order = OrderDetail::where('product_id', $product->id)->first(); $freeproduct = FreeProductOrder::where('order_id', $order->order_id)->first(); } $freeproductId = isset($freeproduct) ? $freeproduct->freeproduct_id : 0; Session::push('suggest-listed', $product->id); $suggestions = $this->getSuggestions(['preferences_key' => $product->id, 'limit' => 4]); Session::forget('suggest-listed'); if (count($product->group)) { $featuresHelper = new featuresHelper(); $product->group = $featuresHelper->group($product->group); } return view('products.show', compact('product', 'panel', 'allWishes', 'jsonDetails', 'freeproductId', 'features', 'suggestions')); } else { return redirect(route('products')); } }