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/farmfun/reserveren.farmfun.be/app/Komma/ShoppingCart/ShoppingCartApiController.php
<?php

namespace App\Komma\ShoppingCart;

use App\Komma\Availability\AvailabilityService;
use App\Komma\Availability\Types\Availability;
use App\Komma\Locations\Models\Location;
use App\Komma\Products\Models\Product;
use App\Komma\ShoppingCart\Interfaces\ShoppingCartInterface;
use App\Komma\ShoppingCart\Requests\ChangeDateRequest;
use App\Komma\ShoppingCart\Requests\FillCartRequest;
use App\Komma\ShoppingCart\Requests\UpdateQuantityOfItemRequest;
use Carbon\Carbon;

/**
 * Represents a shopping cart
 *
 * Class ShoppingCartService
 */
class ShoppingCartApiController
{
    public function fillCart(FillCartRequest $request)
    {
        /** @var AvailabilityService $availabilityService */
        $availabilityService = app(AvailabilityService::class);
        $availability = $availabilityService->makeAvailabilityFromRequest($request->all());

        /** @var ShoppingCartInterface $shoppingCart */
        $shoppingCart = app(ShoppingCartInterface::class);

        // Check if availability is still valid.
        if ($availabilityService->isAvailabilityValid($availability, true)) {
            $availability->setSelectedTimeSlot($request->get('timeSlot'));

            // Add Item to the shopping cart

            try {
                $shoppingCart->addItem($availability);
            } catch(\Exception $exception) {
                return response()->json(['messages' => $shoppingCart->errorMessages], 206);
            }

            return response()->noContent();
        }

        return response()->json([
            'message' => __('site/cart.errors.unknown_valid_error'),
        ], 206);
    }

    /**
     * Remove product from the cart
     *
     * @param Product $product
     * @return \Illuminate\Http\Response
     */
    public function removeItem(Product $product): \Illuminate\Http\Response
    {
        /** @var ShoppingCartInterface $shoppingCart */
        $shoppingCart = app(ShoppingCartInterface::class);

        $shoppingCart->deleteItem($product->id);

        return response()->noContent();
    }

    /**
     * Update the quantity of an item in the cart
     *
     * @param UpdateQuantityOfItemRequest $request
     * @param Product $product
     */
    public function updateItem(UpdateQuantityOfItemRequest $request, Product $product)
    {
        /** @var ShoppingCartInterface $shoppingCart */
        $shoppingCart = app(ShoppingCartInterface::class);

        $quantity = $request->get('amount');
        $timeSlot = $request->get('time');
        $notification = $request->get('notification');

        $shoppingCart->updateItem($product->id, $quantity, $timeSlot, $notification);
        $shoppingCart->updateTimeSlots();
    }

    /**
     * Get the cart.
     */
    public function getCart(): \Illuminate\Http\JsonResponse
    {
        /** @var ShoppingCartInterface $shoppingCart */
        $shoppingCart = app(ShoppingCartInterface::class);
        $shoppingCart->sortChronologically();

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

    /**
     * Change the date of the shopping cart
     *
     * @param ChangeDateRequest $request
     */
    public function changeMeta(ChangeDateRequest $request)
    {
        $date = Carbon::createFromFormat('d-m-Y', request()->post('date'));

        if (! $location = Location::whereId(request()->post('location_id'))->first()) {
            abort(417, 'Locatie niet gevonden');
        }
        $location->load('availability', 'boundProducts');

        if (! $location->availability->{'day_'.$date->dayOfWeek.'_open'}) {
            abort(417, 'Locatie niet open op '.__('calendar.day_names.'.$date->dayOfWeek));
        }

        $hasMissingItems = false;
        $newCart = new ShoppingCart(false);

        /** @var ShoppingCartInterface $shoppingCart */
        $shoppingCart = app(ShoppingCartInterface::class);
        $cartItems = $shoppingCart->getItems();

        /** @var AvailabilityService $availabilityService */
        $availabilityService = app(AvailabilityService::class);

        foreach ($cartItems as $cartItem) {
            // Look up if product exists in location
            if ($location->boundProducts->where('id', $cartItem->getProduct()->id)->isEmpty()) {
                // Else mark it and skip it
                $hasMissingItems = true;
                continue;
            }

            $availability = new Availability($cartItem->getProduct(), $location);
            $availability->setDate($date->format('d-m-Y'));
            $availability->setAmountOfPersons($cartItem->getAmountOfPersons());

            // Check if availability is still valid.
            if (! $availabilityService->isAvailabilityValid($availability, true)) {
                // Else mark it and skip it
                $hasMissingItems = true;
                continue;
            }

            $selectedTimeSlot = $cartItem->getSelectedTimeSlot();
            $availability->setSelectedTimeSlot($selectedTimeSlot->getTimeSlotValue());

            // Add Item to the shopping cart

            try {
                $newCart->addItem($availability);
            } catch(\Exception $exception) {
                $hasMissingItems = true;
            }
        }

        if ($hasMissingItems && $request->post('force_update') == false) {
            abort(412, 'Een of meerdere activiteiten zijn niet beschikbaar op de gewijzigde datum en/of locatie.');
        }

        if ($newCart->getItemsCount() === 0) {
            $newCart->clear();
        } else {
            $newCart->saveSession();
//            $newCart->updateSessionIfNeeded();
        }

        return response()->noContent();
    }
}