Ejemplo n.º 1
1
 public function checkOut(Request $request)
 {
     $address = \StringHelper::filterString($request->input('address'));
     $name = \StringHelper::filterString($request->input('name'));
     $content = \StringHelper::filterString($request->input('comments'));
     $phone = \StringHelper::filterString($request->input('phone'));
     $count = Cart::count();
     if ($phone != "" && $name != "" && $content != "" && $count > 0) {
         $order = new Order();
         $order->order_name = $name;
         $order->status = 1;
         $order->active = 1;
         $order->order_comment = $content;
         $order->order_address = $address;
         $order->order_phone = $phone;
         $order->save();
         $cart = Cart::content();
         foreach ($cart as $item) {
             $order_detail = new OrderDetail();
             $order_detail->dish_id = $item->id;
             $order_detail->dish_number = $item->qty;
             $order_detail->order_id = $order->id;
             $order_detail->save();
         }
         Cart::destroy();
         return Redirect::to(url('menu'))->with('message', 'Order Success !. You can continue buy now !');
     } else {
         return Redirect::to(url('checkout'))->with('message', 'Order Fail !. Something Wrong !');
     }
 }
Ejemplo n.º 2
0
 public function order()
 {
     //get Session option
     $option = Session::get('option');
     //get Cart
     $cart = Cart::content();
     //get type order
     $type = $option['type'];
     //payment method
     $payMethod = Session::get('pay_med');
     if ($option['type'] == 'logged') {
         $order = new Order();
         DB::beginTransaction();
         try {
             $idUser = array_get(session('user'), 'id');
             $order->user_id = $idUser;
             $order->delivery_detail_id = 0;
             $order->comment = $payMethod['comment'];
             $order->save();
             $idOrder = Order::where('user_id', $idUser)->orderBy('id', 'desc')->first()->id;
             //add order detail and sub amount of product
             foreach ($cart as $item) {
                 $root_price = Cd::find($item['id'])->root_price;
                 $modelOrderDetail = new OrderDetail();
                 $modelOrderDetail->order_id = $idOrder;
                 $modelOrderDetail->product_id = $item['id'];
                 $modelOrderDetail->quantity = $item['qty'];
                 $modelOrderDetail->root_price = $root_price;
                 $modelOrderDetail->price = $item->price;
                 $modelOrderDetail->save();
                 //sub qty product
                 $cd = Cd::find($item['id']);
                 if ($cd->quantity - $item['qty'] <= 0) {
                     throw new QtyCDException();
                 }
                 $cd->quantity = $cd->quantity - $item['qty'];
                 $cd->buy_time = $cd->buy_time + $item['qty'];
                 $cd->save();
             }
             DB::commit();
             $this->clear();
             return redirect_success('FrontendController@index', 'Ordered, Check your email to check detail');
         } catch (QtyCDException $e) {
             DB::rollback();
             return redirect_errors($e->notify());
         } catch (\Exception $e) {
             DB::rollback();
             return redirect_errors("Sorry cannot to this deal!");
         }
     } else {
         $deliveryDetail = Session::get('shi_add');
         $order = new Order();
         $modelDeliverDetail = new DeliveryDetail();
         $modelDeliverDetail = autoAssignDataToProperty($modelDeliverDetail, $deliveryDetail);
         //            dd($modelDeliverDetail);
         DB::beginTransaction();
         try {
             $orderHuman = $modelDeliverDetail;
             $modelDeliverDetail->save();
             $newDeliverDetail = new DeliveryDetail();
             $delivery = $newDeliverDetail->getDeliveryDetail($deliveryDetail);
             $order->user_id = 0;
             $order->delivery_detail_id = $delivery->id;
             $order->comment = $payMethod['comment'];
             $order->save();
             $idOrder = Order::where('delivery_detail_id', $delivery->id)->first()->id;
             //add order detail and sub amount of product
             foreach ($cart as $item) {
                 $modelOrderDetail = new OrderDetail();
                 $modelOrderDetail->order_id = $idOrder;
                 $modelOrderDetail->product_id = $item['id'];
                 $modelOrderDetail->quantity = $item['qty'];
                 $modelOrderDetail->price = $item['price'];
                 $modelOrderDetail->save();
                 //
                 $cd = Cd::find($item['id']);
                 if ($cd->quantity - $item['qty'] <= 0) {
                     throw new QtyCDException();
                 } else {
                     $cd->quantity = $cd->quantity - $item['qty'];
                 }
             }
             DB::commit();
             $this->clear();
             //send
             //                Mail::send('auth.mail_welcome', ['last_name' => $orderHuman->lastname, 'key' => $key_active, 'password' => $pass], function ($message) use ($data) {
             //                    $message
             //                        ->to($data['email'], $data['name'])
             //                        ->from('*****@*****.**')
             //                        ->subject('Thank you for your buying!');
             //                });
             return redirect_success('FrontendController@index', 'Ordered, Check your email to check detail');
         } catch (QtyCDException $e) {
             DB::rollback();
             return redirect_errors($e->notify());
         } catch (\Exception $e) {
             DB::rollback();
             return redirect_errors("Sorry cannot to this deal!");
         }
     }
 }
