Files
ladill-frontdesk/app/Http/Controllers/Frontdesk/DashboardController.php
T
isaaccladandCursor 9e2d79936c
Deploy Ladill Frontdesk / deploy (push) Failing after 35s
Test / test (push) Failing after 2m45s
Initial Ladill Frontdesk release with deploy pipeline.
Visitor management app with SSO, kiosk, badges, reports, and Gitea CI deploy to frontdesk.ladill.com.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-27 20:37:15 +00:00

89 lines
3.9 KiB
PHP

<?php
namespace App\Http\Controllers\Frontdesk;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Frontdesk\Concerns\ScopesToAccount;
use App\Models\Visit;
use App\Services\Frontdesk\OrganizationResolver;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\View\View;
class DashboardController extends Controller
{
use ScopesToAccount;
public function index(Request $request): View
{
$this->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'));
}
}