netgescon-day0/app/Console/Commands/GesconResyncStabili.php

103 lines
3.5 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Console\Commands;
use App\Models\GesconImportMapping;
use App\Models\User;
use App\Services\QuickImport\QuickStabiliImporter;
use Illuminate\Console\Command;
use Illuminate\Support\Arr;
class GesconResyncStabili extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'gescon:resync-stabili
{--legacy=* : Limita la sincronizzazione ai codici legacy indicati}
{--tenant= : Forza un tenant_id specifico (fallback "default")}
{--dry : Esegue solo una simulazione senza aggiornare il database}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Reimporta/aggiorna gli stabili NetGescon usando il mapping GESCON salvato, mantenendo sincronizzati i dati catasto e bancari principali.';
public function handle(): int
{
$legacyFilter = array_filter(Arr::wrap($this->option('legacy')));
$dry = (bool) $this->option('dry');
$forcedTenant = $this->option('tenant');
$query = GesconImportMapping::query()
->whereNotNull('legacy_code')
->where('legacy_code', '!=', '');
if (!empty($legacyFilter)) {
$query->whereIn('legacy_code', $legacyFilter);
}
$mappings = $query
->orderBy('legacy_code')
->get()
->unique('legacy_code');
if ($mappings->isEmpty()) {
$this->warn('Nessun mapping disponibile per la sincronizzazione.');
return Command::SUCCESS;
}
$this->info(sprintf('Sincronizzazione stabili (%d) %s', $mappings->count(), $dry ? '(DRY-RUN)' : ''));
$updated = 0;
$created = 0;
foreach ($mappings as $mapping) {
$legacyCode = (string) $mapping->legacy_code;
if ($legacyCode === '' || $legacyCode === '*') {
$this->line('• [*] mapping globale ignorato');
continue;
}
$meta = (array) ($mapping->meta ?? []);
$user = User::find($mapping->user_id);
$tenantId = $forcedTenant ?? ($user->tenant_id ?? 'default');
$importer = new QuickStabiliImporter((string) $tenantId);
if ($dry) {
$this->line(sprintf('• [%s] DRY-RUN → salterei import (tenant "%s")', $legacyCode, $tenantId));
continue;
}
try {
$result = $importer->import($legacyCode, $meta);
if (!($result['ok'] ?? false)) {
$this->error(sprintf('× [%s] Import fallito: %s', $legacyCode, $result['error'] ?? 'errore sconosciuto'));
continue;
}
$created += (int) ($result['created'] ?? 0);
$updated += (int) ($result['updated'] ?? 0);
$stabileId = $result['stabile_id'] ?? null;
$this->line(sprintf(
'✓ [%s] stabile #%s: created=%d updated=%d',
$legacyCode,
$stabileId ?: '—',
(int) ($result['created'] ?? 0),
(int) ($result['updated'] ?? 0)
));
} catch (\Throwable $e) {
$this->error(sprintf('× [%s] Eccezione: %s', $legacyCode, $e->getMessage()));
}
}
if (!$dry) {
$this->info(sprintf('Completato. Nuovi stabili: %d · Aggiornati: %d', $created, $updated));
}
return Command::SUCCESS;
}
}