*/ use HasFactory; protected static function booted(): void { static::created(function (Booking $booking): void { $booking->forceFill(['reference' => $booking->created_at->format('Y').'-'.str_pad((string) $booking->id, 4, '0', STR_PAD_LEFT)])->saveQuietly(); }); } /** * Get the attributes that should be cast. * * @return array */ protected function casts(): array { return [ 'status' => BookingStatus::class, 'arrival' => 'immutable_date', 'departure' => 'immutable_date', 'extras' => 'array', 'quote' => 'array', 'balance_due_on' => 'immutable_date', 'hold_expires_at' => 'immutable_datetime', 'confirmed_at' => 'immutable_datetime', 'cancelled_at' => 'immutable_datetime', ]; } /** * @return BelongsTo */ public function property(): BelongsTo { return $this->belongsTo(Property::class); } /** * @return BelongsTo */ public function guest(): BelongsTo { return $this->belongsTo(Guest::class); } /** * Bookings that currently occupy their dates: confirmed ones and reservations whose hold has not expired. * * @param Builder $query * @return Builder */ #[Scope] protected function occupying(Builder $query): Builder { return $query->where(fn (Builder $query) => $query ->whereIn('status', BookingStatus::occupying()) ->orWhere(fn (Builder $query) => $query ->where('status', BookingStatus::Reserved) ->where('hold_expires_at', '>', now()))); } /** * @param Builder $query * @return Builder */ #[Scope] protected function overlapping(Builder $query, CarbonInterface $from, CarbonInterface $until): Builder { return $query ->whereDate('arrival', '<', $until) ->whereDate('departure', '>', $from); } public function nights(): int { return (int) $this->arrival->diffInDays($this->departure); } public function isHoldActive(): bool { return $this->status === BookingStatus::Reserved && $this->hold_expires_at?->isFuture(); } }