Stornoregeln mit automatischer Kostenberechnung, Storno-Verwaltung, Vertragsanlagen
Deploy FeWo mit PipelineManager / deploy (push) Successful in 3m34s

- 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>
This commit is contained in:
LarsandClaude Sonnet 5.5 committed 2026-10-30 16:32:48 +01:00
1 parent 5baf39441f
commit 0c4187edaf
37 files changed
+1440 -10

No files matched your search

+179
View File
@@ -0,0 +1,179 @@
<?php
namespace Tests\Feature;
use App\Actions\CancelBooking;
use App\Actions\CancelBookingByGuest;
use App\Actions\SendOffer;
use App\Documents\OfferTerms;
use App\Enums\BookingStatus;
use App\Enums\BookingTaskKind;
use App\Enums\CancellationMode;
use App\Filament\Resources\Cancellations\Pages\ListCancellations;
use App\Models\Booking;
use App\Models\BookingTask;
use App\Models\Cancellation;
use App\Models\CancellationPolicy;
use App\Models\ContractTemplate;
use App\Models\Property;
use App\Models\User;
use App\Support\BookingProgress;
use App\Support\CancellationCost;
use App\Support\SelfServiceLinks;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Database\Seeders\SiteSeeder;
use Filament\Actions\Testing\TestAction;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use Livewire\Livewire;
use Tests\TestCase;
/**
* Cancellation rules (stepped percentages), automatic cost calculation and the cancellation management.
*/
class CancellationPolicyTest extends TestCase
{
use RefreshDatabase;
private Property $property;
protected function setUp(): void
{
parent::setUp();
$this->travelTo(CarbonImmutable::parse('2026-10-22 10:00'));
$this->seed([PropertySeeder::class, SiteSeeder::class]);
$this->property = Property::sole();
Mail::fake();
Storage::fake('local');
$this->withoutVite();
}
public function test_the_tier_depends_on_the_days_left_before_arrival(): void
{
$policy = $this->policy()->snapshot();
$this->assertSame(0, CancellationPolicy::tierFor($policy, 90)['percent']);
$this->assertSame(0, CancellationPolicy::tierFor($policy, 60)['percent']);
$this->assertSame(50, CancellationPolicy::tierFor($policy, 59)['percent']);
$this->assertSame(50, CancellationPolicy::tierFor($policy, 30)['percent']);
$this->assertSame(100, CancellationPolicy::tierFor($policy, 29)['percent']);
$this->assertSame(100, CancellationPolicy::tierFor($policy, 0)['percent']);
}
public function test_the_policy_reads_like_the_contract_and_keeps_tiers_sorted(): void
{
$lines = CancellationPolicy::describe($this->policy(['tiers' => [['min_days' => 0, 'percent' => 100], ['min_days' => 60, 'percent' => 0], ['min_days' => 30, 'percent' => 50]], 'replacement_fee' => 3500])->snapshot());
$this->assertSame('Rücktritt bis 60 Tage vor Mietbeginn: 0 % des Mietpreises', $lines[0]);
$this->assertSame('Rücktritt 59 bis 30 Tage vor Mietbeginn: 50 % des Mietpreises', $lines[1]);
$this->assertSame('Rücktritt ab 29 Tage vor Mietbeginn bis Mietbeginn: 100 % des Mietpreises', $lines[2]);
$this->assertStringContainsString('35,00', $lines[3]);
}
public function test_cost_is_calculated_from_the_rent_or_from_the_total_as_the_rule_says(): void
{
$rent = $this->offerWith($this->policy(['base' => CancellationPolicy::BASE_RENT]));
$cost = CancellationCost::calculate($rent, CarbonImmutable::parse('2026-10-24'));
$this->assertSame(CancellationCost::rent($rent), $cost['base_amount']);
$this->assertLessThan($rent->total, $cost['base_amount'], 'cleaning is not part of the rent');
$this->assertSame(100, $cost['percent']);
$total = $this->offerWith($this->policy(['name' => 'Gesamt', 'base' => CancellationPolicy::BASE_TOTAL]), '2026-12-01', '2026-12-04');
$this->assertSame($total->total, CancellationCost::calculate($total, CarbonImmutable::parse('2026-10-24'))['base_amount']);
}
public function test_the_offer_freezes_the_rule_and_the_contract_can_show_it(): void
{
$policy = $this->policy();
$template = ContractTemplate::query()->create(['name' => 'Direkt', 'version' => 1, 'body' => '<p>Storno:</p>{{ storno_regel }}', 'cancellation_policy_id' => $policy->id]);
$offer = app(SendOffer::class)->handle($this->booking(), ['contract_template_id' => $template->id]);
$policy->update(['tiers' => [['min_days' => 0, 'percent' => 5]]]);
$this->assertSame(0, $offer->fresh()->offer_terms['cancellation_policy']['tiers'][0]['percent'] + 0 * 1);
$this->assertStringContainsString('Rücktritt bis 60 Tage vor Mietbeginn: 0 %', OfferTerms::document($offer->fresh())['body']);
}
public function test_cancelling_records_the_cancellation_with_the_proposed_cost_and_a_task(): void
{
$booking = $this->offerWith($this->policy(), '2026-11-02', '2026-11-05', confirm: true);
app(CancelBooking::class)->handle($booking->fresh());
$record = Cancellation::query()->sole();
$this->assertTrue($record->has_policy);
$this->assertSame(100, $record->percent);
$this->assertSame(CancellationCost::rent($booking), $record->base_amount);
$this->assertSame($record->calculated_fee, $record->fee());
$this->assertStringContainsString('Vorschlag nach Stornoregel', BookingTask::query()->where('kind', BookingTaskKind::CancellationFee)->sole()->note);
$this->assertStringContainsString('Stornokosten', BookingProgress::next($booking->fresh())['text']);
}
public function test_guest_link_is_instant_only_while_the_rule_makes_it_free_and_shows_the_cost(): void
{
$this->travelTo(CarbonImmutable::parse('2026-09-25 10:00'));
$this->property->update(['cancellation_mode' => CancellationMode::InstantWhenFree]);
$policy = $this->policy();
$far = $this->offerWith($policy, '2026-11-26', '2026-11-29', confirm: true);
$near = $this->offerWith($policy, '2026-11-20', '2026-11-23', confirm: true);
$cancel = app(CancelBookingByGuest::class);
$this->assertSame(CancellationMode::Instant, $cancel->modeFor($far->fresh()));
$this->assertSame(CancellationMode::Request, $cancel->modeFor($near->fresh()));
$this->get(SelfServiceLinks::cancel($near->fresh()))->assertOk()->assertSee('verursachen', false);
$cancel->handle($far->fresh());
$this->assertSame(0, Cancellation::query()->where('booking_id', $far->id)->sole()->fee());
}
public function test_the_cancellation_management_lists_decides_and_settles(): void
{
$booking = $this->offerWith($this->policy(), '2026-11-02', '2026-11-05', confirm: true);
app(CancelBooking::class)->handle($booking->fresh());
$cancellation = Cancellation::query()->sole();
$this->actingAs(User::factory()->create());
Livewire::test(ListCancellations::class)
->assertSee($booking->reference)
->callAction(TestAction::make('decide')->table($cancellation), ['final_fee' => '123.45', 'note' => 'Kulanz'])
->callAction(TestAction::make('settle')->table($cancellation->fresh()));
$fresh = $cancellation->fresh();
$this->assertSame(12345, $fresh->final_fee);
$this->assertSame(Cancellation::SETTLED, $fresh->status);
$this->assertSame(12345, $fresh->fee());
}
/**
* @param array<string, mixed> $attributes
*/
private function policy(array $attributes = []): CancellationPolicy
{
return CancellationPolicy::query()->create([
'name' => 'Direkt',
'base' => CancellationPolicy::BASE_RENT,
'tiers' => [['min_days' => 60, 'percent' => 0], ['min_days' => 30, 'percent' => 50], ['min_days' => 0, 'percent' => 100]],
...$attributes,
]);
}
private function booking(string $arrival = '2026-11-07', string $departure = '2026-11-10'): Booking
{
return Booking::factory()->for($this->property)->reserved(now()->addHours(48))->create(['arrival' => $arrival, 'departure' => $departure, 'guests' => 2]);
}
private function offerWith(CancellationPolicy $policy, string $arrival = '2026-11-07', string $departure = '2026-11-10', bool $confirm = false): Booking
{
$template = ContractTemplate::query()->firstOrCreate(['name' => 'T'.$policy->id, 'version' => 1], ['cancellation_policy_id' => $policy->id]);
$offer = app(SendOffer::class)->handle($this->booking($arrival, $departure), ['contract_template_id' => $template->id]);
if ($confirm) {
$offer->update(['status' => BookingStatus::Confirmed, 'hold_expires_at' => null]);
}
return $offer->fresh();
}
}
+146
View File
@@ -0,0 +1,146 @@
<?php
namespace Tests\Feature;
use App\Actions\SendOffer;
use App\Documents\OfferTerms;
use App\Filament\Resources\Bookings\Pages\ViewBooking;
use App\Filament\Resources\ContractAnnexes\ContractAnnexResource;
use App\Filament\Resources\ContractAnnexes\Pages\ListContractAnnexes;
use App\Models\Booking;
use App\Models\Channel;
use App\Models\ContractAnnex;
use App\Models\ContractTemplate;
use App\Models\Extra;
use App\Models\Property;
use App\Models\User;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Database\Seeders\SiteSeeder;
use Filament\Actions\Testing\TestAction;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use Livewire\Livewire;
use Tests\TestCase;
/**
* Contract annexes: own documents with conditions, attached per booking, frozen with the offer.
*/
class ContractAnnexTest extends TestCase
{
use RefreshDatabase;
private Property $property;
protected function setUp(): void
{
parent::setUp();
$this->travelTo(CarbonImmutable::parse('2026-10-22 10:00'));
$this->seed([PropertySeeder::class, SiteSeeder::class]);
$this->property = Property::sole();
Mail::fake();
Storage::fake('local');
$this->withoutVite();
}
public function test_conditions_decide_which_annexes_fit_a_booking(): void
{
$linen = Extra::query()->create(['property_id' => $this->property->id, 'name' => 'Wäschepaket', 'price' => 2300, 'pricing' => 'per_item', 'is_active' => true]);
$portal = Channel::factory()->for($this->property)->create(['name' => 'FeWo-direkt']);
$always = $this->annex('WLAN', ContractAnnex::ALWAYS);
$withLinen = $this->annex('Wäsche', ContractAnnex::EXTRA, $linen->id);
$viaPortal = $this->annex('Portal', ContractAnnex::CHANNEL, $portal->id);
$manual = $this->annex('Key', ContractAnnex::MANUAL);
$template = ContractTemplate::query()->create(['name' => 'T', 'version' => 1, 'annex_ids' => [$always->id, $withLinen->id, $viaPortal->id, $manual->id]]);
$plain = $this->booking();
$this->assertSame(['WLAN'], $template->annexesFor($plain)->pluck('name')->all());
$withExtra = $this->booking(['extras' => [$linen->id => 2], 'arrival' => '2026-12-01', 'departure' => '2026-12-04']);
$this->assertEqualsCanonicalizing(['WLAN', 'Wäsche'], $template->annexesFor($withExtra)->pluck('name')->all());
$fromPortal = $this->booking(['import_channel_id' => $portal->id, 'source' => 'portal', 'arrival' => '2026-11-20', 'departure' => '2026-11-23']);
$this->assertEqualsCanonicalizing(['WLAN', 'Portal'], $template->annexesFor($fromPortal)->pluck('name')->all());
}
public function test_the_offer_freezes_the_matching_annexes_and_shows_them_everywhere(): void
{
$annex = $this->annex('WLAN', ContractAnnex::ALWAYS, null, 'Anlage WLAN', '<p>WLAN für {{ mieter }}</p>');
$template = ContractTemplate::query()->create(['name' => 'T', 'version' => 1, 'body' => '<p>Vertrag</p>', 'annex_ids' => [$annex->id]]);
$offer = app(SendOffer::class)->handle($this->booking(), ['contract_template_id' => $template->id]);
$this->assertSame([$annex->id], array_column($offer->offer_terms['annexes'], 'id'));
$annex->update(['body' => '<p>GEÄNDERT</p>']);
$rendered = OfferTerms::annexes($offer->fresh());
$this->assertStringContainsString($offer->guest->full_name, $rendered[0]['html']);
$this->assertStringNotContainsString('GEÄNDERT', $rendered[0]['html']);
$this->get(app(SendOffer::class)->offerUrl($offer))->assertOk()->assertSee('Anlage: Anlage WLAN');
Storage::disk('local')->assertExists($offer->offer_path);
}
public function test_an_explicit_selection_in_the_offer_replaces_the_automatic_one(): void
{
$auto = $this->annex('WLAN', ContractAnnex::ALWAYS);
$manual = $this->annex('Key', ContractAnnex::MANUAL);
$template = ContractTemplate::query()->create(['name' => 'T', 'version' => 1, 'annex_ids' => [$auto->id]]);
$offer = app(SendOffer::class)->handle($this->booking(), ['contract_template_id' => $template->id, 'annex_ids' => [$manual->id]]);
$this->assertSame(['Key'], array_column($offer->offer_terms['annexes'], 'name'));
$none = app(SendOffer::class)->handle($this->booking(['arrival' => '2026-12-01', 'departure' => '2026-12-04']), ['contract_template_id' => $template->id, 'annex_ids' => []]);
$this->assertSame([], $none->offer_terms['annexes']);
}
public function test_used_annex_versions_are_locked_and_new_versions_can_be_made(): void
{
$annex = $this->annex('WLAN', ContractAnnex::ALWAYS);
$template = ContractTemplate::query()->create(['name' => 'T', 'version' => 1, 'annex_ids' => [$annex->id]]);
$this->assertTrue(ContractAnnexResource::canEdit($annex));
app(SendOffer::class)->handle($this->booking(), ['contract_template_id' => $template->id]);
$this->assertFalse(ContractAnnexResource::canEdit($annex->fresh()));
$this->actingAs(User::factory()->create());
Livewire::test(ListContractAnnexes::class)->callAction(TestAction::make('newVersion')->table($annex), []);
$this->assertSame([1, 2], ContractAnnex::query()->where('name', 'WLAN')->orderBy('version')->pluck('version')->all());
}
public function test_recording_extra_services_in_the_booking_changes_the_preselected_annexes(): void
{
$linen = Extra::query()->create(['property_id' => $this->property->id, 'name' => 'Wäschepaket', 'price' => 0, 'pricing' => 'per_item', 'is_active' => true]);
$annex = $this->annex('Wäsche', ContractAnnex::EXTRA, $linen->id);
$template = ContractTemplate::query()->create(['name' => 'T', 'version' => 1, 'annex_ids' => [$annex->id]]);
$this->property->update(['contract_template_id' => $template->id]);
$booking = $this->booking();
$this->actingAs(User::factory()->create());
$this->assertSame([], OfferTerms::templateFor($booking)->annexesFor($booking)->all());
Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()])
->callAction('services', ['items' => [['extra_id' => $linen->id, 'quantity' => 2]]])
->assertNotified('Zusatzleistungen gespeichert');
$booking = $booking->fresh();
$this->assertSame([$linen->id => 2], $booking->extras);
$this->assertSame(['Wäsche'], OfferTerms::templateFor($booking)->annexesFor($booking)->pluck('name')->all());
}
private function annex(string $name, string $type, ?int $conditionId = null, string $title = 'Anlage', string $body = '<p>Text</p>'): ContractAnnex
{
return ContractAnnex::query()->create(['name' => $name, 'version' => 1, 'title' => $title, 'body' => $body, 'condition_type' => $type, 'condition_id' => $conditionId]);
}
/**
* @param array<string, mixed> $attributes
*/
private function booking(array $attributes = []): Booking
{
return Booking::factory()->for($this->property)->reserved(now()->addHours(48))->create([
'arrival' => '2026-11-07', 'departure' => '2026-11-10', 'guests' => 2, ...$attributes,
]);
}
}