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/ShoppingCartController.php
<?php

namespace App\Komma\ShoppingCart;

use App\Http\Requests\MailProgramRequest;
use App\Komma\Availability\AvailabilityService;
use App\Komma\Base\Controller;
use App\Komma\Locations\LocationService;
use App\Komma\Reservations\Models\RequestedProgram;
use App\Komma\ShoppingCart\Interfaces\ShoppingCartInterface;
use App\Komma\Teamleader\TeamleaderService;
use App\Mail\SendProgramAdminMail;
use App\Mail\SendProgramMail;
use Carbon\Carbon;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Mail;

/**
 * Represents a shopping cart
 *
 * Class ShoppingCartService
 */
class ShoppingCartController extends Controller
{
    /** @var AvailabilityService */
    private $availabilityService;

    public function __construct()
    {
        parent::__construct();
        $this->availabilityService = app()->make(AvailabilityService::class);
    }

    public function show()
    {
        /** @var ShoppingCartInterface $shoppingCart */
        $shoppingCart = app()->make(ShoppingCartInterface::class);
        $addons = null;

        // Fill the addons if the date and location has been defined
        if ($shoppingCart->getDate() !== null && $shoppingCart->getLocation() !== null) {
//            $shoppingCart->updateSessionIfNeeded();

            /**
             * Note: because of the lack of time we dont take the date into account...
             */
            $addons = $this->availabilityService->getAmountOfFoodAvailabilities(20, [], $shoppingCart->getLocation());
        }

        $shoppingCart->updateTimeSlots();
        $shoppingCart->sortChronologically();

        if ($shoppingCart->getItemsCount() > 0) {
            /** @var LocationService $locationService */
            $locationService = app()->make(LocationService::class);
            $locationOptions = $locationService->getLocations()
                ->map(function ($location) use ($shoppingCart) {
                    return (object) [
                        'id' => $location->id,
                        'label' => $location->translation->name,
                        'open' => $location->availability->{'day_'.$shoppingCart->getDate()->dayOfWeek.'_open'},
                    ];
                });
        } else {
            $locationOptions = [];
        }

        return view('site.templates.shoppingCart', [
            'links'        => $this->links,
            'shoppingCart' => $shoppingCart,
            'addons' => $addons,
            'locationOptions' => $locationOptions,
        ]);
    }

    public function generateCart()
    {
        if (empty(request()->all())) {
            return;
        }

        $location = request()->get('location');
        $date = Carbon::createFromFormat('d-m-Y', request()->get('date'));
        $products = request()->get('products');

        $shoppingCart = app()->make(ShoppingCartInterface::class);

        $shoppingCart->deleteAll();

        $decodedProducts = collect(json_decode($products));

        foreach ($decodedProducts as $product) {
            $availability = $this->availabilityService->makeAvailabilityFromGetRequest($product, (int) $location, $date);

            // Check if availability is still valid.
            if ($this->availabilityService->isAvailabilityValid($availability)) {
                // Add Item to the shopping cart
                $shoppingCart->addGeneratedItem($availability, false);
            }
        }

        $shoppingCart->updateTimeSlots();

        foreach ($shoppingCart->getItems() as $cartItem) {
            $decodedProduct = $decodedProducts->where('id', $cartItem->getProduct()->id)->first();
            if (isset($decodedProduct->timeSlot)) {
                $cartItem->setSelectedTimeSlot($decodedProduct->timeSlot);
            }
        }

        return redirect(localized_route('shoppingCart'));
    }

    /**
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function mailProgram(MailProgramRequest $request)
    {
        /** @var ShoppingCartInterface $shoppingCart */
        $shoppingCart = app()->make(ShoppingCartInterface::class);

        $cartGenerationLink = $shoppingCart->generateLink();

        $requestedToday = RequestedProgram::where('created_at', '>=', now()->subMinutes(10))
            ->where('email', $request->get('email'))
            ->where('program_url', $cartGenerationLink)
            ->first();
        if(app()->environment('local')) $requestedToday = null;

        // Make sure that same email and program isn't send again within 10 minutes (mainly for double click event)
        if (! $requestedToday) {
            RequestedProgram::create([
                'email' => $request->get('email'),
                'program_url' => $cartGenerationLink,
            ]);

            // Send data to Teamleader
            $teamleaderService = new TeamleaderService();
            [$savedToTeamleader, $messageLines] = $teamleaderService->insertShoppingCartIntoTeamLeader($shoppingCart, $request);

            Mail::send(new SendProgramMail($shoppingCart, $request->get('email'), $cartGenerationLink));
            Mail::send(new SendProgramAdminMail($shoppingCart, $request->get('email'), $cartGenerationLink, $savedToTeamleader, $messageLines));
        }

        return redirect(localized_route('shoppingCart.mail.success'));
    }

    public function programSend(): View
    {
        return view('site.templates.program_send', [
            'links' => $this->links,
        ]);
    }

    public function offer(): View
    {
        /** @var ShoppingCartInterface $shoppingCart */
        $shoppingCart = app()->make(ShoppingCartInterface::class);

        return view('site.templates.create_offer', [
            'links'        => $this->links,
            'shoppingCart' => $shoppingCart,
        ]);
    }
}