/**
  * 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 = ['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'));
     }
 }
 public function run()
 {
     $faker = Faker::create();
     $users = UserAddress::get();
     $status_list = array_keys(trans('globals.order_status'));
     for ($i = 0; $i < 40; $i++) {
         $user = $users->random(1);
         $products = Product::where('id', '!=', $user->user_id)->where('type', '!=', 'freeproduct')->get();
         $type = 'freeproduct';
         $status = 'paid';
         $order = Order::create(['user_id' => $user->user_id, 'seller_id' => '3', 'address_id' => $user->address_id, 'status' => 'paid', 'type' => $type, 'description' => '', 'end_date' => null]);
         //Productos a insertar a la orden. Se haran copias de los productos que esten en las ordenes y se le asignara el tipo freeproduct
         $num = $faker->numberBetween(2, 5);
         $list = [];
         if ($num > 1 && count($products) - 1 < $num) {
             $num = count($products) - 1;
         }
         for ($j = 0; $j < $num; $j++) {
             do {
                 $a = true;
                 $product = $products->random(1);
                 $quantity = $faker->numberBetween(1, 5);
                 if (in_array($product->id, $list)) {
                     $a = false;
                 } else {
                     //duplico el producto
                     $productactual = $product->toArray();
                     unset($productactual['id']);
                     unset($productactual['num_of_reviews']);
                     $productactual['user_id'] = '3';
                     //$user->user_id;
                     $productactual['type'] = 'freeproduct';
                     $productactual['parent_id'] = $product->id;
                     $newproduct = Product::create($productactual);
                     $list[] = $product->id;
                 }
             } while ($a == false);
             //el nuevo producto lo asocio al detalle de la orden
             OrderDetail::create(['order_id' => $order->id, 'product_id' => $newproduct->id, 'price' => $product->price, 'quantity' => $quantity, 'delivery_date' => null]);
             //Actualizo el stock
             $product->stock = $product->stock - $quantity;
             $product->save();
         }
         $order->sendNotice();
     }
     $orders = Order::where('type', '=', 'freeproduct')->get();
     $users = User::where('type', 'trusted')->get();
     $list_orders = [];
     for ($i = 0; $i < 15; $i++) {
         //Se crea el free product y se asocia las ordenes
         $user = $users->random(1);
         $end_date = \Carbon\Carbon::now()->addDays($faker->numberBetween(5, 30));
         $freeproduct = FreeProduct::create(['user_id' => '3', 'description' => $faker->unique()->catchPhrase, 'start_date' => \Carbon\Carbon::now(), 'end_date' => $end_date, 'participation_cost' => $faker->numberBetween(10000, 50000), 'min_participants' => $min_participants = $faker->numberBetween(1, 15), 'max_participants' => $faker->numberBetween($min_participants, $min_participants * 2), 'max_participations_per_user' => $faker->numberBetween(1, 5), 'draw_number' => $faker->numberBetween(1, 3), 'draw_date' => $end_date->addDays($faker->numberBetween(1, 5)), 'status' => $faker->randomElement([0, 1])]);
         //asocio una o mas ordenes a un free product
         $num = $faker->numberBetween(1, 3);
         if ($num > 1 && count($orders) - 1 < $num) {
             $num = count($orders) - 1;
         }
         for ($j = 0; $j < $num; $j++) {
             do {
                 $a = true;
                 $order = $orders->random(1);
                 if (in_array($order->id, $list_orders)) {
                     $a = false;
                 } else {
                     $list_orders[] = $order->id;
                 }
             } while ($a == false);
             FreeProductOrder::create(['order_id' => $order->id, 'freeproduct_id' => $freeproduct->id]);
         }
     }
 }
Beispiel #4
0
 /**
  * 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'));
     }
 }