Add supplier archive provisioning command
This commit is contained in:
parent
c114090882
commit
1e132db408
81
app/Console/Commands/ProvisionFornitoriArchives.php
Normal file
81
app/Console/Commands/ProvisionFornitoriArchives.php
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\Fornitore;
|
||||
use App\Support\ArchivioPaths;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class ProvisionFornitoriArchives extends Command
|
||||
{
|
||||
protected $signature = 'netgescon:provision-fornitori-archives
|
||||
{fornitore_id? : ID singolo fornitore}
|
||||
{--admin-id= : Limita ai fornitori di uno specifico amministratore}
|
||||
{--all : Esegue su tutti i fornitori}
|
||||
{--legacy-too : Mostra anche i path legacy annidati sotto amministratori}';
|
||||
|
||||
protected $description = 'Crea o riallinea le cartelle archivio dei fornitori esistenti.';
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
$query = Fornitore::query()->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;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user