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' => '

Storno:

{{ 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', CaseActivities::next($booking->fresh())?->label); } 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 $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(); } }