62 lines
2.4 KiB
PHP
62 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
// DashboardController — gestisce la pagina principale del portale
|
|
//
|
|
// Un Controller riceve la richiesta HTTP, recupera i dati necessari
|
|
// e li passa alla View (template Blade) per la visualizzazione.
|
|
//
|
|
// Flusso di una richiesta:
|
|
// Browser → routes/web.php → Controller → View → risposta HTML
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
|
|
use App\Models\Customer;
|
|
use App\Services\SettingService;
|
|
|
|
class DashboardController extends Controller
|
|
{
|
|
// Dependency Injection: Laravel istanzia SettingService automaticamente
|
|
public function __construct(
|
|
private SettingService $settings
|
|
) {}
|
|
|
|
// Corrisponde alla route: GET /
|
|
public function index()
|
|
{
|
|
// Statistiche aggregate sui clienti
|
|
$stats = [
|
|
'total' => Customer::count(),
|
|
'active' => Customer::active()->count(),
|
|
'prospect' => Customer::where('status', 'prospect')->count(),
|
|
'inactive' => Customer::where('status', 'inattivo')->count(),
|
|
// Somma contratti clienti attivi
|
|
'total_contract_value' => Customer::active()->sum('contract_value'),
|
|
];
|
|
|
|
// Ultimi 5 clienti aggiunti (per "Attività recente")
|
|
$recentCustomers = Customer::latest()->take(5)->get();
|
|
|
|
// Clienti per città (top 5 - per widget grafico)
|
|
$byCity = Customer::selectRaw('city, count(*) as total')
|
|
->groupBy('city')
|
|
->orderByDesc('total')
|
|
->take(5)
|
|
->pluck('total', 'city')
|
|
->toArray();
|
|
|
|
// Messaggio di benvenuto dinamico (da impostazioni)
|
|
$welcomeMessage = $this->settings->get('welcome_message');
|
|
|
|
// compact() è una shorthand PHP per creare un array associativo
|
|
// equivalente a: ['stats' => $stats, 'recentCustomers' => $recentCustomers, ...]
|
|
return view('dashboard', compact(
|
|
'stats',
|
|
'recentCustomers',
|
|
'byCity',
|
|
'welcomeMessage'
|
|
));
|
|
}
|
|
}
|