199 lines
5.7 KiB
TypeScript
199 lines
5.7 KiB
TypeScript
import { Component } from '@angular/core';
|
|
import { FormsModule } from '@angular/forms';
|
|
import { Table, TableModule } from 'primeng/table';
|
|
import { InputTextModule } from 'primeng/inputtext';
|
|
import { SelectModule } from 'primeng/select';
|
|
import { ButtonModule } from 'primeng/button';
|
|
import { TooltipModule } from 'primeng/tooltip';
|
|
import * as FileSaver from 'file-saver';
|
|
import { Workbook } from 'exceljs';
|
|
|
|
@Component({
|
|
selector: 'app-actualizacion-pd',
|
|
imports: [
|
|
FormsModule,
|
|
TableModule,
|
|
InputTextModule,
|
|
SelectModule,
|
|
ButtonModule,
|
|
TooltipModule
|
|
],
|
|
templateUrl: './actualizacion-pd.component.html',
|
|
styleUrl: './actualizacion-pd.component.scss',
|
|
standalone: true
|
|
})
|
|
export class ActualizacionPdComponent {
|
|
pageTitle: string = 'Cronogramas cargados:';
|
|
select1: any = '';
|
|
selectedCity: any = '';
|
|
empresas: any[] = [{ name: 'Empresa A' }, { name: 'Empresa B' }, { name: 'Empresa C' }];
|
|
estadoAprobacion = [
|
|
{ name: 'Aprobado', value: true },
|
|
{ name: 'Rechazado', value: false },
|
|
];
|
|
products: any[] = [
|
|
{
|
|
empresa: 'Empresa A',
|
|
codigoCronograma: '123',
|
|
codigoCronogramaAjuste: '456',
|
|
tipoCarga: 'Inicial',
|
|
estadoRevision: 'Ingresado',
|
|
analista: 'No asignado',
|
|
fechaIngreso: '2025-04-29',
|
|
semaforo: 'green',
|
|
dato9: 'Ingresado',
|
|
dato10: 'No asignado',
|
|
dato13: 'Rechazado',
|
|
dato14: 'green',
|
|
},
|
|
{
|
|
empresa: 'Empresa A',
|
|
codigoCronograma: '123',
|
|
codigoCronogramaAjuste: '456',
|
|
tipoCarga: 'Inicial',
|
|
estadoRevision: 'En revisión',
|
|
analista: 'Gabriel Torres',
|
|
fechaIngreso: '2025-04-29',
|
|
semaforo: 'yellow',
|
|
dato9: 'Ingresado',
|
|
dato10: 'No asignado',
|
|
dato13: 'Rechazado',
|
|
dato14: '',
|
|
},
|
|
{
|
|
empresa: 'Empresa A',
|
|
codigoCronograma: '123',
|
|
codigoCronogramaAjuste: '456',
|
|
tipoCarga: 'Inicial',
|
|
estadoRevision: 'Rechazado',
|
|
analista: 'Jorge Muñoz',
|
|
fechaIngreso: '2025-04-29',
|
|
semaforo: 'red',
|
|
dato9: 'Ingresado',
|
|
dato10: 'No asignado',
|
|
dato13: 'Aprobado',
|
|
dato14: '',
|
|
},
|
|
{
|
|
empresa: 'Empresa A',
|
|
codigoCronograma: '123',
|
|
codigoCronogramaAjuste: '456',
|
|
tipoCarga: 'Inicial',
|
|
estadoRevision: 'Aprobado',
|
|
analista: 'Jarolt Matamoros',
|
|
fechaIngreso: '2025-04-29',
|
|
semaforo: 'green',
|
|
dato9: 'Ingresado',
|
|
dato10: 'No asignado',
|
|
dato11: '2025-04-29',
|
|
dato12: 'green',
|
|
dato13: 'Aprobado',
|
|
dato14: '',
|
|
},
|
|
];
|
|
/**
|
|
* Exporta la tabla a Excel usando ExcelJS
|
|
* @param table Referencia a la tabla PrimeNG
|
|
*/
|
|
|
|
exportExcelWithStyles(table: Table): void {
|
|
// Creamos un nuevo libro de trabajo
|
|
const workbook = new Workbook();
|
|
const worksheet = workbook.addWorksheet('Datos');
|
|
|
|
// Exportamos los datos y aplicamos estilos
|
|
this.addDataToWorksheet(worksheet, table);
|
|
this.applyHeaderStyles(worksheet);
|
|
this.configureWorksheet(worksheet);
|
|
this.saveExcelFile(workbook);
|
|
|
|
|
|
}
|
|
/**
|
|
* Añade los datos de la tabla al worksheet
|
|
*/
|
|
private addDataToWorksheet(worksheet: any, table: Table): void {
|
|
// Obtenemos los datos a exportar
|
|
const data = table.filteredValue || table.value;
|
|
|
|
// Definimos las cabeceras
|
|
const headers = [
|
|
'Empresa', 'Código de cronograma', 'Etapa del Servicio',
|
|
'Nombre sistema', 'Tipo de inversión', 'Código de glosa PD',
|
|
'Descripción glosa', 'Año de Inicio', 'Año de Término',
|
|
'Estado aprobación'
|
|
];
|
|
|
|
// Añadimos cabeceras y datos
|
|
worksheet.addRow(headers);
|
|
data.forEach(item => {
|
|
worksheet.addRow([
|
|
item.empresa,
|
|
item.codigoCronograma,
|
|
item.codigoCronogramaAjuste,
|
|
item.tipoCarga,
|
|
item.estadoRevision,
|
|
item.analista,
|
|
item.fechaIngreso,
|
|
item.dato9,
|
|
item.dato10,
|
|
item.dato13
|
|
]);
|
|
});
|
|
}
|
|
/**
|
|
* Aplica estilos a los encabezados de la tabla
|
|
*/
|
|
private applyHeaderStyles(worksheet: any): void {
|
|
const headerRow = worksheet.getRow(1);
|
|
headerRow.height = 25;
|
|
|
|
headerRow.eachCell((cell: any) => {
|
|
cell.font = { name: 'Arial', size: 12, bold: true, color: { argb: '000000' } };
|
|
cell.border = {
|
|
top: { style: 'thin' },
|
|
left: { style: 'thin' },
|
|
bottom: { style: 'thin' },
|
|
right: { style: 'thin' }
|
|
};
|
|
cell.alignment = { horizontal: 'center', vertical: 'middle' };
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Configura aspectos generales de la hoja de trabajo
|
|
*/
|
|
private configureWorksheet(worksheet: any): void {
|
|
// Ajustamos el ancho de las columnas automáticamente
|
|
if (worksheet.columns) {
|
|
worksheet.columns.forEach((column: any) => {
|
|
if (column) {
|
|
let maxLength = 0;
|
|
column.eachCell({ includeEmpty: true }, (cell: any) => {
|
|
const columnLength = cell.value ? cell.value.toString().length : 10;
|
|
if (columnLength > maxLength) {
|
|
maxLength = columnLength;
|
|
}
|
|
});
|
|
column.width = maxLength < 10 ? 10 : maxLength + 2;
|
|
}
|
|
});
|
|
}
|
|
|
|
// Congelamos la primera fila
|
|
worksheet.views = [{ state: 'frozen', xSplit: 0, ySplit: 1 }];
|
|
}
|
|
/**
|
|
* Guarda el archivo Excel
|
|
*/
|
|
private async saveExcelFile(workbook: any): Promise<void> {
|
|
const today = new Date();
|
|
const fileName = `Cronograma_temporal_por_actualización_de_PD${today.getFullYear()}${(today.getMonth() + 1).toString().padStart(2, '0')}${today.getDate().toString().padStart(2, '0')}.xlsx`;
|
|
|
|
const buffer = await workbook.xlsx.writeBuffer();
|
|
const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
|
|
FileSaver.saveAs(blob, fileName);
|
|
}
|
|
|
|
}
|