netgescon-master/app/Helpers/SidebarStatsHelper.php
2025-07-20 14:57:25 +00:00

200 lines
6.1 KiB
PHP

<?php
namespace App\Helpers;
use App\Models\Stabile;
use App\Models\Soggetto;
use App\Models\Ticket;
use App\Models\Fornitore;
use App\Models\Rata;
use App\Models\UnitaImmobiliare;
use App\Models\Assemblea;
use App\Models\MovimentoContabile;
use App\Models\Documento;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Carbon\Carbon;
class SidebarStatsHelper
{
/**
* Ottiene statistiche per la sidebar con cache
*/
public static function getStats()
{
return Cache::remember('sidebar_stats', 300, function () { // Cache per 5 minuti
return [
'stabili' => self::getStabiliStats(),
'condomini' => self::getCondominiStats(),
'tickets' => self::getTicketsStats(),
'contabilita' => self::getContabilitaStats(),
'fornitori' => self::getFornitoriStats(),
'assemblee' => self::getAssembleeStats(),
'documenti' => self::getDocumentiStats(),
];
});
}
/**
* Statistiche Stabili
*/
private static function getStabiliStats()
{
try {
$totaleUnita = UnitaImmobiliare::count();
$unitaOccupate = UnitaImmobiliare::whereHas('proprietari')->count();
return [
'totale' => Stabile::count(),
'attivi' => Stabile::where('stato', 'attivo')->count(),
'unita_totali' => $totaleUnita,
'unita_libere' => $totaleUnita - $unitaOccupate,
];
} catch (\Exception $e) {
return ['totale' => 0, 'attivi' => 0, 'unita_totali' => 0, 'unita_libere' => 0];
}
}
/**
* Statistiche Condomini
*/
private static function getCondominiStats()
{
try {
return [
'totale' => Soggetto::count(),
'proprietari' => Soggetto::where('tipo', 'proprietario')->count(),
'inquilini' => Soggetto::where('tipo', 'inquilino')->count(),
];
} catch (\Exception $e) {
return ['totale' => 0, 'proprietari' => 0, 'inquilini' => 0];
}
}
/**
* Statistiche Tickets
*/
private static function getTicketsStats()
{
try {
return [
'aperti' => Ticket::where('stato', 'aperto')->count(),
'urgenti' => Ticket::where('priorita', 'alta')
->where('stato', '!=', 'chiuso')
->count(),
'in_lavorazione' => Ticket::where('stato', 'in_lavorazione')->count(),
];
} catch (\Exception $e) {
return ['aperti' => 0, 'urgenti' => 0, 'in_lavorazione' => 0];
}
}
/**
* Statistiche Contabilità
*/
private static function getContabilitaStats()
{
try {
$oggi = Carbon::now();
$meseCorrente = $oggi->format('Y-m');
return [
'rate_scadute' => Rata::where('data_scadenza', '<', $oggi)
->where('stato', '!=', 'pagata')
->count(),
'incassi_mese' => Rata::whereMonth('data_pagamento', $oggi->month)
->whereYear('data_pagamento', $oggi->year)
->where('stato', 'pagata')
->sum('importo'),
'movimenti_mese' => MovimentoContabile::whereMonth('data_movimento', $oggi->month)
->whereYear('data_movimento', $oggi->year)
->count(),
];
} catch (\Exception $e) {
return ['rate_scadute' => 0, 'incassi_mese' => 0, 'movimenti_mese' => 0];
}
}
/**
* Statistiche Fornitori
*/
private static function getFornitoriStats()
{
try {
return [
'totale' => Fornitore::count(),
'attivi' => Fornitore::where('stato', 'attivo')->count(),
'fatture_pending' => 0, // Da implementare quando avremo il modello Fattura
];
} catch (\Exception $e) {
return ['totale' => 0, 'attivi' => 0, 'fatture_pending' => 0];
}
}
/**
* Statistiche Assemblee
*/
private static function getAssembleeStats()
{
try {
$oggi = Carbon::now();
return [
'prossime' => Assemblea::where('data_assemblea', '>', $oggi)->count(),
'questo_mese' => Assemblea::whereMonth('data_assemblea', $oggi->month)
->whereYear('data_assemblea', $oggi->year)
->count(),
'delibere_da_approvare' => Assemblea::where('stato', 'bozza')->count(),
];
} catch (\Exception $e) {
return ['prossime' => 0, 'questo_mese' => 0, 'delibere_da_approvare' => 0];
}
}
/**
* Statistiche Documenti
*/
private static function getDocumentiStats()
{
try {
$oggi = Carbon::now();
return [
'totali' => Documento::count(),
'caricati_oggi' => Documento::whereDate('created_at', $oggi->toDateString())->count(),
'da_revisionare' => Documento::where('stato', 'bozza')->count(),
];
} catch (\Exception $e) {
return ['totali' => 0, 'caricati_oggi' => 0, 'da_revisionare' => 0];
}
}
/**
* Pulisce la cache delle statistiche
*/
public static function clearCache()
{
Cache::forget('sidebar_stats');
}
/**
* Badge per contatori con colori dinamici
*/
public static function getBadge($count, $type = 'info')
{
if ($count == 0) return '';
$colors = [
'success' => 'bg-success',
'warning' => 'bg-warning text-dark',
'danger' => 'bg-danger',
'info' => 'bg-info',
'primary' => 'bg-primary'
];
$colorClass = $colors[$type] ?? 'bg-secondary';
return "<span class=\"badge {$colorClass} ms-2\">{$count}</span>";
}
}