*/ use HasFactory; /** * Get the attributes that should be cast. * * @return array */ 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 */ public function property(): BelongsTo { return $this->belongsTo(Property::class); } /** * @param Builder $query * @return Builder */ #[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 $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 */ 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 $rates * @return list */ private function sortedAscending(array $rates): array { sort($rates); return $rates; } }