52 lines
1.4 KiB
PHP
52 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
class CleanupOldEvents extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'app:cleanup-old-events';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Elimina eventos y QRs antiguos según la configuración del sistema.';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle()
|
|
{
|
|
$dias = \App\Models\Configuracion::get('dias_expiracion_eventos', 30);
|
|
$fechaLimite = now()->subDays((int)$dias);
|
|
|
|
$eventosAEliminar = \App\Models\Evento::withTrashed()
|
|
->where('fecha_evento', '<', $fechaLimite->toDateString())
|
|
->get();
|
|
$total = $eventosAEliminar->count();
|
|
|
|
if ($total === 0) {
|
|
$this->info("No hay eventos antiguos para eliminar.");
|
|
return;
|
|
}
|
|
|
|
foreach ($eventosAEliminar as $evento) {
|
|
/** @var \App\Models\Evento $evento */
|
|
// Eliminar QRs asociados
|
|
$evento->qrCodes()->delete();
|
|
// Ya no eliminamos el evento para mantener registro de puntos y goleadores
|
|
// $evento->delete();
|
|
}
|
|
|
|
$this->info("Se han limpiado los QRs de $total eventos antiguos (Antigüedad > $dias días). Los eventos permanecen en el sistema.");
|
|
}
|
|
}
|