Generar Excel de un Empleado dentro de EmployeeView
This commit is contained in:
parent
116f094eb2
commit
fae732079b
@ -2,6 +2,7 @@ package com.primefactorsolutions.service;
|
|||||||
|
|
||||||
import com.openhtmltopdf.pdfboxout.PdfBoxRenderer;
|
import com.openhtmltopdf.pdfboxout.PdfBoxRenderer;
|
||||||
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
|
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
|
||||||
|
import com.primefactorsolutions.model.Employee;
|
||||||
import com.primefactorsolutions.repositories.HoursWorkedRepository;
|
import com.primefactorsolutions.repositories.HoursWorkedRepository;
|
||||||
import freemarker.template.Configuration;
|
import freemarker.template.Configuration;
|
||||||
import freemarker.template.DefaultObjectWrapper;
|
import freemarker.template.DefaultObjectWrapper;
|
||||||
@ -174,4 +175,80 @@ public class ReportService {
|
|||||||
|
|
||||||
return cfg;
|
return cfg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public byte[] generateExcelReport(Employee employee) {
|
||||||
|
try (Workbook workbook = new XSSFWorkbook()) {
|
||||||
|
Sheet sheet = workbook.createSheet("Empleado");
|
||||||
|
Row titleRow = sheet.createRow(0);
|
||||||
|
Cell titleCell = titleRow.createCell(0);
|
||||||
|
titleCell.setCellValue("Información General del Empleado");
|
||||||
|
CellStyle titleStyle = workbook.createCellStyle();
|
||||||
|
Font titleFont = workbook.createFont();
|
||||||
|
titleFont.setBold(true);
|
||||||
|
titleFont.setFontHeightInPoints((short) 16);
|
||||||
|
titleStyle.setFont(titleFont);
|
||||||
|
titleStyle.setAlignment(HorizontalAlignment.CENTER);
|
||||||
|
titleCell.setCellStyle(titleStyle);
|
||||||
|
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 40));
|
||||||
|
Row header = sheet.createRow(2);
|
||||||
|
String[] headers = {
|
||||||
|
"Username", "Nombres", "Apellidos", "Estado", "Género", "Fecha de Nacimiento", "Edad",
|
||||||
|
"Ciudad y País de Nacimiento", "Dirección de Residencia", "Departamento y Provincia de Residencia",
|
||||||
|
"Estado Civil", "Número de Hijos", "CI", "Expedido en", "Teléfono", "E-mail Personal",
|
||||||
|
"Teléfono Laboral",
|
||||||
|
"E-mail Laboral", "Nombres y Apellidos de Contacto", "Dirección de Contacto",
|
||||||
|
"Teléfono de Contacto",
|
||||||
|
"Email de Contacto", "Código de Empleado", "Cargo", "Equipo", "Lead/Manager", "Fecha de Ingreso",
|
||||||
|
"Fecha de Retiro", "Tipo de Contrato", "Tipo de Contrato Personalizado", "Antigüedad",
|
||||||
|
"Salario Total",
|
||||||
|
"Salario Básico", "Bono de Antigüedad", "Bono Profesional", "Banco", "Número de Cuenta",
|
||||||
|
"Código Único de Asegurado (GPSS)", "Matrícula de Asegurado (SSS)", "Derechohabiente 1",
|
||||||
|
"Derechohabiente 2"
|
||||||
|
};
|
||||||
|
CellStyle headerStyle = workbook.createCellStyle();
|
||||||
|
Font headerFont = workbook.createFont();
|
||||||
|
headerFont.setBold(true);
|
||||||
|
headerStyle.setFont(headerFont);
|
||||||
|
headerStyle.setAlignment(HorizontalAlignment.CENTER);
|
||||||
|
headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||||
|
for (int i = 0; i < headers.length; i++) {
|
||||||
|
Cell cell = header.createCell(i);
|
||||||
|
cell.setCellValue(headers[i]);
|
||||||
|
cell.setCellStyle(headerStyle);
|
||||||
|
}
|
||||||
|
Row dataRow = sheet.createRow(3); // Fila 3
|
||||||
|
String[] employeeData = {
|
||||||
|
employee.getUsername(), employee.getFirstName(), employee.getLastName(),
|
||||||
|
employee.getStatus().toString(),
|
||||||
|
employee.getGender().toString(), employee.getBirthday().toString(),
|
||||||
|
String.valueOf(employee.getAge()),
|
||||||
|
employee.getBirthCity(), employee.getResidenceAddress(), employee.getLocalAddress(),
|
||||||
|
employee.getMaritalStatus().toString(), String.valueOf(employee.getNumberOfChildren()),
|
||||||
|
employee.getCi(),
|
||||||
|
employee.getIssuedIn(), employee.getPhoneNumber(), employee.getPersonalEmail(),
|
||||||
|
employee.getPhoneNumberProfesional(), employee.getProfesionalEmail(), employee.getEmergencyCName(),
|
||||||
|
employee.getEmergencyCAddress(), employee.getEmergencyCPhone(), employee.getEmergencyCEmail(),
|
||||||
|
employee.getCod(), employee.getPosition(), employee.getTeam().getName(), employee.getLeadManager(),
|
||||||
|
employee.getDateOfEntry().toString(), employee.getDateOfExit() != null ? employee.getDateOfExit()
|
||||||
|
.toString() : "",
|
||||||
|
employee.getContractType().toString(), employee.getCustomContractType(),
|
||||||
|
employee.getSeniority(), employee.getSalarytotal(), employee.getSalaryBasic(),
|
||||||
|
employee.getAntiguedad(), employee.getBonoProfesional(), employee.getBankName(),
|
||||||
|
employee.getAccountNumber(), employee.getGpss(), employee.getSss(),
|
||||||
|
employee.getBeneficiarie1(), employee.getBeneficiarie2()
|
||||||
|
};
|
||||||
|
for (int i = 0; i < employeeData.length; i++) {
|
||||||
|
dataRow.createCell(i).setCellValue(employeeData[i] != null ? employeeData[i] : "");
|
||||||
|
}
|
||||||
|
for (int i = 0; i < headers.length; i++) {
|
||||||
|
sheet.autoSizeColumn(i);
|
||||||
|
}
|
||||||
|
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
|
||||||
|
workbook.write(outputStream);
|
||||||
|
return outputStream.toByteArray();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException("Error al generar el reporte Excel", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -0,0 +1,60 @@
|
|||||||
|
package com.primefactorsolutions.views;
|
||||||
|
|
||||||
|
import com.primefactorsolutions.model.Employee;
|
||||||
|
import com.primefactorsolutions.service.EmployeeService;
|
||||||
|
import com.primefactorsolutions.service.ReportService;
|
||||||
|
import com.vaadin.flow.component.UI;
|
||||||
|
import com.vaadin.flow.component.button.Button;
|
||||||
|
import com.vaadin.flow.component.html.Anchor;
|
||||||
|
import com.vaadin.flow.component.notification.Notification;
|
||||||
|
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
|
||||||
|
import com.vaadin.flow.router.*;
|
||||||
|
import com.vaadin.flow.server.StreamResource;
|
||||||
|
import jakarta.annotation.security.PermitAll;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.util.UUID;
|
||||||
|
@PermitAll
|
||||||
|
@PageTitle("Reporte excel")
|
||||||
|
@Route("employee-report")
|
||||||
|
public class EmployeeReportView extends VerticalLayout implements HasUrlParameter<String> {
|
||||||
|
private final EmployeeService employeeService;
|
||||||
|
private final ReportService reportService;
|
||||||
|
|
||||||
|
public EmployeeReportView(EmployeeService employeeService, ReportService reportService) {
|
||||||
|
this.employeeService = employeeService;
|
||||||
|
this.reportService = reportService;
|
||||||
|
Button backButton = new Button("Volver al Reporte de Empleados", event ->
|
||||||
|
UI.getCurrent().navigate(EmployeesListView.class));
|
||||||
|
backButton.addClassName("back-button");
|
||||||
|
add(backButton);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setParameter(BeforeEvent event, @OptionalParameter String employeeId) {
|
||||||
|
if (employeeId != null) {
|
||||||
|
UUID id = UUID.fromString(employeeId);
|
||||||
|
Employee employee = employeeService.getEmployee(id);
|
||||||
|
if (employee != null) {
|
||||||
|
generateExcelReport(employee);
|
||||||
|
} else {
|
||||||
|
Notification.show("Empleado no encontrado", 3000, Notification.Position.MIDDLE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void generateExcelReport(Employee employee) {
|
||||||
|
try {
|
||||||
|
byte[] excelContent = reportService.generateExcelReport(employee); // Implementa esta lógica en tu `ReportService`.
|
||||||
|
StreamResource resource = new StreamResource(
|
||||||
|
employee.getFirstName() + "_" + employee.getLastName() + "_report.xlsx",
|
||||||
|
() -> new ByteArrayInputStream(excelContent)
|
||||||
|
);
|
||||||
|
Anchor downloadLink = new Anchor(resource, "Descargar Reporte Excel");
|
||||||
|
downloadLink.getElement().setAttribute("download", true);
|
||||||
|
add(downloadLink);
|
||||||
|
} catch (Exception e) {
|
||||||
|
Notification.show("Error al generar el reporte Excel", 3000, Notification.Position.MIDDLE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -16,6 +16,7 @@ import com.vaadin.flow.component.button.ButtonVariant;
|
|||||||
import com.vaadin.flow.component.combobox.ComboBox;
|
import com.vaadin.flow.component.combobox.ComboBox;
|
||||||
import com.vaadin.flow.component.dialog.Dialog;
|
import com.vaadin.flow.component.dialog.Dialog;
|
||||||
import com.vaadin.flow.component.html.*;
|
import com.vaadin.flow.component.html.*;
|
||||||
|
import com.vaadin.flow.component.icon.VaadinIcon;
|
||||||
import com.vaadin.flow.component.notification.Notification;
|
import com.vaadin.flow.component.notification.Notification;
|
||||||
import com.vaadin.flow.component.orderedlayout.FlexComponent;
|
import com.vaadin.flow.component.orderedlayout.FlexComponent;
|
||||||
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
|
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
|
||||||
@ -126,6 +127,7 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
|
|||||||
private final Button reportButton = new Button("Generar Ficha de Contratación");
|
private final Button reportButton = new Button("Generar Ficha de Contratación");
|
||||||
private final Dialog dialog = new Dialog();
|
private final Dialog dialog = new Dialog();
|
||||||
private final PdfViewer pdfViewer = new PdfViewer();
|
private final PdfViewer pdfViewer = new PdfViewer();
|
||||||
|
private final Button excelReportButton = new Button("Información General del Empleado Excel", VaadinIcon.FILE_TABLE.create());
|
||||||
private final H2 infoPer = new H2("Información Personal");
|
private final H2 infoPer = new H2("Información Personal");
|
||||||
private final H3 infoGenr = new H3("Información General");
|
private final H3 infoGenr = new H3("Información General");
|
||||||
private final H5 imagenSub = new H5("Insertar una imagen .jpg:");
|
private final H5 imagenSub = new H5("Insertar una imagen .jpg:");
|
||||||
@ -150,8 +152,14 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
|
|||||||
this.requestService = requestService;
|
this.requestService = requestService;
|
||||||
this.teamService = teamService;
|
this.teamService = teamService;
|
||||||
saveButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
|
saveButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
|
||||||
|
excelReportButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
|
||||||
configureComponents();
|
configureComponents();
|
||||||
addClassName("main-layout");
|
addClassName("main-layout");
|
||||||
|
excelReportButton.addClickListener(e ->
|
||||||
|
getUI().ifPresent(ui ->
|
||||||
|
ui.navigate(EmployeeReportView.class, getEntity().getId().toString())
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void makeUpperCase(final TextField textField) {
|
private void makeUpperCase(final TextField textField) {
|
||||||
@ -704,7 +712,7 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
|
|||||||
salaryBasic, bonoProfesional, antiguedad, salaryTotal,
|
salaryBasic, bonoProfesional, antiguedad, salaryTotal,
|
||||||
datBanc, bankName, accountNumber,
|
datBanc, bankName, accountNumber,
|
||||||
datGest, gpss, sss, beneficiarie1, beneficiarie2,
|
datGest, gpss, sss, beneficiarie1, beneficiarie2,
|
||||||
saveButton, editButton, reportButton, dialog
|
saveButton, editButton, reportButton, excelReportButton, dialog
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user