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

namespace App\Komma\Reservations\Models;

use App\Komma\Availability\AvailabilityService;
use App\Komma\Locations\Models\Location;
use App\Komma\Locations\Types\DayInfo;
use App\Komma\Products\Models\Product;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;

/**
 * Class Page
 *
 * @property Carbon $date
 * @mixin \Eloquent
 * @property int $id
 * @property \Carbon\Carbon|null $created_at
 * @property \Carbon\Carbon|null $updated_at
 */
final class ReservationItem extends Model
{
    use SoftDeletes;

    protected $class = Reservation::class;

    public $fillable = [
        'date',
        'location_id',
        'start_time',
        'end_time',
        'product_id',
        'quantity',
        'remarks',

        'price_each_unit',
        'price_start_up',
        'price_total',
    ];

    /**
     * @return BelongsTo
     */
    public function reservation(): BelongsTo
    {
        return $this->belongsTo(Reservation::class);
    }

    /**
     * @return BelongsTo
     */
    public function location(): BelongsTo
    {
        return $this->belongsTo(Location::class);
    }

    /**
     * @return BelongsTo
     */
    public function product(): BelongsTo
    {
        return $this->belongsTo(Product::class)
            ->withTrashed();
    }

    /**
     * Get the quantity label in mailing
     *
     * @return string
     */
    public function getQuantityLabel()
    {
        if (! $this->product->has_fixed_price) {
            return $this->quantity . '';
        }

        return '1';
    }

    /**
     * Get the price for each label in mailing
     *
     * @return string
     */
    public function getPriceEach()
    {
        if ($this->price_start_up > 0) {
            if ($this->price_each_unit != 0) {
                return config('site.shop_currency') . ' ' . euro_pricing_format($this->price_each_unit) . ' + ' . config('site.shop_currency') . ' ' . euro_pricing_format($this->price_start_up) . ' opstart';
            } else {
                return config('site.shop_currency') . ' ' . euro_pricing_format($this->price_start_up);
            }
        } else {
            return config('site.shop_currency') . ' ' . euro_pricing_format($this->price_each_unit);
        }
    }

    /**
     * Get the total price of the reservation item in mailing
     *
     * @return string
     */
    public function getTotal()
    {
        return config('site.shop_currency') . ' ' . euro_pricing_format($this->price_total);
    }

    /**
     * Get the start time in Carbon DateTime
     *
     * @return Carbon
     */
    public function getStartInDateTime(): ?\Carbon\Carbon
    {
        if (! isset($this->start_time)) {
            return null;
        }

        return Carbon::createFromFormat('Y-m-d H:i:s', $this->date . ' ' . $this->start_time);
    }

    /**
     * Get the start time in Carbon DateTime
     *
     * @param bool $forUser
     * @return Carbon
     */
    public function getEndInDateTime(bool $forUser = false): ?\Carbon\Carbon
    {
        if ($forUser && $this->product) {
            $startTime = $this->getStartInDateTime();
            if (! isset($startTime)) {
                return null;
            }
            $startTime->addMinutes($this->product->duration * 60);

            return $startTime;
        }

        if (! isset($this->end_time)) {
            return null;
        }

        return Carbon::createFromFormat('Y-m-d H:i:s', $this->date . ' ' . $this->end_time);
    }

    /**
     * Get all time slots for this day
     * Should only be used for the KMS, because it doesn't take capacity into account
     *
     * @return \Illuminate\Support\Collection
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function makeTimeSlotsForToday()
    {
        $date = Carbon::createFromFormat('Y-m-d', $this->date)->startOfDay();

        /** @var DayInfo $dayInfo */
        $dayInfo = $this->location->availability->infoForDay($date);

        /** @var AvailabilityService $availabilityService */
        $availabilityService = app()->make(AvailabilityService::class);
        $timeSlots = $availabilityService->makeTimeSlots($dayInfo, $this->product->system_duration, $this->product->duration);

        // Set the selected time slot
        foreach ($timeSlots as $timeSlot) {
            if ($timeSlot->start == $this->getStartInDateTime()) {
                $timeSlot->selected = true;
            }
        }

        return $timeSlots;
    }
}