Files
fewo-stommer/app/Models/Booking.php
T
Lars StommerandClaude Opus 5.5 0b7205e050 Portal-Synchronisation: unser System führt
- Connector-System (app/Channels, config/channels.php) mit iCal-Connector, robustem Parser und Export-Feed je Portal (ohne eigene Belegungen, ohne Gästedaten)
- Abgleich alle 5 Min. mit Protokoll, Echo-Erkennung, Konflikt-Mail bei möglicher Doppelbuchung
- Überwachung: Störungs-Mail, Status-Endpunkt /health/sync für n8n
- Portal-Aufgaben für Sofortbuchungsportale (FeWo-direkt): automatisch aus Buchungen/Sperren/anderen Portalen, Mail + Erinnerung, Abhaken im Admin
- Optionaler signierter Automatisierungs-Webhook mit Rückmeldung
- Herkunftsmessung: Abfrage im Formular + UTM-Parameter
- Portal-Analyse: AGB-Prüfung Automatisierung, Umsetzungsbeschreibung

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

145 lines
3.9 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\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',
'quote',
'total',
'deposit',
'balance_due_on',
'hold_expires_at',
'message',
'internal_notes',
'confirmed_at',
'cancelled_at',
'review_requested_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',
'balance_due_on' => 'immutable_date',
'hold_expires_at' => 'immutable_datetime',
'confirmed_at' => 'immutable_datetime',
'cancelled_at' => 'immutable_datetime',
'review_requested_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 reservations 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
->where('status', BookingStatus::Reserved)
->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);
}
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();
}
}