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

checkOutResume() public method

Starts the checkout process.
public checkOutResume ( integer $addressId ) : Response
$addressId integer The address id selected to be copied
return Response
    public function checkOutResume($addressId)
    {
        $user = \Auth::user();
        $cart = Order::ofType('cart')->with('details')->where('user_id', $user->id)->first();
        $cartDetail = OrderDetail::where('order_id', $cart->id)->get();
        $address = Address::find($addressId);
        $totalAmount = 0;
        $totalItems = 0;
        //Checks if the user selected an address that belongs to him/her
        $userAddress = Address::where('user_id', $user->id)->where('id', $address->id)->first();
        if ($userAddress) {
            //Checks if the user has points for the cart price and the store has stock
            $total_points = 0;
            foreach ($cartDetail as $orderDetail) {
                $product = Product::find($orderDetail->product_id);
                $totalItems += $orderDetail->quantity;
                $totalAmount += $orderDetail->quantity * $orderDetail->price;
                if ($product->stock < $orderDetail->quantity) {
                    return redirect()->route('orders.show_cart')->withErrors(['main_error' => [trans('store.insufficientStock')]]);
                }
            }
            //Checks if the user has points for the cart price
            if ($user->current_points < $total_points && config('app.payment_method') == 'Points') {
                return redirect()->route('orders.show_cart')->withErrors(['main_error' => [trans('store.cart_view.insufficient_funds')]]);
            } else {
                //Copies the Address to a new one and attaches it to the order or replaces the old one
                $cartAddress = Address::find($cart->address_id);
                if (!$cartAddress) {
                    //if the order does not has an address yet
                    $newAddress = new Address();
                    $newAddress->line1 = $address->line1;
                    $newAddress->line2 = $address->line2;
                    $newAddress->phone = $address->phone;
                    $newAddress->name_contact = $address->name_contact;
                    $newAddress->zipcode = $address->zipcode;
                    $newAddress->city = $address->city;
                    $newAddress->country = $address->country;
                    $newAddress->state = $address->state;
                    $newAddress->save();
                    $cart->address_id = $newAddress->id;
                    $cart->save();
                    $cartAddress = $newAddress;
                } else {
                    //if the order needs to be updated
                    $cartAddress->line1 = $address->line1;
                    $cartAddress->line2 = $address->line2;
                    $cartAddress->phone = $address->phone;
                    $cartAddress->name_contact = $address->name_contact;
                    $cartAddress->zipcode = $address->zipcode;
                    $cartAddress->city = $address->city;
                    $cartAddress->country = $address->country;
                    $cartAddress->state = $address->state;
                    $cartAddress->save();
                }
                $panel = ['center' => ['width' => '12']];
                //Sets the resume option to use the same view
                $isResume = true;
                $is_logged = true;
                return view('orders.cart', compact('cart', 'user', 'panel', 'isResume', 'cartAddress', 'totalItems', 'totalAmount'));
            }
        } else {
            return redirect()->route('orders.show_cart')->withErrors(['main_error' => [trans('store.errorOnAddress')]]);
        }
    }