get('stabile_id', 1); // Ottieni le gestioni condominiali $gestioni = GestioneCondominiale::where('stabile_id', $stabile_id) ->orderBy('anno_inizio', 'desc') ->get(); // Ottieni le tabelle millesimali $tabelle_millesimali = TabellaMillesimale::where('stabile_id', $stabile_id) ->with('dettagliMillesimi') ->orderBy('tipo_tabella') ->orderBy('nome_tabella') ->get(); // Statistiche riassuntive $statistiche = [ 'totale_gestioni' => $gestioni->count(), 'gestioni_attive' => $gestioni->where('stato', 'ATTIVA')->count(), 'totale_tabelle' => $tabelle_millesimali->count(), 'totale_dettagli' => DettaglioMillesimi::whereIn('tabella_millesimale_id', $tabelle_millesimali->pluck('id'))->count() ]; return view('piano_conti.index', compact('gestioni', 'tabelle_millesimali', 'statistiche', 'stabile_id')); } /** * Show the form for creating a new Piano dei Conti account */ public function create() { return view('piano_conti.create'); } /** * Store a newly created Piano dei Conti account in storage */ public function store(Request $request) { $validated = $request->validate([ 'mastro' => 'required|string|size:3', 'conto' => 'required|string|size:8', 'sottoconto' => 'required|string|size:5', 'denominazione' => 'required|string|max:255', 'descrizione' => 'nullable|string', 'tipo_conto' => 'required|in:ATTIVO,PASSIVO,RICAVO,COSTO', 'livello' => 'required|integer|min:1|max:3', 'stabile_id' => 'required|exists:stabili,id' ]); PianoDeiConti::create($validated); return redirect()->route('piano_conti.index') ->with('success', 'Conto creato con successo!'); } /** * Display the specified Piano dei Conti account */ public function show($id) { $conto = PianoDeiConti::with(['stabili'])->findOrFail($id); return view('piano_conti.show', compact('conto')); } /** * Show millesimi details for a specific table */ public function showTabella($id) { $tabella = TabellaMillesimale::with([ 'dettagliMillesimi.unitaImmobiliare' => function ($q) { $q->select('id', 'scala', 'interno', 'palazzina', 'denominazione'); } ])->findOrFail($id); // Calcola statistiche per la tabella $statistiche_tabella = [ 'totale_unita' => $tabella->dettagliMillesimi->count(), 'totale_millesimi' => $tabella->dettagliMillesimi->sum('millesimi'), 'media_millesimi' => $tabella->dettagliMillesimi->avg('millesimi'), 'min_millesimi' => $tabella->dettagliMillesimi->min('millesimi'), 'max_millesimi' => $tabella->dettagliMillesimi->max('millesimi') ]; // Raggruppa per scala $millesimi_per_scala = $tabella->dettagliMillesimi ->groupBy('unitaImmobiliare.scala') ->map(function ($gruppo) { return [ 'count' => $gruppo->count(), 'totale' => $gruppo->sum('millesimi'), 'dettagli' => $gruppo->sortBy('unitaImmobiliare.interno') ]; }); return view('piano_conti.tabella_millesimi', compact('tabella', 'statistiche_tabella', 'millesimi_per_scala')); } /** * Generate report riassuntivo per una gestione */ public function reportGestione($gestione_id) { $gestione = GestioneCondominiale::with('stabile')->findOrFail($gestione_id); // Ottieni tutte le tabelle millesimali dello stesso stabile $tabelle = TabellaMillesimale::where('stabile_id', $gestione->stabile_id) ->with(['dettagliMillesimi.unitaImmobiliare']) ->orderBy('tipo_tabella') ->orderBy('nome_tabella') ->get(); // Crea matrice millesimi per unità $unita = UnitaImmobiliare::where('stabile_id', $gestione->stabile_id) ->whereHas('dettagliMillesimi') ->orderBy('scala') ->orderBy('interno') ->get(); $matrice_millesimi = []; foreach ($unita as $unita_item) { $row = [ 'unita' => $unita_item, 'millesimi' => [] ]; foreach ($tabelle as $tabella) { $dettaglio = $unita_item->dettagliMillesimi ->where('tabella_millesimale_id', $tabella->id) ->first(); $row['millesimi'][$tabella->codice_tabella ?: $tabella->nome_tabella] = $dettaglio ? $dettaglio->millesimi : 0; } $matrice_millesimi[] = $row; } return view('piano_conti.report_gestione', compact('gestione', 'tabelle', 'matrice_millesimi')); } /** * Export tabelle millesimali to CSV */ public function exportMillesimi($stabile_id, Request $request) { $formato = $request->get('formato', 'csv'); $tabelle = TabellaMillesimale::where('stabile_id', $stabile_id) ->with(['dettagliMillesimi.unitaImmobiliare']) ->get(); $unita = UnitaImmobiliare::where('stabile_id', $stabile_id) ->whereHas('dettagliMillesimi') ->orderBy('scala') ->orderBy('interno') ->get(); $headers = [ 'Content-Type' => 'text/csv', 'Content-Disposition' => 'attachment; filename="tabelle_millesimi_stabile_' . $stabile_id . '.csv"', ]; $callback = function () use ($tabelle, $unita) { $file = fopen('php://output', 'w'); // Header CSV $header_row = ['Scala', 'Interno', 'Denominazione']; foreach ($tabelle as $tabella) { $header_row[] = $tabella->nome_tabella; } fputcsv($file, $header_row); // Righe dati foreach ($unita as $unita_item) { $row = [ $unita_item->scala, $unita_item->interno, $unita_item->denominazione ?: 'N/A' ]; foreach ($tabelle as $tabella) { $dettaglio = $unita_item->dettagliMillesimi ->where('tabella_millesimale_id', $tabella->id) ->first(); $row[] = $dettaglio ? number_format($dettaglio->millesimi, 4, '.', '') : '0.0000'; } fputcsv($file, $row); } fclose($file); }; return response()->stream($callback, 200, $headers); } }