Files
fewo-stommer/app/Models/SpecialOffer.php
T
Lars StommerandClaude Opus 5.5 7d0bc93abc Offers by their own rules, seasonal photos per section, theme authoring tooling
- Special offers: kinds (rule, fixed dates, gap filler), package price, priority, highlight;
  GapFinder detects calendar gaps (optionally waiving the minimum stay); OfferShowcase decides
  what the website advertises and prices fixed dates/gaps with PriceCalculator; offers section
  shows them side by side, as a list or rotating; hero can show the top offer. Appearances no
  longer carry a featured offer.
- Sections get stable ids; appearances set photos per section ("Bilder je Baustein") and
  sections can be limited to appearances. Photo pickers show thumbnails everywhere.
- Themes: themes:make (scaffold, never overwrites), themes:check (contract, build, markup,
  colours, external resources, WCAG contrast), themes:preview (signed links per page and
  appearance), signed ?theme= preview and preview action in the site form, inherited colour
  tokens in the admin. Base CSS split into theme.css + reusable theme-base.css.
- docs/THEME-ERSTELLUNG.md: binding guide for building additional themes elsewhere.

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

201 lines
8.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Models;
use App\Enums\DiscountType;
use App\Enums\OfferKind;
use App\Pricing\StayRequest;
use App\Support\Money;
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,
* a fixed date package or a gap filler (see OfferKind). Every condition left empty means "no restriction".
*/
#[Fillable([
'property_id',
'name',
'kind',
'public_description',
'is_active',
'is_public',
'is_combinable',
'priority',
'is_featured',
'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',
'max_gap_nights',
'waives_min_stay',
'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',
'is_featured' => 'boolean',
'waives_min_stay' => 'boolean',
'kind' => OfferKind::class,
'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.
* $gapNights is the length of the calendar gap containing the stay (null = not inside a gap); it only
* matters for gap fillers.
*/
public function appliesTo(StayRequest $stay, string $arrivalSeasonName, CarbonImmutable $bookedAt, ?int $gapNights = null): bool
{
$nights = $stay->nights();
$daysBeforeArrival = (int) $bookedAt->startOfDay()->diffInDays($stay->arrival, false);
$kindMatches = match ($this->kind ?? OfferKind::Rule) {
OfferKind::Rule => true,
OfferKind::FixedDates => $this->stay_from !== null && $this->stay_until !== null
&& $stay->arrival->isSameDay($this->stay_from) && $stay->departure->isSameDay($this->stay_until),
OfferKind::GapFiller => $gapNights !== null && ($this->max_gap_nights === null || $gapNights <= $this->max_gap_nights),
};
return $this->is_active
&& $kindMatches
&& ($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)),
DiscountType::FixedStayPrice => max(0, $accommodation - $this->discount_value),
};
return min($discount, $accommodation);
}
/**
* Short label of the advantage, e.g. "−10 %", "59,00 € / Nacht" or "Pauschal 560,00 €".
*/
public function advantageLabel(): string
{
return match ($this->discount_type) {
DiscountType::Percent => "−{$this->discount_value} %",
DiscountType::AmountPerStay => '−'.Money::format($this->discount_value),
DiscountType::NightlyRate => Money::format($this->discount_value).' / Nacht',
DiscountType::FreeNights => $this->discount_value === 1 ? '1 Nacht gratis' : "{$this->discount_value} Nächte gratis",
DiscountType::FixedStayPrice => 'Pauschal '.Money::format($this->discount_value),
};
}
/**
* The conditions in plain German for guests, e.g. ["ab 3 Nächten", "Anreise freitags", "bis 14 Tage vor Anreise"].
*
* @return list<string>
*/
public function conditionSummary(): array
{
$weekdays = [1 => 'montags', 2 => 'dienstags', 3 => 'mittwochs', 4 => 'donnerstags', 5 => 'freitags', 6 => 'samstags', 7 => 'sonntags'];
return array_values(array_filter([
match (true) {
$this->kind === OfferKind::FixedDates => null,
$this->min_nights !== null && $this->max_nights !== null && $this->min_nights === $this->max_nights => "genau {$this->min_nights} Nächte",
$this->min_nights !== null && $this->max_nights !== null => "{$this->min_nights} bis {$this->max_nights} Nächte",
$this->min_nights !== null => "ab {$this->min_nights} Nächten",
$this->max_nights !== null => "bis {$this->max_nights} Nächte",
default => null,
},
empty($this->arrival_weekdays) ? null : 'Anreise '.collect($this->arrival_weekdays)->map(fn ($day): string => $weekdays[(int) $day] ?? '')->implode(' oder '),
$this->max_days_before_arrival !== null ? "Buchung bis {$this->max_days_before_arrival} Tage vor Anreise" : null,
$this->min_days_before_arrival !== null ? "Buchung mindestens {$this->min_days_before_arrival} Tage vor Anreise" : null,
empty($this->season_names) ? null : 'Anreise in: '.implode(', ', $this->season_names),
$this->kind !== OfferKind::FixedDates && ($this->stay_from || $this->stay_until)
? 'Reisezeitraum '.($this->stay_from?->format('d.m.Y') ?? 'ab sofort').' – '.($this->stay_until?->format('d.m.Y') ?? 'offen')
: null,
$this->bookable_until !== null ? 'buchbar bis '.$this->bookable_until->format('d.m.Y') : null,
]));
}
/**
* @param list<int> $rates
* @return list<int>
*/
private function sortedAscending(array $rates): array
{
sort($rates);
return $rates;
}
}