Files
fewo-stommer/app/Models/CancellationPolicy.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

107 lines
3.9 KiB
PHP

<?php
namespace App\Models;
use App\Models\Concerns\BelongsToTenant;
use App\Support\Money;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Model;
/**
* A cancellation rule: stepped percentages by the days left before arrival, calculated from the rent (accommodation
* without cleaning, service and tourist tax) or from the total price, plus an optional fee for a replacement tenant.
* Used by contract templates; an offer freezes a copy ({@see snapshot()}), so later edits never change it.
*/
#[Fillable(['name', 'base', 'tiers', 'replacement_fee', 'notes'])]
class CancellationPolicy extends Model
{
use BelongsToTenant;
public const BASE_RENT = 'rent';
public const BASE_TOTAL = 'total';
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return ['tiers' => 'array', 'replacement_fee' => 'integer'];
}
/**
* The frozen form stored in an offer.
*
* @return array{name: string, base: string, tiers: list<array{min_days: int, percent: int}>, replacement_fee: ?int}
*/
public function snapshot(): array
{
return [
'name' => $this->name,
'base' => $this->base,
'tiers' => self::normalise((array) $this->tiers),
'replacement_fee' => $this->replacement_fee,
];
}
/**
* Tiers sorted from the earliest to the latest cancellation: "from N days before arrival: P %".
*
* @param array<int, array<string, mixed>> $tiers
* @return list<array{min_days: int, percent: int}>
*/
public static function normalise(array $tiers): array
{
$clean = array_map(fn (array $tier): array => ['min_days' => max(0, (int) ($tier['min_days'] ?? 0)), 'percent' => min(100, max(0, (int) ($tier['percent'] ?? 0)))], $tiers);
usort($clean, fn (array $a, array $b): int => $b['min_days'] <=> $a['min_days']);
return array_values($clean);
}
/**
* The tier that applies when cancelling this many days before arrival (the last tier covers everything later).
*
* @param array{tiers: list<array{min_days: int, percent: int}>} $policy
* @return array{min_days: int, percent: int}
*/
public static function tierFor(array $policy, int $daysBefore): array
{
foreach ($policy['tiers'] as $tier) {
if ($daysBefore >= $tier['min_days']) {
return $tier;
}
}
return $policy['tiers'][array_key_last($policy['tiers'])] ?? ['min_days' => 0, 'percent' => 100];
}
/**
* The rule as readable lines for contracts and pages, e.g. "Rücktritt bis 60 Tage vor Mietbeginn: 0 % …".
*
* @param array{base: string, tiers: list<array{min_days: int, percent: int}>, replacement_fee: ?int} $policy
* @return list<string>
*/
public static function describe(array $policy): array
{
$base = $policy['base'] === self::BASE_TOTAL ? 'des Gesamtpreises' : 'des Mietpreises';
$lines = [];
$tiers = $policy['tiers'];
foreach ($tiers as $index => $tier) {
$next = $tiers[$index + 1] ?? null;
$when = match (true) {
$index === 0 => 'Rücktritt bis '.$tier['min_days'].' Tage vor Mietbeginn',
$next === null => 'Rücktritt ab '.($tiers[$index - 1]['min_days'] - 1).' Tage vor Mietbeginn bis Mietbeginn',
default => 'Rücktritt '.($tiers[$index - 1]['min_days'] - 1).' bis '.$tier['min_days'].' Tage vor Mietbeginn',
};
$lines[] = $when.': '.$tier['percent'].' % '.$base;
}
if (($policy['replacement_fee'] ?? null) !== null) {
$lines[] = 'Stellung eines Ersatzmieters: Bearbeitungsgebühr '.Money::format((int) $policy['replacement_fee']);
}
return $lines;
}
}