netgescon-day0/app/Http/Controllers/Admin/AnagraficaDemoController.php

178 lines
8.4 KiB
PHP
Executable File

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class AnagraficaDemoController extends Controller
{
public function show(Request $request)
{
$stagingDisponibile = Schema::connection('gescon_import')->hasTable('condomin');
$subject = null;
$condominium = null;
$unitAssociations = [];
$indicators = [];
$tasks = collect();
$sidebarStats = collect();
$actions = collect();
$alerts = [];
if ($stagingDisponibile) {
$query = DB::connection('gescon_import')->table('condomin');
if ($request->filled('cod_cond')) {
$query->where('cod_cond', $request->get('cod_cond'));
}
$record = $query->orderBy('cod_cond')->first();
if ($record) {
$fullName = trim(trim((string)($record->cognome ?? '')) . ' ' . trim((string)($record->nome ?? '')));
$subject = [
'id' => $record->cod_cond ?? null,
'name' => $fullName !== '' ? $fullName : 'Soggetto senza nome',
'code' => (string)($record->cod_cond ?? 'N/D'),
'roles' => array_values(array_filter([(string)($record->tipo ?? $record->tipo_sogg ?? '')])),
'status' => 'Importato (staging)',
'phone' => $record->telefono ?? $record->cellulare ?? null,
'email' => $record->email ?? $record->mail ?? null,
'codice_fiscale' => $record->codice_fiscale ?? null,
'partita_iva' => $record->partita_iva ?? null,
'documento' => $record->documento_identita ?? null,
];
$unitAssociations[] = [
'label' => trim(
($record->scala ? 'Scala ' . $record->scala : '') .
(($record->scala && $record->interno) ? ' · ' : '') .
($record->interno ? 'Interno ' . $record->interno : '')
) ?: ($record->cod_unita ?? $record->cod_cond ?? 'Unità'),
'millesimi' => $record->millesimi_generali ?? $record->millesimi_risc ?? null,
'tipo' => $record->tipo ?? $record->tipo_sogg ?? null,
'note' => $record->note ?? null,
'piano' => $record->piano ?? null,
'uso' => $record->tipo_unita ?? null,
];
$colUnita = Schema::connection('gescon_import')->hasColumn('condomin', 'cod_unita') ? 'cod_unita' : 'cod_cond';
$totUnitaStabile = DB::connection('gescon_import')->table('condomin')
->where('cod_stabile', $record->cod_stabile)
->distinct($colUnita)
->count($colUnita);
$condominium = [
'name' => $record->denominazione_stabile ?? ('Stabile ' . ($record->cod_stabile ?? 'N/D')),
'code' => $record->cod_stabile ?? 'N/D',
'address' => trim(($record->via ?? '') . ' ' . ($record->citta ?? '')) ?: null,
'units' => $totUnitaStabile > 0 ? $totUnitaStabile : null,
];
$indicators = [
['label' => 'Unità collegate', 'value' => count($unitAssociations), 'icon' => 'fa-building', 'tone' => 'indigo'],
['label' => 'Stabile', 'value' => $condominium['code'] ?? 'N/D', 'icon' => 'fa-home', 'tone' => 'emerald'],
['label' => 'Stato anagrafica', 'value' => $subject['status'], 'icon' => 'fa-shield-check', 'tone' => 'emerald'],
['label' => 'Email', 'value' => $subject['email'] ?? '—', 'icon' => 'fa-envelope', 'tone' => 'cyan'],
];
$sidebarStats = collect([
['label' => 'Stabile', 'value' => $condominium['code'] ?? 'N/D', 'hint' => $condominium['name'] ?? null, 'tone' => 'info'],
['label' => 'Telefono', 'value' => $subject['phone'] ?? '—', 'hint' => 'Recapito importato', 'tone' => 'warning'],
['label' => 'Email', 'value' => $subject['email'] ?? '—', 'hint' => 'Dato da staging', 'tone' => 'success'],
]);
$tasks = collect([
['title' => 'Verifica codice fiscale importato', 'due' => 'Oggi', 'priority' => 'high'],
['title' => 'Completa dati di contatto', 'due' => 'Domani', 'priority' => 'medium'],
['title' => 'Collega unità', 'due' => 'Questa settimana', 'priority' => 'low'],
]);
$actions = collect([
['label' => 'Apri in Rubrica', 'icon' => 'fa-address-book', 'color' => 'primary'],
['label' => 'Invia email', 'icon' => 'fa-envelope', 'color' => 'success'],
['label' => 'Aggiungi nota CRM', 'icon' => 'fa-note-sticky', 'color' => 'warning'],
]);
} else {
$alerts[] = 'Nessun record disponibile in condomin (staging).';
}
} else {
$alerts[] = 'Tabella condomin non presente nella connessione gescon_import.';
}
// Fallback dati mock per mostrare la UI
if (!$subject) {
$subject = [
'id' => 2149,
'name' => 'Michele Barone',
'code' => 'SUB-2149',
'roles' => ['Proprietario', 'Cliente'],
'status' => 'Attivo',
'phone' => '+39 0639731100',
'email' => 'm.barone@mclink.it',
'codice_fiscale' => 'BRNMHL69M11H501K',
'partita_iva' => '09878630582',
'documento' => 'Patente RM3549466G',
];
}
if (!$condominium) {
$condominium = [
'name' => 'Condominio Via Germanico 12',
'code' => 'COND-0021',
'address' => 'Via Germanico 12, 00192 Roma',
'units' => 42,
];
}
if (empty($unitAssociations)) {
$unitAssociations = [
['label' => 'Scala A · Interno 5', 'millesimi' => 45, 'tipo' => 'Proprietario', 'note' => 'Locale abitativo', 'piano' => '2', 'uso' => 'Abitazione'],
['label' => 'Scala B · Locale Tecnico', 'millesimi' => null, 'tipo' => 'Manutentore', 'note' => 'Locale contatori', 'piano' => 'T', 'uso' => 'Locale tecnico'],
['label' => 'Box 18', 'millesimi' => null, 'tipo' => 'Pertinenza', 'note' => 'Pertinenza collegata a Scala A', 'piano' => '-1', 'uso' => 'Box'],
];
}
if (empty($indicators)) {
$indicators = [
['label' => 'Unità collegate', 'value' => count($unitAssociations), 'icon' => 'fa-building', 'tone' => 'indigo'],
['label' => 'Rate scadute', 'value' => '€ 1.240', 'icon' => 'fa-file-invoice-dollar', 'tone' => 'orange'],
['label' => 'Ticket aperti', 'value' => 1, 'icon' => 'fa-headset', 'tone' => 'cyan'],
['label' => 'Stato anagrafica', 'value' => $subject['status'], 'icon' => 'fa-shield-check', 'tone' => 'emerald'],
];
}
$tabs = [
'overview' => 'Panoramica',
'addresses' => 'Indirizzi & recapiti',
'fiscal' => 'Dati fiscali',
'relations' => 'Relazioni condominiali',
'crm' => 'CRM & attività',
'documents' => 'Documenti',
'channels' => 'Canali digitali',
'financial' => 'Pagamenti & banche',
'rentals' => 'Affitti collegati',
];
$breadcrumbs = [
['label' => 'Dashboard', 'icon' => 'fa-home', 'route' => route('admin.dashboard')],
['label' => 'Anagrafica', 'icon' => 'fa-address-book', 'route' => route('admin.rubrica.index')],
['label' => 'Scheda soggetto', 'icon' => 'fa-id-card', 'route' => null],
];
return view('anagrafica.show', [
'subject' => $subject,
'condominium' => $condominium,
'indicators' => $indicators,
'tabs' => $tabs,
'breadcrumbs' => $breadcrumbs,
'actions' => $actions,
'sidebarStats' => $sidebarStats,
'tasks' => $tasks,
'unitAssociations' => $unitAssociations,
'alerts' => $alerts,
]);
}
}