HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/douven.komma.pro/app/KommaApp/Shop/Cart/ShoppingCartController.php
<?php

namespace App\KommaApp\Shop\Cart;


use App\Http\Controllers\Controller;
use App\Http\Requests\ContactRequest;
use App\KommaApp\Forms\FormService;
use App\KommaApp\Pages\PageService;
use App\KommaApp\Shop\Discounts\DiscountServiceInterface;
use App\KommaApp\Shop\Products\Product\Product;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;

/**
 * Represents a shopping cart
 *
 * Class ShoppingCartService
 * @package App\KommaApp\Shop\Cart
 */
class ShoppingCartController extends Controller
{
    /**
     * @var ShoppingCartService $shoppingCartService
     */
    private $shoppingCartService;

    /**
     * @var DiscountServiceInterface $discountService
     */
    private $discountService;

    /**
     * @var FormService $formService
     */
    private $formService;

    public function __construct(PageService $pageService)
    {
        parent::__construct();
        $this->shoppingCartService = \App::make(ShoppingCartService::class);
        $this->discountService = \App::make(DiscountServiceInterface::class);
        $this->formService = \App::make(FormService::class);
        $this->formService->setOrigin('offerte');
    }

    /**
     * Get the shopping cart index
     *
     * @return View
     */
    public function index($send = null): View
    {
//        dd($this->shoppingCartService); //Use me to view the complete shopping cart when testing. Including discounts

        $items = $this->shoppingCartService->getItems();
        $subTotal = $this->shoppingCartService->getTotal(false);
        $itemCount = $this->shoppingCartService->getItemsCount();
        $subTotalWithDiscounts = $this->shoppingCartService->getTotal(true);

        $view = \View::make('shop.pages.shoppingCart', [
            'items' => $items,
            'subTotal' => $subTotal,
            'itemCount' => $itemCount,
            'links' => $this->links,
            'subTotalWithDiscounts' => $subTotalWithDiscounts,
            'categoryTree' => $this->categoryTree,
            'send' => $send
        ]);

        return $view;
    }

    public function process(ContactRequest $request): View
    {


        if($request->has('products')) {
            $order = '';
            foreach ($request->products as $product) {
                $order .= $product['amount'] . 'x ' . $product['name'] . '\n';
            }

            $order = nl2br($order);
            $request->request->add(['aanvraag' => $order]);
            $request->request->remove('products');
        }

        // Store request in the database
        $this->formService->storeRequest($request);

        // Send request to the site owner
        $this->formService->sendRequest($request, true);

        $this->shoppingCartService->clear();

        // Send to the success route
        return $this->index(true);
    }

    /**
     * Add a product to the cart
     *
     * @return View
     */
    public function addProductToShoppingCart(Request $request): View
    {
        $this->shoppingCartService->addProduct($request->itemID, $request->amount);

        return $this->cartView();
    }

    /**
     * Add a product to the cart404
     *
     * @param Request $request
     * @return View
     */
    public function removeItemFromShoppingCart(Request $request): View
    {
        if ($request->has('itemID')) {
            $this->shoppingCartService->deleteItem($request->itemID);
        }

        return $this->cartView();
    }

    /**
     * Add a product to the cart
     *
     * @param Request $request
     * @return View
     */
    public function setItemAmountInShoppingCart(Request $request): View
    {
        if ($request->has('itemID')) {
            $this->shoppingCartService->setItemAmount($request->itemID, $request->amount);
        }

        return $this->cartView();
    }

    public function getCartItemCount() : int
    {
        return $this->shoppingCartService->getItemsCount();
    }

    private function cartView(): View
    {
        $items = $this->shoppingCartService->getItems();
        $subTotal = $this->shoppingCartService->getTotal();
        $subTotalWithDiscounts = $this->shoppingCartService->getTotal(true);

        $view = \View::make('shop.partials.shoppingCart.content', [
            'items' => $items,
            'subTotal' => $subTotal,
            'subTotalWithDiscounts' => $subTotalWithDiscounts,
        ]);

        return $view;
    }


}