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

71 lines
2.4 KiB
PHP
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\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;
}
}