Dashboard: Kennzahlen und nächste Aufenthalte
- Offene Anfragen, Anreisen 14 Tage, offene/überfällige Zahlungen, Auslastung, Portal-Status - Tabelle der Aufenthalte der nächsten 30 Tage mit offenen Beträgen und Anreiseinfo-Status - Nur für Rollen Administrator und Vermietung Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0bf302fcf9
commit
99e0ba4040
3 files changed
+181
No files matched your search
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Widgets;
|
||||
|
||||
use App\Enums\BookingStatus;
|
||||
use App\Enums\PortalEmailStatus;
|
||||
use App\Enums\UserRole;
|
||||
use App\Filament\Resources\Bookings\BookingResource;
|
||||
use App\Filament\Resources\ChannelTasks\ChannelTaskResource;
|
||||
use App\Models\BlockedPeriod;
|
||||
use App\Models\Booking;
|
||||
use App\Models\Channel;
|
||||
use App\Models\ChannelTask;
|
||||
use App\Models\PortalEmail;
|
||||
use App\Support\Money;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Filament\Widgets\StatsOverviewWidget;
|
||||
use Filament\Widgets\StatsOverviewWidget\Stat;
|
||||
|
||||
class BookingStats extends StatsOverviewWidget
|
||||
{
|
||||
protected static ?int $sort = 1;
|
||||
|
||||
public static function canView(): bool
|
||||
{
|
||||
return (bool) auth()->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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Widgets;
|
||||
|
||||
use App\Enums\BookingStatus;
|
||||
use App\Enums\UserRole;
|
||||
use App\Filament\Resources\Bookings\BookingResource;
|
||||
use App\Models\Booking;
|
||||
use App\Support\Money;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Table;
|
||||
use Filament\Widgets\TableWidget;
|
||||
|
||||
class UpcomingStays extends TableWidget
|
||||
{
|
||||
protected static ?int $sort = 2;
|
||||
|
||||
protected int|string|array $columnSpan = 'full';
|
||||
|
||||
protected static ?string $heading = 'Nächste Aufenthalte (30 Tage)';
|
||||
|
||||
public static function canView(): bool
|
||||
{
|
||||
return (bool) auth()->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'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Enums\BookingStatus;
|
||||
use App\Enums\UserRole;
|
||||
use App\Filament\Widgets\BookingStats;
|
||||
use App\Filament\Widgets\UpcomingStays;
|
||||
use App\Models\Booking;
|
||||
use App\Models\Property;
|
||||
use App\Models\User;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Database\Seeders\PropertySeeder;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Livewire\Livewire;
|
||||
use Tests\TestCase;
|
||||
|
||||
class DashboardTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->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');
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user