From 7d20703a7d92b172ec0d959e2abceb6237e1f122 Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Mon, 28 Oct 2024 00:55:41 -0400 Subject: [PATCH] =?UTF-8?q?#55=20Perfil=20de=20Personal=20Administrativo?= =?UTF-8?q?=20-=20A=C3=B1adir=20Vacaciones=20del=20Empleado=20(Verificacio?= =?UTF-8?q?n=20de=20validaciones)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../views/RequestRegisterView.java | 62 +++++++++++-------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java index 1176231..d380fed 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java @@ -24,7 +24,6 @@ import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.UUID; -import java.util.stream.Collectors; @SpringComponent @PermitAll @@ -132,7 +131,8 @@ public class RequestRegisterView extends VerticalLayout { categoryComboBox.setItems(availableCategories); } - private boolean isCategoryAvailable(List employeeRequests, TimeOffRequestType category) { + private boolean isCategoryAvailable(final List employeeRequests, + final TimeOffRequestType category) { List requestsByCategory = employeeRequests.stream() .filter(request -> request.getCategory() == category) .toList(); @@ -148,7 +148,7 @@ public class RequestRegisterView extends VerticalLayout { return latestRequest.getState() == TimeOffRequestStatus.EXPIRED; } - private boolean isCategoryAllowedByGender(TimeOffRequestType category, Employee.Gender gender) { + private boolean isCategoryAllowedByGender(final TimeOffRequestType category, final Employee.Gender gender) { if (gender == Employee.Gender.MALE) { return category != TimeOffRequestType.MATERNITY && category != TimeOffRequestType.MOTHERS_DAY @@ -196,29 +196,43 @@ public class RequestRegisterView extends VerticalLayout { private void setDatePickerLimits(final Vacation vacation) { LocalDate startDate; + endDate = null; + if (vacation.getMonthOfYear() != null && vacation.getDayOfMonth() != null) { - startDate = LocalDate.of(LocalDate.now().getYear(), vacation.getMonthOfYear(), vacation.getDayOfMonth()); + startDate = LocalDate.of( + LocalDate.now().getYear(), + vacation.getMonthOfYear().intValue(), + vacation.getDayOfMonth().intValue() + ); endDate = startDate.plusDays(vacation.getExpiration().intValue() - 1); - startDatePicker.setValue(startDate); - endDatePicker.setValue(startDate.plusDays(vacation.getDuration().intValue() - 1)); } else { - if (vacation.getCategory() == TimeOffRequestType.BIRTHDAY) { + if (vacation.getCategory() == TimeOffRequestType.BIRTHDAY && employee.getBirthday() != null) { startDate = employee.getBirthday(); endDate = startDate.plusDays(vacation.getExpiration().intValue() - 1); - startDatePicker.setValue(startDate); - endDatePicker.setValue(startDate.plusDays(vacation.getDuration().intValue() - 1)); + } else if (vacation.getCategory() == TimeOffRequestType.HEALTH_PERMIT) { + startDate = LocalDate.now(); + endDate = LocalDate.of(LocalDate.now().getYear(), 12, 31); } else { startDate = LocalDate.now(); - endDate = null; } } + startDatePicker.setValue(startDate); + + if ((vacation.getDuration() != null && vacation.getDuration() == 0.5) + || vacation.getCategory() == TimeOffRequestType.HEALTH_PERMIT) { + endDatePicker.setValue(startDate); + } else { + int durationDays = (vacation.getDuration() != null ? vacation.getDuration().intValue() - 1 : 0); + endDatePicker.setValue(startDate.plusDays(durationDays)); + } + startDatePicker.setMin(startDate); startDatePicker.setMax(endDate); endDatePicker.setMin(startDate); endDatePicker.setMax(endDate); - } + private void updateDatePickerMinValues() { LocalDate startDate = startDatePicker.getValue(); if (vacation.getDuration() == 0.5) { @@ -236,15 +250,12 @@ public class RequestRegisterView extends VerticalLayout { if (startDate != null && endDate != null) { double daysToBeTaken = java.time.temporal.ChronoUnit.DAYS.between(startDate, endDate) + 1; - if (daysToBeTaken == 1 && vacation.getDuration() == 0.5) { - daysToBeTaken = 0.5; - } - if (vacation.getCategory() == TimeOffRequestType.HEALTH_PERMIT && daysToBeTaken == 1) { + if (vacation.getCategory() == TimeOffRequestType.HEALTH_PERMIT || vacation.getDuration() == 0.5) { daysToBeTakenField.setValue(0.5); } else { daysToBeTakenField.setValue(daysToBeTaken); } - double balanceDays = availableDays - daysToBeTaken; + double balanceDays = availableDays - daysToBeTakenField.getValue(); balanceDaysField.setValue(balanceDays); } } @@ -272,18 +283,19 @@ public class RequestRegisterView extends VerticalLayout { if (binder.validate().isOk()) { TimeOffRequest request = binder.getBean(); request.setStartDate(startDatePicker.getValue()); - if (vacation.getCategory() == TimeOffRequestType.HEALTH_PERMIT) { - request.setExpiration(LocalDate.of(LocalDate.now().getYear(), 12, 31)); - } else if (request.getExpiration() == null) { - request.setExpiration(startDatePicker.getValue().plusDays(vacation.getExpiration().intValue() - 1)); - } - List existingRequests = requestService.findByEmployeeAndCategory(employee.getId(), request.getCategory()); + request.setAvailableDays(availableDaysField.getValue()); + request.setExpiration(endDate); + request.setState(TimeOffRequestStatus.REQUESTED); + + List existingRequests = + requestService.findByEmployeeAndCategory(employee.getId(), request.getCategory()); if (existingRequests.size() >= 2) { - existingRequests.stream().min(Comparator.comparing(TimeOffRequest::getStartDate)).ifPresent(oldestRequest -> requestService.deleteTimeOffRequest(oldestRequest.getId())); - + existingRequests.stream() + .min(Comparator.comparing(TimeOffRequest::getStartDate)) + .ifPresent(oldestRequest -> requestService.deleteTimeOffRequest(oldestRequest.getId())); } - request.setState(TimeOffRequestStatus.REQUESTED); + updateBalanceForCategory(request); requestService.saveTimeOffRequest(request); Notification.show("Request saved successfully.");