authorizeAbility($request, 'dashboard.view'); $owner = $this->ownerRef($request); $organization = $this->organization($request); $visitQuery = Visit::owned($owner)->where('organization_id', $organization->id); $this->scopeToBranch($request, $visitQuery); $branchScope = app(OrganizationResolver::class)->branchScope($this->member($request)); $cacheKey = "fd:dashboard:{$owner}:{$organization->id}:".($branchScope ?? 'all'); $stats = Cache::remember($cacheKey, 60, function () use ($visitQuery) { $today = (clone $visitQuery)->where(function ($q) { $q->whereDate('checked_in_at', today()) ->orWhereDate('scheduled_at', today()); }); return [ 'visitors_today' => (clone $today)->count(), 'currently_inside' => (clone $visitQuery)->currentlyInside()->count(), 'expected_arrivals' => (clone $visitQuery)->whereIn('status', [ Visit::STATUS_EXPECTED, Visit::STATUS_SCHEDULED, Visit::STATUS_OVERDUE, ])->whereDate('scheduled_at', today())->count(), 'waiting' => (clone $visitQuery)->where('status', Visit::STATUS_WAITING)->count(), 'overdue' => (clone $visitQuery)->where('status', Visit::STATUS_OVERDUE)->count(), 'checked_out_today' => (clone $visitQuery)->where('status', Visit::STATUS_CHECKED_OUT) ->whereDate('checked_out_at', today())->count(), 'deliveries_today' => (clone $visitQuery)->where('visitor_type', 'delivery') ->whereDate('checked_in_at', today())->count(), 'contractors_today' => (clone $visitQuery)->where('visitor_type', 'contractor') ->whereDate('checked_in_at', today())->count(), 'pending_approvals' => (clone $visitQuery)->where('status', Visit::STATUS_WAITING) ->whereNull('checked_in_at') ->where(function ($q) { $q->whereJsonContains('contractor_details', ['_awaiting_approval' => true]) ->orWhereJsonContains('delivery_details', ['_awaiting_approval' => true]); })->count(), ]; }); $currentVisitors = (clone $visitQuery)->currentlyInside() ->with(['visitor', 'host']) ->latest('checked_in_at') ->limit(10) ->get(); $expectedVisitors = (clone $visitQuery) ->whereIn('status', [Visit::STATUS_EXPECTED, Visit::STATUS_SCHEDULED, Visit::STATUS_WAITING, Visit::STATUS_OVERDUE]) ->whereDate('scheduled_at', today()) ->with(['visitor', 'host']) ->orderBy('scheduled_at') ->limit(10) ->get(); $pendingApprovals = (clone $visitQuery) ->where('status', Visit::STATUS_WAITING) ->whereNull('checked_in_at') ->where(function ($q) { $q->whereJsonContains('contractor_details', ['_awaiting_approval' => true]) ->orWhereJsonContains('delivery_details', ['_awaiting_approval' => true]); }) ->with(['visitor', 'host']) ->latest() ->limit(10) ->get(); return view('frontdesk.dashboard', compact('stats', 'currentVisitors', 'expectedVisitors', 'pendingApprovals', 'organization')); } }