Files
fewo-stommer/app/Support/CancellationCost.php
LarsandClaude Sonnet 5.5 0c4187edaf
Deploy FeWo mit PipelineManager / deploy (push) Successful in 3m34s
Stornoregeln mit automatischer Kostenberechnung, Storno-Verwaltung, Vertragsanlagen
- Stornoregel (Stufen nach Tagen vor Anreise, Mietpreis oder Gesamtpreis, Ersatzmieter-Gebuehr), je Vertragsvorlage,
  beim Angebot eingefroren, als Platzhalter im Vertrag
- Stornierung (Gast-Link oder Verwaltung) berechnet die Kosten, legt einen Stornofall an; Verwaltung Stornierungen
  mit Vorschlag, Festlegung, Saldo, Abrechnung und Erlass; Gast sieht die Kosten vorab auf der Storno-Seite
- Vertragsanlagen als eigene versionierte Dokumente mit Bedingung (immer, Zusatzleistung, Portal, von Hand),
  je Angebot ausgewaehlt und eingefroren; Zusatzleistungen in der Buchung erfassbar
- FeWo-direkt-Installation legt die Stornoregel aus Paragraf 13 an

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-30 16:32:48 +01:00

54 lines
2.2 KiB
PHP
Raw Permalink 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\Support;
use App\Enums\QuoteLineType;
use App\Models\Booking;
use App\Models\CancellationPolicy;
use Carbon\CarbonInterface;
/**
* Calculates what a cancellation costs according to the cancellation rule frozen in the offer. Returns null when
* the booking has no rule (then the landlord decides by hand). The result is a proposal: the landlord can adjust it.
*/
class CancellationCost
{
/**
* @return array{policy: string, base: string, days_before: int, percent: int, base_amount: int, fee: int, free: bool, tier_text: string}|null
*/
public static function calculate(Booking $booking, ?CarbonInterface $on = null): ?array
{
$policy = $booking->offer_terms['cancellation_policy'] ?? null;
if (! is_array($policy) || empty($policy['tiers'])) {
return null;
}
$on ??= today();
$daysBefore = max(0, (int) $on->copy()->startOfDay()->diffInDays($booking->arrival, false));
$tier = CancellationPolicy::tierFor($policy, $daysBefore);
$baseAmount = $policy['base'] === CancellationPolicy::BASE_TOTAL ? (int) $booking->total : self::rent($booking);
$fee = (int) round($baseAmount * $tier['percent'] / 100);
return [
'policy' => (string) $policy['name'],
'base' => (string) $policy['base'],
'days_before' => $daysBefore,
'percent' => $tier['percent'],
'base_amount' => $baseAmount,
'fee' => $fee,
'free' => $fee === 0,
'tier_text' => $tier['percent'].' % '.($policy['base'] === CancellationPolicy::BASE_TOTAL ? 'des Gesamtpreises' : 'des Mietpreises').' ('.$daysBefore.' Tage vor Anreise)',
];
}
/**
* The rent: accommodation, extra guests, discounts and manual adjustments – without cleaning, services and extras.
*/
public static function rent(Booking $booking): int
{
$rentTypes = [QuoteLineType::Accommodation->value, QuoteLineType::ExtraGuest->value, QuoteLineType::Discount->value, QuoteLineType::Adjustment->value];
return (int) collect($booking->quote['lines'] ?? [])->filter(fn (array $line): bool => in_array($line['type'] ?? null, $rentTypes, true))->sum('amount');
}
}