#55 Perfil de Personal Administrativo - Añadir Vacaciones del Empleado (Configurar si DaysBalance sea mayor a cero)
This commit is contained in:
parent
a5e166351a
commit
0117915b9d
@ -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<TimeOffRequest, UUID> {
|
||||
List<TimeOffRequest> findByEmployeeId(UUID idEmployee);
|
||||
Optional<TimeOffRequest> findByEmployeeIdAndState(UUID employeeId, TimeOffRequestStatus state);
|
||||
List<TimeOffRequest> findByEmployeeIdAndCategory(UUID employeeId, TimeOffRequestType category);
|
||||
}
|
||||
|
@ -38,4 +38,8 @@ public class TimeOffRequestService {
|
||||
public Optional<TimeOffRequest> findByEmployeeAndState(final UUID employeeId, final TimeOffRequestStatus state) {
|
||||
return timeOffRequestRepository.findByEmployeeIdAndState(employeeId, state);
|
||||
}
|
||||
|
||||
public List<TimeOffRequest> findByEmployeeAndCategory(final UUID employeeId, final TimeOffRequestType category) {
|
||||
return timeOffRequestRepository.findByEmployeeIdAndCategory(employeeId, category);
|
||||
}
|
||||
}
|
@ -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<TimeOffRequest> employeeRequests = requestService.findRequestsByEmployeeId(employee.getId());
|
||||
List<TimeOffRequestType> requestedCategories = employeeRequests.stream()
|
||||
.filter(request -> request.getDaysBalance() <= 0)
|
||||
.map(TimeOffRequest::getCategory)
|
||||
.toList();
|
||||
List<TimeOffRequestType> 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<TimeOffRequest> requests = requestService.findByEmployeeAndCategory(employeeComboBox.getValue().getId(), selectedCategory);
|
||||
if (vacation != null) {
|
||||
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<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() {
|
||||
getUI().ifPresent(ui -> ui.navigate(RequestsListView.class));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user