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

55 lines
1.7 KiB
PHP
Executable File

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\UnitaImmobiliare;
class GesconCleanupUnitaZero extends Command
{
protected $signature = 'gescon:cleanup-unita-zero {--apply : Esegue la cancellazione (soft-delete). Senza flag è solo report}';
protected $description = 'Rimuove SOLO unità senza codice e senza interno (soft delete). Non tocca cantine/box/magazzini con interno 0.';
public function handle(): int
{
$apply = (bool)$this->option('apply');
// Candidati: unità che non hanno né codice né interno valorizzato
$query = UnitaImmobiliare::where(function ($q) {
$q->whereNull('codice_unita')
->orWhereIn('codice_unita', ['0', '', '0 ', ' 0', '0.0'])
->orWhere('codice_unita', 'like', '%-0');
})->where(function ($q) {
$q->whereNull('interno')
->orWhereIn('interno', ['0', 0, '', '0 ', ' 0']);
})->whereNotIn('tipo_unita', [
'cantina',
'box',
'garage',
'magazzino',
'deposito',
'posto_auto',
]);
$count = $query->count();
if ($count === 0) {
$this->info('Nessuna unità con codice_unita nullo/0 trovata.');
return 0;
}
$this->info(($apply ? 'Cancellazione' : 'Report') . " su {$count} unità.");
if (!$apply) {
return 0;
}
$query->chunkById(200, function ($chunk) {
foreach ($chunk as $unit) {
$unit->delete();
}
});
$this->info('Cancellazione completata (soft delete).');
return 0;
}
}