add: historial de tags de un vehiculo
This commit is contained in:
parent
4f9b4a6098
commit
2ca6950751
@ -403,25 +403,38 @@ public function searchRecord(Request $request)
|
|||||||
$paginatedRecords->getCollection()->transform(function ($record) {
|
$paginatedRecords->getCollection()->transform(function ($record) {
|
||||||
$latestLog = $record->vehicle->vehicleTagLogs->first();
|
$latestLog = $record->vehicle->vehicleTagLogs->first();
|
||||||
|
|
||||||
// Detectar si hubo sustitución y obtener datos del tag anterior
|
// Construir historial completo de tags
|
||||||
$substitutionData = null;
|
$tagsHistory = [];
|
||||||
|
$order = 1;
|
||||||
|
$vehicleLogs = $record->vehicle->vehicleTagLogs->sortBy('created_at');
|
||||||
|
$processedTags = [];
|
||||||
|
|
||||||
// Buscar si existe ALGÚN log de sustitución (no solo el último)
|
foreach ($vehicleLogs as $log) {
|
||||||
$substitutionLogs = $record->vehicle->vehicleTagLogs
|
$tagId = $log->tag_id;
|
||||||
->where('action_type', 'sustitucion')
|
if ($tagId && !in_array($tagId, $processedTags)) {
|
||||||
->sortBy('id')
|
$processedTags[] = $tagId;
|
||||||
->take(2);
|
$tag = $log->tag;
|
||||||
|
|
||||||
if ($substitutionLogs->count() >= 2) {
|
// Buscar fecha de cancelación si existe
|
||||||
$oldTagLog = $substitutionLogs->first(); // Tag cancelado
|
$cancelLog = $vehicleLogs
|
||||||
$newTagLog = $substitutionLogs->last(); // Tag nuevo
|
->where('tag_id', $tagId)
|
||||||
|
->whereIn('action_type', ['cancelacion', 'sustitucion'])
|
||||||
|
->whereNotNull('cancellation_at')
|
||||||
|
->first();
|
||||||
|
|
||||||
$substitutionData = [
|
$tagsHistory[] = [
|
||||||
'old_folio' => $oldTagLog->tag?->folio ?? null,
|
'order' => $order++,
|
||||||
'old_tag_number' => $oldTagLog->tag?->tag_number ?? null,
|
'tag_id' => $tagId,
|
||||||
'new_folio' => $newTagLog->tag?->folio ?? $record->folio,
|
'folio' => $tag?->folio,
|
||||||
'new_tag_number' => $newTagLog->tag?->tag_number ?? null,
|
'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 [
|
return [
|
||||||
@ -433,8 +446,9 @@ public function searchRecord(Request $request)
|
|||||||
'action_type' => $latestLog?->action_type ?? 'inscripcion',
|
'action_type' => $latestLog?->action_type ?? 'inscripcion',
|
||||||
'action_date' => $latestLog?->created_at ?? $record->created_at,
|
'action_date' => $latestLog?->created_at ?? $record->created_at,
|
||||||
|
|
||||||
// SUSTITUCIÓN
|
// HISTORIAL DE TAGS
|
||||||
'substitution' => $substitutionData,
|
'tags_history' => $tagsHistory,
|
||||||
|
'total_tags' => count($tagsHistory),
|
||||||
|
|
||||||
// MÓDULO
|
// MÓDULO
|
||||||
'module' => $record->module ? [
|
'module' => $record->module ? [
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user