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/Eurotools/euro-tools.nl/app/KommaApp/Shop/Cart/ShoppingCartController.php
<?php
namespace App\KommaApp\Shop\Cart;


use App\Http\Controllers\Controller;
use App\KommaApp\Pages\Models\Page;
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\RedirectResponse;
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;

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

    /**
     * Get the shopping cart index
     *
     * @return View
     */
    public function index() : View
    {

        $otherLanguageRoutes = (object)
        array(
            'metaTranslationRoutes' => array(
                '104' => (object) array(
                    "iso" => "nl",
                    "route" => 'nl/'. strtolower( __('shop/cart.cart', [], 'nl'))
                ),
                '50' => (object) array(
                    "iso" => "de",
                    "route" => 'de/'. strtolower( __('shop/cart.cart', [], 'de'))
                ),
                '40' => (object) array(
                    "iso" => "en",
                    "route" => 'en/'. strtolower( __('shop/cart.cart', [], 'en'))
                ),
            ),

            'languageMenuRoutes' => array(
                '104' => (object) array(
                    "iso" => "nl",
                    "route" => 'nl/'. strtolower( __('shop/cart.cart', [], 'nl'))
                ),
                '50' => (object) array(
                    "iso" => "de",
                    "route" => 'de/'. strtolower( __('shop/cart.cart', [], 'de'))
                ),
                '40' => (object) array(
                    "iso" => "en",
                    "route" => 'en/'. strtolower( __('shop/cart.cart', [], 'en'))
                )
            )
        );

        $items = $this->shoppingCartService->getItems();

        $subTotal = $this->shoppingCartService->getTotal(false);
        $taxes = $subTotal*0.21;
        $total = $subTotal+$taxes;

        $this->shoppingCartService->updateDiscounts();

        $itemCount = $this->shoppingCartService->getItemsCount();
        $subTotalWithDiscounts = $this->shoppingCartService->getTotal(true);
        $taxesWithDiscounts = $subTotalWithDiscounts*0.21;
        $totalWithDiscounts = $subTotalWithDiscounts+$taxesWithDiscounts;

        $user = \Auth::user();

        $view = \View::make('shop.pages.shoppingCart', [
            'user' => $user,
//            'page' => $page,
            'items' => $items,
            'subTotal' => $subTotal,
            'taxes' => $taxes,
            'total' => $total,
            'itemCount' => $itemCount,
            'links' => $this->links,
            'categories' => $this->categories,
            'subTotalWithDiscounts' => $subTotalWithDiscounts,
            'taxesWithDiscounts' => $taxesWithDiscounts,
            'totalWithDiscounts' => $totalWithDiscounts,
            'otherLanguages' => $otherLanguageRoutes,
        ]);

        return $view;
    }

    /**
     * Add a product to the cart
     *
     * @return String
     */
    public function addProductToShoppingCart(Request $request) : String
    {
        $productable = urldecode($request->itemType)::where('id', $request->productableID)->first();
        $this->shoppingCartService->addProductable($productable, $request->amount);

        return route(\App::getLanguage()->iso_2.'.shoppingcart');
    }

    /**
     * Add a product to the cart
     *
     * @param Request $request
     * @return View
     */
    public function removeItemFromShoppingCart(Request $request) : View
    {
        if($request->has('productableID')) {
            $productable = urldecode($request->itemType)::where('id', $request->productableID)->first();
            $this->shoppingCartService->deleteItem($productable);
        }

        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();
    }

    /**
     * count the items in the cart
     *
     * @return int
     */
    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);
        $user = \Auth::user();

        $view = \View::make('shop.partials.shoppingcart.content', [
            'user' => $user,
            'links' => $this->links,
            'categories' => $this->categories,
            'items' => $items,
            'subTotal' => $subTotal,
            'subTotalWithDiscounts' => $subTotalWithDiscounts,
        ]);

        return $view;
    }


}