71 lines
2.4 KiB
PHP
71 lines
2.4 KiB
PHP
<?php
|
||
|
||
namespace App\Console\Commands;
|
||
|
||
use App\Models\Amministratore;
|
||
use App\Models\GesconImportMapping;
|
||
use App\Services\GesconImport\EssentialImportService;
|
||
use Illuminate\Console\Command;
|
||
use Illuminate\Support\Arr;
|
||
|
||
class GesconImportBanche extends Command
|
||
{
|
||
protected $signature = 'gescon:import-banche
|
||
{--legacy=* : Uno o più codici legacy da processare (default: tutti)}
|
||
{--path= : Path base degli archivi legacy}
|
||
{--limit= : Limita il numero di stabili}
|
||
{--dry : Simula senza scrivere dati}';
|
||
|
||
protected $description = 'Importa/aggiorna le anagrafiche banche/casse per gli stabili mappati.';
|
||
|
||
public function handle(): int
|
||
{
|
||
$amm = Amministratore::first();
|
||
if (!$amm) {
|
||
$this->error('Nessun amministratore presente: impossibile procedere.');
|
||
return self::FAILURE;
|
||
}
|
||
|
||
$codesFilter = array_values(array_filter(array_map('trim', Arr::wrap($this->option('legacy')))));
|
||
$query = GesconImportMapping::query()
|
||
->whereNotNull('legacy_code')
|
||
->where('legacy_code', '!=', '')
|
||
->orderBy('legacy_code');
|
||
if (!empty($codesFilter)) {
|
||
$query->whereIn('legacy_code', $codesFilter);
|
||
}
|
||
if ($limit = $this->option('limit')) {
|
||
$query->limit((int) $limit);
|
||
}
|
||
$codes = $query->pluck('legacy_code')->unique()->values();
|
||
if ($codes->isEmpty()) {
|
||
$this->warn('Nessuno stabile con legacy_code disponibile.');
|
||
return self::SUCCESS;
|
||
}
|
||
|
||
$svc = new EssentialImportService();
|
||
$path = $this->option('path') ?: null;
|
||
$dry = (bool) $this->option('dry');
|
||
$this->info(sprintf('Import banche per %d stabili %s', $codes->count(), $dry ? '(DRY-RUN)' : ''));
|
||
|
||
$errors = 0;
|
||
foreach ($codes as $legacyCode) {
|
||
try {
|
||
$svc->importBanche($amm, (string) $legacyCode, ['path' => $path, 'dry' => $dry]);
|
||
$this->line(sprintf('✓ [%s] completato', $legacyCode));
|
||
} catch (\Throwable $e) {
|
||
$errors++;
|
||
$this->error(sprintf('× [%s] %s', $legacyCode, $e->getMessage()));
|
||
}
|
||
}
|
||
|
||
if ($errors > 0) {
|
||
$this->warn(sprintf('Import concluso con %d errori.', $errors));
|
||
return self::FAILURE;
|
||
}
|
||
|
||
$this->info('Import banche completato con successo.');
|
||
return self::SUCCESS;
|
||
}
|
||
}
|