147 lines
5.9 KiB
PHP
147 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\RubricaUniversale;
|
|
use App\Support\StabileContext;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Artisan;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
class GesconSyncAnagraficaConsolidata extends Command
|
|
{
|
|
protected $signature = 'gescon:sync-anagrafica-consolidata
|
|
{amministratore_id=12 : ID dell\'amministratore a cui associare i contatti}
|
|
{--mdb=/mnt/gescon-archives/gescon/dbc/Fornitori.mdb : Percorso file Fornitori.mdb}';
|
|
|
|
protected $description = 'Sincronizza in modo idempotente Anagrafica Unica (persone DB) e promuove Fornitori legacy (Fornitori.mdb)';
|
|
|
|
public function handle(): int
|
|
{
|
|
$adminId = (int) $this->argument('amministratore_id');
|
|
$mdb = (string) $this->option('mdb');
|
|
|
|
$this->info("Inizio sincronizzazione Anagrafica Unica e Fornitori per Amministratore ID {$adminId}...");
|
|
|
|
// 1. Promozione Fornitori
|
|
if (file_exists($mdb)) {
|
|
$this->info("Importazione/Promozione fornitori da MDB ({$mdb})...");
|
|
Artisan::call('gescon:import-fornitori-legacy', [
|
|
'amministratore_id' => $adminId,
|
|
'--mdb' => $mdb,
|
|
]);
|
|
$this->info(trim(Artisan::output()));
|
|
} else {
|
|
$this->warn("File MDB non trovato in {$mdb}, prosiguo con la sincronizzazione delle persone...");
|
|
}
|
|
|
|
// 2. Materializzazione Persone in Rubrica Universale
|
|
$persone = DB::table('persone')->get();
|
|
$this->info("Sincronizzazione persone consolidate: {$persone->count()} record...");
|
|
|
|
$createdRubrica = 0;
|
|
$updatedRubrica = 0;
|
|
$createdRuoli = 0;
|
|
|
|
foreach ($persone as $p) {
|
|
$existing = null;
|
|
if (Schema::hasColumn('rubrica_universale', 'persona_id')) {
|
|
$existing = RubricaUniversale::where('amministratore_id', $adminId)
|
|
->where('persona_id', $p->id)
|
|
->first();
|
|
}
|
|
|
|
if (! $existing && ! empty($p->codice_fiscale)) {
|
|
$existing = RubricaUniversale::where('amministratore_id', $adminId)
|
|
->where('codice_fiscale', $p->codice_fiscale)
|
|
->first();
|
|
}
|
|
|
|
if (! $existing && ! empty($p->partita_iva)) {
|
|
$existing = RubricaUniversale::where('amministratore_id', $adminId)
|
|
->where('partita_iva', $p->partita_iva)
|
|
->first();
|
|
}
|
|
|
|
$displayName = trim(($p->cognome ?? '') . ' ' . ($p->nome ?? ''));
|
|
if ($displayName === '' && ! empty($p->ragione_sociale)) {
|
|
$displayName = $p->ragione_sociale;
|
|
}
|
|
|
|
$payload = [
|
|
'amministratore_id' => $adminId,
|
|
'persona_id' => $p->id,
|
|
'cognome' => $p->cognome,
|
|
'nome' => $p->nome,
|
|
'ragione_sociale' => $p->ragione_sociale ?: ($displayName !== '' ? $displayName : 'Persona Consolidata #' . $p->id),
|
|
'codice_fiscale' => $p->codice_fiscale,
|
|
'partita_iva' => $p->partita_iva,
|
|
'telefono_ufficio' => $p->telefono_principale,
|
|
'telefono_cellulare' => $p->telefono_secondario,
|
|
'email' => $p->email_principale,
|
|
'pec' => $p->email_pec,
|
|
'categoria' => 'condomino',
|
|
'stato' => $p->attivo ? 'attivo' : 'inattivo',
|
|
'tipo_contatto' => ($p->tipologia === 'giuridica' || ! empty($p->partita_iva)) ? 'persona_giuridica' : 'persona_fisica',
|
|
'note' => $p->note,
|
|
];
|
|
|
|
if (! Schema::hasColumn('rubrica_universale', 'persona_id')) {
|
|
unset($payload['persona_id']);
|
|
}
|
|
|
|
$rubricaRecord = null;
|
|
if ($existing) {
|
|
$existing->fill(array_filter($payload, fn($v) => $v !== null && $v !== ''));
|
|
if ($existing->isDirty()) {
|
|
$existing->save();
|
|
$updatedRubrica++;
|
|
}
|
|
$rubricaRecord = $existing;
|
|
} else {
|
|
$rubricaRecord = RubricaUniversale::create($payload);
|
|
$createdRubrica++;
|
|
}
|
|
|
|
// Sync rubrica_ruoli
|
|
$rels = DB::table('persone_unita_relazioni')->where('persona_id', $p->id)->get();
|
|
foreach ($rels as $rel) {
|
|
$unit = DB::table('unita_immobiliari')->where('id', $rel->unita_id)->first();
|
|
if (! $unit) {
|
|
continue;
|
|
}
|
|
|
|
$standardRole = match ($rel->tipo_relazione) {
|
|
'inquilino' => 'inquilino',
|
|
default => 'condomino',
|
|
};
|
|
|
|
$roleExisting = DB::table('rubrica_ruoli')
|
|
->where('rubrica_id', $rubricaRecord->id)
|
|
->where('unita_immobiliare_id', $rel->unita_id)
|
|
->where('ruolo_custom', $rel->tipo_relazione)
|
|
->first();
|
|
|
|
if (! $roleExisting) {
|
|
DB::table('rubrica_ruoli')->insert([
|
|
'rubrica_id' => $rubricaRecord->id,
|
|
'stabile_id' => $unit->stabile_id,
|
|
'unita_immobiliare_id' => $rel->unita_id,
|
|
'ruolo_standard' => $standardRole,
|
|
'ruolo_custom' => $rel->tipo_relazione,
|
|
'is_attivo' => $rel->attivo,
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
$createdRuoli++;
|
|
}
|
|
}
|
|
}
|
|
|
|
$this->info("✅ Sincronizzazione anagrafica completata! Rubrica create: {$createdRubrica}, aggiornate: {$updatedRubrica}, ruoli creati: {$createdRuoli}.");
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
}
|