Files
fewo-stommer/app/Models/SpecialOffer.php
T
Lars StommerandClaude Opus 5.5 08b6582fa1 Phase 2: Kalender, Anfrage, Vorreservierung & Sonderangebote
- Gäste, Buchungen (Status-Workflow), Sperrzeiten, Sonderangebote
- Vorreservierung mit Haltezeit je Wohnung; abgelaufene Holds geben den Kalender sofort frei, Cron setzt Status und benachrichtigt
- Sonderangebote/Sonderkonditionen: %, € pro Aufenthalt, Sonderpreis/Nacht, Gratis-Nächte; Bedingungen u. a. Saison, Vorlauf, Wochentag, Nächte, Aktionscode; bestes Einzel- vs. kombinierte Angebote
- Öffentliche Seite /buchen: Belegungskalender, Live-Preis, Anfrageformular (Honeypot, Rate-Limit)
- E-Mails: Angebot an Gast, Benachrichtigung Vermieter, Bestätigung, Absage, Ablauf
- Filament: Anfragen & Buchungen (bestätigen, ablehnen, verlängern, stornieren, Telefonbuchung), Gäste, Sperrzeiten, Sonderangebote
- Deutsche Validierungstexte

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

137 lines
4.6 KiB
PHP

<?php
namespace App\Models;
use App\Enums\DiscountType;
use App\Pricing\StayRequest;
use Carbon\CarbonImmutable;
use Database\Factories\SpecialOfferFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
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;
/**
* A discount rule such as last minute, early bird, long stay, special weekends or a code for regular guests.
* Every condition left empty means "no restriction".
*/
#[Fillable([
'property_id',
'name',
'public_description',
'is_active',
'is_public',
'is_combinable',
'code',
'discount_type',
'discount_value',
'stay_from',
'stay_until',
'bookable_from',
'bookable_until',
'min_days_before_arrival',
'max_days_before_arrival',
'min_nights',
'max_nights',
'arrival_weekdays',
'season_names',
])]
class SpecialOffer extends Model
{
/** @use HasFactory<SpecialOfferFactory> */
use HasFactory;
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'is_active' => 'boolean',
'is_public' => 'boolean',
'is_combinable' => 'boolean',
'discount_type' => DiscountType::class,
'stay_from' => 'immutable_date',
'stay_until' => 'immutable_date',
'bookable_from' => 'immutable_datetime',
'bookable_until' => 'immutable_datetime',
'arrival_weekdays' => 'array',
'season_names' => 'array',
];
}
/**
* @return BelongsTo<Property, $this>
*/
public function property(): BelongsTo
{
return $this->belongsTo(Property::class);
}
/**
* @param Builder<SpecialOffer> $query
* @return Builder<SpecialOffer>
*/
#[Scope]
protected function active(Builder $query): Builder
{
return $query->where('is_active', true);
}
/**
* Whether all conditions are met. Offers with a code only apply when the guest entered that code.
*/
public function appliesTo(StayRequest $stay, string $arrivalSeasonName, CarbonImmutable $bookedAt): bool
{
$nights = $stay->nights();
$daysBeforeArrival = (int) $bookedAt->startOfDay()->diffInDays($stay->arrival, false);
return $this->is_active
&& ($this->code === null || strcasecmp($this->code, (string) $stay->promoCode) === 0)
&& ($this->stay_from === null || $stay->arrival->greaterThanOrEqualTo($this->stay_from))
&& ($this->stay_until === null || $stay->departure->lessThanOrEqualTo($this->stay_until))
&& ($this->bookable_from === null || $bookedAt->greaterThanOrEqualTo($this->bookable_from))
&& ($this->bookable_until === null || $bookedAt->lessThanOrEqualTo($this->bookable_until))
&& ($this->min_days_before_arrival === null || $daysBeforeArrival >= $this->min_days_before_arrival)
&& ($this->max_days_before_arrival === null || $daysBeforeArrival <= $this->max_days_before_arrival)
&& ($this->min_nights === null || $nights >= $this->min_nights)
&& ($this->max_nights === null || $nights <= $this->max_nights)
&& (empty($this->arrival_weekdays) || in_array($stay->arrival->isoWeekday(), array_map('intval', $this->arrival_weekdays), true))
&& (empty($this->season_names) || in_array($arrivalSeasonName, $this->season_names, true));
}
/**
* The discount in cents for the given nightly rates (one entry per night, in cents).
*
* @param list<int> $nightlyRates
*/
public function discountFor(array $nightlyRates): int
{
$accommodation = array_sum($nightlyRates);
$discount = match ($this->discount_type) {
DiscountType::Percent => intdiv($accommodation * min($this->discount_value, 100) + 50, 100),
DiscountType::AmountPerStay => $this->discount_value,
DiscountType::NightlyRate => array_sum(array_map(fn (int $rate): int => max(0, $rate - $this->discount_value), $nightlyRates)),
DiscountType::FreeNights => array_sum(array_slice($this->sortedAscending($nightlyRates), 0, $this->discount_value)),
};
return min($discount, $accommodation);
}
/**
* @param list<int> $rates
* @return list<int>
*/
private function sortedAscending(array $rates): array
{
sort($rates);
return $rates;
}
}