Files
L-Ami-Fiduciaire/app/Http/Controllers/DashboardController.php

127 lines
4.8 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
use App\Enums\FolderStatus;
use App\Models\Folder;
use App\Models\Workspace;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;
class DashboardController extends Controller
{
/**
* Display the dashboard with assigned folders and notifications.
*/
public function __invoke(Request $request): Response
{
$user = $request->user();
$workspaceId = $request->session()->get('current_workspace_id');
$workspace = $workspaceId ? Workspace::query()->find($workspaceId) : null;
$assignedFolders = [];
$notifications = [];
if ($workspace && $user) {
$assignedFolders = $workspace->folders()
->where('assigned_to', $user->id)
->whereNotIn('status', [FolderStatus::Closed, FolderStatus::Cancelled])
->with('client:id,company_name')
->orderByRaw('CASE WHEN due_date IS NULL THEN 1 ELSE 0 END, due_date ASC')
->limit(50)
->get()
->map(fn (Folder $f) => [
'id' => $f->id,
'title' => $f->title,
'type' => $f->type->value,
'client_name' => $f->client->company_name,
'status' => $f->status->value,
'due_date' => $f->due_date?->format('Y-m-d'),
'priority' => $f->priority?->value,
'showUrl' => route('folders.show', $f),
])
->all();
$overdue = $workspace->folders()
->where('assigned_to', $user->id)
->where('due_date', '<', now()->startOfDay())
->whereNotIn('status', [FolderStatus::Closed, FolderStatus::Cancelled])
->with('client:id,company_name')
->orderBy('due_date')
->limit(10)
->get()
->map(fn (Folder $f) => [
'id' => $f->id,
'title' => $f->title,
'client_name' => $f->client->company_name,
'due_date' => $f->due_date?->format('Y-m-d'),
'showUrl' => route('folders.show', $f),
])
->all();
$dueSoon = $workspace->folders()
->where('assigned_to', $user->id)
->whereBetween('due_date', [now()->startOfDay(), now()->addDays(7)->endOfDay()])
->whereNotIn('status', [FolderStatus::Closed, FolderStatus::Cancelled])
->with('client:id,company_name')
->orderBy('due_date')
->limit(10)
->get()
->map(fn (Folder $f) => [
'id' => $f->id,
'title' => $f->title,
'client_name' => $f->client->company_name,
'due_date' => $f->due_date?->format('Y-m-d'),
'showUrl' => route('folders.show', $f),
])
->all();
$documentsReceived = $workspace->folders()
->where('assigned_to', $user->id)
->where('status', FolderStatus::DocumentsReceived)
->with('client:id,company_name')
->orderBy('updated_at', 'desc')
->limit(10)
->get()
->map(fn (Folder $f) => [
'id' => $f->id,
'title' => $f->title,
'client_name' => $f->client->company_name,
'showUrl' => route('folders.show', $f),
])
->all();
$awaitingValidation = $workspace->folders()
->where('assigned_to', $user->id)
->where('status', FolderStatus::WaitingClientValidation)
->with('client:id,company_name')
->orderBy('confirmation_requested_at', 'desc')
->limit(10)
->get()
->map(fn (Folder $f) => [
'id' => $f->id,
'title' => $f->title,
'client_name' => $f->client->company_name,
'showUrl' => route('folders.show', $f),
])
->all();
$notifications = [
'overdue' => $overdue,
'due_soon' => $dueSoon,
'documents_received' => $documentsReceived,
'awaiting_validation' => $awaitingValidation,
];
}
return Inertia::render('Dashboard', [
'assignedFolders' => $assignedFolders,
'notifications' => $notifications,
'workspaceName' => $workspace?->name ?? null,
'foldersUrl' => $workspace ? route('folders.index') : null,
'clientsUrl' => $workspace ? route('clients.index') : null,
]);
}
}