En-desarrollo #62

Merged
alex merged 14 commits from En-desarrollo into main 2024-10-30 20:14:59 +00:00
Showing only changes of commit 7d20703a7d - Show all commits

View File

@ -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<TimeOffRequest> employeeRequests, TimeOffRequestType category) {
private boolean isCategoryAvailable(final List<TimeOffRequest> employeeRequests,
final TimeOffRequestType category) {
List<TimeOffRequest> 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<TimeOffRequest> existingRequests = requestService.findByEmployeeAndCategory(employee.getId(), request.getCategory());
request.setAvailableDays(availableDaysField.getValue());
request.setExpiration(endDate);
request.setState(TimeOffRequestStatus.REQUESTED);
List<TimeOffRequest> 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.");