81 lines
3.9 KiB
PHP
81 lines
3.9 KiB
PHP
<?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()->occupying()->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);
|
|
}
|
|
}
|