feat: Actualizacion sistema SIO Backend
- Nuevo modulo de historial de cambios (ServicioHistorial) - Observer para tracking automatico de cambios en servicios - Correccion de variables auxiliar en ServiciosController - Actualizacion de configuraciones y migraciones - Endpoint para consultar historial de cambios Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
135
app/Observers/ServicioDetObserver.php
Normal file
135
app/Observers/ServicioDetObserver.php
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Models\ServicioDet;
|
||||
use App\Models\ServicioHistorial;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Request;
|
||||
|
||||
class ServicioDetObserver
|
||||
{
|
||||
// Campos que queremos rastrear
|
||||
protected $camposRastreados = [
|
||||
'estatus_servicio_id',
|
||||
'operador_id',
|
||||
'vehiculo_id',
|
||||
'auxiliar_1',
|
||||
'auxiliar_2',
|
||||
'costo_servicio',
|
||||
'tipo_servicio_id',
|
||||
'servicio_id',
|
||||
'aceptado',
|
||||
'observacion',
|
||||
'observacion_atencion_cliente',
|
||||
'requiere_encuesta',
|
||||
'facturado',
|
||||
'fecha_solicitud',
|
||||
'duracion',
|
||||
'cat_motivos_estatus_id'
|
||||
];
|
||||
|
||||
/**
|
||||
* Handle the ServicioDet "created" event.
|
||||
*/
|
||||
public function created(ServicioDet $servicioDet)
|
||||
{
|
||||
$usuarioId = Auth::check() ? Auth::user()->id : null;
|
||||
|
||||
if ($usuarioId) {
|
||||
ServicioHistorial::create([
|
||||
'servicio_det_id' => $servicioDet->id,
|
||||
'usuario_id' => $usuarioId,
|
||||
'campo' => 'servicio',
|
||||
'valor_anterior' => null,
|
||||
'valor_nuevo' => 'Servicio creado',
|
||||
'accion' => 'crear',
|
||||
'ip_address' => Request::ip()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the ServicioDet "updating" event.
|
||||
*/
|
||||
public function updating(ServicioDet $servicioDet)
|
||||
{
|
||||
$usuarioId = Auth::check() ? Auth::user()->id : null;
|
||||
|
||||
if (!$usuarioId) {
|
||||
return;
|
||||
}
|
||||
|
||||
$cambios = $servicioDet->getDirty();
|
||||
$original = $servicioDet->getOriginal();
|
||||
|
||||
foreach ($cambios as $campo => $valorNuevo) {
|
||||
if (in_array($campo, $this->camposRastreados)) {
|
||||
$valorAnterior = $original[$campo] ?? null;
|
||||
|
||||
// Convertir IDs a nombres legibles
|
||||
$valorAnteriorLegible = $this->obtenerValorLegible($campo, $valorAnterior);
|
||||
$valorNuevoLegible = $this->obtenerValorLegible($campo, $valorNuevo);
|
||||
|
||||
ServicioHistorial::create([
|
||||
'servicio_det_id' => $servicioDet->id,
|
||||
'usuario_id' => $usuarioId,
|
||||
'campo' => $campo,
|
||||
'valor_anterior' => $valorAnteriorLegible,
|
||||
'valor_nuevo' => $valorNuevoLegible,
|
||||
'accion' => 'actualizar',
|
||||
'ip_address' => Request::ip()
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convierte IDs a valores legibles
|
||||
*/
|
||||
protected function obtenerValorLegible($campo, $valor)
|
||||
{
|
||||
if ($valor === null || $valor === '') {
|
||||
return 'Sin asignar';
|
||||
}
|
||||
|
||||
switch ($campo) {
|
||||
case 'estatus_servicio_id':
|
||||
$estatus = \App\Models\CatEstatuServicio::find($valor);
|
||||
return $estatus ? $estatus->nombre : $valor;
|
||||
|
||||
case 'operador_id':
|
||||
case 'auxiliar_1':
|
||||
case 'auxiliar_2':
|
||||
$usuario = \App\Models\User::find($valor);
|
||||
return $usuario ? $usuario->nombre . ' ' . $usuario->apellido_paterno : $valor;
|
||||
|
||||
case 'vehiculo_id':
|
||||
$vehiculo = \App\Models\Vehiculo::find($valor);
|
||||
return $vehiculo ? $vehiculo->num_economico : $valor;
|
||||
|
||||
case 'tipo_servicio_id':
|
||||
$tipo = \App\Models\CatTipoServicio::find($valor);
|
||||
return $tipo ? $tipo->nombre : $valor;
|
||||
|
||||
case 'servicio_id':
|
||||
$servicio = \App\Models\CatServicio::find($valor);
|
||||
return $servicio ? $servicio->nombre : $valor;
|
||||
|
||||
case 'aceptado':
|
||||
case 'requiere_encuesta':
|
||||
case 'facturado':
|
||||
return $valor ? 'Sí' : 'No';
|
||||
|
||||
case 'costo_servicio':
|
||||
return '$' . number_format($valor, 2);
|
||||
|
||||
case 'cat_motivos_estatus_id':
|
||||
$motivo = \App\Models\CatMotivoEstatus::find($valor);
|
||||
return $motivo ? $motivo->nombre : $valor;
|
||||
|
||||
default:
|
||||
return $valor;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user