From 98ed64dfda50e85272411deed7a34cc02afba69a Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Tue, 29 Oct 2024 15:28:20 -0400 Subject: [PATCH] Listado Solicitudes de Empleado - Calcular totales --- .../service/VacationService.java | 2 +- .../views/RequestEmployeeView.java | 39 ++++++++++++++++++- .../views/RequestRegisterView.java | 20 +++++++--- 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/service/VacationService.java b/src/main/java/com/primefactorsolutions/service/VacationService.java index 084c610..94cb763 100644 --- a/src/main/java/com/primefactorsolutions/service/VacationService.java +++ b/src/main/java/com/primefactorsolutions/service/VacationService.java @@ -20,4 +20,4 @@ public class VacationService { public List findVacations() { return vacationRepository.findAll(); } -} +} \ No newline at end of file diff --git a/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java b/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java index f5f8e10..26872a7 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java @@ -3,13 +3,16 @@ package com.primefactorsolutions.views; import com.primefactorsolutions.model.*; import com.primefactorsolutions.service.EmployeeService; import com.primefactorsolutions.service.TimeOffRequestService; +import com.primefactorsolutions.service.VacationService; import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.combobox.ComboBox; import com.vaadin.flow.component.grid.Grid; import com.vaadin.flow.component.html.Div; import com.vaadin.flow.component.html.H3; +import com.vaadin.flow.component.html.Span; import com.vaadin.flow.component.notification.Notification; import com.vaadin.flow.component.orderedlayout.HorizontalLayout; +import com.vaadin.flow.component.orderedlayout.VerticalLayout; import com.vaadin.flow.router.BeforeEvent; import com.vaadin.flow.router.HasUrlParameter; import com.vaadin.flow.router.PageTitle; @@ -33,6 +36,7 @@ public class RequestEmployeeView extends Div implements HasUrlParameter private final TimeOffRequestService requestService; private final EmployeeService employeeService; + private final VacationService vacationService; private final Grid requestGrid = new Grid<>(TimeOffRequest.class); private List requests = Collections.emptyList(); private ComboBox categoryFilter; @@ -40,15 +44,18 @@ public class RequestEmployeeView extends Div implements HasUrlParameter private UUID employeeId; private TimeOffRequest request; - public RequestEmployeeView(final TimeOffRequestService requestService, final EmployeeService employeeService) { + public RequestEmployeeView(final TimeOffRequestService requestService, + final EmployeeService employeeService, + final VacationService vacationService) { this.requestService = requestService; this.employeeService = employeeService; + this.vacationService = vacationService; } private void initializeView() { setupFilters(); setupGrid(); - add(requestGrid, createActionButtons()); + add(requestGrid, createActionButtons(), createSummaryLayout()); refreshRequestGrid(null, null); } @@ -92,6 +99,34 @@ public class RequestEmployeeView extends Div implements HasUrlParameter }); } + private VerticalLayout createSummaryLayout() { + double totalHoliday = requests.stream() + .filter(this::verificationIsHoliday) + .mapToDouble(TimeOffRequest::getAvailableDays) + .sum(); + double totalVacations = requests.stream() + .filter(req -> req.getCategory().toString().startsWith("VACATION")) + .mapToDouble(TimeOffRequest::getAvailableDays) + .sum(); + double totalPersonalDays = requests.stream() + .filter(req -> !verificationIsHoliday(req)) // Solo los de tipo OTHER + .mapToDouble(TimeOffRequest::getAvailableDays) + .sum(); + double totalAvailableDays = totalHoliday + totalVacations + totalPersonalDays; + + return new VerticalLayout( + new Span("TOTAL HOLIDAYS: " + totalHoliday), + new Span("TOTAL VACATIONS: " + totalVacations), + new Span("TOTAL PERSONAL DAYS: " + totalPersonalDays), + new Span("TOTAL GENERAL: " + totalAvailableDays) + ); + } + + private Boolean verificationIsHoliday(final TimeOffRequest request) { + Vacation vacation = vacationService.findVacationByCategory(request.getCategory()); + return vacation.getType() != Vacation.Type.OTHER; + } + private HorizontalLayout createActionButtons() { Button viewButton = new Button("View", event -> { if (request != null) { diff --git a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java index b81047c..a3b1f54 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java @@ -148,8 +148,8 @@ public class RequestRegisterView extends VerticalLayout { if (category == TimeOffRequestType.HEALTH_PERMIT || category == TimeOffRequestType.VACATION_CURRENT_MANAGEMENT || category == TimeOffRequestType.VACATION_PREVIOUS_MANAGEMENT) { - return latestRequest.getState() == TimeOffRequestStatus.EXPIRED || - (latestRequest.getState() == TimeOffRequestStatus.TAKEN && latestRequest.getDaysBalance() > 0); + return latestRequest.getState() == TimeOffRequestStatus.EXPIRED + || (latestRequest.getState() == TimeOffRequestStatus.TAKEN && latestRequest.getDaysBalance() > 0); } else { return latestRequest.getState() == TimeOffRequestStatus.EXPIRED; } @@ -180,7 +180,8 @@ public class RequestRegisterView extends VerticalLayout { List requests = requestService.findByEmployeeAndCategory(employeeId, selectedCategory); if (vacation != null) { TimeOffRequest requestWithBalance = requests.stream() - .filter(request -> request.getDaysBalance() > 0 && request.getState() != TimeOffRequestStatus.EXPIRED) + .filter(request -> request.getDaysBalance() > 0 + && request.getState() != TimeOffRequestStatus.EXPIRED) .max(Comparator.comparing(TimeOffRequest::getStartDate)) .orElse(null); if (requestWithBalance != null) { @@ -214,7 +215,8 @@ public class RequestRegisterView extends VerticalLayout { endDate = null; UUID employeeId = employee.getId(); - List previousRequests = requestService.findByEmployeeAndCategory(employeeId, vacation.getCategory()); + List previousRequests + = requestService.findByEmployeeAndCategory(employeeId, vacation.getCategory()); int startYear; if (previousRequests.isEmpty()) { @@ -236,11 +238,17 @@ public class RequestRegisterView extends VerticalLayout { } if (vacation.getMonthOfYear() != null && vacation.getDayOfMonth() != null) { - startDate = LocalDate.of(startYear, vacation.getMonthOfYear().intValue(), vacation.getDayOfMonth().intValue()); + startDate = LocalDate.of( + startYear, + vacation.getMonthOfYear().intValue(), + vacation.getDayOfMonth().intValue()); endDate = startDate.plusDays(vacation.getExpiration().intValue() - 1); } else { if (vacation.getCategory() == TimeOffRequestType.BIRTHDAY && employee.getBirthday() != null) { - startDate = LocalDate.of(startYear, employee.getBirthday().getMonth(), employee.getBirthday().getDayOfMonth()); + startDate = LocalDate.of( + startYear, + employee.getBirthday().getMonth(), + employee.getBirthday().getDayOfMonth()); endDate = startDate.plusDays(vacation.getExpiration().intValue() - 1); } else if (vacation.getCategory() == TimeOffRequestType.HEALTH_PERMIT) { startDate = LocalDate.now();