98 lines
4.0 KiB
PHP
98 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class GesconBonificaDuplicatiBenedettoCommand extends Command
|
|
{
|
|
protected $signature = 'gescon:bonifica-duplicati-benedetto';
|
|
|
|
protected $description = 'Bonifica idempotente delle rubriche duplicate di Daniela Benedetto verso la Persona 323 / Rubrica 697 (CF BNDDNL86M58H224O)';
|
|
|
|
public function handle(): int
|
|
{
|
|
$this->info('Starting bonifica idempotente Benedetto Daniela...');
|
|
|
|
$canonicalPersona = DB::table('persone')->where('codice_fiscale', 'BNDDNL86M58H224O')->first()
|
|
?? DB::table('persone')->find(323);
|
|
$canonicalPersonaId = $canonicalPersona?->id ?? 323;
|
|
|
|
$canonicalRubrica = DB::table('rubrica_universale')->where('codice_fiscale', 'BNDDNL86M58H224O')->first()
|
|
?? DB::table('rubrica_universale')->where('codice_univoco', '000000JC')->first()
|
|
?? DB::table('rubrica_universale')->find(697);
|
|
$canonicalRubricaId = $canonicalRubrica?->id ?? 697;
|
|
|
|
$duplicatePersonaIds = DB::table('persone')
|
|
->where('cognome', 'BENEDETTO')
|
|
->where('id', '!=', $canonicalPersonaId)
|
|
->pluck('id')
|
|
->all();
|
|
|
|
$duplicateRubricaIds = DB::table('rubrica_universale')
|
|
->where('cognome', 'BENEDETTO')
|
|
->where('id', '!=', $canonicalRubricaId)
|
|
->pluck('id')
|
|
->all();
|
|
|
|
// 1. Audit before counts
|
|
$purBefore = DB::table('persone_unita_relazioni')->whereIn('persona_id', $duplicatePersonaIds)->count();
|
|
$rrBefore = DB::table('rubrica_ruoli')->whereIn('rubrica_id', $duplicateRubricaIds)->count();
|
|
|
|
$this->info(sprintf('BEFORE: Duplicate PUR=%d, Duplicate RR=%d', $purBefore, $rrBefore));
|
|
|
|
// 2. Remap relations in persone_unita_relazioni
|
|
if (! empty($duplicatePersonaIds)) {
|
|
DB::table('persone_unita_relazioni')
|
|
->whereIn('persona_id', $duplicatePersonaIds)
|
|
->update([
|
|
'persona_id' => $canonicalPersonaId,
|
|
'updated_at' => now(),
|
|
]);
|
|
|
|
DB::table('persone')
|
|
->whereIn('id', $duplicatePersonaIds)
|
|
->update([
|
|
'attivo' => 0,
|
|
'note' => '[ARCHIVIATO_DUPLICATO] Riconfigurato verso Persona ' . $canonicalPersonaId . ' CF BNDDNL86M58H224O',
|
|
'updated_at' => now(),
|
|
]);
|
|
}
|
|
|
|
// 3. Remap roles in rubrica_ruoli & soft archive rubrica
|
|
if (! empty($duplicateRubricaIds)) {
|
|
DB::table('rubrica_ruoli')
|
|
->whereIn('rubrica_id', $duplicateRubricaIds)
|
|
->update([
|
|
'rubrica_id' => $canonicalRubricaId,
|
|
'updated_at' => now(),
|
|
]);
|
|
|
|
DB::table('rubrica_universale')
|
|
->whereIn('id', $duplicateRubricaIds)
|
|
->update([
|
|
'stato' => 'inattivo',
|
|
'note' => '[ARCHIVIATO_DUPLICATO] Soft archived verso Rubrica ' . $canonicalRubricaId . ' (CF BNDDNL86M58H224O)',
|
|
'updated_at' => now(),
|
|
]);
|
|
}
|
|
|
|
// 4. Ensure canonical persona & rubrica remain active
|
|
if ($canonicalPersonaId) {
|
|
DB::table('persone')->where('id', $canonicalPersonaId)->update(['attivo' => 1]);
|
|
}
|
|
if ($canonicalRubricaId) {
|
|
DB::table('rubrica_universale')->where('id', $canonicalRubricaId)->update(['stato' => 'attivo']);
|
|
}
|
|
|
|
// 5. Audit after counts
|
|
$purAfter = DB::table('persone_unita_relazioni')->whereIn('persona_id', $duplicatePersonaIds)->count();
|
|
$rrAfter = DB::table('rubrica_ruoli')->whereIn('rubrica_id', $duplicateRubricaIds)->count();
|
|
|
|
$this->info(sprintf('AUDIT: Remaining Duplicate PUR=%d, Remaining Duplicate RR=%d, Canonical Persona Active=1, Canonical Rubrica Status=attivo', $purAfter, $rrAfter));
|
|
|
|
return Command::SUCCESS;
|
|
}
|
|
}
|