diff --git a/src/main/java/com/primefactorsolutions/repositories/TimeOffRequestRepository.java b/src/main/java/com/primefactorsolutions/repositories/TimeOffRequestRepository.java index 7225b28..1485fa0 100644 --- a/src/main/java/com/primefactorsolutions/repositories/TimeOffRequestRepository.java +++ b/src/main/java/com/primefactorsolutions/repositories/TimeOffRequestRepository.java @@ -2,6 +2,7 @@ package com.primefactorsolutions.repositories; import com.primefactorsolutions.model.TimeOffRequest; import com.primefactorsolutions.model.TimeOffRequestStatus; +import com.primefactorsolutions.model.TimeOffRequestType; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; @@ -11,4 +12,5 @@ import java.util.UUID; public interface TimeOffRequestRepository extends JpaRepository { List findByEmployeeId(UUID idEmployee); Optional findByEmployeeIdAndState(UUID employeeId, TimeOffRequestStatus state); + List findByEmployeeIdAndCategory(UUID employeeId, TimeOffRequestType category); } diff --git a/src/main/java/com/primefactorsolutions/service/TimeOffRequestService.java b/src/main/java/com/primefactorsolutions/service/TimeOffRequestService.java index 2f7f661..c5eba83 100644 --- a/src/main/java/com/primefactorsolutions/service/TimeOffRequestService.java +++ b/src/main/java/com/primefactorsolutions/service/TimeOffRequestService.java @@ -38,4 +38,8 @@ public class TimeOffRequestService { public Optional findByEmployeeAndState(final UUID employeeId, final TimeOffRequestStatus state) { return timeOffRequestRepository.findByEmployeeIdAndState(employeeId, state); } + + public List findByEmployeeAndCategory(final UUID employeeId, final TimeOffRequestType category) { + return timeOffRequestRepository.findByEmployeeIdAndCategory(employeeId, category); + } } \ No newline at end of file diff --git a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java index c4e0fd1..8c08c66 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java @@ -67,6 +67,8 @@ public class RequestRegisterView extends VerticalLayout { employeeComboBox.setItemLabelGenerator(emp -> emp.getFirstName() + " " + emp.getLastName()); employeeComboBox.addValueChangeListener(event -> handleEmployeeSelection(event.getValue())); categoryComboBox.setEnabled(false); + startDatePicker.setEnabled(false); + endDatePicker.setEnabled(false); categoryComboBox.addValueChangeListener(event -> handleCategorySelection(event.getValue())); startDatePicker.addValueChangeListener(event -> updateDatePickerMinValues()); endDatePicker.addValueChangeListener(event -> calculateDays()); @@ -115,6 +117,7 @@ public class RequestRegisterView extends VerticalLayout { private void filterCategories(final Employee employee) { List employeeRequests = requestService.findRequestsByEmployeeId(employee.getId()); List requestedCategories = employeeRequests.stream() + .filter(request -> request.getDaysBalance() <= 0) .map(TimeOffRequest::getCategory) .toList(); List availableCategories = Arrays.stream(TimeOffRequestType.values()) @@ -138,13 +141,20 @@ public class RequestRegisterView extends VerticalLayout { clearForm(); if (selectedCategory != null) { updateAvailableDays(selectedCategory); + startDatePicker.setEnabled(true); + endDatePicker.setEnabled(true); } } private void updateAvailableDays(final TimeOffRequestType selectedCategory) { vacation = vacationService.findVacationByCategory(selectedCategory); + List requests = requestService.findByEmployeeAndCategory(employeeComboBox.getValue().getId(), selectedCategory); if (vacation != null) { - availableDaysField.setValue(vacation.getDuration()); + if (!requests.isEmpty()) { + availableDaysField.setValue(requests.getFirst().getDaysBalance()); + } else { + availableDaysField.setValue(vacation.getDuration()); + } setDatePickerLimits(vacation); } } @@ -154,6 +164,8 @@ public class RequestRegisterView extends VerticalLayout { if (vacation.getVacationDate() != null) { startDate = vacation.getVacationDate(); endDate = startDate.plusDays(vacation.getExpiration().intValue() - 1); + startDatePicker.setValue(startDate); + endDatePicker.setValue(startDate.plusDays(vacation.getDuration().intValue() - 1)); } else { startDate = LocalDate.now(); endDate = null; @@ -234,6 +246,7 @@ public class RequestRegisterView extends VerticalLayout { TimeOffRequest request = binder.getBean(); request.setExpiration(endDate); request.setState(TimeOffRequestStatus.REQUESTED); + updateBalanceForCategory(request); requestService.saveTimeOffRequest(request); Notification.show("Request saved successfully."); closeForm(); @@ -242,6 +255,17 @@ public class RequestRegisterView extends VerticalLayout { } } + private void updateBalanceForCategory(TimeOffRequest newRequest) { + List requests = requestService.findByEmployeeAndCategory( + newRequest.getEmployee().getId(), newRequest.getCategory()); + + for (TimeOffRequest request : requests) { + double newBalance = request.getDaysBalance() - newRequest.getDaysToBeTake(); + request.setDaysBalance(newBalance); + requestService.saveTimeOffRequest(request); + } + } + private void closeForm() { getUI().ifPresent(ui -> ui.navigate(RequestsListView.class)); }