Files
fewo-stommer/tests/Feature/ContractAnnexTest.php
T
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

147 lines
7.2 KiB
PHP

<?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,
]);
}
}