amministratore->id_amministratore ?? \Illuminate\Support\Facades\Auth::user()->id; if ($stabile->amministratore_id !== $amministratoreId) { abort(403); } $data = $request->validate([ 'anno_gestione' => 'required|integer|min:2000|max:2100', 'tipo_gestione' => 'required|in:ordinaria,riscaldamento,straordinaria', 'numero_straordinaria' => 'nullable|integer|min:0', 'denominazione' => 'nullable|string|max:255', 'data_inizio' => 'nullable|date', 'data_fine' => 'nullable|date|after_or_equal:data_inizio', 'codice_archivio_legacy' => 'nullable|string|max:100', 'mesi' => 'array', 'mesi.*' => 'in:on,1,true,TRUE', ]); $gestione = new GestioneContabile($data); $gestione->tenant_id = (string) (\Illuminate\Support\Facades\Auth::user()->tenant_id ?? 'default'); $gestione->stabile_id = $stabile->id; $gestione->stato = 'aperta'; $gestione->protocollo_prefix = strtoupper(substr($data['tipo_gestione'], 0, 1)) . $data['anno_gestione']; $gestione->ultimo_protocollo = 0; $gestione->gestione_attiva = true; // Se non indicate, default periodo intero anno if (empty($gestione->data_inizio)) { $gestione->data_inizio = $data['anno_gestione'] . '-01-01'; } if (empty($gestione->data_fine)) { $gestione->data_fine = $data['anno_gestione'] . '-12-31'; } if (empty($gestione->denominazione)) { $gestione->denominazione = 'Gestione ' . ucfirst($data['tipo_gestione']) . ' ' . $data['anno_gestione']; } // Pianificazione mesi per-gestione (checkbox mesi[1..12]) $mesiInput = (array)($data['mesi'] ?? $request->input('mesi', [])); if (!empty($mesiInput)) { $gestione->setMesiFromCheckboxes($data['tipo_gestione'], $mesiInput); } $gestione->save(); return redirect()->route('admin.stabili.show', $stabile) ->with('success', 'Gestione creata'); } public function update(HttpRequest $request, Stabile $stabile, GestioneContabile $gestione) { $amministratoreId = \Illuminate\Support\Facades\Auth::user()->amministratore->id_amministratore ?? \Illuminate\Support\Facades\Auth::user()->id; if ($stabile->amministratore_id !== $amministratoreId) { abort(403); } if ($gestione->stabile_id !== $stabile->id) { abort(404); } if ($request->get('toggle') === 'stato') { // Toggle stato aperta/chiusa if ($gestione->stato === 'aperta') { $gestione->stato = 'chiusa'; $gestione->gestione_attiva = false; } else { $gestione->stato = 'aperta'; $gestione->gestione_attiva = true; } $gestione->save(); } else { $data = $request->validate([ 'denominazione' => 'nullable|string|max:255', 'percentuale_fondo_riserva' => 'nullable|numeric|min:0|max:100', ]); $gestione->update($data); } return redirect()->route('admin.stabili.show', $stabile) ->with('success', 'Gestione aggiornata'); } public function destroy(Stabile $stabile, GestioneContabile $gestione) { $amministratoreId = \Illuminate\Support\Facades\Auth::user()->amministratore->id_amministratore ?? \Illuminate\Support\Facades\Auth::user()->id; if ($stabile->amministratore_id !== $amministratoreId) { abort(403); } if ($gestione->stabile_id !== $stabile->id) { abort(404); } // Non eliminare se ha operazioni if ($gestione->operazioni()->exists()) { return redirect()->route('admin.stabili.show', $stabile) ->with('error', 'Impossibile eliminare: gestione con operazioni.'); } $gestione->delete(); return redirect()->route('admin.stabili.show', $stabile) ->with('success', 'Gestione eliminata'); } }