Пример #1
0
 public function addressStore(Request $request)
 {
     $keys = 'realname,province,city,counties,address,national_code,postal_code,phone';
     $data = $this->autoValidate($request, 'member.address', $keys);
     $address = UserAddress::create($data + ['uid' => $this->user->getKey(), 'used_at' => Carbon::now()]);
     return $this->success('member.success_address', FALSE, $address->toArray());
 }
Пример #2
0
 public function deliver($id)
 {
     $order = Order::with(['details', 'order_express'])->find($id);
     $this->_data = $order;
     if ($order->order_express->uaid) {
         $this->_user_address = UserAddress::find($order->order_express->uaid)->getFullAddressAttribute();
     } else {
         $this->_user_store = Store::find($order->order_express->sid);
     }
     if ($order->order_express->uaid) {
         $keys = 'express_name,no';
         $this->_validates = $this->getScriptValidate('order.deliver', $keys);
     }
     return $this->view('admin.order.deliver');
 }
Пример #3
0
 public function edit($id)
 {
     $order = Order::with(['order_express'])->where('fid', $this->factory->getKey())->find($id);
     if (empty($order)) {
         return $this->failure_noexists();
     }
     if ($order->order_express->express_type == 39) {
         $this->_user_address = UserAddress::find($order->order_express->uaid)->getFullAddressAttribute();
         $keys = 'express_name,no';
         $this->_validates = $this->getScriptValidate('order.deliver', $keys);
     } else {
         $this->_user_stores = User::find($order->uid)->stores()->get();
         $keys = 'sid';
         $this->_validates = $this->getScriptValidate('order.express_store', $keys);
     }
     $this->_data = $order;
     return $this->view('factory-backend.express.edit');
 }
 public function updateAddress()
 {
     if (User::where('id', '=', Input::get('id'))->exists()) {
         $userData['user_id'] = Input::get('id');
         $userData['fullname'] = Input::get('fullName');
         $userData['address'] = Input::get('address');
         $userData['city'] = Input::get('city');
         $userData['state'] = Input::get('state');
         $userData['pincode'] = Input::get('pincode');
         $userData['country'] = Input::get('country');
         $createAddress = UserAddress::create($userData);
         if (Input::get('defaults')) {
             $defaultAddress['default_address'] = $createAddress->id;
             User::where('id', Input::get('id'))->update($defaultAddress);
         }
         $Response = array('success' => '1');
     } else {
         $Response = array('success' => '0', 'err' => $validation->messages());
     }
     return $Response;
 }
Пример #5
0
 public function edit($id)
 {
     $order = Order::with(['details', 'order_express'])->join('order_expresses', 'order_expresses.id', '=', 'orders.id', 'INNER')->whereIn('order_expresses.sid', $this->stores_ids)->find($id);
     if (empty($order)) {
         return $this->failure_noexists();
     }
     if ($order->order_express->express_type == 39) {
         $this->_express_address = UserAddress::find($order->order_express->uaid)->getFullAddressAttribute();
     } else {
         $user_stores = Store::with('user')->find($order->order_express->sid);
         $this->_express_address = $user_stores->name . '-店主:' . $user_stores->user->realname . '-电话:' . $user_stores->phone . '-地址:' . $user_stores->address ?: '无';
     }
     $keys = 'expresses_money';
     $this->_validates = $this->getScriptValidate('order.express', $keys);
     $this->_data = $order;
     return $this->view('agent-backend.order.edit');
 }
 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]);
         }
     }
 }
 /**
  * Subscription free product, validating the conditions and validity of the same
  * @param  int  $id
  * @return Response
  */
 public function suscribe($id)
 {
     $freeproduct = FreeProduct::find($id);
     $user = \Auth::user();
     if ($freeproduct) {
         //It validates that the user has a registered address in the store. Of winning, this address will be used to create you an order (will contain the product to win)
         $useraddress = UserAddress::where('user_id', $user->id)->orderBy('default', 'DESC')->first();
         if ($useraddress) {
             //It verifies that the product is in effect free
             $dateactual = date('Y-m-d');
             if ($freeproduct->start_date >= $dateactual && $freeproduct->end_date >= $dateactual) {
                 //It has enough points to participate
                 if ($user->current_points > $freeproduct->participation_cost) {
                     //Total of Participants
                     $totalparticipants = FreeProductParticipant::where('freeproduct_id', $id)->count();
                     //Total equity investments in the product user free
                     $userholdings = FreeProductParticipant::MyParticipations($id)->count();
                     //Validate that there is still quota to participate
                     if ($freeproduct->max_participants > $totalparticipants) {
                         //Then, the user is not participating or not exceeding the number of shares in the free product
                         if ($freeproduct->max_participations_per_user > $userholdings) {
                             //Subtract points because of user participation
                             $negativeTotal = -1 * $freeproduct->participation_cost;
                             //12 is the action type id for free product checkout
                             $pointsModified = $user->modifyPoints($negativeTotal, 12, $id);
                             if ($pointsModified) {
                                 //If you meet these conditions, the user is registered as a participant free product
                                 $newparticipant = new FreeProductParticipant();
                                 $newparticipant->freeproduct_id = $id;
                                 $newparticipant->user_id = $user->id;
                                 $newparticipant->status = 'registered';
                                 $newparticipant->save();
                                 //Report by email to the participant
                                 $data = ['freeproduct_id' => $id];
                                 Mail::queue('emails.freeproducts.participate', $data, function ($message) use($user) {
                                     $message->to($user->email)->subject(trans('email.free_products_participation.subject'));
                                 });
                                 //It is sent to the view (dashboard) where the user can view their participation in it.
                                 Session::flash('message', trans('freeproduct.congratulations_participate'));
                                 return redirect()->route('freeproducts.show', [$freeproduct->id]);
                             } else {
                             }
                         } else {
                             Session::flash('message', trans('freeproduct.max_participations_for_user'));
                         }
                     } else {
                         Session::flash('message', trans('freeproduct.participations_not_available'));
                     }
                 } else {
                     Session::flash('message', trans('freeproduct.not_enough_point'));
                 }
             } else {
                 Session::flash('message', trans('freeproduct.freeproduct_not_available'));
             }
         } else {
             Session::flash('message', trans('freeproduct.address_not_registered'));
         }
         return redirect()->route('freeproducts.show', [$id]);
     } else {
         Session::flash('message', trans('freeproduct.freeproduct_not_exist'));
         return redirect(route('products'));
     }
 }
Пример #8
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');
     }
 }
 /**
  * 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.');
     }
 }
 public function setUserAddress()
 {
     if (User::where('id', '=', Input::get('id'))->exists()) {
         if (UserAddress::where('id', '=', Input::get('default_address'))->where('user_id', '=', Input::get('id'))->exists()) {
             $updateData['default_address'] = Input::get('default_address');
             User::where('id', '=', Input::get('id'))->update($updateData);
             $Response = array('success' => '1');
         } else {
             $Response = array('success' => '2', 'error' => 'User Address Not Matched');
         }
     } else {
         $Response = array('success' => '0', 'error' => 'User Not Found');
     }
     return $Response;
 }