198 lines
6.9 KiB
PHP
198 lines
6.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\BlockedPeriodPurpose;
|
|
use App\Enums\OccupationKind;
|
|
use App\Models\Concerns\BelongsToTenant;
|
|
use App\Models\Concerns\HasChannelProvenance;
|
|
use App\Observers\BlockedPeriodObserver;
|
|
use Carbon\CarbonInterface;
|
|
use Database\Factories\BlockedPeriodFactory;
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
|
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
|
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
|
|
use Illuminate\Database\Eloquent\Attributes\Scope;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
/**
|
|
* Dates that are not bookable (own use, maintenance, bookings from portals).
|
|
* Like bookings, ends_on is the first free day again.
|
|
*/
|
|
#[ObservedBy(BlockedPeriodObserver::class)]
|
|
#[Hidden(['ical_properties', 'ical_raw', 'ical_interpretation'])]
|
|
#[Fillable(['property_id', 'channel_id', 'origin_channel_id', 'guest_id', 'occupation_kind', 'block_purpose', 'block_note', 'is_private', 'starts_on', 'ends_on', 'reason', 'source', 'external_uid', 'needs_review', 'review_note', 'missing_imports', 'is_representation', 'canonical_block_id', 'linked_booking_id', 'export_approved', 'reviewed_at'])]
|
|
class BlockedPeriod extends Model
|
|
{
|
|
/** @use HasFactory<BlockedPeriodFactory> */
|
|
use BelongsToTenant, HasChannelProvenance, HasFactory, SoftDeletes;
|
|
|
|
protected $attributes = ['is_representation' => false, 'export_approved' => true];
|
|
|
|
/**
|
|
* Get the attributes that should be cast.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'block_purpose' => BlockedPeriodPurpose::class,
|
|
'is_private' => 'boolean',
|
|
'occupation_kind' => OccupationKind::class,
|
|
'starts_on' => 'immutable_date',
|
|
'ends_on' => 'immutable_date',
|
|
'needs_review' => 'boolean',
|
|
'missing_imports' => 'integer',
|
|
'is_representation' => 'boolean',
|
|
'export_approved' => 'boolean',
|
|
'reviewed_at' => 'immutable_datetime',
|
|
'ical_properties' => 'encrypted:array',
|
|
'ical_raw' => 'encrypted',
|
|
'ical_interpretation' => 'encrypted:array',
|
|
'leading_priority' => 'integer',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<Property, $this>
|
|
*/
|
|
public function property(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Property::class);
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<Channel, $this>
|
|
*/
|
|
public function channel(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Channel::class);
|
|
}
|
|
|
|
/** @return BelongsTo<Channel, $this> */
|
|
public function originChannel(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Channel::class);
|
|
}
|
|
|
|
/** @return BelongsTo<BlockedPeriod, $this> */
|
|
public function canonicalBlock(): BelongsTo
|
|
{
|
|
return $this->belongsTo(self::class, 'canonical_block_id')->withTrashed();
|
|
}
|
|
|
|
/** @return BelongsTo<Booking, $this> */
|
|
public function linkedBooking(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Booking::class, 'linked_booking_id');
|
|
}
|
|
|
|
/** @return BelongsTo<Guest, $this> */
|
|
public function guest(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Guest::class);
|
|
}
|
|
|
|
public function occupationKind(): OccupationKind
|
|
{
|
|
if ($this->source === 'manual') {
|
|
return OccupationKind::Block;
|
|
}
|
|
|
|
return $this->occupation_kind ?? ($this->ical_event_type === 'portal_reservation' ? OccupationKind::Reservation : OccupationKind::Unknown);
|
|
}
|
|
|
|
public function guestName(): ?string
|
|
{
|
|
if ($this->is_representation) {
|
|
return $this->linkedBooking?->guest?->full_name ?? $this->canonicalBlock?->guestName();
|
|
}
|
|
|
|
return $this->guest?->full_name ?? ($this->ical_interpretation['fields']['guest_full_name'] ?? $this->ical_interpretation['fields']['guest_first_name'] ?? null);
|
|
}
|
|
|
|
public function bookingChannelLabel(): string
|
|
{
|
|
return $this->is_private ? 'Privat' : ($this->originChannel?->name ?? $this->channel?->name ?? ($this->source === 'manual' ? 'Eigene Sperre' : 'Ursprung unbekannt'));
|
|
}
|
|
|
|
public function blockDescription(): ?string
|
|
{
|
|
if ($this->occupationKind() !== OccupationKind::Block) {
|
|
return $this->reason;
|
|
}
|
|
|
|
return collect([$this->block_purpose?->getLabel(), $this->block_note])->filter()->implode(' · ') ?: $this->reason;
|
|
}
|
|
|
|
public function assignedGuest(): ?Guest
|
|
{
|
|
return $this->is_representation ? ($this->linkedBooking?->guest ?? $this->canonicalBlock?->assignedGuest()) : $this->guest;
|
|
}
|
|
|
|
/** @return HasMany<BlockedPeriod, $this> */
|
|
public function channelRepresentations(): HasMany
|
|
{
|
|
return $this->hasMany(self::class, 'canonical_block_id');
|
|
}
|
|
|
|
/** @param Builder<BlockedPeriod> $query
|
|
* @return Builder<BlockedPeriod>
|
|
*/
|
|
#[Scope]
|
|
protected function reservations(Builder $query): Builder
|
|
{
|
|
return $query->whereIn('source', ['channel', 'email'])->where(fn (Builder $query) => $query
|
|
->where('occupation_kind', OccupationKind::Reservation)
|
|
->orWhere(fn (Builder $query) => $query->whereNull('occupation_kind')->where('ical_event_type', 'portal_reservation')));
|
|
}
|
|
|
|
/** @param Builder<BlockedPeriod> $query
|
|
* @return Builder<BlockedPeriod>
|
|
*/
|
|
#[Scope]
|
|
protected function explicitBlocks(Builder $query): Builder
|
|
{
|
|
return $query->where(fn (Builder $query) => $query->where('source', 'manual')->orWhere('occupation_kind', OccupationKind::Block));
|
|
}
|
|
|
|
/** @param Builder<BlockedPeriod> $query
|
|
* @return Builder<BlockedPeriod>
|
|
*/
|
|
#[Scope]
|
|
protected function unclear(Builder $query): Builder
|
|
{
|
|
return $query->whereIn('source', ['channel', 'email'])->where(fn (Builder $query) => $query
|
|
->where('needs_review', true)
|
|
->orWhere('occupation_kind', OccupationKind::Unknown)
|
|
->orWhere(fn (Builder $query) => $query->whereNull('occupation_kind')->where(fn (Builder $query) => $query->whereNull('ical_event_type')->orWhere('ical_event_type', '!=', 'portal_reservation'))));
|
|
}
|
|
|
|
/** @param Builder<BlockedPeriod> $query
|
|
* @return Builder<BlockedPeriod>
|
|
*/
|
|
#[Scope]
|
|
protected function occupying(Builder $query): Builder
|
|
{
|
|
return $query->where('is_representation', false);
|
|
}
|
|
|
|
/**
|
|
* @param Builder<BlockedPeriod> $query
|
|
* @return Builder<BlockedPeriod>
|
|
*/
|
|
#[Scope]
|
|
protected function overlapping(Builder $query, CarbonInterface $from, CarbonInterface $until): Builder
|
|
{
|
|
return $query
|
|
->whereDate('starts_on', '<', $until)
|
|
->whereDate('ends_on', '>', $from);
|
|
}
|
|
}
|