fix: mejorar lógica de consulta de logs en excel para sustituciones
This commit is contained in:
parent
e272b60c43
commit
80e5ebc352
@ -326,26 +326,19 @@ public function constanciasSustituidas(Request $request)
|
||||
|
||||
$module = $moduleId ? Module::find($moduleId) : null;
|
||||
|
||||
// Consulta de Logs
|
||||
// Consulta de Logs: solo sustitución
|
||||
$logs = VehicleTagLog::with(['vehicle', 'tag', 'cancellationReason'])
|
||||
->where(function ($query) use ($fechaInicio, $fechaFin) {
|
||||
// sustitucion normal: el log del TAG viejo tiene cancellation_at
|
||||
// sustitucion: tag viejo cancelado o inscripción con folio previo en REPUVE
|
||||
$query->where(function ($q) use ($fechaInicio, $fechaFin) {
|
||||
$q->where('action_type', 'sustitucion')
|
||||
->whereNotNull('cancellation_at')
|
||||
->whereBetween('cancellation_at', [$fechaInicio, $fechaFin]);
|
||||
})
|
||||
// sustitucion_primera_vez activo: un solo log sin cancellation_at, filtrar por created_at
|
||||
// sustitucion_primera_vez: primera asignación, sin folio previo en REPUVE
|
||||
->orWhere(function ($q) use ($fechaInicio, $fechaFin) {
|
||||
$q->where('action_type', 'sustitucion_primera_vez')
|
||||
->whereNull('cancellation_at')
|
||||
->whereBetween('created_at', [$fechaInicio, $fechaFin]);
|
||||
})
|
||||
// sustitucion_primera_vez cancelado: fue el tag viejo en una sustitución posterior
|
||||
->orWhere(function ($q) use ($fechaInicio, $fechaFin) {
|
||||
$q->where('action_type', 'sustitucion_primera_vez')
|
||||
->whereNotNull('cancellation_at')
|
||||
->whereBetween('cancellation_at', [$fechaInicio, $fechaFin]);
|
||||
});
|
||||
})
|
||||
->when($moduleId, function ($query) use ($moduleId) {
|
||||
@ -362,8 +355,8 @@ public function constanciasSustituidas(Request $request)
|
||||
|
||||
// PREPARACIÓN DE DATOS
|
||||
$data = $logs->map(function ($log) {
|
||||
// sustitucion_primera_vez activo (sin cancellation_at): el log mismo ES el nuevo TAG asignado
|
||||
if ($log->action_type === 'sustitucion_primera_vez' && is_null($log->cancellation_at)) {
|
||||
// sustitucion_primera_vez: el log mismo ES el nuevo TAG asignado (no hay folio anterior)
|
||||
if ($log->action_type === 'sustitucion_primera_vez') {
|
||||
return [
|
||||
'niv' => $log->vehicle->niv ?? '',
|
||||
'nrpv' => $log->vehicle->nrpv ?? '',
|
||||
@ -378,7 +371,7 @@ public function constanciasSustituidas(Request $request)
|
||||
];
|
||||
}
|
||||
|
||||
// sustitucion normal: este log es el TAG viejo cancelado, buscar el nuevo TAG
|
||||
// sustitucion: este log es el TAG viejo cancelado, buscar el nuevo TAG
|
||||
$newTagLog = VehicleTagLog::with('tag')
|
||||
->where('vehicle_id', $log->vehicle_id)
|
||||
->whereIn('action_type', ['sustitucion_primera_vez', 'sustitucion'])
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user