File: /var/www/api-talleres.bradford/app/Models/ExcelUploadLog.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class ExcelUploadLog extends Model
{
protected $table = 'excel_upload_logs';
public $timestamps = false; // Solo tiene created_at
protected $fillable = [
'upload_id',
'action',
'description',
'data',
'user_id',
'created_at',
];
protected $casts = [
'data' => 'array',
'created_at' => 'datetime',
];
/**
* Relación con el upload
*/
public function upload(): BelongsTo
{
return $this->belongsTo(ExcelUpload::class, 'upload_id');
}
/**
* Usuario que realizó la acción
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* Scope para obtener logs de una acción específica
*/
public function scopeAction($query, string $action)
{
return $query->where('action', $action);
}
/**
* Scope para obtener logs recientes
*/
public function scopeRecent($query, int $days = 7)
{
return $query->where('created_at', '>=', now()->subDays($days));
}
/**
* Scope para obtener logs de un usuario
*/
public function scopeByUser($query, int $userId)
{
return $query->where('user_id', $userId);
}
/**
* Scope para ordenar por más reciente
*/
public function scopeLatest($query)
{
return $query->orderBy('created_at', 'desc');
}
/**
* Crear log de creación
*/
public static function logCreated(int $uploadId, int $userId, array $data = []): self
{
return self::create([
'upload_id' => $uploadId,
'action' => 'CREATED',
'description' => 'Carga creada',
'data' => $data,
'user_id' => $userId,
'created_at' => now(),
]);
}
/**
* Crear log de inicio de procesamiento
*/
public static function logProcessingStarted(int $uploadId, int $userId, array $data = []): self
{
return self::create([
'upload_id' => $uploadId,
'action' => 'PROCESSING_STARTED',
'description' => 'Procesamiento iniciado',
'data' => $data,
'user_id' => $userId,
'created_at' => now(),
]);
}
/**
* Crear log de procesamiento completado
*/
public static function logProcessingCompleted(int $uploadId, int $userId, array $data = []): self
{
return self::create([
'upload_id' => $uploadId,
'action' => 'PROCESSING_COMPLETED',
'description' => 'Procesamiento completado',
'data' => $data,
'user_id' => $userId,
'created_at' => now(),
]);
}
/**
* Crear log de línea editada
*/
public static function logLineEdited(int $uploadId, int $userId, int $lineNumber, array $data = []): self
{
return self::create([
'upload_id' => $uploadId,
'action' => 'LINE_EDITED',
'description' => "Línea #{$lineNumber} editada",
'data' => array_merge(['line_number' => $lineNumber], $data),
'user_id' => $userId,
'created_at' => now(),
]);
}
/**
* Crear log de confirmación
*/
public static function logConfirmed(int $uploadId, int $userId, array $data = []): self
{
return self::create([
'upload_id' => $uploadId,
'action' => 'CONFIRMED',
'description' => 'Carga confirmada y registros creados',
'data' => $data,
'user_id' => $userId,
'created_at' => now(),
]);
}
/**
* Crear log de error
*/
public static function logError(int $uploadId, int $userId, string $errorMessage, array $data = []): self
{
return self::create([
'upload_id' => $uploadId,
'action' => 'ERROR_OCCURRED',
'description' => $errorMessage,
'data' => $data,
'user_id' => $userId,
'created_at' => now(),
]);
}
/**
* Crear log de cancelación
*/
public static function logCancelled(int $uploadId, int $userId, string $reason = '', array $data = []): self
{
return self::create([
'upload_id' => $uploadId,
'action' => 'CANCELLED',
'description' => $reason ?: 'Carga cancelada',
'data' => $data,
'user_id' => $userId,
'created_at' => now(),
]);
}
/**
* Obtener resumen de la acción
*/
public function getSummary(): string
{
$userName = $this->user ? $this->user->name : 'Sistema';
$date = $this->created_at->format('d/m/Y H:i');
return "{$this->description} por {$userName} el {$date}";
}
/**
* Verificar si es una acción de error
*/
public function isError(): bool
{
return $this->action === 'ERROR_OCCURRED';
}
/**
* Verificar si es una acción de éxito
*/
public function isSuccess(): bool
{
return in_array($this->action, ['PROCESSING_COMPLETED', 'CONFIRMED']);
}
}