Exemplo n.º 1
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');
     }
 }
Exemplo n.º 2
0
 /**
  * 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'));
     }
 }
Exemplo n.º 3
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'));
     }
 }