diff --git a/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java b/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java index ea74641..055bced 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java @@ -23,9 +23,7 @@ import org.vaadin.firitin.components.grid.PagingGrid; import java.time.LocalDate; import java.time.Period; -import java.util.Collections; -import java.util.List; -import java.util.UUID; +import java.util.*; import java.util.stream.Collectors; @SpringComponent @@ -106,51 +104,68 @@ public class RequestEmployeeView extends Div implements HasUrlParameter }); } + private Set getStandardExclusions() { + return Set.of( + TimeOffRequestType.MATERNIDAD, + TimeOffRequestType.PATERNIDAD, + TimeOffRequestType.MATRIMONIO, + TimeOffRequestType.DUELO_1ER_GRADO, + TimeOffRequestType.DUELO_2ER_GRADO, + TimeOffRequestType.DIA_DEL_PADRE, + TimeOffRequestType.DIA_DE_LA_MADRE + ); + } + + private Set getMaleSpecificExclusions() { + return Set.of( + TimeOffRequestType.DIA_DE_LA_MUJER_INTERNACIONAL, + TimeOffRequestType.DIA_DE_LA_MUJER_NACIONAL + ); + } + private VerticalLayout createSummaryLayout() { + Employee employee = employeeService.getEmployee(employeeId); + boolean isMale = employee.getGender() == Employee.Gender.MALE; + List vacations = vacationService.findVacations(); - double holiday = vacations.stream() + double totalFixedAndMovableHolidays = calculateHolidayDays(vacations); + double totalPersonalDays = calculatePersonalDays(vacations, isMale); + double totalVacationDays = calculateVacationDays(employee); + + double utilizedFixedAndMovableHolidays = calculateHolidayUtilizedDays(); + double utilizedVacationDays = calculateVacationUtilizedDays(); + double utilizedPersonalDays = calculatePersonalDaysUtilized(isMale); + + double remainingHolidayDays = totalFixedAndMovableHolidays - utilizedFixedAndMovableHolidays; + double remainingPersonalDays = totalPersonalDays - utilizedPersonalDays; + double remainingVacationDays = totalVacationDays - utilizedVacationDays; + + double totalAvailableDays = remainingHolidayDays + remainingPersonalDays + remainingVacationDays; + + return new VerticalLayout( + new Span("Total feriados fijos y movibles: " + remainingHolidayDays), + new Span("Total días libres personales: " + remainingPersonalDays), + new Span("Total vacaciones pendientes de uso: " + remainingVacationDays), + new Span("Total general de días disponibles: " + totalAvailableDays) + ); + } + + private double calculateHolidayDays(final List vacations) { + return vacations.stream() .filter(req -> req.getType() != Vacation.Type.OTHER) .mapToDouble(Vacation::getDuration) .sum(); + } - double personalDays = vacations.stream() + private double calculatePersonalDays(final List vacations, final boolean isMale) { + return vacations.stream() .filter(req -> req.getType() == Vacation.Type.OTHER) + .filter(req -> !getStandardExclusions().contains(req.getCategory())) + .filter(req -> !(isMale && getMaleSpecificExclusions().contains(req.getCategory()))) .filter(req -> !req.getCategory().name().startsWith("VACACION")) - .mapToDouble(Vacation::getDuration) .sum(); - - double vacation = calculateVacationDays(employeeService.getEmployee(employeeId)); - - double holidayUtilized = requests.stream() - .filter(this::verificationIsHoliday) - .mapToDouble(TimeOffRequest::getAvailableDays) - .sum(); - - double vacationUtilized = requests.stream() - .filter(req -> req.getCategory() == TimeOffRequestType.VACACION_GESTION_ACTUAL) - .mapToDouble(TimeOffRequest::getAvailableDays) - .sum(); - - double personalDaysUtilized = requests.stream() - .filter(req -> !verificationIsHoliday(req)) - .filter(req -> !req.getCategory().name().startsWith("VACACION")) - .mapToDouble(TimeOffRequest::getAvailableDays) - .sum(); - - double totalHoliday = holiday - holidayUtilized; - double totalPersonalDays = personalDays - personalDaysUtilized; - double totalVacations = vacation - vacationUtilized; - - double totalAvailableDays = totalHoliday + totalVacations + totalPersonalDays; - - return new VerticalLayout( - new Span("Total feriados fijos y movibles: " + totalHoliday), - new Span("Total días libres personales: " + totalPersonalDays), - new Span("Total vacaciones pendientes de uso: " + totalVacations), - new Span("Total general de días disponibles: " + totalAvailableDays) - ); } private double calculateVacationDays(final Employee employee) { @@ -196,6 +211,30 @@ public class RequestEmployeeView extends Div implements HasUrlParameter } } + private double calculateHolidayUtilizedDays() { + return requests.stream() + .filter(this::verificationIsHoliday) + .mapToDouble(TimeOffRequest::getAvailableDays) + .sum(); + } + + private double calculateVacationUtilizedDays() { + return requests.stream() + .filter(req -> req.getCategory() == TimeOffRequestType.VACACION_GESTION_ACTUAL) + .mapToDouble(TimeOffRequest::getAvailableDays) + .sum(); + } + + private double calculatePersonalDaysUtilized(final boolean isMale) { + return requests.stream() + .filter(req -> !verificationIsHoliday(req)) + .filter(req -> !getStandardExclusions().contains(req.getCategory())) + .filter(req -> !(isMale && getMaleSpecificExclusions().contains(req.getCategory()))) + .filter(req -> !req.getCategory().name().startsWith("VACACION")) + .mapToDouble(TimeOffRequest::getAvailableDays) + .sum(); + } + private double calculateVacationDaysSinceEntry(final LocalDate dateOfEntry, final LocalDate date) { int yearsOfService = dateOfEntry != null ? Period.between(dateOfEntry, date).getYears() : 0; if (yearsOfService > 10) { diff --git a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java index d5d677a..864b9e5 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java @@ -171,7 +171,8 @@ public class RequestRegisterView extends VerticalLayout { return latestRequest.getState() == TimeOffRequestStatus.VENCIDO || (latestRequest.getState() == TimeOffRequestStatus.TOMADO && latestRequest.getDaysBalance() > 0); } else { - return latestRequest.getState() == TimeOffRequestStatus.VENCIDO || latestRequest.getState() == TimeOffRequestStatus.RECHAZADO; + return latestRequest.getState() == TimeOffRequestStatus.VENCIDO + || latestRequest.getState() == TimeOffRequestStatus.RECHAZADO; } }