*/ use BelongsToTenant, HasFactory; protected static function booted(): void { static::created(function (Booking $booking): void { $booking->forceFill(['reference' => $booking->nextReference()])->saveQuietly(); }); } /** * Next booking number of this landlord in the year of creation, continuing after the highest * number already issued. Existing numbers are never rewritten. */ public function nextReference(): string { $year = $this->created_at->format('Y'); $references = static::withoutGlobalScope('tenant') ->where('tenant_id', $this->tenant_id) ->where('reference', 'like', $year.'-%') ->whereKeyNot($this->id) ->lockForUpdate() ->pluck('reference'); $number = 1; foreach ($references as $reference) { if (is_string($reference) && preg_match('/-(\d+)$/', $reference, $match) === 1) { $number = max($number, ((int) $match[1]) + 1); } } return $year.'-'.str_pad((string) $number, 4, '0', STR_PAD_LEFT); } /** * Get the attributes that should be cast. * * @return array */ protected function casts(): array { return [ 'status' => BookingStatus::class, 'referral_source' => ReferralSource::class, 'arrival' => 'immutable_date', 'departure' => 'immutable_date', 'extras' => 'array', 'quote' => 'array', 'deposit_due_on' => 'immutable_date', 'balance_due_on' => 'immutable_date', 'hold_expires_at' => 'immutable_datetime', 'confirmed_at' => 'immutable_datetime', 'offer_sent_at' => 'immutable_datetime', 'accepted_at' => 'immutable_datetime', 'acceptance' => 'array', 'cancelled_at' => 'immutable_datetime', 'review_requested_at' => 'immutable_datetime', 'deposit_reminded_at' => 'immutable_datetime', 'balance_requested_at' => 'immutable_datetime', 'balance_reminded_at' => 'immutable_datetime', 'arrival_info_sent_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 requests/offers 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 ->whereIn('status', BookingStatus::holding()) ->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); } /** * @return HasOne */ public function review(): HasOne { return $this->hasOne(Review::class); } /** * @return HasMany */ public function payments(): HasMany { return $this->hasMany(Payment::class)->orderBy('paid_on'); } /** * Sum of all payments in cents (refunds are negative). */ public function paidAmount(): int { return (int) ($this->relationLoaded('payments') ? $this->payments->sum('amount') : $this->payments()->sum('amount')); } public function openAmount(): int { return max(0, $this->total - $this->paidAmount()); } public function openDeposit(): int { return max(0, $this->deposit - $this->paidAmount()); } /** * When the balance is already due at confirmation (short-term booking), the guest pays everything at once. */ public function requiresFullPayment(): bool { return $this->deposit_due_on !== null && $this->balance_due_on->lessThanOrEqualTo($this->deposit_due_on); } public function nights(): int { return (int) $this->arrival->diffInDays($this->departure); } public function isHoldActive(): bool { return in_array($this->status, BookingStatus::holding(), true) && $this->hold_expires_at?->isFuture(); } /** * The website this stay belongs to, regardless of which landlord is current. */ public function website(): ?Site { return $this->property()->withoutGlobalScope('tenant')->first() ?->site()->withoutGlobalScope('tenant')->first(); } }