Phase 1: Wohnungen, Saisons & Preisberechnung
- Datenmodell Property/Season/Extra (Beträge in Cent), Seeder mit Preisliste 2026 - PriceCalculator: Saison-Splitting, Mindestaufenthalt, Zusatzperson, Endreinigung, Extras, Anzahlung/Restzahlung - Filament-Verwaltung: Wohnungen, Saisons (Überschneidungsprüfung, Lückenwarnung, Kopie ins Folgejahr, Preisrechner), Zusatzleistungen - Tests für Preisberechnung und Saisonverwaltung Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bf4c1c0838
commit
2e9a391b37
35 files changed
+1766
-12
No files matched your search
@@ -0,0 +1,167 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Enums\ExtraPricing;
|
||||
use App\Enums\QuoteLineType;
|
||||
use App\Models\Extra;
|
||||
use App\Models\Property;
|
||||
use App\Models\Season;
|
||||
use App\Pricing\PriceCalculator;
|
||||
use App\Pricing\QuoteException;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Database\Seeders\PropertySeeder;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PriceCalculatorTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private PriceCalculator $calculator;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->calculator = new PriceCalculator;
|
||||
}
|
||||
|
||||
/**
|
||||
* The old price list showed "first night incl. cleaning" plus "each further night".
|
||||
*
|
||||
* @return array<string, array{string, string, int}>
|
||||
*/
|
||||
public static function oldPriceListProvider(): array
|
||||
{
|
||||
return [
|
||||
'Winter, 3 Nächte' => ['2026-01-10', '2026-01-13', 163_00 + 2 * 55_00],
|
||||
'Hochsaison, 7 Nächte' => ['2026-07-04', '2026-07-11', 223_00 + 6 * 115_00],
|
||||
'Zwischensaison, 2 Nächte' => ['2026-11-07', '2026-11-09', 173_00 + 65_00],
|
||||
];
|
||||
}
|
||||
|
||||
#[DataProvider('oldPriceListProvider')]
|
||||
public function test_totals_match_the_previous_price_list(string $arrival, string $departure, int $expectedTotal): void
|
||||
{
|
||||
$this->seed(PropertySeeder::class);
|
||||
|
||||
$quote = $this->calculator->calculate(Property::sole(), CarbonImmutable::parse($arrival), CarbonImmutable::parse($departure), 2);
|
||||
|
||||
$this->assertSame($expectedTotal, $quote->total());
|
||||
}
|
||||
|
||||
public function test_stay_across_two_seasons_is_split_per_season(): void
|
||||
{
|
||||
$this->seed(PropertySeeder::class);
|
||||
|
||||
$quote = $this->calculator->calculate(Property::sole(), CarbonImmutable::parse('2026-06-18'), CarbonImmutable::parse('2026-06-22'), 2);
|
||||
|
||||
$accommodation = array_values(array_filter($quote->lines, fn ($line) => $line->type === QuoteLineType::Accommodation));
|
||||
$this->assertCount(2, $accommodation);
|
||||
$this->assertSame([2, 9500], [$accommodation[0]->quantity, $accommodation[0]->unitPrice]);
|
||||
$this->assertSame([2, 11500], [$accommodation[1]->quantity, $accommodation[1]->unitPrice]);
|
||||
$this->assertSame(2 * 9500 + 2 * 11500 + 10800, $quote->total());
|
||||
}
|
||||
|
||||
public function test_deposit_is_a_share_of_the_rent_and_balance_is_due_before_arrival(): void
|
||||
{
|
||||
$property = $this->propertyWithSeason(nightlyRate: 5500, cleaningFee: 10800, depositPercent: 35);
|
||||
|
||||
$quote = $this->calculator->calculate($property, CarbonImmutable::parse('2026-03-10'), CarbonImmutable::parse('2026-03-13'), 3);
|
||||
|
||||
$rent = 3 * 5500 + 3 * 750;
|
||||
$this->assertSame($rent, $quote->rent());
|
||||
$this->assertSame((int) round($rent * 0.35), $quote->deposit());
|
||||
$this->assertSame($quote->total() - $quote->deposit(), $quote->balance());
|
||||
$this->assertSame('2026-02-10', $quote->balanceDueOn->toDateString());
|
||||
}
|
||||
|
||||
public function test_extras_are_priced_per_item_or_per_night(): void
|
||||
{
|
||||
$property = $this->propertyWithSeason();
|
||||
$linen = Extra::factory()->for($property)->create(['price' => 1400, 'pricing' => ExtraPricing::PerItem]);
|
||||
$babyBed = Extra::factory()->for($property)->create(['price' => 300, 'pricing' => ExtraPricing::PerNight]);
|
||||
|
||||
$quote = $this->calculator->calculate($property, CarbonImmutable::parse('2026-03-10'), CarbonImmutable::parse('2026-03-14'), 2, [
|
||||
$linen->id => 2,
|
||||
$babyBed->id => 1,
|
||||
]);
|
||||
|
||||
$extras = array_values(array_filter($quote->lines, fn ($line) => $line->type === QuoteLineType::Extra));
|
||||
$this->assertSame([2 * 1400, 4 * 300], array_map(fn ($line) => $line->amount, $extras));
|
||||
}
|
||||
|
||||
public function test_minimum_stay_of_the_arrival_season_is_enforced(): void
|
||||
{
|
||||
$this->seed(PropertySeeder::class);
|
||||
|
||||
$this->expectException(QuoteException::class);
|
||||
$this->expectExceptionMessage('Mindestaufenthalt 3 Nächte');
|
||||
|
||||
$this->calculator->calculate(Property::sole(), CarbonImmutable::parse('2026-08-29'), CarbonImmutable::parse('2026-08-31'), 2);
|
||||
}
|
||||
|
||||
public function test_nights_without_a_season_cannot_be_priced(): void
|
||||
{
|
||||
$property = $this->propertyWithSeason();
|
||||
|
||||
$this->expectException(QuoteException::class);
|
||||
$this->expectExceptionMessage('01.04.2026');
|
||||
|
||||
$this->calculator->calculate($property, CarbonImmutable::parse('2026-03-30'), CarbonImmutable::parse('2026-04-02'), 2);
|
||||
}
|
||||
|
||||
public function test_guest_count_is_limited_by_the_property(): void
|
||||
{
|
||||
$property = $this->propertyWithSeason();
|
||||
|
||||
$this->expectException(QuoteException::class);
|
||||
|
||||
$this->calculator->calculate($property, CarbonImmutable::parse('2026-03-10'), CarbonImmutable::parse('2026-03-12'), $property->max_guests + 1);
|
||||
}
|
||||
|
||||
public function test_departure_must_be_after_arrival(): void
|
||||
{
|
||||
$property = $this->propertyWithSeason();
|
||||
|
||||
$this->expectException(QuoteException::class);
|
||||
|
||||
$this->calculator->calculate($property, CarbonImmutable::parse('2026-03-10'), CarbonImmutable::parse('2026-03-10'), 2);
|
||||
}
|
||||
|
||||
public function test_inactive_extras_cannot_be_booked(): void
|
||||
{
|
||||
$property = $this->propertyWithSeason();
|
||||
$extra = Extra::factory()->for($property)->create(['is_active' => false]);
|
||||
|
||||
$this->expectException(QuoteException::class);
|
||||
|
||||
$this->calculator->calculate($property, CarbonImmutable::parse('2026-03-10'), CarbonImmutable::parse('2026-03-12'), 2, [$extra->id => 1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* A property with a single season covering March 2026.
|
||||
*/
|
||||
private function propertyWithSeason(int $nightlyRate = 5500, int $cleaningFee = 10800, int $depositPercent = 35): Property
|
||||
{
|
||||
$property = Property::factory()->create([
|
||||
'max_guests' => 3,
|
||||
'included_guests' => 2,
|
||||
'extra_guest_fee_per_night' => 750,
|
||||
'cleaning_fee' => $cleaningFee,
|
||||
'deposit_percent' => $depositPercent,
|
||||
'balance_due_days_before_arrival' => 28,
|
||||
]);
|
||||
|
||||
Season::factory()->for($property)->create([
|
||||
'starts_on' => '2026-03-01',
|
||||
'ends_on' => '2026-04-01',
|
||||
'nightly_rate' => $nightlyRate,
|
||||
'min_nights' => 2,
|
||||
]);
|
||||
|
||||
return $property;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Actions\CopySeasonsToNextYear;
|
||||
use App\Filament\Resources\Properties\Pages\EditProperty;
|
||||
use App\Filament\Resources\Properties\PropertyResource;
|
||||
use App\Filament\Resources\Properties\RelationManagers\SeasonsRelationManager;
|
||||
use App\Models\Property;
|
||||
use App\Models\Season;
|
||||
use App\Models\User;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Actions\Testing\TestAction;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Livewire\Livewire;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SeasonManagementTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private Property $property;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->actingAs(User::factory()->create());
|
||||
$this->property = Property::factory()->create();
|
||||
Season::factory()->for($this->property)->create([
|
||||
'name' => 'Winter',
|
||||
'starts_on' => '2026-01-03',
|
||||
'ends_on' => '2026-03-07',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_season_is_created_with_euro_amounts_stored_as_cents(): void
|
||||
{
|
||||
$this->seasonsManager()
|
||||
->callAction(TestAction::make(CreateAction::class)->table(), [
|
||||
'name' => 'Nebensaison',
|
||||
'starts_on' => '2026-03-07',
|
||||
'ends_on' => '2026-04-03',
|
||||
'nightly_rate' => '65.50',
|
||||
'min_nights' => 2,
|
||||
])
|
||||
->assertHasNoFormErrors();
|
||||
|
||||
$this->assertDatabaseHas('seasons', [
|
||||
'property_id' => $this->property->id,
|
||||
'name' => 'Nebensaison',
|
||||
'nightly_rate' => 6550,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_overlapping_season_is_rejected(): void
|
||||
{
|
||||
$this->seasonsManager()
|
||||
->callAction(TestAction::make(CreateAction::class)->table(), [
|
||||
'name' => 'Überschneidung',
|
||||
'starts_on' => '2026-03-01',
|
||||
'ends_on' => '2026-03-14',
|
||||
'nightly_rate' => '70',
|
||||
'min_nights' => 2,
|
||||
])
|
||||
->assertHasFormErrors(['starts_on']);
|
||||
|
||||
$this->assertDatabaseMissing('seasons', ['name' => 'Überschneidung']);
|
||||
}
|
||||
|
||||
public function test_uncovered_periods_are_reported(): void
|
||||
{
|
||||
Season::factory()->for($this->property)->create(['starts_on' => '2026-04-03', 'ends_on' => '2026-05-23']);
|
||||
|
||||
$gaps = $this->property->uncoveredPeriods(CarbonImmutable::parse('2026-01-01'), CarbonImmutable::parse('2026-06-01'));
|
||||
|
||||
$this->assertSame(
|
||||
[['2026-01-01', '2026-01-03'], ['2026-03-07', '2026-04-03'], ['2026-05-23', '2026-06-01']],
|
||||
array_map(fn (array $gap): array => [$gap['from']->toDateString(), $gap['until']->toDateString()], $gaps),
|
||||
);
|
||||
}
|
||||
|
||||
public function test_seasons_are_copied_to_the_next_year_without_overwriting(): void
|
||||
{
|
||||
Season::factory()->for($this->property)->create(['name' => 'Bereits gepflegt', 'starts_on' => '2027-01-10', 'ends_on' => '2027-01-20']);
|
||||
Season::factory()->for($this->property)->create(['name' => 'Sommer', 'starts_on' => '2026-06-20', 'ends_on' => '2026-08-29', 'nightly_rate' => 11500]);
|
||||
|
||||
$result = (new CopySeasonsToNextYear)->handle($this->property, 2026);
|
||||
|
||||
$this->assertSame(['created' => 1, 'skipped' => 1], $result);
|
||||
$this->assertTrue($this->property->seasons()
|
||||
->where('name', 'Sommer')
|
||||
->where('nightly_rate', 11500)
|
||||
->whereDate('starts_on', '2027-06-20')
|
||||
->whereDate('ends_on', '2027-08-29')
|
||||
->exists());
|
||||
}
|
||||
|
||||
public function test_admin_pages_render(): void
|
||||
{
|
||||
$this->get(PropertyResource::getUrl('index'))->assertOk()->assertSee($this->property->name);
|
||||
$this->get(PropertyResource::getUrl('edit', ['record' => $this->property]))->assertOk();
|
||||
|
||||
$this->seasonsManager()
|
||||
->set('tableFilters.year.value', '2026')
|
||||
->assertCanSeeTableRecords($this->property->seasons)
|
||||
->assertSee('Ohne Preis');
|
||||
}
|
||||
|
||||
public function test_price_calculator_action_shows_the_total(): void
|
||||
{
|
||||
$this->seasonsManager()
|
||||
->callAction(TestAction::make('calculatePrice')->table(), [
|
||||
'arrival' => '2026-01-10',
|
||||
'departure' => '2026-01-13',
|
||||
'guests' => 2,
|
||||
])
|
||||
->assertNotified();
|
||||
}
|
||||
|
||||
private function seasonsManager(): mixed
|
||||
{
|
||||
return Livewire::test(SeasonsRelationManager::class, [
|
||||
'ownerRecord' => $this->property,
|
||||
'pageClass' => EditProperty::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user