From 1e132db4085e15b453b0afb31d11ae8c83cb23ca Mon Sep 17 00:00:00 2001 From: michele Date: Tue, 14 Apr 2026 22:00:33 +0000 Subject: [PATCH] Add supplier archive provisioning command --- .../Commands/ProvisionFornitoriArchives.php | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 app/Console/Commands/ProvisionFornitoriArchives.php diff --git a/app/Console/Commands/ProvisionFornitoriArchives.php b/app/Console/Commands/ProvisionFornitoriArchives.php new file mode 100644 index 0000000..fc3ae61 --- /dev/null +++ b/app/Console/Commands/ProvisionFornitoriArchives.php @@ -0,0 +1,81 @@ +with('amministratore:id,codice_amministratore,codice_univoco'); + + $fornitoreId = $this->argument('fornitore_id'); + $adminId = (int) $this->option('admin-id'); + + if ($fornitoreId !== null) { + $query->whereKey((int) $fornitoreId); + } elseif ($adminId > 0) { + $query->where('amministratore_id', $adminId); + } elseif (! $this->option('all')) { + $this->error('Specifica fornitore_id, oppure --admin-id=..., oppure usa --all.'); + + return self::FAILURE; + } + + $processed = 0; + $created = 0; + + $query->orderBy('amministratore_id')->orderBy('id')->chunkById(100, function ($fornitori) use (&$processed, &$created): void { + foreach ($fornitori as $fornitore) { + if (! $fornitore instanceof Fornitore) { + continue; + } + + $processed++; + + $canonicalBase = ArchivioPaths::fornitoreBase($fornitore); + $legacyBase = ArchivioPaths::fornitoreLegacyBase($fornitore); + + if (! is_string($canonicalBase) || $canonicalBase === '') { + $this->warn('Saltato fornitore #' . (int) $fornitore->id . ': codice archivio non disponibile.'); + continue; + } + + $before = Storage::disk('local')->exists($canonicalBase); + $fornitore->ensureArchiveStructure(); + $after = Storage::disk('local')->exists($canonicalBase); + + if (! $before && $after) { + $created++; + } + + $message = '#'. (int) $fornitore->id + . ' ' . trim((string) ($fornitore->ragione_sociale ?: ('Fornitore ' . $fornitore->id))) + . ' -> ' . storage_path('app/' . $canonicalBase) + . ' [' . ($after ? ($before ? 'gia_esistente' : 'creata') : 'non_creata') . ']'; + + $this->line($message); + + if ($this->option('legacy-too') && is_string($legacyBase) && $legacyBase !== '') { + $this->line(' legacy: ' . storage_path('app/' . $legacyBase) . ' [' . (Storage::disk('local')->exists($legacyBase) ? 'presente' : 'assente') . ']'); + } + } + }); + + $this->info('Provisioning completato. Fornitori processati: ' . $processed . '. Root canonici creati ora: ' . $created . '.'); + + return self::SUCCESS; + } +} \ No newline at end of file