App\Http\Controllers\OrdersController::moveFromOrder PHP Method

moveFromOrder() public method

Removes the selected item from the cart, and stores it back in the Later Cart.
public moveFromOrder ( integer $origin, integer $destination, integer $productId ) : Redirects
$origin integer type of the origin order ('cart','later',etc)
$destination integer type of the destination order ('cart','later',etc)
$productId integer 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');
    }