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