Observaciones-vacaciones #83
@ -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<Str
|
||||
return existingRequest.isEmpty();
|
||||
}
|
||||
|
||||
private String getDateString(final LocalDate date) {
|
||||
return (date != null) ? date.toString() : "";
|
||||
}
|
||||
|
||||
private ByteArrayInputStream generatePdfReport() {
|
||||
try (PDDocument document = new PDDocument(); ByteArrayOutputStream out = new ByteArrayOutputStream()) {
|
||||
PDPage page = new PDPage();
|
||||
document.addPage(page);
|
||||
Employee employee = employeeService.getEmployee(employeeId);
|
||||
|
||||
PDPageContentStream contentStream = null;
|
||||
try {
|
||||
contentStream = new PDPageContentStream(document, page);
|
||||
|
||||
contentStream.setFont(PDType1Font.TIMES_BOLD, 18);
|
||||
contentStream.beginText();
|
||||
contentStream.newLineAtOffset(200, 750);
|
||||
contentStream.showText("Reporte de Vacaciones");
|
||||
contentStream.endText();
|
||||
|
||||
contentStream.setFont(PDType1Font.TIMES_ROMAN, 14);
|
||||
contentStream.beginText();
|
||||
contentStream.newLineAtOffset(50, 700);
|
||||
contentStream.showText("Empleado: " + employee.getFirstName() + " " + employee.getLastName());
|
||||
contentStream.newLineAtOffset(0, -15);
|
||||
contentStream.showText("Equipo: " + employee.getTeam().getName());
|
||||
contentStream.endText();
|
||||
|
||||
float tableTopY = 650;
|
||||
float margin = 50;
|
||||
float cellHeight = 20;
|
||||
String[] headers = {"Categoría", "Estado", "Fecha de Inicio", "Fecha de Fin", "Días a Tomar"};
|
||||
int columns = headers.length;
|
||||
|
||||
float[] columnWidths = new float[columns];
|
||||
for (int i = 0; i < columns; i++) {
|
||||
columnWidths[i] = getMaxColumnWidth(headers[i], requests, i);
|
||||
}
|
||||
|
||||
contentStream.setFont(PDType1Font.TIMES_BOLD, 10);
|
||||
float currentX = margin;
|
||||
for (int i = 0; i < columns; i++) {
|
||||
contentStream.addRect(currentX, tableTopY, columnWidths[i], -cellHeight);
|
||||
contentStream.beginText();
|
||||
contentStream.newLineAtOffset(currentX + 5, tableTopY - 15);
|
||||
contentStream.showText(headers[i]);
|
||||
contentStream.endText();
|
||||
currentX += columnWidths[i];
|
||||
}
|
||||
contentStream.stroke();
|
||||
|
||||
contentStream.setFont(PDType1Font.TIMES_ROMAN, 10);
|
||||
float currentY = tableTopY - cellHeight;
|
||||
for (TimeOffRequest request : requests) {
|
||||
String startDate = getDateString(request.getStartDate());
|
||||
String endDate = getDateString(request.getEndDate());
|
||||
|
||||
String[] rowData = {
|
||||
request.getCategory().name() != null ? request.getCategory().name() : "",
|
||||
request.getState().name() != null ? request.getState().name() : "",
|
||||
startDate,
|
||||
endDate,
|
||||
String.valueOf(request.getDaysToBeTake() != null ? request.getDaysToBeTake() : 0)
|
||||
};
|
||||
|
||||
currentX = margin;
|
||||
for (int i = 0; i < columns; i++) {
|
||||
contentStream.addRect(currentX, currentY, columnWidths[i], -cellHeight);
|
||||
contentStream.beginText();
|
||||
contentStream.newLineAtOffset(currentX + 5, currentY - 15);
|
||||
contentStream.showText(rowData[i]);
|
||||
contentStream.endText();
|
||||
currentX += columnWidths[i];
|
||||
}
|
||||
contentStream.stroke();
|
||||
currentY -= cellHeight;
|
||||
|
||||
if (currentY < 50) {
|
||||
contentStream.close();
|
||||
page = new PDPage();
|
||||
document.addPage(page);
|
||||
contentStream = new PDPageContentStream(document, page);
|
||||
currentY = 750;
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
if (contentStream != null) {
|
||||
contentStream.close();
|
||||
}
|
||||
}
|
||||
document.save(out);
|
||||
return new ByteArrayInputStream(out.toByteArray());
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException("Error al generar el reporte", e);
|
||||
}
|
||||
}
|
||||
|
||||
private float getMaxColumnWidth(final String header, final List<TimeOffRequest> 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);
|
||||
|
@ -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<Employee> employees) {
|
||||
private ByteArrayInputStream generateExcelReport(final List<Employee> 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<Employee> employees = employeeService.findAllEmployees();
|
||||
ByteArrayInputStream excelStream = generateExcelReport(employees);
|
||||
|
||||
return new StreamResource("reporte_general_de_vacaciones_" + LocalDate.now() + ".xlsx",
|
||||
() -> excelStream);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user