add: historial de tags de un vehiculo

This commit is contained in:
Juan Felipe Zapata Moreno 2026-01-14 12:45:13 -06:00
parent 4f9b4a6098
commit 2ca6950751

View File

@ -403,25 +403,38 @@ public function searchRecord(Request $request)
$paginatedRecords->getCollection()->transform(function ($record) {
$latestLog = $record->vehicle->vehicleTagLogs->first();
// Detectar si hubo sustitución y obtener datos del tag anterior
$substitutionData = null;
// Construir historial completo de tags
$tagsHistory = [];
$order = 1;
$vehicleLogs = $record->vehicle->vehicleTagLogs->sortBy('created_at');
$processedTags = [];
// Buscar si existe ALGÚN log de sustitución (no solo el último)
$substitutionLogs = $record->vehicle->vehicleTagLogs
->where('action_type', 'sustitucion')
->sortBy('id')
->take(2);
foreach ($vehicleLogs as $log) {
$tagId = $log->tag_id;
if ($tagId && !in_array($tagId, $processedTags)) {
$processedTags[] = $tagId;
$tag = $log->tag;
if ($substitutionLogs->count() >= 2) {
$oldTagLog = $substitutionLogs->first(); // Tag cancelado
$newTagLog = $substitutionLogs->last(); // Tag nuevo
// Buscar fecha de cancelación si existe
$cancelLog = $vehicleLogs
->where('tag_id', $tagId)
->whereIn('action_type', ['cancelacion', 'sustitucion'])
->whereNotNull('cancellation_at')
->first();
$substitutionData = [
'old_folio' => $oldTagLog->tag?->folio ?? null,
'old_tag_number' => $oldTagLog->tag?->tag_number ?? null,
'new_folio' => $newTagLog->tag?->folio ?? $record->folio,
'new_tag_number' => $newTagLog->tag?->tag_number ?? null,
];
$tagsHistory[] = [
'order' => $order++,
'tag_id' => $tagId,
'folio' => $tag?->folio,
'tag_number' => $tag?->tag_number,
'status' => $tag?->status?->code ?? 'unknown',
'assigned_at' => $vehicleLogs->where('tag_id', $tagId)
->whereIn('action_type', ['inscripcion', 'sustitucion'])
->first()?->created_at,
'cancelled_at' => $cancelLog?->cancellation_at,
'is_current' => $tag?->id === $record->vehicle->tag?->id,
];
}
}
return [
@ -433,8 +446,9 @@ public function searchRecord(Request $request)
'action_type' => $latestLog?->action_type ?? 'inscripcion',
'action_date' => $latestLog?->created_at ?? $record->created_at,
// SUSTITUCIÓN
'substitution' => $substitutionData,
// HISTORIAL DE TAGS
'tags_history' => $tagsHistory,
'total_tags' => count($tagsHistory),
// MÓDULO
'module' => $record->module ? [