fix: mejorar lógica de consulta de logs en excel para sustituciones

This commit is contained in:
Juan Felipe Zapata Moreno 2026-04-04 10:31:49 -06:00
parent e272b60c43
commit 80e5ebc352

View File

@ -326,26 +326,19 @@ public function constanciasSustituidas(Request $request)
$module = $moduleId ? Module::find($moduleId) : null; $module = $moduleId ? Module::find($moduleId) : null;
// Consulta de Logs // Consulta de Logs: solo sustitución
$logs = VehicleTagLog::with(['vehicle', 'tag', 'cancellationReason']) $logs = VehicleTagLog::with(['vehicle', 'tag', 'cancellationReason'])
->where(function ($query) use ($fechaInicio, $fechaFin) { ->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) { $query->where(function ($q) use ($fechaInicio, $fechaFin) {
$q->where('action_type', 'sustitucion') $q->where('action_type', 'sustitucion')
->whereNotNull('cancellation_at') ->whereNotNull('cancellation_at')
->whereBetween('cancellation_at', [$fechaInicio, $fechaFin]); ->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) { ->orWhere(function ($q) use ($fechaInicio, $fechaFin) {
$q->where('action_type', 'sustitucion_primera_vez') $q->where('action_type', 'sustitucion_primera_vez')
->whereNull('cancellation_at')
->whereBetween('created_at', [$fechaInicio, $fechaFin]); ->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) { ->when($moduleId, function ($query) use ($moduleId) {
@ -362,8 +355,8 @@ public function constanciasSustituidas(Request $request)
// PREPARACIÓN DE DATOS // PREPARACIÓN DE DATOS
$data = $logs->map(function ($log) { $data = $logs->map(function ($log) {
// sustitucion_primera_vez activo (sin cancellation_at): el log mismo ES el nuevo TAG asignado // sustitucion_primera_vez: el log mismo ES el nuevo TAG asignado (no hay folio anterior)
if ($log->action_type === 'sustitucion_primera_vez' && is_null($log->cancellation_at)) { if ($log->action_type === 'sustitucion_primera_vez') {
return [ return [
'niv' => $log->vehicle->niv ?? '', 'niv' => $log->vehicle->niv ?? '',
'nrpv' => $log->vehicle->nrpv ?? '', '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') $newTagLog = VehicleTagLog::with('tag')
->where('vehicle_id', $log->vehicle_id) ->where('vehicle_id', $log->vehicle_id)
->whereIn('action_type', ['sustitucion_primera_vez', 'sustitucion']) ->whereIn('action_type', ['sustitucion_primera_vez', 'sustitucion'])