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 "{$count}"; } }