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')); }
/** * build the free prodcuts tree where parent will be a free product and children are the products list associated. * * @param [array] $select [array to ask for products table field] * @param [integer] $limit [to control the request limit] */ public static function getParentAndChildren($select = ['id', 'description'], $limit = 5) { $events = FreeProduct::select($select)->where('status', '1')->with('orders')->get()->take($limit); $list = []; $events->each(function ($event) use(&$list, $select) { foreach ($select as $value) { $list[$event->id][$value] = $event->{$value}; } $products = FreeProduct::find($event->id)->products->take(6)->toArray(); if ($products) { $list[$event->id]['products'] = $products; } unset($products); }); return $list; }
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]); } } }
public static function getNextEvents($fields = ['*'], $limit = 5, $date) { $events = FreeProduct::select($fields)->OfStatus('1')->IsValidIn($date)->with('orders')->orderBy('draw_date')->take($limit)->get(); return FreeProduct::getWithProducts($events); }
/** * Execute the console command. * * @return mixed */ public function fire() { try { //Find all freeproducts that can be processed; that is, they are in the correct date range and are active. $this->info('----- STARTING THE PROCESS FOR SELECTION OF WINNERS -----'); $dateactual = date('Y-m-d'); $freeproducts = FreeProduct::where('status', 1)->where('draw_date', $dateactual)->get(); if ($freeproducts) { $this->info('Free Products to be processed: ' . $freeproducts->count()); foreach ($freeproducts as $freeproduct) { //Check the total participants. Depending on this the freeproduct be processed. Remember that there is a minimum of participation to select the winners. Still it not defined to do if the minimum is not met. $participants = FreeProductParticipant::where('freeproduct_id', $freeproduct->id)->where('status', 'registered')->get(); if ($freeproduct->min_participants <= $participants->count()) { //Select the winners, as defined in the product free draw_number field. $list_winners = []; for ($i = 0; $i < $freeproduct->draw_number; $i++) { $user_winner = $participants->random(1); $list_winners[] = $user_winner->user_id; $user_winner->status = 'winner'; $user_winner->save(); } $this->info('Total winners -> ' . count($list_winners)); //We mail to notify the winners and create an order for you to communicate with the seller of the product. The first is to list all the products contained in the orders associated with that freeproduct //Collection Orders with Products in details $orders = FreeProduct::find($freeproduct->id)->orders()->with('products')->get(); //Collection Products $list_products_orders = Collection::make(); foreach ($orders as $order) { $list_products_orders = $list_products_orders->merge($order->products); } $this->info('Total Products to prize: ' . count($list_products_orders)); $list_awards = []; foreach ($list_winners as $user_id) { $winner = User::find($user_id); $this->info("Processing user -> ID={$winner->id} "); //In this part of the process, we should when creating the freeproduct, indicate how the prizes will be distributed, something like for position, and indicate that many products will be delivered by position. For now, a product be taken at random. do { $product_award = $list_products_orders->random(1); $in_product_award_list = true; if (in_array($product_award->id, $list_awards)) { $in_product_award_list = false; } else { $list_awards[] = $product_award->id; $this->info("Product selected-> ID={$product_award->id} "); } } while (!$in_product_award_list); //Creating the order with the product won user $winner_address = UserAddress::where('user_id', $winner->id)->orderBy('default', 'DESC')->first(); $newOrder = new Order(); $newOrder->user_id = $winner->id; $newOrder->address_id = $winner_address->id; $newOrder->status = 'pending'; $newOrder->type = 'freeproduct'; $newOrder->seller_id = $freeproduct->user_id; $newOrder->save(); $newOrder->sendNotice(); $this->info("Creating order: Result -> ID {$newOrder->id}"); //Order detail. Just take the product won $newOrderDetail = new OrderDetail(); $newOrderDetail->order_id = $newOrder->id; $newOrderDetail->product_id = $product_award->id; $newOrderDetail->price = $freeproduct->participation_cost; $newOrderDetail->quantity = 1; $newOrderDetail->status = 1; $newOrderDetail->save(); //Off product will deliver a prize $product_award->status = 0; $product_award->save(); //Notify the user that was selected as winner of that freeproduct $data = ['product' => $product_award]; Mail::queue('emails.freeproducts.winner', $data, function ($message) use($winner) { $message->to($winner->email)->subject(trans('email.free_products_winner.subject')); }); $this->info('email sent notice that won'); //He also sent an email indicating that a new order was created.(tracking) $data = ['orderId' => $newOrder->id]; Mail::queue('emails.neworder', $data, function ($message) use($winner) { $message->to($winner->email)->subject(trans('email.new_order_for_user.subject')); }); $this->info('email I sent notice that an order for the product won'); } //Freeproduct inactive, so they do not take into account again for next draw $freeproduct->status = 0; $freeproduct->save(); //Se le notifica al dueno del freeproduct que se seleccionaron a los ganadores $this->info("FreeProduct -> ID={$freeproduct->id} PROCESSED"); } else { $this->info("FreeProduct -> ID={$freeproduct->id} The minimum participation condition for the free product does not comply."); } } } $this->info('----- FINISHED THE PROCESS FOR SELECTION OF WINNERS -----'); } catch (ModelNotFoundException $e) { Log::error($e); $this->error('They received errors when running the process. View Log File.'); } }