Ejemplo n.º 3
0
 /**
  * Removes the selected item from the cart, and stores it back in the Later Cart
  *
  * @param  int  $origin type of the origin order ('cart','later',etc)
  * @param  int  $destination type of the destination order ('cart','later',etc)
  * @param  int  $productId of the product
  * @return Redirects back to de cart
  */
 public function moveFromOrder($origin, $destination, $productId)
 {
     /**
      * validating if the product requested is valid.
      * if it fails, there will be an 404 exception threw
      */
     try {
         $product = Product::findOrFail($productId);
     } catch (ModelNotFoundException $e) {
         throw new NotFoundHttpException();
     }
     $user = \Auth::user();
     /**
      * $originType allows tracking the type of origin, if it is coming from a specific wish list
      * @var string
      */
     $originType = '';
     //if it came from a specific wish list
     if ($origin != 'later' && $origin != 'cart' && $origin != 'wishlist' && is_string($origin)) {
         //getting the list type
         $originType = Order::select(['id', 'type'])->where('description', 'LIKE', $origin)->first();
         //getting the list information
         $basicCart = Order::ofType($originType->type)->where('user_id', $user->id)->where('id', $originType->id)->first();
     } else {
         //getting the list information
         $basicCart = Order::ofType($origin)->where('user_id', $user->id)->first();
     }
     //getting information of the destination order
     $destinationOrder = Order::ofType($destination)->where('user_id', $user->id)->first();
     //if there is not destination, it is created
     if (!$destinationOrder) {
         $destinationOrder = new Order();
         $destinationOrder->user_id = $user->id;
         $destinationOrder->type = $destination;
         $destinationOrder->status = 'open';
         $destinationOrder->save();
         $log = Log::create(['action_type_id' => '1', 'details' => $destinationOrder->id, 'source_id' => $destinationOrder->id, 'user_id' => $user->id]);
     }
     //checking if the user already has a product in the origin order, if so, it can be read to update the destination order.
     $originDetail = OrderDetail::where('order_id', $basicCart->id)->where('product_id', $product->id)->first();
     if ($originDetail) {
         $oldQuantity = $originDetail->quantity;
         $originDetail->delete();
     } else {
         $oldQuantity = 1;
     }
     //checking if the product exist in the destination, if so, it can be updated
     $orderMoved = OrderDetail::where('order_id', $destinationOrder->id)->where('product_id', $product->id)->first();
     //creating the new orden
     if ($orderMoved) {
         $orderMoved->price = $product->price;
         $orderMoved->quantity = $orderMoved->quantity + $oldQuantity;
     } else {
         $orderMoved = new OrderDetail();
         $orderMoved->order_id = $destinationOrder->id;
         $orderMoved->product_id = $product->id;
         $orderMoved->price = $product->price;
         $orderMoved->quantity = $oldQuantity;
         $orderMoved->status = 1;
     }
     //save new order
     $orderMoved->save();
     if ($product->type != 'item') {
         $virtual = VirtualProduct::where('product_id', $product->id)->first();
         //updating the virtual product order
         VirtualProductOrder::where('virtual_product_id', $virtual->id)->where('order_id', $basicCart->id)->update(['order_id' => $destinationOrder->id]);
     }
     if ($destination == 'later') {
         Session::push('message', trans('store.productSavedForLater'));
     } elseif ($destination == 'cart') {
         Session::push('message', trans('store.productAdded'));
     }
     return redirect()->route('orders.show_cart');
 }
 /**
  * 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.');
     }
 }
Ejemplo n.º 5
0
 public function postCheckOut(CheckOutRequest $request)
 {
     try {
         if (Auth::check()) {
             $customer = Customer::find(Auth::user()->userable_id);
             if ($customer->name != $request->name) {
                 $customer = new Customer();
                 $customer->name = $request->name;
                 $customer->address = $request->address;
                 $customer->phone = $request->phone;
                 $customer->save();
             }
         } else {
             $customer = Customer::where('phone', '=', $request->phone)->first();
             if ($customer->count() == 0) {
                 $customer = new Customer();
                 $customer->name = $request->name;
                 $customer->address = $request->address;
                 $customer->phone = $request->phone;
                 $customer->save();
             }
         }
         $_token = csrf_token();
         $carts = Cart::where(function ($query) use($_token) {
             $query->where('user_id', '=', Auth::check() ? Auth::user()->id : 0)->orWhere('remember_token', '=', $_token);
         });
         if ($carts->count() > 0) {
             $order = new Order();
             $order->customer_id = $customer->id;
             $order->note = $request->note;
             $order->ship_time = $request->ship_time;
             $order->save();
             foreach ($carts->get() as $cart) {
                 $order_detail = new OrderDetail();
                 $order_detail->order_id = $order->id;
                 $order_detail->book_id = $cart->book_id;
                 $order_detail->quantity = $cart->quantity;
                 $order_detail->save();
             }
             $carts->delete();
             return redirect('/')->with('message', 'Đặt hàng thành công. Chúng tôi sẽ sớm liên lạc với bạn!')->with('alert-class', 'alert-success')->with('fa-class', 'fa-check');
         } else {
             return redirect('/checkout')->with('message', 'Không có hàng trong giỏ.')->with('alert-class', 'alert-warning')->with('fa-class', 'fa-warning');
         }
     } catch (ValidationException $e) {
         return redirect('/checkout')->with('message', 'Xảy ra lỗi khi lưu đơn hàng, vui lòng thử lại!!!')->with('alert-class', 'alert-danger')->with('fa-class', 'fa-ban');
     }
 }