#55 Perfil de Personal Administrativo - Añadir Vacaciones del Empleado (Configurar si DaysBalance sea mayor a cero)

This commit is contained in:
jesus.pelaez 2024-10-22 00:20:13 -04:00
parent a5e166351a
commit 0117915b9d
3 changed files with 31 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package com.primefactorsolutions.repositories;
import com.primefactorsolutions.model.TimeOffRequest; import com.primefactorsolutions.model.TimeOffRequest;
import com.primefactorsolutions.model.TimeOffRequestStatus; import com.primefactorsolutions.model.TimeOffRequestStatus;
import com.primefactorsolutions.model.TimeOffRequestType;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List; import java.util.List;
@ -11,4 +12,5 @@ import java.util.UUID;
public interface TimeOffRequestRepository extends JpaRepository<TimeOffRequest, UUID> { public interface TimeOffRequestRepository extends JpaRepository<TimeOffRequest, UUID> {
List<TimeOffRequest> findByEmployeeId(UUID idEmployee); List<TimeOffRequest> findByEmployeeId(UUID idEmployee);
Optional<TimeOffRequest> findByEmployeeIdAndState(UUID employeeId, TimeOffRequestStatus state); Optional<TimeOffRequest> findByEmployeeIdAndState(UUID employeeId, TimeOffRequestStatus state);
List<TimeOffRequest> findByEmployeeIdAndCategory(UUID employeeId, TimeOffRequestType category);
} }

View File

@ -38,4 +38,8 @@ public class TimeOffRequestService {
public Optional<TimeOffRequest> findByEmployeeAndState(final UUID employeeId, final TimeOffRequestStatus state) { public Optional<TimeOffRequest> findByEmployeeAndState(final UUID employeeId, final TimeOffRequestStatus state) {
return timeOffRequestRepository.findByEmployeeIdAndState(employeeId, state); return timeOffRequestRepository.findByEmployeeIdAndState(employeeId, state);
} }
public List<TimeOffRequest> findByEmployeeAndCategory(final UUID employeeId, final TimeOffRequestType category) {
return timeOffRequestRepository.findByEmployeeIdAndCategory(employeeId, category);
}
} }

View File

@ -67,6 +67,8 @@ public class RequestRegisterView extends VerticalLayout {
employeeComboBox.setItemLabelGenerator(emp -> emp.getFirstName() + " " + emp.getLastName()); employeeComboBox.setItemLabelGenerator(emp -> emp.getFirstName() + " " + emp.getLastName());
employeeComboBox.addValueChangeListener(event -> handleEmployeeSelection(event.getValue())); employeeComboBox.addValueChangeListener(event -> handleEmployeeSelection(event.getValue()));
categoryComboBox.setEnabled(false); categoryComboBox.setEnabled(false);
startDatePicker.setEnabled(false);
endDatePicker.setEnabled(false);
categoryComboBox.addValueChangeListener(event -> handleCategorySelection(event.getValue())); categoryComboBox.addValueChangeListener(event -> handleCategorySelection(event.getValue()));
startDatePicker.addValueChangeListener(event -> updateDatePickerMinValues()); startDatePicker.addValueChangeListener(event -> updateDatePickerMinValues());
endDatePicker.addValueChangeListener(event -> calculateDays()); endDatePicker.addValueChangeListener(event -> calculateDays());
@ -115,6 +117,7 @@ public class RequestRegisterView extends VerticalLayout {
private void filterCategories(final Employee employee) { private void filterCategories(final Employee employee) {
List<TimeOffRequest> employeeRequests = requestService.findRequestsByEmployeeId(employee.getId()); List<TimeOffRequest> employeeRequests = requestService.findRequestsByEmployeeId(employee.getId());
List<TimeOffRequestType> requestedCategories = employeeRequests.stream() List<TimeOffRequestType> requestedCategories = employeeRequests.stream()
.filter(request -> request.getDaysBalance() <= 0)
.map(TimeOffRequest::getCategory) .map(TimeOffRequest::getCategory)
.toList(); .toList();
List<TimeOffRequestType> availableCategories = Arrays.stream(TimeOffRequestType.values()) List<TimeOffRequestType> availableCategories = Arrays.stream(TimeOffRequestType.values())
@ -138,13 +141,20 @@ public class RequestRegisterView extends VerticalLayout {
clearForm(); clearForm();
if (selectedCategory != null) { if (selectedCategory != null) {
updateAvailableDays(selectedCategory); updateAvailableDays(selectedCategory);
startDatePicker.setEnabled(true);
endDatePicker.setEnabled(true);
} }
} }
private void updateAvailableDays(final TimeOffRequestType selectedCategory) { private void updateAvailableDays(final TimeOffRequestType selectedCategory) {
vacation = vacationService.findVacationByCategory(selectedCategory); vacation = vacationService.findVacationByCategory(selectedCategory);
List<TimeOffRequest> requests = requestService.findByEmployeeAndCategory(employeeComboBox.getValue().getId(), selectedCategory);
if (vacation != null) { if (vacation != null) {
availableDaysField.setValue(vacation.getDuration()); if (!requests.isEmpty()) {
availableDaysField.setValue(requests.getFirst().getDaysBalance());
} else {
availableDaysField.setValue(vacation.getDuration());
}
setDatePickerLimits(vacation); setDatePickerLimits(vacation);
} }
} }
@ -154,6 +164,8 @@ public class RequestRegisterView extends VerticalLayout {
if (vacation.getVacationDate() != null) { if (vacation.getVacationDate() != null) {
startDate = vacation.getVacationDate(); startDate = vacation.getVacationDate();
endDate = startDate.plusDays(vacation.getExpiration().intValue() - 1); endDate = startDate.plusDays(vacation.getExpiration().intValue() - 1);
startDatePicker.setValue(startDate);
endDatePicker.setValue(startDate.plusDays(vacation.getDuration().intValue() - 1));
} else { } else {
startDate = LocalDate.now(); startDate = LocalDate.now();
endDate = null; endDate = null;
@ -234,6 +246,7 @@ public class RequestRegisterView extends VerticalLayout {
TimeOffRequest request = binder.getBean(); TimeOffRequest request = binder.getBean();
request.setExpiration(endDate); request.setExpiration(endDate);
request.setState(TimeOffRequestStatus.REQUESTED); request.setState(TimeOffRequestStatus.REQUESTED);
updateBalanceForCategory(request);
requestService.saveTimeOffRequest(request); requestService.saveTimeOffRequest(request);
Notification.show("Request saved successfully."); Notification.show("Request saved successfully.");
closeForm(); closeForm();
@ -242,6 +255,17 @@ public class RequestRegisterView extends VerticalLayout {
} }
} }
private void updateBalanceForCategory(TimeOffRequest newRequest) {
List<TimeOffRequest> 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() { private void closeForm() {
getUI().ifPresent(ui -> ui.navigate(RequestsListView.class)); getUI().ifPresent(ui -> ui.navigate(RequestsListView.class));
} }