fix: excel searchRecord
This commit is contained in:
parent
1d7afe1b9a
commit
2411ff1eab
@ -970,12 +970,18 @@ public function exportSearchRecords(Request $request)
|
|||||||
$excelRows = [];
|
$excelRows = [];
|
||||||
|
|
||||||
foreach ($allRecords as $record) {
|
foreach ($allRecords as $record) {
|
||||||
$niv = $record->vehicle->niv ?? '';
|
$vehicle = $record->vehicle;
|
||||||
$currentTagId = $record->vehicle->tag?->id;
|
$niv = $vehicle->niv ?? '';
|
||||||
$recordRows = []; // Filas para este registro principal
|
$nrpv = $vehicle->nrpv ?? '';
|
||||||
|
$marca = $vehicle->marca ?? '';
|
||||||
|
$placa = $vehicle->placa ?? '';
|
||||||
|
$modelo = $vehicle->modelo ?? '';
|
||||||
|
$moduleName = $record->module->name ?? '';
|
||||||
|
$operador = $record->user->name ?? $record->user->username ?? '';
|
||||||
|
$currentTagId = $vehicle->tag?->id;
|
||||||
|
|
||||||
// Obtener todos los logs ordenados por fecha ascendente
|
// Obtener todos los logs ordenados por fecha ascendente
|
||||||
$vehicleLogs = $record->vehicle->vehicleTagLogs->sortBy('created_at');
|
$vehicleLogs = $vehicle->vehicleTagLogs->sortBy('created_at');
|
||||||
$processedTags = [];
|
$processedTags = [];
|
||||||
|
|
||||||
// Procesar tags del historial (excluyendo el tag actual)
|
// Procesar tags del historial (excluyendo el tag actual)
|
||||||
@ -1001,16 +1007,19 @@ public function exportSearchRecords(Request $request)
|
|||||||
->whereIn('action_type', ['inscripcion', 'sustitucion'])
|
->whereIn('action_type', ['inscripcion', 'sustitucion'])
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
// Determinar fecha: si fue cancelado, usar fecha de cancelación, sino fecha de asignación
|
// Determinar fecha, status y tipo de trámite
|
||||||
$fecha = null;
|
$fecha = null;
|
||||||
$status = 'unknown';
|
$status = 'unknown';
|
||||||
|
$tipoTramite = '';
|
||||||
|
|
||||||
if ($cancelLog && $cancelLog->cancellation_at) {
|
if ($cancelLog && $cancelLog->cancellation_at) {
|
||||||
$fecha = Carbon::parse($cancelLog->cancellation_at);
|
$fecha = Carbon::parse($cancelLog->cancellation_at);
|
||||||
$status = 'Cancelado';
|
$status = 'Cancelado';
|
||||||
|
$tipoTramite = ucfirst($cancelLog->action_type);
|
||||||
} elseif ($assignedLog && $assignedLog->created_at) {
|
} elseif ($assignedLog && $assignedLog->created_at) {
|
||||||
$fecha = Carbon::parse($assignedLog->created_at);
|
$fecha = Carbon::parse($assignedLog->created_at);
|
||||||
$status = ucfirst($assignedLog->action_type);
|
$status = $tag?->status?->name ?? ucfirst($assignedLog->action_type);
|
||||||
|
$tipoTramite = ucfirst($assignedLog->action_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Si no hay fecha, usar el status del tag
|
// Si no hay fecha, usar el status del tag
|
||||||
@ -1018,59 +1027,74 @@ public function exportSearchRecords(Request $request)
|
|||||||
$status = $tag?->status?->name ?? 'unknown';
|
$status = $tag?->status?->name ?? 'unknown';
|
||||||
}
|
}
|
||||||
|
|
||||||
$recordRows[] = [
|
// Obtener caja del tag
|
||||||
|
$caja = $tag?->package?->box_number ?? '';
|
||||||
|
|
||||||
|
$excelRows[] = [
|
||||||
'status' => $status,
|
'status' => $status,
|
||||||
'folio' => $tag?->folio ?? '',
|
'modulo' => $tag?->module?->name ?? $moduleName,
|
||||||
'tag' => $tag?->tag_number ?? '',
|
'operador' => $operador,
|
||||||
|
'tipo_tramite' => $tipoTramite,
|
||||||
'niv' => $niv,
|
'niv' => $niv,
|
||||||
|
'nrpv' => $nrpv,
|
||||||
|
'marca' => $marca,
|
||||||
|
'placa' => $placa,
|
||||||
|
'modelo' => $modelo,
|
||||||
|
'folio' => $tag?->folio ?? '',
|
||||||
|
'caja' => $caja,
|
||||||
|
'tag' => $tag?->tag_number ?? '',
|
||||||
'fecha' => $fecha ? $fecha->format('d/m/Y H:i') : '',
|
'fecha' => $fecha ? $fecha->format('d/m/Y H:i') : '',
|
||||||
'sort_date' => $fecha ? $fecha->timestamp : 0,
|
'sort_date' => $fecha ? $fecha->timestamp : 0,
|
||||||
'record_id' => $record->id, // Para mantener agrupado
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ordenar las filas del historial por fecha ascendente
|
// Agregar el registro principal (tag actual)
|
||||||
usort($recordRows, function ($a, $b) {
|
if ($vehicle->tag) {
|
||||||
return $a['sort_date'] <=> $b['sort_date'];
|
$currentTag = $vehicle->tag;
|
||||||
});
|
$latestLog = $vehicle->vehicleTagLogs->first();
|
||||||
|
|
||||||
// Agregar el registro principal (tag actual) al final del grupo
|
// Determinar fecha, status y tipo de trámite del tag actual
|
||||||
if ($record->vehicle->tag) {
|
|
||||||
$currentTag = $record->vehicle->tag;
|
|
||||||
$latestLog = $record->vehicle->vehicleTagLogs->first();
|
|
||||||
|
|
||||||
// Determinar fecha y status del tag actual
|
|
||||||
$fechaActual = null;
|
$fechaActual = null;
|
||||||
$statusActual = $currentTag->status?->name ?? 'unknown';
|
$statusActual = $currentTag->status?->name ?? 'unknown';
|
||||||
|
$tipoTramiteActual = '';
|
||||||
|
|
||||||
if ($latestLog) {
|
if ($latestLog) {
|
||||||
|
$tipoTramiteActual = ucfirst($latestLog->action_type);
|
||||||
if ($latestLog->cancellation_at) {
|
if ($latestLog->cancellation_at) {
|
||||||
$fechaActual = Carbon::parse($latestLog->cancellation_at);
|
$fechaActual = Carbon::parse($latestLog->cancellation_at);
|
||||||
$statusActual = 'Cancelado';
|
$statusActual = 'Cancelado';
|
||||||
} else {
|
} else {
|
||||||
$fechaActual = Carbon::parse($latestLog->created_at);
|
$fechaActual = Carbon::parse($latestLog->created_at);
|
||||||
$statusActual = ucfirst($latestLog->action_type);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$fechaActual = $record->created_at ? Carbon::parse($record->created_at) : now();
|
$fechaActual = $record->created_at ? Carbon::parse($record->created_at) : now();
|
||||||
}
|
}
|
||||||
|
|
||||||
$recordRows[] = [
|
$excelRows[] = [
|
||||||
'status' => $statusActual,
|
'status' => $statusActual,
|
||||||
'folio' => $currentTag->folio ?? '',
|
'modulo' => $currentTag->module?->name ?? $moduleName,
|
||||||
'tag' => $currentTag->tag_number ?? '',
|
'operador' => $operador,
|
||||||
|
'tipo_tramite' => $tipoTramiteActual,
|
||||||
'niv' => $niv,
|
'niv' => $niv,
|
||||||
|
'nrpv' => $nrpv,
|
||||||
|
'marca' => $marca,
|
||||||
|
'placa' => $placa,
|
||||||
|
'modelo' => $modelo,
|
||||||
|
'folio' => $currentTag->folio ?? '',
|
||||||
|
'caja' => $currentTag->package?->box_number ?? '',
|
||||||
|
'tag' => $currentTag->tag_number ?? '',
|
||||||
'fecha' => $fechaActual->format('d/m/Y H:i'),
|
'fecha' => $fechaActual->format('d/m/Y H:i'),
|
||||||
'sort_date' => $fechaActual->timestamp,
|
'sort_date' => $fechaActual->timestamp,
|
||||||
'record_id' => $record->id,
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Agregar todas las filas de este registro al array principal
|
|
||||||
$excelRows = array_merge($excelRows, $recordRows);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ordenar todas las filas por fecha ascendente
|
||||||
|
usort($excelRows, function ($a, $b) {
|
||||||
|
return $a['sort_date'] <=> $b['sort_date'];
|
||||||
|
});
|
||||||
|
|
||||||
// Crear Excel
|
// Crear Excel
|
||||||
$fileName = 'Busqueda_Registros_' . now()->format('Ymd_His') . '.xlsx';
|
$fileName = 'Busqueda_Registros_' . now()->format('Ymd_His') . '.xlsx';
|
||||||
$filePath = storage_path('app/temp/' . $fileName);
|
$filePath = storage_path('app/temp/' . $fileName);
|
||||||
@ -1095,13 +1119,21 @@ public function exportSearchRecords(Request $request)
|
|||||||
'alignment' => ['vertical' => Alignment::VERTICAL_CENTER, 'wrapText' => true],
|
'alignment' => ['vertical' => Alignment::VERTICAL_CENTER, 'wrapText' => true],
|
||||||
];
|
];
|
||||||
|
|
||||||
// Encabezados (solo las 5 columnas solicitadas)
|
// Encabezados con todos los campos solicitados
|
||||||
$headers = [
|
$headers = [
|
||||||
'A' => 'Status',
|
'A' => 'Estatus',
|
||||||
'B' => 'Folio',
|
'B' => 'Módulo',
|
||||||
'C' => 'Tag',
|
'C' => 'Operador',
|
||||||
'D' => 'NIV',
|
'D' => 'Tipo de Trámite',
|
||||||
'E' => 'Fecha de Registro o Cancelación',
|
'E' => 'NIV',
|
||||||
|
'F' => 'NRPV',
|
||||||
|
'G' => 'Marca',
|
||||||
|
'H' => 'Placa',
|
||||||
|
'I' => 'Año',
|
||||||
|
'J' => 'Folio',
|
||||||
|
'K' => 'Caja',
|
||||||
|
'L' => 'Tag ID',
|
||||||
|
'M' => 'Fecha',
|
||||||
];
|
];
|
||||||
|
|
||||||
$row = 1;
|
$row = 1;
|
||||||
@ -1115,29 +1147,49 @@ public function exportSearchRecords(Request $request)
|
|||||||
$row = 2;
|
$row = 2;
|
||||||
foreach ($excelRows as $item) {
|
foreach ($excelRows as $item) {
|
||||||
$sheet->setCellValue('A' . $row, $item['status']);
|
$sheet->setCellValue('A' . $row, $item['status']);
|
||||||
$sheet->setCellValue('B' . $row, $item['folio']);
|
$sheet->setCellValue('B' . $row, $item['modulo']);
|
||||||
$sheet->setCellValue('C' . $row, $item['tag']);
|
$sheet->setCellValue('C' . $row, $item['operador']);
|
||||||
$sheet->setCellValue('D' . $row, $item['niv']);
|
$sheet->setCellValue('D' . $row, $item['tipo_tramite']);
|
||||||
$sheet->setCellValue('E' . $row, $item['fecha']);
|
$sheet->setCellValue('E' . $row, $item['niv']);
|
||||||
|
$sheet->setCellValue('F' . $row, $item['nrpv']);
|
||||||
|
$sheet->setCellValue('G' . $row, $item['marca']);
|
||||||
|
$sheet->setCellValue('H' . $row, $item['placa']);
|
||||||
|
$sheet->setCellValue('I' . $row, $item['modelo']);
|
||||||
|
$sheet->setCellValue('J' . $row, $item['folio']);
|
||||||
|
$sheet->setCellValue('K' . $row, $item['caja']);
|
||||||
|
$sheet->setCellValue('L' . $row, $item['tag']);
|
||||||
|
$sheet->setCellValue('M' . $row, $item['fecha']);
|
||||||
|
|
||||||
// Aplicar estilos
|
// Aplicar estilos a todas las columnas
|
||||||
foreach (range('A', 'E') as $col) {
|
foreach (range('A', 'M') as $col) {
|
||||||
$sheet->getStyle("{$col}{$row}")->applyFromArray($cellStyle);
|
$sheet->getStyle("{$col}{$row}")->applyFromArray($cellStyle);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Centrar algunas columnas
|
// Centrar columnas específicas
|
||||||
$sheet->getStyle("A{$row}")->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
|
$sheet->getStyle("A{$row}")->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
|
||||||
$sheet->getStyle("E{$row}")->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
|
$sheet->getStyle("D{$row}")->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
|
||||||
|
$sheet->getStyle("I{$row}")->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
|
||||||
|
$sheet->getStyle("K{$row}")->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
|
||||||
|
$sheet->getStyle("L{$row}")->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
|
||||||
|
$sheet->getStyle("M{$row}")->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
|
||||||
|
|
||||||
$row++;
|
$row++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ajustar anchos de columna
|
// Ajustar anchos de columna
|
||||||
$sheet->getColumnDimension('A')->setWidth(20);
|
$sheet->getColumnDimension('A')->setWidth(15); // Estatus
|
||||||
$sheet->getColumnDimension('B')->setWidth(18);
|
$sheet->getColumnDimension('B')->setWidth(20); // Módulo
|
||||||
$sheet->getColumnDimension('C')->setWidth(20);
|
$sheet->getColumnDimension('C')->setWidth(20); // Operador
|
||||||
$sheet->getColumnDimension('D')->setWidth(22);
|
$sheet->getColumnDimension('D')->setWidth(18); // Tipo de Trámite
|
||||||
$sheet->getColumnDimension('E')->setWidth(25);
|
$sheet->getColumnDimension('E')->setWidth(22); // NIV
|
||||||
|
$sheet->getColumnDimension('F')->setWidth(14); // NRPV
|
||||||
|
$sheet->getColumnDimension('G')->setWidth(15); // Marca
|
||||||
|
$sheet->getColumnDimension('H')->setWidth(12); // Placa
|
||||||
|
$sheet->getColumnDimension('I')->setWidth(8); // Año
|
||||||
|
$sheet->getColumnDimension('J')->setWidth(18); // Folio
|
||||||
|
$sheet->getColumnDimension('K')->setWidth(10); // Caja
|
||||||
|
$sheet->getColumnDimension('L')->setWidth(28); // Tag ID
|
||||||
|
$sheet->getColumnDimension('M')->setWidth(18); // Fecha
|
||||||
|
|
||||||
// Guardar archivo
|
// Guardar archivo
|
||||||
$writer = new Xlsx($spreadsheet);
|
$writer = new Xlsx($spreadsheet);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user