Files
fewo-stommer/app/Models/Booking.php
T
Lars StommerandClaude Opus 5.5 4f69b426a4 Booking path 1 (offer + guest acceptance), 2FA for all, privacy hardening, brand graphics in theme
- Offer workflow: "Angebot senden" in the admin (dates, guests, promo code, manual price adjustment,
  validity from the property's hold time, message, guest data for the contract) with live price;
  contract draft PDF; signed, versioned offer page for the guest to accept (acceptance record without
  IP) or decline; expiry job, landlord notifications, editable offer texts, wording on the website.
- 2FA is mandatory for every admin account.
- Privacy: complete anonymisation (notes, attribution, review names, offer messages), shorter
  periods for unbinding requests, portal mail excerpts and attribution (configurable proposals),
  iCal summary not stored, known e-mail no longer overwrites guest data, security headers, bearer
  tokens for automations, metadata stripped from photo originals, review link throttled.
- Daily database backup (backup:database) and scripts/backup-pull.sh for the NAS.
- Theme: brand mark in header/footer, favicon/touch icon, wave divider, icons, illustration as
  image placeholder; navy follows the text colour for dark appearances.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 21:38:22 +02:00

203 lines
5.6 KiB
PHP

<?php
namespace App\Models;
use App\Enums\BookingStatus;
use App\Enums\ReferralSource;
use App\Observers\BookingObserver;
use Carbon\CarbonInterface;
use Database\Factories\BookingFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
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\Relations\HasOne;
/**
* A reservation or booking. arrival is the first night, departure the day the guest leaves,
* so a departure and the next arrival on the same day do not collide.
*/
#[ObservedBy(BookingObserver::class)]
#[Fillable([
'property_id',
'guest_id',
'status',
'source',
'referral_source',
'utm_source',
'utm_medium',
'utm_campaign',
'arrival',
'departure',
'guests',
'extras',
'promo_code',
'price_adjustment',
'price_adjustment_label',
'quote',
'total',
'deposit',
'deposit_due_on',
'balance_due_on',
'contract_path',
'hold_expires_at',
'offer_version',
'offer_sent_at',
'offer_message',
'offer_path',
'message',
'internal_notes',
'confirmed_at',
'accepted_at',
'acceptance',
'declined_by',
'cancelled_at',
'review_requested_at',
'deposit_reminded_at',
'balance_requested_at',
'balance_reminded_at',
'arrival_info_sent_at',
])]
class Booking extends Model
{
/** @use HasFactory<BookingFactory> */
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<string, string>
*/
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<Property, $this>
*/
public function property(): BelongsTo
{
return $this->belongsTo(Property::class);
}
/**
* @return BelongsTo<Guest, $this>
*/
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<Booking> $query
* @return Builder<Booking>
*/
#[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<Booking> $query
* @return Builder<Booking>
*/
#[Scope]
protected function overlapping(Builder $query, CarbonInterface $from, CarbonInterface $until): Builder
{
return $query
->whereDate('arrival', '<', $until)
->whereDate('departure', '>', $from);
}
/**
* @return HasOne<Review, $this>
*/
public function review(): HasOne
{
return $this->hasOne(Review::class);
}
/**
* @return HasMany<Payment, $this>
*/
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();
}
}