make(\Illuminate\Contracts\Console\Kernel::class); $kernel->bootstrap(); use App\Services\GesconImport\LegacyArchiveService; use App\Models\UserSetting; $legacy = $argv[1] ?? null; $rawAnnoOrFolder = $argv[2] ?? null; $anno = null; if ($rawAnnoOrFolder !== null) { if (ctype_digit($rawAnnoOrFolder) && strlen($rawAnnoOrFolder) === 4 && (int)$rawAnnoOrFolder >= 1900) { $anno = (int)$rawAnnoOrFolder; } else { // keep folder string like '0001' $anno = $rawAnnoOrFolder; // will be resolved by service } } $limit = isset($argv[3]) ? (int)$argv[3] : 20; $hasMdbExport = trim((string)@shell_exec('command -v mdb-export')) !== ''; $hasMdbTables = trim((string)@shell_exec('command -v mdb-tables')) !== ''; $base = UserSetting::get('gescon.source_path', '/mnt/gescon-archives/gescon'); fwrite(STDERR, "[diag] mdb-export=" . ($hasMdbExport ? 'yes' : 'no') . " mdb-tables=" . ($hasMdbTables ? 'yes' : 'no') . " base={$base}\n"); if (!$legacy) { // Prova a inferire legacy dal primo stabile con anni disponibili su filesystem if (is_dir($base)) { foreach (scandir($base) as $c) { if ($c === '.' || $c === '..') continue; $full = rtrim($base, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $c; if (!is_dir($full)) continue; // cerca anni con singolo_anno.mdb $found = false; $foundAnno = null; foreach (scandir($full) as $yy) { if (!preg_match('/^\d{4}$/', $yy)) continue; $sa = $full . DIRECTORY_SEPARATOR . $yy . DIRECTORY_SEPARATOR . 'singolo_anno.mdb'; if (is_file($sa)) { $found = true; $foundAnno = (int)$yy; break; } } if ($found) { $legacy = $c; $anno = $anno ?: $foundAnno; break; } } } else { fwrite(STDERR, "[diag] base path non esistente: {$base}\n"); } } if (!$legacy) { // Fallback: prendi il primo cod_stabile disponibile dallo staging (condomin) try { if (\Illuminate\Support\Facades\Schema::connection('gescon_import')->hasTable('condomin')) { $row = \Illuminate\Support\Facades\DB::connection('gescon_import') ->table('condomin')->select('cod_stabile')->whereNotNull('cod_stabile') ->orderBy('cod_stabile')->limit(1)->first(); if ($row && isset($row->cod_stabile)) { $legacy = (string)$row->cod_stabile; } } } catch (\Throwable $e) { } } if (!$legacy) { fwrite(STDERR, "legacy_code non fornito e non deducibile. Uso: php scripts/smoke_unita.php [anno] [limit]\n"); exit(2); } $svc = new LegacyArchiveService(); if (!$anno) { $years = $svc->scanLegacyYears($legacy); $anno = !empty($years) ? (int)end($years) : null; } fwrite(STDERR, "[diag] tentative legacy={$legacy} anno=" . ($anno ?: 'null') . " limit={$limit}\n"); // Probe gestioni for resolution and diagnostics $gest = $svc->gestioniYear($legacy, $anno ?? $rawAnnoOrFolder); if (($gest['ok'] ?? false)) { $meta = $gest['meta'] ?? []; $resolvedAnno = $gest['anno'] ?? null; $resolvedFolder = $meta['folder'] ?? null; $period = $meta['period'] ?? null; fwrite(STDERR, "[diag] resolved folder=" . ($resolvedFolder ?? 'null') . " anno=" . ($resolvedAnno ?? 'null') . ($period ? ' periodo=' . (json_encode($period)) : '') . "\n"); $o = $gest['ordinaria'] ?? null; $r = $gest['riscaldamento'] ?? null; if ($o || $r) { fwrite(STDERR, "[diag] gestioni: "); if ($o) fwrite(STDERR, "ORD(stato=" . ($o['stato'] ?? '') . " rate=" . count(($o['rate'] ?? [])) . ") "); if ($r) fwrite(STDERR, "RISC(stato=" . ($r['stato'] ?? '') . " rate=" . count(($r['rate'] ?? [])) . ") "); fwrite(STDERR, "\n"); } if ($resolvedAnno) { $anno = $resolvedAnno; } } $result = $svc->unitaYear($legacy, $anno ?? $rawAnnoOrFolder, $limit); $ok = $result['ok'] ?? false; if (!$ok) { fwrite(STDERR, "Errore: " . (($result['error'] ?? 'sconosciuto')) . "\n"); // prova fallback: anno nullo per staging $res2 = $svc->unitaYear($legacy, null, $limit); if ($res2['ok'] ?? false) { $result = $res2; $ok = true; fwrite(STDERR, "Fallback staging riuscito.\n"); } } if ($ok) { echo "OK legacy={$legacy} anno=" . ($result['anno'] ?? 'null') . " limit={$limit}\n"; $headers = $result['headers'] ?? []; $rows = $result['rows'] ?? []; echo "Headers (" . count($headers) . "): " . implode(', ', $headers) . "\n"; echo "Rows: " . count($rows) . "\n"; foreach (array_slice($rows, 0, min(3, count($rows))) as $i => $r) { echo "Row " . ($i + 1) . ": " . json_encode($r, JSON_UNESCAPED_UNICODE) . "\n"; } if (isset($result['staging_connection'])) { echo "Staging: conn=" . $result['staging_connection'] . " table=" . ($result['staging_table'] ?? '') . "\n"; } exit(0); } else { exit(1); }