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/farmfun.komma.pro/app/Komma/Reservations/ReservationController.php
<?php

namespace App\Komma\Reservations;

use App\Komma\Availability\Types\Availability;
use App\Komma\Base\Controller;
use App\Komma\Reservations\Models\Reservation;
use App\Komma\Reservations\Models\ReservationItem;
use App\Mail\ChangedReservationAdminMail;
use App\Mail\UpdateReservationMail;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

final class ReservationController extends Controller
{
    /**
     * Show receipt of reservation
     *
     * @param Reservation $reservation
     * @return mixed
     */
    public function receipt(Reservation $reservation)
    {
        $reservation->load('location', 'items', 'items.product');

        $pdf = \PDF::loadView('pdf.receipt', ['reservation' => $reservation]);

        return $pdf->stream('Orderbon-'.$reservation->reservation_number.'.pdf');
    }

    public function change(Reservation $reservation)
    {
        $reservation->load('location', 'items.product.translation');

        // Return view
        return view('site.templates.change_reservation', [
            'links' => $this->links,
            'reservation' => $reservation,
            'message' => '',
        ]);
    }

    /**
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function process(Request $request)
    {
        $reservationArray = $request->input('reservation');

        if (empty($reservationArray)) {
            throw new \RuntimeException('No reservation found to process.');
        }

        $changedLog = [];

        foreach ($reservationArray as $id => $reservationData) {
            $reservation = Reservation::find($id);

            if ($reservation) {
                $initialReservation = clone $reservation;

                $reservation->remarks = $reservationData['remarks'];
                $reservation->reason = $reservationData['reason'];
                $reservation->save();

                foreach ($reservation->getChanges() as $changedAttr => $newValue) {
                    if ($changedAttr === 'updated_at') {
                        continue;
                    }

                    $changedLog[] = (object) [
                        'name' => __('site/form.'.$changedAttr.'.label'),
                        'from' => $initialReservation->{$changedAttr},
                        'to' => $newValue,
                    ];
                }
            }

            foreach ($reservationData['quantity'] as $reservationItemId => $quantity) {
                $reservationItem = ReservationItem::find($reservationItemId);

                if ($reservationItem && $quantity > 0 && $reservationItem->quantity != $quantity) {
                    $initialReservationItem = clone $reservationItem;
                    $reservationItem->quantity = $quantity;

                    $availability = new Availability($reservationItem->product, $reservationItem->location);
                    $availability->setAmountOfPersons($quantity);

                    // Recalculate total of the reservation item
                    $reservationItem->price_total = $availability->getTotal(true, false);
                    $reservationItem->save();

                    $changedLog[] = (object) [
                        'name' => __('site/form.estimation_persons.label')." '".$reservationItem->product->translation->name."'",
                        'from' => $initialReservationItem->quantity,
                        'to' => $quantity,
                    ];
                }
            }

            Mail::send(new ChangedReservationAdminMail($reservation, $changedLog));
            Mail::send(new UpdateReservationMail($reservation));
        }

        return redirect(localized_route('reservation.change.success'));
    }

    /**
     * The success page of the checkout
     *
     * @return View
     */
    public function success(): View
    {
        return view('site.templates.change_reservation_success', [
            'links' => $this->links,
        ]);
    }

    /**
     * Show extra information questions of reservation
     *
     * @param Reservation $reservation
     * @return Application|Factory|\Illuminate\View\View
     */
    public function information(Reservation $reservation)
    {
        $reservation->load('location', 'items.product.translation');

        $infoFound = ['mond tot mond', 'Google of ander zoekmachine', 'sociale media', 'een teambuildings verzamelwebsite', 'advertentie of brochure op papier', 'wegreclame', 'een evenement of beurs', 'ik weet het niet'];
        $companyType = ['commercieel bedrijf', 'publieke dienstverlening (school, zorginstelling, gemeente, VZW,…)', 'vriendengroep', 'vereniging / jeugdbeweging / sportclub', 'familie', 'andere doelgroep'];

        // Return view
        return view('site.templates.reservation_information', [
            'links' => $this->links,
            'reservation' => $reservation,
            'infoFoundQuestions' => $infoFound,
            'companyTypeQuestions' => $companyType,
        ]);
    }

    /**
     * @param Request $request
     * @return Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function informationProcess(Request $request)
    {
        $reservationArray = $request->input('reservation');

        if (empty($reservationArray)) {
            throw new \RuntimeException('No reservation found to process.');
        }

        foreach ($reservationArray as $id => $reservationData) {
            $reservation = Reservation::find($id);
            if ($reservation) {
                $reservation->info_found = $reservationData['info_found'] ?? '';
                $reservation->company_type = $reservationData['company_type'] ?? '';
                $reservation->save();
            }
        }

        return redirect(localized_route('reservation.information.success'));

    }

    /**
     * @return View
     */
    public function informationSuccess(): View
    {
        return view('site.templates.reservation_information_success', [
            'links' => $this->links,
        ]);
    }
}