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/blijegasten/blijegasten.be/app/Komma/Shop/Cart/ShoppingCartController.php
<?php
namespace App\Komma\Shop\Cart;


use App\Komma\Base\Controller;
use App\Komma\Globalization\RegionInfo;
use App\Komma\Globalization\RegionInfoInterface;
use App\Komma\Pages\PageService;
use App\Komma\Shop\Cart\Requests\AddProductableRequest;
use App\Komma\Shop\Cart\Requests\RemoveProductableRequest;
use App\Komma\Shop\Cart\Requests\UpdateProductableQuantityRequest;
use App\Komma\Shop\Discounts\DiscountServiceInterface;
use Illuminate\Contracts\View\View;

/**
 * Represents a shopping cart
 *
 * Class ShoppingCartService
 * @package App\Komma\Shop\Cart
 */
class ShoppingCartController extends Controller
{

    /**
     * Get the shopping cart
     *
     * @return View
     */
    public function show() : View
    {
        /** @var ShoppingCartServiceInterface $shoppingCart */
        $shoppingCart = app(ShoppingCartServiceInterface::class);
        $regionInfo = app(RegionInfoInterface::class);

        $languageMenu = [];
        foreach ($this->site->languages as $language)
        {
            $languageMenu[$language->iso_2] = '/' . $language->iso_2.'/'.__('site/routes.shoppingcart', [], $language->iso_2);
        }

//        $neutralCulturesByIso3 = RegionInfo::getNeutralCultures()->map(function(RegionInfo $regionInfo) {
//            return [$regionInfo->getThreeLetterISORegionName() => $regionInfo->getNativeName() !== '' ? $regionInfo->getNativeName() : $regionInfo->getDisplayName()];
//        })->collapse()->sort();

        $neutralCulturesHotList = RegionInfo::getNeutralCultures()->filter(function(RegionInfo $regionInfo) {
            return in_array($regionInfo->getThreeLetterISORegionName(), config('shop.country_hotlist', []), true);
        })->map(function(RegionInfo $regionInfo) {
            return [$regionInfo->getName() => $regionInfo->getNativeName() !== '' ? $regionInfo->getNativeName() : $regionInfo->getDisplayName()];
        })->collapse()->sort();


        return  view('site.templates.shoppingCart', [
            'shoppingCart' => $shoppingCart,
            'links' => $this->links,
            'regionInfo' => $regionInfo,
            'languageMenu' => $languageMenu,
//            'neutralCulturesByIso3' => $neutralCulturesByIso3,
            'neutralCulturesHotlistByIso3' => $neutralCulturesHotList,
        ]);
    }

    /**
     * Add a product to the cart
     *
     * @param AddProductableRequest $request
     * @throws \Exception
     * @return string
     */
    public function addProductToShoppingCart(AddProductableRequest $request)
    {
        /** @var ShoppingCartServiceInterface $shoppingCartService */
        $shoppingCartService = app(ShoppingCartServiceInterface::class);

        $type               = urldecode($request->get('itemType'));
        $productableId      = $request->get('productableId');
        $quantity             = $request->get('quantity');

        $productable = $type::where('id', $productableId)->with('translations', 'categories')->first();
        $shoppingCartService->addProductable($productable, $quantity);

        return localized_route('shoppingcart');
    }

    /**
     * Remove a product to the cart
     *
     * @param RemoveProductableRequest $request
     * @return string
     */
    public function removeItemFromShoppingCart(RemoveProductableRequest $request)
    {
        $shoppingCartService = app(ShoppingCartServiceInterface::class);

//        $type               = urldecode($request->get('itemType'));
//        $productableId      = $request->get('productableId');
//
//        if($request->has('productableId')) {
//            /** @var ShoppingCartServiceInterface $shoppingCartService */
//            $productable = $type::where('id', $productableId)->first();
//            $shoppingCartService->deleteItem($productable);
//        }

        if($request->has('itemId')) {
            /** @var ShoppingCartServiceInterface $shoppingCartService */
            $shoppingCartService = app(ShoppingCartServiceInterface::class);
            $shoppingCartService->deleteItem($request->get('itemId'));
        }


        return route('shoppingcart');
    }

    /**
     * Add a product to the cart
     *
     * @param UpdateProductableQuantityRequest $request
     * @return string
     */
    public function setItemQuantityInShoppingcart(UpdateProductableQuantityRequest $request)
    {
        if($request->has('itemId')) {
            /** @var ShoppingCartServiceInterface $shoppingCartService */
            $shoppingCartService = app(ShoppingCartServiceInterface::class);
            $shoppingCartService->setItemQuantity($request->get('itemId'), $request->get('quantity'));
        }

        return route('shoppingcart');
    }

    public function getShoppingCartInformation() {

        $shoppingCart = app(ShoppingCartServiceInterface::class);
        $regionInfo = app(RegionInfoInterface::class);

        // Response data
        $response = [
            'raw' => [
                'productTotal' => $shoppingCart->getProductTotal(),
                'shippingCost' => $shoppingCart->getShippingCost(),
                'cartTotal' => $shoppingCart->getTotal(),
                'vat' => $shoppingCart->getVat(),
                'deposit' => $shoppingCart->getDeposit(),
                'totalIncludingDeposit' => $shoppingCart->getTotalIncludingDeposit(),
            ],
            'formated' => [],
            'items' => [],
            'itemCount' => $shoppingCart->getItemsCount(),
            'useShipping' => $shoppingCart->getShippingCost() === 0 ? false : true,
        ];

        debug($shoppingCart->getShippingCost());

        // If shipping cost are null then we don't know it
        if($shoppingCart->getShippingCost() === null) {
            $response['shipping_undefined'] = __('shop/cart.shipping_undefined');
        }

        // Add the shopping cart items to response (unformatted and formatted)
        foreach($shoppingCart->getItems() as $cartItem) {

            $productJson = [
                'id' => $cartItem->getId(),
                'raw' => $cartItem->getTotal(),
                'formatted' => $regionInfo->getNumberFormat()->centsToCurrency($cartItem->getTotal(), true, true),
                'quantity' => $cartItem->getQuantity(),
            ];

            $response['items'][] = $productJson;
        }


        // Loop through the unformatted info, and create and append a formatted version to the cart
        foreach ($response['raw'] as $infoKey => $infoLine)
        {
            if(!empty($infoLine)){
                $response['formated'][$infoKey] = $regionInfo->getNumberFormat()->centsToCurrency($infoLine, true, true);
            }
        }

        // If shipping is not used we change the formatted to free
        if(!$response['useShipping']) $response['formated']['shippingCost'] = __('shop/cart.free');

        return response()->json($response);
    }
}