From caab437d178b50f4bd82e4031415e767c866b972 Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Sat, 23 Nov 2024 14:55:12 -0400 Subject: [PATCH] Descargar reporte general de vacaciones en excel. --- Reporte_Vacaciones.xlsx | Bin 0 -> 4181 bytes .../views/RequestsListView.java | 62 +++++++++++++++++- 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 Reporte_Vacaciones.xlsx diff --git a/Reporte_Vacaciones.xlsx b/Reporte_Vacaciones.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..8ce8134d9c64dbcc7b5e2998c2636aa3375c88d2 GIT binary patch literal 4181 zcmaJ^c|4T;79UK^SjUoVWsQtpON1yywi!FoRCa?bGh-|J-YCgZVhSZYWfvlivG4m9 zku0H*HDtRpb>GW%@7tNr^UU+dbI#{E-*bN7^F4ZS3Lpyr1OfphFn17uL&i*ezM+Cf zxg$~T=0;u^q?@Uzr?b-=!&X#{IArHZ(;!rOP4DXQAQkRL*6cNkn?8@PZJy45xwK(Q zB^zsSpW0=`+Sbn2j>zYes&{v{X!!ZuipB&zzT%G+=HF6$*}%SzsmMhPh!-LbdPLG8ZW<`&mtyO;4 zw48M#0-V-xibuY+Fdn?-qqBzd4;#9+9D=Fm@^aMGdI@BUcGL+pUQAog{D@b+?vE{a z_dLLb?MJ`|2A*(Bc5c^upM7-kWO{Hakauj0i#!KaUlxCE);VI4OzMWFmR0M}a9q`Ho9GQVHRt>p0l$5!p4*CH znpOIu&r^dudV^E!KWq(gdMaFcy{L4YFpHVcpyaFdFY`+Qr=wyCnJG{{R*DdfiouGZ zAo4gYbN%p~ELH6FFJ?j|{6ErUoPA!c>QW|3_II;4sh3fD)O>u*h7;QD@ba}hl@Ol$ zFzN~(Je{{1NAF}#p^9jFj{{WA7j4~kapIO7)Yh8ABkftkBR@j>fh`l+Hzg|&;zfyb zXxLy>3I$k>Whcc9G*`?$A?r$5^aJGEP2&?|*Be2a16r|!H{#?ZPlmpqeC zBcSbJAo)3(Dpr%F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al 0 HcmV?d00001 diff --git a/src/main/java/com/primefactorsolutions/views/RequestsListView.java b/src/main/java/com/primefactorsolutions/views/RequestsListView.java index ce4fd56..49ae898 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestsListView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestsListView.java @@ -8,6 +8,8 @@ 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.button.Button; import com.vaadin.flow.component.combobox.ComboBox; import com.vaadin.flow.component.contextmenu.MenuItem; import com.vaadin.flow.component.icon.VaadinIcon; @@ -17,13 +19,23 @@ import com.vaadin.flow.component.orderedlayout.HorizontalLayout; import com.vaadin.flow.function.ValueProvider; 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.poi.ss.usermodel.*; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; 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.DayOfWeek; import java.time.LocalDate; import java.time.Period; +import java.time.temporal.WeekFields; import java.util.*; import java.util.stream.Collectors; @@ -61,6 +73,8 @@ public class RequestsListView extends BaseView { private void initializeView() { requestService.updateRequestStatuses(); + Button downloadButton = new Button("Descargar reporte", event -> downloadReport()); + getCurrentPageLayout().add(downloadButton); setupFilters(); setupRequestGrid(); getCurrentPageLayout().add(requestGrid); @@ -71,7 +85,6 @@ public class RequestsListView extends BaseView { hl.add(createEmployeeFilter()); hl.add(createTeamFilter()); hl.add(createStateFilter()); - getCurrentPageLayout().add(hl); } @@ -414,4 +427,51 @@ public class RequestsListView extends BaseView { private void navigateToTimeOffRequestView(final UUID idEmployee) { getUI().ifPresent(ui -> ui.navigate("requests/" + idEmployee.toString())); } + + private ByteArrayInputStream generateExcelReport(List employees) { + try (Workbook workbook = new XSSFWorkbook()) { + Sheet sheet = workbook.createSheet("Reporte Horas Trabajadas"); + Row headerRow = sheet.createRow(0); + + String[] headers = {"Nombre", "Equipo", "Estado", "Total Horas"}; + for (int i = 0; i < headers.length; i++) { + Cell cell = headerRow.createCell(i); + cell.setCellValue(headers[i]); + } + + int rowIndex = 1; + for (Employee employee : employees) { + Row row = sheet.createRow(rowIndex++); + row.createCell(0).setCellValue(getEmployeeFullName(employee)); + row.createCell(1).setCellValue(getTeamName(employee)); + row.createCell(2).setCellValue(getEmployeeStatus(employee)); + row.createCell(3).setCellValue(getGeneralTotal(employee)); + } + + try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { + workbook.write(out); + return new ByteArrayInputStream(out.toByteArray()); + } + } catch (IOException e) { + throw new UncheckedIOException("Error al generar el archivo Excel", e); + } + } + + private StreamResource generateGeneralVacationReport() { + List employees = employeeService.findAllEmployees(); + ByteArrayInputStream excelStream = generateExcelReport(employees); + + return new StreamResource("reporte_general_de_vacaciones_" + LocalDate.now() + ".xlsx", + () -> excelStream); + } + + private void downloadReport() { + StreamResource resource = generateGeneralVacationReport(); + 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()); + } } \ No newline at end of file