File: D:/HostingSpaces/farmfun/reserveren.farmfun.be/app/Komma/Reservations/Models/Reservation.php
<?php
namespace App\Komma\Reservations\Models;
use App\Komma\Kms\Core\Attributes\Models\Traits\HasThumbnailInterface;
use App\Komma\Kms\Core\Attributes\Models\Traits\HasThumbnailTrait;
use App\Komma\Kms\Core\Entities\DisplayNameInterface;
use App\Komma\Kms\Core\Entities\DisplayNameTrait;
use App\Komma\Locations\Models\Location;
use App\Komma\Orders\Models\Order;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
/**
* Class Reservation
*
* @mixin \Eloquent
* @property int $id
* @property int $status
* @property Carbon $date
* @property \Carbon\Carbon|null $created_at
* @property \Carbon\Carbon|null $updated_at
*/
final class Reservation extends Model implements DisplayNameInterface, HasThumbnailInterface
{
use HasThumbnailTrait;
use DisplayNameTrait;
use SoftDeletes;
const STATUS_NEW = 0;
const STATUS_CONFIRMED = 1;
protected $class = self::class;
protected $protected = ['id', 'created_at', 'updated_at'];
/**
* @return HasMany
*/
public function items(): HasMany
{
return $this->hasMany(ReservationItem::class)
->orderByRaw('ISNULL(start_time), start_time ASC');
}
/**
* @return BelongsTo
*/
public function order(): BelongsTo
{
return $this->belongsTo(Order::class);
}
/**
* @return BelongsTo
*/
public function location(): BelongsTo
{
return $this->belongsTo(Location::class);
}
/**
* @return Carbon
*/
public function getDateAsDateTime(): Carbon
{
$date = Carbon::createFromFormat('Y-m-d', $this->date);
return $date->startOfDay();
}
/**
* Determine if
*
* @return bool
*/
public function hasDrinksArrangement(): bool
{
foreach ($this->items as $item) {
if (! isset($item->product)) continue;
if (in_array($item->product->code_name, ['drank_during_diner', 'drank_during_activities'])) {
return true;
}
}
return false;
}
/**
* Get a collection of all the statuses of this model
*
* @return \Illuminate\Support\Collection
* @throws \ReflectionException
*/
public static function getPossibleStatuses()
{
$reflectionClass = new \ReflectionClass(self::class);
$constants = $reflectionClass->getConstants();
// Loop through the constants and only get the ones starting with 'STATUS_
$statuses = collect();
foreach ($constants as $key => $value) {
if (Str::startsWith($key, 'STATUS_')) {
$statuses->push((object) [
'id' => $value,
'full_name' => $key,
'short_name' => strtolower(str_replace('STATUS_', '', $key)),
]);
}
}
return $statuses;
}
/**
* @return string
*/
public function getSidebarName():string
{
$translationKey = Str::camel($this->getTable()).'.entity';
$sidebarName = __('kms/'.$translationKey).' #'.$this->reservation_number;
$clientName = $this->first_name;
if (! empty($this->name_preposition)) {
$clientName .= ' '.$this->name_preposition;
}
$clientName .= ' '.$this->last_name;
$sidebarName .= '<br/><sub>'.$clientName.'</sub>';
if (isset($this->date)) {
$sidebarName .= '<br/><sub>'.$this->getDateAsDateTime()->format('d-m-Y').'</sub>';
}
return $sidebarName;
}
public static $requiredMailFields = [
'reservation_number',
'first_name',
'last_name',
'email',
'phone',
];
/**
* Validate if the required fields are set for the mail buttons
*
* @return bool
*/
public function areRequiredFieldsForMailSet(): bool
{
foreach (self::$requiredMailFields as $requiredField) {
if (empty($this->{$requiredField})) {
return false;
}
}
return true;
}
/**
* @return bool
*/
public function canEdit(): bool
{
$reservationStart = Carbon::createFromFormat(Carbon::DEFAULT_TO_STRING_FORMAT, $this->date.' '.$this->items()->min('start_time'));
if(now() >= $reservationStart) return false;
// https://app.asana.com/0/1204095245059239/1205567371951303/f
// Het is echter nu de bedoeling dat deze lock eraf gehaald wordt zodat alleen KMS-gebruikers de
// reservering nog kunnen aanpassen, zodat er opnieuw een mail naar alle betrokkenen gestuurd
// wordt met geupdate informatie.
return Auth::guard('kms')->check() || now()->diffInHours($reservationStart) >= 72;
}
}