diff --git a/app/Filament/Widgets/BookingStats.php b/app/Filament/Widgets/BookingStats.php new file mode 100644 index 0000000..b5f58a8 --- /dev/null +++ b/app/Filament/Widgets/BookingStats.php @@ -0,0 +1,80 @@ +user()?->hasRole(UserRole::Admin, UserRole::Rentals); + } + + protected function getStats(): array + { + $today = CarbonImmutable::today(); + $openRequests = Booking::query()->where('status', BookingStatus::Reserved)->count(); + $arrivals = Booking::query()->whereIn('status', BookingStatus::occupying())->whereBetween('arrival', [$today, $today->addDays(14)])->count(); + + $unpaid = Booking::query()->whereIn('status', [BookingStatus::Confirmed, BookingStatus::DepositPaid])->with('payments')->get(); + $overdue = $unpaid->filter(fn (Booking $booking): bool => ($booking->deposit_due_on?->isPast() && $booking->openDeposit() > 0) + || ($booking->balance_due_on->isPast() && $booking->openAmount() > 0)); + + $portalProblems = Channel::query()->active()->get()->filter(fn (Channel $channel): bool => $channel->isStale())->count() + + ChannelTask::query()->open()->count() + + PortalEmail::query()->where('status', PortalEmailStatus::NeedsReview)->count(); + + return [ + Stat::make('Offene Anfragen', $openRequests) + ->description($openRequests > 0 ? 'warten auf Ihre Bestätigung' : 'alles bearbeitet') + ->color($openRequests > 0 ? 'warning' : 'success') + ->url(BookingResource::getUrl('index', ['filters' => ['status' => ['values' => [BookingStatus::Reserved->value]]]])), + Stat::make('Anreisen (14 Tage)', $arrivals), + Stat::make('Offene Zahlungen', Money::format($unpaid->sum(fn (Booking $booking): int => $booking->openAmount()))) + ->description($overdue->isEmpty() ? 'nichts überfällig' : "{$overdue->count()} überfällig") + ->color($overdue->isEmpty() ? 'success' : 'danger'), + Stat::make('Auslastung '.$today->year, $this->occupancy($today->startOfYear(), $today->endOfYear()->startOfDay()->addDay()).' %') + ->description('eigene Buchungen und Portale'), + Stat::make('Portale', $portalProblems === 0 ? 'OK' : "{$portalProblems} offen") + ->description('Abgleich, Aufgaben, E-Mails') + ->color($portalProblems === 0 ? 'success' : 'danger') + ->url(ChannelTaskResource::getUrl('index')), + ]; + } + + /** + * Share of nights in [$from, $until) occupied by bookings or portal occupations (own blocks do not count). + */ + private function occupancy(CarbonImmutable $from, CarbonImmutable $until): int + { + $nights = []; + $periods = Booking::query()->whereIn('status', BookingStatus::occupying())->overlapping($from, $until)->get(['arrival', 'departure']) + ->map(fn (Booking $booking): array => [$booking->arrival, $booking->departure]) + ->concat(BlockedPeriod::query()->whereNotNull('channel_id')->overlapping($from, $until)->get(['starts_on', 'ends_on']) + ->map(fn (BlockedPeriod $block): array => [$block->starts_on, $block->ends_on])); + + foreach ($periods as [$start, $end]) { + for ($night = $start->max($from); $night->lessThan($end) && $night->lessThan($until); $night = $night->addDay()) { + $nights[$night->toDateString()] = true; + } + } + + return (int) round(count($nights) / max(1, $from->diffInDays($until)) * 100); + } +} diff --git a/app/Filament/Widgets/UpcomingStays.php b/app/Filament/Widgets/UpcomingStays.php new file mode 100644 index 0000000..ff2752d --- /dev/null +++ b/app/Filament/Widgets/UpcomingStays.php @@ -0,0 +1,52 @@ +user()?->hasRole(UserRole::Admin, UserRole::Rentals); + } + + public function table(Table $table): Table + { + return $table + ->query(fn () => Booking::query() + ->with(['guest', 'payments']) + ->whereIn('status', BookingStatus::occupying()) + ->whereDate('departure', '>=', today()) + ->whereDate('arrival', '<=', today()->addDays(30))) + ->defaultSort('arrival') + ->paginated(false) + ->emptyStateHeading('Keine Aufenthalte in den nächsten 30 Tagen') + ->recordUrl(fn (Booking $record): string => BookingResource::getUrl('view', ['record' => $record])) + ->columns([ + TextColumn::make('arrival')->label('Anreise')->date('D, d.m.') + ->description(fn (Booking $record): ?string => $record->arrival->isToday() ? 'heute' : ($record->arrival->isPast() ? 'läuft' : null)), + TextColumn::make('departure')->label('Abreise')->date('D, d.m.'), + TextColumn::make('guest.last_name')->label('Gast')->formatStateUsing(fn (Booking $record): string => $record->guest->full_name), + TextColumn::make('guests')->label('Pers.'), + TextColumn::make('status')->label('Status')->badge(), + TextColumn::make('open')->label('Offen') + ->state(fn (Booking $record): string => $record->openAmount() > 0 ? Money::format($record->openAmount()) : '–') + ->color(fn (Booking $record): ?string => $record->openAmount() > 0 ? 'danger' : null), + TextColumn::make('arrival_info_sent_at')->label('Anreiseinfo')->since()->placeholder('noch nicht'), + ]); + } +} diff --git a/tests/Feature/DashboardTest.php b/tests/Feature/DashboardTest.php new file mode 100644 index 0000000..168cce6 --- /dev/null +++ b/tests/Feature/DashboardTest.php @@ -0,0 +1,49 @@ +travelTo(CarbonImmutable::parse('2026-10-22 10:00')); + $this->seed(PropertySeeder::class); + } + + public function test_dashboard_shows_key_figures_and_upcoming_stays(): void + { + $this->actingAs(User::factory()->create()); + $property = Property::sole(); + Booking::factory()->for($property)->reserved()->create(['arrival' => '2026-12-01', 'departure' => '2026-12-05']); + $soon = Booking::factory()->for($property)->create(['status' => BookingStatus::Confirmed, 'arrival' => '2026-10-30', 'departure' => '2026-11-02', 'deposit_due_on' => '2026-10-20']); + + $this->get('/admin')->assertOk(); + Livewire::test(BookingStats::class)->assertSee('Offene Anfragen')->assertSee('1 überfällig'); + Livewire::test(UpcomingStays::class)->assertCanSeeTableRecords([$soon]); + } + + public function test_content_role_does_not_see_booking_widgets(): void + { + $this->actingAs(User::factory()->role(UserRole::Content)->create()); + + $this->assertFalse(BookingStats::canView()); + $this->get('/admin')->assertOk()->assertDontSee('Offene Anfragen'); + } +}