user(); $isRestricted = !$user->hasRole(['admin', 'directeur', 'raf']); $serviceId = $user->service_id; $baseCommande = Commande::query(); if ($isRestricted) { $baseCommande->where('service_id', $serviceId); } $stats = [ 'total' => (clone $baseCommande)->count(), 'en_cours' => (clone $baseCommande)->enCours()->count(), 'en_retard' => (clone $baseCommande)->enRetard()->count(), 'brouillons' => (clone $baseCommande)->parStatut('brouillon')->count(), 'en_attente_validation' => (clone $baseCommande)->parStatut('en_attente_validation')->count(), 'validees' => (clone $baseCommande)->parStatut('validee')->count(), 'commandees' => (clone $baseCommande)->parStatut('commandee')->count(), 'partiellement_recues' => (clone $baseCommande)->parStatut('partiellement_recue')->count(), 'recues_complete' => (clone $baseCommande)->parStatut('recue_complete')->count(), ]; $commandesRecentes = (clone $baseCommande)->with(['service', 'fournisseur', 'demandeur']) ->latest() ->limit(8) ->get(); $commandesEnRetard = (clone $baseCommande)->enRetard() ->with(['service', 'fournisseur', 'demandeur']) ->orderBy('date_souhaitee') ->limit(10) ->get(); $commandesUrgentes = (clone $baseCommande)->urgentes() ->enCours() ->with(['service', 'fournisseur', 'demandeur']) ->latest() ->limit(5) ->get(); $statsParStatut = (clone $baseCommande)->select('statut', DB::raw('count(*) as total')) ->groupBy('statut') ->get() ->keyBy('statut'); $statsParServiceQuery = Service::withCount([ 'commandes', 'commandes as commandes_en_cours_count' => fn ($q) => $q->enCours(), ]); if ($isRestricted) { $statsParServiceQuery->where('id', $serviceId); } $statsParService = $statsParServiceQuery->get(); $montantParMois = (clone $baseCommande)->select( DB::raw('YEAR(date_demande) as annee'), DB::raw('MONTH(date_demande) as mois'), DB::raw('SUM(montant_ttc) as total_ttc'), DB::raw('COUNT(*) as nb_commandes') ) ->whereYear('date_demande', now()->year) ->whereNotIn('statut', ['annulee']) ->groupBy('annee', 'mois') ->orderBy('mois') ->get(); // Stats Contrats $contratsQuery = Contrat::query(); if (!$user->hasRole(['admin', 'raf'])) { $contratsQuery->where('service_id', $user->service_id); } $tousContrats = $contratsQuery->get(); $statsContrats = [ 'total' => $tousContrats->count(), 'proches' => $tousContrats->filter(fn($c) => $c->est_proche_echeance && !$c->est_en_retard)->count(), 'en_retard' => $tousContrats->filter(fn($c) => $c->est_en_retard)->count(), ]; // Stats Domaines (Visibles par tous) $tousDomaines = Domaine::all(); $statsDomaines = [ 'total' => $tousDomaines->count(), 'proches' => $tousDomaines->filter(fn($d) => $d->est_proche_echeance && !$d->est_en_retard)->count(), 'en_retard' => $tousDomaines->filter(fn($d) => $d->est_en_retard)->count(), ]; // Stats Budget $anneeCourante = now()->year; $budgetQuery = LigneBudget::whereHas('budget', function($q) use ($anneeCourante) { $q->where('annee', $anneeCourante); }); if ($isRestricted) { $budgetQuery->whereHas('budget', function($q) use ($serviceId) { $q->where('service_id', $serviceId); }); } $budgetsStats = []; foreach (['agglo', 'mutualise'] as $typeBudget) { $budgetQueryType = (clone $budgetQuery)->whereHas('budget', fn($q) => $q->where('type_budget', $typeBudget)); $baseCommandeType = (clone $baseCommande)->whereHas('ligneBudget.budget', function($q) use ($anneeCourante, $typeBudget) { $q->where('annee', $anneeCourante)->where('type_budget', $typeBudget); })->whereNotIn('statut', ['annulee']); $budgetsStats[$typeBudget] = [ 'total_arbitre' => $budgetQueryType->sum('montant_arbitre'), 'consomme' => $baseCommandeType->sum('montant_ttc'), 'fonctionnement' => [ 'total_arbitre' => (clone $budgetQueryType)->where('type_depense', 'fonctionnement')->sum('montant_arbitre'), 'consomme' => (clone $baseCommandeType)->whereHas('ligneBudget', fn($q) => $q->where('type_depense', 'fonctionnement'))->sum('montant_ttc'), ], 'investissement' => [ 'total_arbitre' => (clone $budgetQueryType)->where('type_depense', 'investissement')->sum('montant_arbitre'), 'consomme' => (clone $baseCommandeType)->whereHas('ligneBudget', fn($q) => $q->where('type_depense', 'investissement'))->sum('montant_ttc'), ], ]; } return Inertia::render('Dashboard/Index', compact( 'stats', 'commandesRecentes', 'commandesEnRetard', 'commandesUrgentes', 'statsParStatut', 'statsParService', 'montantParMois', 'statsContrats', 'statsDomaines', 'budgetsStats', )); } }