diff --git a/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java b/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java index cc2662c..bc8a08b 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java @@ -7,6 +7,7 @@ import com.primefactorsolutions.service.VacationService; import com.vaadin.flow.component.ClickEvent; import com.vaadin.flow.component.Component; import com.vaadin.flow.component.ComponentEventListener; +import com.vaadin.flow.component.UI; import com.vaadin.flow.component.combobox.ComboBox; import com.vaadin.flow.component.contextmenu.MenuItem; import com.vaadin.flow.component.html.H3; @@ -21,11 +22,21 @@ import com.vaadin.flow.router.BeforeEvent; import com.vaadin.flow.router.HasUrlParameter; import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.Route; +import com.vaadin.flow.server.StreamRegistration; +import com.vaadin.flow.server.StreamResource; import com.vaadin.flow.spring.annotation.SpringComponent; import jakarta.annotation.security.PermitAll; +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.PDPage; +import org.apache.pdfbox.pdmodel.PDPageContentStream; +import org.apache.pdfbox.pdmodel.font.PDType1Font; import org.springframework.context.annotation.Scope; import org.vaadin.firitin.components.grid.PagingGrid; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.UncheckedIOException; import java.time.LocalDate; import java.time.Period; import java.util.*; @@ -446,6 +457,145 @@ public class RequestEmployeeView extends BaseView implements HasUrlParameter requests, final int columnIndex) { + float maxWidth = header.length(); + for (TimeOffRequest request : requests) { + String value = switch (columnIndex) { + case 0 -> request.getCategory().name(); + case 1 -> request.getState().name(); + case 2 -> getDateString(request.getStartDate()); + case 3 -> getDateString(request.getEndDate()); + case 4 -> String.valueOf(request.getDaysToBeTake()); + default -> ""; + }; + if (value != null) { + maxWidth = Math.max(maxWidth, value.length()); + } + } + return maxWidth * 7; + } + + private StreamResource generateVacationReport() { + Employee employee = employeeService.getEmployee(employeeId); + String fileName = String.format("%s_%s-reporte_de_vacaciones_%s.pdf", + employee.getFirstName(), + employee.getLastName(), + LocalDate.now()); + + ByteArrayInputStream pdfStream = generatePdfReport(); + + return new StreamResource(fileName, () -> pdfStream) + .setContentType("application/pdf") + .setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); + } + + private void downloadReport() { + StreamResource resource = generateVacationReport(); + getUI().ifPresent(ui -> openDocumentStream(resource, ui)); + } + + private void openDocumentStream(final StreamResource resource, final UI ui) { + StreamRegistration registration = ui.getSession().getResourceRegistry().registerResource(resource); + ui.getPage().open(registration.getResourceUri().toString()); + } + @Override public void setParameter(final BeforeEvent event, final String parameter) { employeeId = UUID.fromString(parameter); diff --git a/src/main/java/com/primefactorsolutions/views/RequestsListView.java b/src/main/java/com/primefactorsolutions/views/RequestsListView.java index 49ae898..1c9c1d3 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestsListView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestsListView.java @@ -32,10 +32,8 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UncheckedIOException; -import java.time.DayOfWeek; import java.time.LocalDate; import java.time.Period; -import java.time.temporal.WeekFields; import java.util.*; import java.util.stream.Collectors; @@ -428,12 +426,12 @@ public class RequestsListView extends BaseView { getUI().ifPresent(ui -> ui.navigate("requests/" + idEmployee.toString())); } - private ByteArrayInputStream generateExcelReport(List employees) { + private ByteArrayInputStream generateExcelReport(final List employees) { try (Workbook workbook = new XSSFWorkbook()) { - Sheet sheet = workbook.createSheet("Reporte Horas Trabajadas"); + Sheet sheet = workbook.createSheet("REPORTE_GENERAL_DE_VACACIONES"); Row headerRow = sheet.createRow(0); - String[] headers = {"Nombre", "Equipo", "Estado", "Total Horas"}; + String[] headers = {"Empleado", "Equipo", "Estado", "Total Horas"}; for (int i = 0; i < headers.length; i++) { Cell cell = headerRow.createCell(i); cell.setCellValue(headers[i]); @@ -460,7 +458,6 @@ public class RequestsListView extends BaseView { private StreamResource generateGeneralVacationReport() { List employees = employeeService.findAllEmployees(); ByteArrayInputStream excelStream = generateExcelReport(employees); - return new StreamResource("reporte_general_de_vacaciones_" + LocalDate.now() + ".xlsx", () -> excelStream); }