From 2d2ffffa33101e52322393964ca059f6c71e5944 Mon Sep 17 00:00:00 2001 From: Lars Date: Mon, 2 Nov 2026 19:53:13 +0100 Subject: [PATCH] =?UTF-8?q?N=C3=A4chster=20Schritt=20sofort=20sichtbar,=20?= =?UTF-8?q?Saisonkopie=20ohne=20Preisl=C3=BCcke?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Vorgang: Die Kopfaktionen werden nach jeder Aktion neu aufgebaut, damit der große Schalter gleich den nächsten Schritt zeigt (Filament baut sie vor der Aktion auf). - In Folgejahr kopieren: Reicht eine bestehende Saison (z. B. Neujahr über den Jahreswechsel) nur über Beginn oder Ende der Kopie, wird die Kopie dort gekürzt statt ganz übersprungen; sonst blieb z. B. Januar bis März ohne Preis. Co-Authored-By: Claude Opus 5.5 --- app/Actions/CopySeasonsToNextYear.php | 37 +++++++++++++++++-- .../Resources/Bookings/Pages/ViewBooking.php | 18 +++++++++ .../SeasonsRelationManager.php | 8 +++- tests/Feature/MinimumStayRulesTest.php | 16 ++++++++ tests/Feature/PortalBookingTest.php | 15 ++++++++ tests/Feature/SeasonManagementTest.php | 2 +- 6 files changed, 90 insertions(+), 6 deletions(-) diff --git a/app/Actions/CopySeasonsToNextYear.php b/app/Actions/CopySeasonsToNextYear.php index 5b6d20b..2e7cbd3 100644 --- a/app/Actions/CopySeasonsToNextYear.php +++ b/app/Actions/CopySeasonsToNextYear.php @@ -14,12 +14,13 @@ class CopySeasonsToNextYear { /** * @param int|null $alignToWeekday ISO weekday (1 = Monday … 7 = Sunday) or null for the same calendar dates - * @return array{created: int, skipped: int} + * @return array{created: int, skipped: int, trimmed: int} */ public function handle(Property $property, int $year, ?int $alignToWeekday = null): array { $created = 0; $skipped = 0; + $trimmed = 0; $seasons = $property->seasons()->whereYear('starts_on', $year)->orderBy('starts_on')->get(); @@ -38,7 +39,8 @@ public function handle(Property $property, int $year, ?int $alignToWeekday = nul continue; } - if ($property->seasons()->overlapping($startsOn, $endsOn)->exists()) { + [$startsOn, $endsOn, $wasTrimmed] = $this->fitBetweenExisting($property, $startsOn, $endsOn); + if ($startsOn === null || $endsOn === null) { $skipped++; continue; @@ -50,9 +52,38 @@ public function handle(Property $property, int $year, ?int $alignToWeekday = nul 'ends_on' => $endsOn, ]); $created++; + $trimmed += $wasTrimmed ? 1 : 0; } - return ['created' => $created, 'skipped' => $skipped]; + return ['created' => $created, 'skipped' => $skipped, 'trimmed' => $trimmed]; + } + + /** + * Existing seasons win. One that only reaches over the start or the end of the copy shortens the copy there (e.g. + * a season over the turn of the year that ends a day after the moved start), so no nights stay without a price; + * the copy is left out only when an existing season lies within it or covers it completely. + * + * @return array{0: ?CarbonImmutable, 1: ?CarbonImmutable, 2: bool} + */ + private function fitBetweenExisting(Property $property, CarbonImmutable $startsOn, CarbonImmutable $endsOn): array + { + $originalStart = $startsOn; + $originalEnd = $endsOn; + + foreach ($property->seasons()->overlapping($startsOn, $endsOn)->get() as $existing) { + if ($existing->starts_on->lessThanOrEqualTo($startsOn) && $existing->ends_on->greaterThan($startsOn)) { + $startsOn = $existing->ends_on; + } + if ($existing->ends_on->greaterThanOrEqualTo($endsOn) && $existing->starts_on->lessThan($endsOn)) { + $endsOn = $existing->starts_on; + } + } + + if ($endsOn->lessThanOrEqualTo($startsOn) || $property->seasons()->overlapping($startsOn, $endsOn)->exists()) { + return [null, null, false]; + } + + return [$startsOn, $endsOn, ! $startsOn->equalTo($originalStart) || ! $endsOn->equalTo($originalEnd)]; } /** diff --git a/app/Filament/Resources/Bookings/Pages/ViewBooking.php b/app/Filament/Resources/Bookings/Pages/ViewBooking.php index 8e232f7..718b051 100644 --- a/app/Filament/Resources/Bookings/Pages/ViewBooking.php +++ b/app/Filament/Resources/Bookings/Pages/ViewBooking.php @@ -97,6 +97,24 @@ public function getSubheading(): string|Htmlable|null return CaseActivities::situation($this->record); } + /** + * Filament builds the header actions before an action runs; rebuild them afterwards so the large button already + * shows the next step instead of the one just done. + */ + protected function afterActionCalled(Action $action): void + { + parent::afterActionCalled($action); + + $fresh = $this->record->exists ? $this->record->fresh() : null; + if ($fresh === null) { + return; + } + + $this->record = $fresh; + $this->cachedHeaderActions = []; + $this->cacheInteractsWithHeaderActions(); + } + protected function getHeaderActions(): array { $actions = collect([ diff --git a/app/Filament/Resources/Properties/RelationManagers/SeasonsRelationManager.php b/app/Filament/Resources/Properties/RelationManagers/SeasonsRelationManager.php index 0bf7c84..1af31c7 100644 --- a/app/Filament/Resources/Properties/RelationManagers/SeasonsRelationManager.php +++ b/app/Filament/Resources/Properties/RelationManagers/SeasonsRelationManager.php @@ -213,7 +213,7 @@ private function copyToNextYearAction(): Action ->label('In Folgejahr kopieren') ->icon(Heroicon::OutlinedDocumentDuplicate) ->color('gray') - ->modalDescription('Alle Saisons, die im gewählten Jahr beginnen, werden mit Preisen und Mindestnächten ins Folgejahr übernommen. Bereits belegte Zeiträume werden übersprungen.') + ->modalDescription('Alle Saisons, die im gewählten Jahr beginnen, werden mit Preisen und Mindestnächten ins Folgejahr übernommen. Bestehende Saisons bleiben unverändert: Reicht eine davon in den neuen Zeitraum hinein, beginnt bzw. endet die Kopie an ihrer Grenze; liegt der Zeitraum ganz in bestehenden Saisons, wird er übersprungen.') ->schema([ Select::make('year') ->label('Jahr kopieren') @@ -232,7 +232,11 @@ private function copyToNextYearAction(): Action Notification::make() ->title("{$result['created']} Saisons nach ".((int) $data['year'] + 1).' kopiert') - ->body($result['skipped'] > 0 ? "{$result['skipped']} übersprungen, da der Zeitraum bereits belegt ist." : 'Bitte Daten und Preise prüfen.') + ->body(collect([ + $result['trimmed'] > 0 ? "{$result['trimmed']} an bestehende Saisons angepasst (Beginn oder Ende verschoben)." : null, + $result['skipped'] > 0 ? "{$result['skipped']} übersprungen, da der Zeitraum bereits belegt ist." : null, + 'Bitte Daten und Preise prüfen.', + ])->filter()->implode(' ')) ->success() ->send(); }); diff --git a/tests/Feature/MinimumStayRulesTest.php b/tests/Feature/MinimumStayRulesTest.php index 6f65280..f4e6e2f 100644 --- a/tests/Feature/MinimumStayRulesTest.php +++ b/tests/Feature/MinimumStayRulesTest.php @@ -104,6 +104,22 @@ public function test_copying_seasons_moves_their_start_to_the_weekday_and_keeps_ $this->assertTrue($copies->every(fn (Season $season): bool => $season->starts_on->isSaturday())); } + public function test_a_season_over_the_turn_of_the_year_shortens_the_copy_instead_of_leaving_a_gap(): void + { + Season::factory()->for($this->property)->create(['name' => 'Winter', 'starts_on' => '2026-01-03', 'ends_on' => '2026-03-07', 'nightly_rate' => 5500, 'min_nights' => 2]); + Season::factory()->for($this->property)->create(['name' => 'Nebensaison', 'starts_on' => '2026-03-07', 'ends_on' => '2026-04-03', 'nightly_rate' => 6500, 'min_nights' => 2]); + Season::factory()->for($this->property)->create(['name' => 'Neujahr', 'starts_on' => '2026-12-26', 'ends_on' => '2027-01-03', 'nightly_rate' => 11500, 'min_nights' => 6]); + + $result = app(CopySeasonsToNextYear::class)->handle($this->property, 2026, 6); + + $winter = $this->property->seasons()->where('name', 'Winter')->whereYear('starts_on', 2027)->sole(); + $this->assertSame(['2027-01-03', '2027-03-06'], [$winter->starts_on->toDateString(), $winter->ends_on->toDateString()]); + $this->assertSame([0, 1], [$result['skipped'], $result['trimmed']]); + + $again = app(CopySeasonsToNextYear::class)->handle($this->property, 2026, 6); + $this->assertSame([0, 0], [$again['created'], $again['trimmed']]); + } + public function test_rules_are_edited_next_to_the_seasons(): void { $this->actingAs(User::factory()->create()); diff --git a/tests/Feature/PortalBookingTest.php b/tests/Feature/PortalBookingTest.php index 9d7d152..f2c4626 100644 --- a/tests/Feature/PortalBookingTest.php +++ b/tests/Feature/PortalBookingTest.php @@ -175,6 +175,21 @@ public function test_the_new_dialog_takes_over_a_portal_booking_and_the_page_lea $this->assertSame(2, $booking->guests); } + public function test_the_large_button_shows_the_next_step_right_after_the_portal_price_is_saved(): void + { + $booking = $this->takeOver(reference: 'HA-11', guest: ['first_name' => 'Erika', 'last_name' => 'Muster', 'email' => 'erika@example.test']); + $this->assertSame('portalPrice', CaseActivities::next($booking)?->action); + $this->actingAs(User::factory()->create()); + + $page = Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()]) + ->callAction(TestAction::make('portalPrice')->arguments([]), ['portal_total' => '979.51', 'guests' => 2, 'stated_on' => '2026-10-22']) + ->assertNotified('Beträge gespeichert'); + + $primary = $page->instance()->getCachedHeaderActions()[0]->getName(); + $this->assertNotSame('portalPrice', $primary); + $this->assertSame(CaseActivities::next($booking->fresh())?->action, $primary); + } + public function test_status_can_be_corrected_by_hand_but_a_binding_booking_never_falls_back_to_a_request(): void { $booking = $this->takeOver(); diff --git a/tests/Feature/SeasonManagementTest.php b/tests/Feature/SeasonManagementTest.php index 5bad351..127f802 100644 --- a/tests/Feature/SeasonManagementTest.php +++ b/tests/Feature/SeasonManagementTest.php @@ -88,7 +88,7 @@ public function test_seasons_are_copied_to_the_next_year_without_overwriting(): $result = (new CopySeasonsToNextYear)->handle($this->property, 2026); - $this->assertSame(['created' => 1, 'skipped' => 1], $result); + $this->assertSame(['created' => 1, 'skipped' => 1, 'trimmed' => 0], $result); $this->assertTrue($this->property->seasons() ->where('name', 'Sommer') ->where('nightly_rate', 11500)