diff --git a/src/main/java/com/primefactorsolutions/Application.java b/src/main/java/com/primefactorsolutions/Application.java index 298fd56..e66d089 100644 --- a/src/main/java/com/primefactorsolutions/Application.java +++ b/src/main/java/com/primefactorsolutions/Application.java @@ -4,7 +4,6 @@ import com.vaadin.flow.component.page.AppShellConfigurator; import com.vaadin.flow.theme.Theme; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; - /** * The entry point of the Spring Boot application. * diff --git a/src/main/java/com/primefactorsolutions/model/TimeOffRequestStatus.java b/src/main/java/com/primefactorsolutions/model/TimeOffRequestStatus.java index ac6f552..6f0a2d0 100644 --- a/src/main/java/com/primefactorsolutions/model/TimeOffRequestStatus.java +++ b/src/main/java/com/primefactorsolutions/model/TimeOffRequestStatus.java @@ -3,13 +3,14 @@ package com.primefactorsolutions.model; public enum TimeOffRequestStatus { TODOS, TOMADO, - SOLICITADO, APROBADO, EN_USO, - EN_REVISION, PENDIENTE, RECHAZADO, + VENCIDO, + + SOLICITADO, + EN_REVISION, COMPLETADO, CANCELADO, - VENCIDO } diff --git a/src/main/java/com/primefactorsolutions/service/TimeOffRequestService.java b/src/main/java/com/primefactorsolutions/service/TimeOffRequestService.java index 65651a8..e652266 100644 --- a/src/main/java/com/primefactorsolutions/service/TimeOffRequestService.java +++ b/src/main/java/com/primefactorsolutions/service/TimeOffRequestService.java @@ -5,6 +5,7 @@ import com.primefactorsolutions.repositories.TimeOffRequestRepository; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; +import java.time.LocalDate; import java.util.List; import java.util.UUID; import java.util.Optional; @@ -50,4 +51,32 @@ public class TimeOffRequestService { public List findByEmployeeAndCategory(final UUID employeeId, final TimeOffRequestType category) { return timeOffRequestRepository.findByEmployeeIdAndCategory(employeeId, category); } + + public void updateRequestStatuses() { + List requests = findAllTimeOffRequests(); + LocalDate now = LocalDate.now(); + LocalDate startOfYear = LocalDate.of(now.getYear(), 1, 1); + + requests.removeIf(request -> request.getCategory() == TimeOffRequestType.VACACION_GESTION_ANTERIOR); + + for (TimeOffRequest request : requests) { + if (request.getCategory() == TimeOffRequestType.VACACION_GESTION_ACTUAL && now.isEqual(startOfYear)) { + request.setCategory(TimeOffRequestType.VACACION_GESTION_ANTERIOR); + } + + if (request.getState() == TimeOffRequestStatus.APROBADO + || request.getState() == TimeOffRequestStatus.EN_USO) { + LocalDate startDate = request.getStartDate(); + LocalDate endDate = request.getEndDate(); + + if (now.isAfter(endDate)) { + request.setState(TimeOffRequestStatus.TOMADO); + } else if (now.isEqual(startDate) || (now.isAfter(startDate) && now.isBefore(endDate))) { + request.setState(TimeOffRequestStatus.EN_USO); + } + } + } + + saveAll(requests); + } } \ No newline at end of file diff --git a/src/main/java/com/primefactorsolutions/views/MainView.java b/src/main/java/com/primefactorsolutions/views/MainView.java index bc3239d..7dc4aba 100644 --- a/src/main/java/com/primefactorsolutions/views/MainView.java +++ b/src/main/java/com/primefactorsolutions/views/MainView.java @@ -1,7 +1,5 @@ package com.primefactorsolutions.views; -import com.primefactorsolutions.model.TimeOffRequest; -import com.primefactorsolutions.model.TimeOffRequestStatus; import com.primefactorsolutions.service.TimeOffRequestService; import com.vaadin.flow.component.Text; import com.vaadin.flow.component.html.Main; @@ -9,42 +7,12 @@ import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.Route; import jakarta.annotation.security.PermitAll; -import java.time.LocalDate; -import java.util.List; - @PageTitle("Home") @Route(value = "", layout = MainLayout.class) @PermitAll public class MainView extends Main { - private final TimeOffRequestService requestService; - public MainView(final TimeOffRequestService requestService) { - this.requestService = requestService; add(new Text("Welcome")); - updateRequestStatuses(); - } - - private void updateRequestStatuses() { - List requests = requestService.findAllTimeOffRequests(); - LocalDate now = LocalDate.now(); - - for (TimeOffRequest request : requests) { - if (request.getState() == TimeOffRequestStatus.APROBADO) { - LocalDate expirationDate = request.getExpiration(); - LocalDate startDate = request.getStartDate(); - LocalDate endDate = request.getEndDate(); - - if (now.isAfter(expirationDate)) { - request.setState(TimeOffRequestStatus.VENCIDO); - } else if (now.isAfter(endDate) && now.isBefore(expirationDate)) { - request.setState(TimeOffRequestStatus.TOMADO); - } else if (now.isEqual(startDate) || now.isAfter(startDate) && now.isBefore(endDate)) { - request.setState(TimeOffRequestStatus.EN_USO); - } - } - } - - requestService.saveAll(requests); } } diff --git a/src/main/java/com/primefactorsolutions/views/PendingRequestsListView.java b/src/main/java/com/primefactorsolutions/views/PendingRequestsListView.java index 1ef9f4e..db9b783 100644 --- a/src/main/java/com/primefactorsolutions/views/PendingRequestsListView.java +++ b/src/main/java/com/primefactorsolutions/views/PendingRequestsListView.java @@ -113,7 +113,7 @@ public class PendingRequestsListView extends Main { final Team team, final TimeOffRequestType category) { List filteredPendingRequests - = requestService.findRequestsByState(TimeOffRequestStatus.PENDIENTE); + = requestService.findRequestsByState(TimeOffRequestStatus.SOLICITADO); if (employee != null && !"TODOS".equals(employee.getFirstName())) { filteredPendingRequests = filteredPendingRequests.stream() diff --git a/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java b/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java index 50445d4..6737dc5 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java @@ -23,9 +23,7 @@ import org.vaadin.firitin.components.grid.PagingGrid; import java.time.LocalDate; import java.time.Period; -import java.util.Collections; -import java.util.List; -import java.util.UUID; +import java.util.*; import java.util.stream.Collectors; @SpringComponent @@ -54,6 +52,7 @@ public class RequestEmployeeView extends Div implements HasUrlParameter } private void initializeView() { + requestService.updateRequestStatuses(); setupFilters(); setupGrid(); add(requestGrid, createActionButtons(), createSummaryLayout()); @@ -106,29 +105,98 @@ public class RequestEmployeeView extends Div implements HasUrlParameter }); } - private VerticalLayout createSummaryLayout() { - double totalHoliday = requests.stream() - .filter(this::verificationIsHoliday) - .mapToDouble(TimeOffRequest::getAvailableDays) - .sum(); - double totalVacations = calculateVacationDays(employeeService.getEmployee(employeeId)); - double totalPersonalDays = requests.stream() - .filter(req -> !verificationIsHoliday(req)) - .filter(req -> !req.getCategory().name().startsWith("VACACION")) - .mapToDouble(TimeOffRequest::getAvailableDays) - .sum(); - - double totalAvailableDays = totalHoliday + totalVacations + totalPersonalDays; - - return new VerticalLayout( - new Span("Total feriados: " + totalHoliday), - new Span("Total vacaciones: " + totalVacations), - new Span("Total días personales: " + totalPersonalDays), - new Span("Total general: " + totalAvailableDays) + private Set getStandardExclusions() { + return Set.of( + TimeOffRequestType.MATERNIDAD, + TimeOffRequestType.PATERNIDAD, + TimeOffRequestType.MATRIMONIO, + TimeOffRequestType.DUELO_1ER_GRADO, + TimeOffRequestType.DUELO_2ER_GRADO, + TimeOffRequestType.DIA_DEL_PADRE, + TimeOffRequestType.DIA_DE_LA_MADRE ); } - private double calculateVacationDays(final Employee employee) { + private Set getMaleSpecificExclusions() { + return Set.of( + TimeOffRequestType.DIA_DE_LA_MUJER_INTERNACIONAL, + TimeOffRequestType.DIA_DE_LA_MUJER_NACIONAL + ); + } + + private VerticalLayout createSummaryLayout() { + Employee employee = employeeService.getEmployee(employeeId); + boolean isMale = employee.getGender() == Employee.Gender.MALE; + int currentYear = LocalDate.now().getYear(); + + List vacations = vacationService.findVacations(); + + double healthLicence = 2; + List healthRequests = requestService + .findByEmployeeAndCategory(employeeId, TimeOffRequestType.PERMISOS_DE_SALUD); + if (healthRequests != null && !healthRequests.isEmpty()) { + healthLicence = healthRequests.getLast().getDaysBalance(); + } + + double totalFixedAndMovableHolidays = calculateHolidayDays(vacations); + double totalPersonalDays = calculatePersonalDays(vacations, isMale); + List vacationDays = calculateVacationDays(employee); + + double utilizedVacationCurrentDays = vacationDays.get(1); + List vacationCurrentRequests = requestService + .findByEmployeeAndCategory(employeeId, TimeOffRequestType.VACACION_GESTION_ACTUAL); + if (vacationCurrentRequests != null && !vacationCurrentRequests.isEmpty()) { + utilizedVacationCurrentDays = vacationCurrentRequests.getLast().getDaysBalance(); + } + double totalVacationCurrentDays = vacationDays.get(1) - (vacationDays.get(1) - utilizedVacationCurrentDays); + + double utilizedVacationPreviousDays = vacationDays.get(0); + List vacationPreviousRequests = requestService + .findByEmployeeAndCategory(employeeId, TimeOffRequestType.VACACION_GESTION_ANTERIOR); + if (vacationPreviousRequests != null && !vacationPreviousRequests.isEmpty()) { + utilizedVacationPreviousDays = vacationPreviousRequests.getLast().getDaysBalance(); + } + double totalVacationPreviousDays = vacationDays.getFirst() + - (vacationDays.getFirst() - utilizedVacationPreviousDays); + + double utilizedFixedAndMovableHolidays = calculateHolidayUtilizedDays(currentYear); + double utilizedPersonalDays = calculatePersonalDaysUtilized(isMale, currentYear); + + double remainingHolidayDays = totalFixedAndMovableHolidays - utilizedFixedAndMovableHolidays; + double remainingPersonalDays = (totalPersonalDays - utilizedPersonalDays) + healthLicence; + double remainingVacationDays = totalVacationCurrentDays + totalVacationPreviousDays; + + double totalAvailableDays = remainingHolidayDays + remainingPersonalDays + remainingVacationDays; + + return new VerticalLayout( + new Span("Total feriados fijos y movibles: " + remainingHolidayDays), + new Span("Total días libres personales: " + remainingPersonalDays), + new Span("Total vacaciones pendientes de uso: " + remainingVacationDays), + new Span("TOTAL GENERAL DE DÍAS DISPONIBLES: " + totalAvailableDays) + ); + } + + private double calculateHolidayDays(final List vacations) { + return vacations.stream() + .filter(req -> req.getType() != Vacation.Type.OTHER) + .mapToDouble(Vacation::getDuration) + .sum(); + } + + private double calculatePersonalDays(final List vacations, final boolean isMale) { + return vacations.stream() + .filter(req -> req.getType() == Vacation.Type.OTHER) + .filter(req -> !getStandardExclusions().contains(req.getCategory())) + .filter(req -> !(isMale && getMaleSpecificExclusions().contains(req.getCategory()))) + .filter(req -> !req.getCategory().name().startsWith("VACACION")) + .filter(req -> req.getCategory() != TimeOffRequestType.PERMISOS_DE_SALUD) + .mapToDouble(Vacation::getDuration) + .sum(); + } + + private List calculateVacationDays(final Employee employee) { + List vacationDays = new ArrayList<>(); + if (employee.getDateOfEntry() != null) { LocalDate entryDate = employee.getDateOfEntry(); LocalDate today = LocalDate.now(); @@ -164,11 +232,48 @@ public class RequestEmployeeView extends Div implements HasUrlParameter ); } - return calculateVacationDaysSinceEntry(entryDate, previousVacationYearDate) - + calculateVacationDaysSinceEntry(entryDate, currentVacationYearDate); + vacationDays.add(calculateVacationDaysSinceEntry(entryDate, previousVacationYearDate)); + vacationDays.add(calculateVacationDaysSinceEntry(entryDate, currentVacationYearDate)); } else { - return 0.0; + vacationDays.add(0.0); + vacationDays.add(0.0); } + return vacationDays; + } + + private double calculateHolidayUtilizedDays(final int year) { + return requests.stream() + .filter(this::verificationIsHoliday) + .filter(req -> getStartDateYear(req) == year) + .mapToDouble(TimeOffRequest::getDaysToBeTake) + .sum(); + } + + private double calculateVacationUtilizedDays(final int year) { + return requests.stream() + .filter(req -> req.getCategory().name().startsWith("VACACION")) + .filter(req -> getStartDateYear(req) == year || getStartDateYear(req) == year - 1) + .mapToDouble(TimeOffRequest::getDaysBalance) + .sum(); + } + + private double calculatePersonalDaysUtilized(final boolean isMale, final int year) { + return requests.stream() + .filter(req -> !verificationIsHoliday(req)) + .filter(req -> !getStandardExclusions().contains(req.getCategory())) + .filter(req -> !(isMale && getMaleSpecificExclusions().contains(req.getCategory()))) + .filter(req -> !req.getCategory().name().startsWith("VACACION")) + .filter(req -> req.getCategory() != TimeOffRequestType.PERMISOS_DE_SALUD) + .filter(req -> getStartDateYear(req) == year) + .mapToDouble(TimeOffRequest::getDaysToBeTake) + .sum(); + } + + private int getStartDateYear(final TimeOffRequest request) { + if (request.getStartDate() != null) { + return request.getStartDate().getYear(); + } + return 0; } private double calculateVacationDaysSinceEntry(final LocalDate dateOfEntry, final LocalDate date) { @@ -236,7 +341,9 @@ public class RequestEmployeeView extends Div implements HasUrlParameter final int pageSize, final TimeOffRequestType category, final TimeOffRequestStatus state) { + requests = requestService.findRequestsByEmployeeId(employeeId); + generateRequests(); if (category != null && !"TODOS".equals(category.name())) { requests = requests.stream() .filter(req -> req.getCategory().equals(category)) @@ -251,6 +358,58 @@ public class RequestEmployeeView extends Div implements HasUrlParameter return requests.subList(start, end); } + public void generateRequests() { + boolean isMale = isEmployeeMale(); + + for (TimeOffRequestType type : TimeOffRequestType.values()) { + if (shouldIncludeRequest(type) && isValidRequestType(type, isMale)) { + TimeOffRequest request = createRequest(type); + if (isVacationExpired(request)) { + request.setState(TimeOffRequestStatus.VENCIDO); + } else { + request.setState(TimeOffRequestStatus.PENDIENTE); + } + requests.add(request); + } + } + } + + private boolean isEmployeeMale() { + return employeeService.getEmployee(employeeId).getGender() == Employee.Gender.MALE; + } + + private boolean isValidRequestType(final TimeOffRequestType type, final boolean isMale) { + return !getStandardExclusions().contains(type) + && !(isMale && getMaleSpecificExclusions().contains(type)) + && type != TimeOffRequestType.TODOS; + } + + private TimeOffRequest createRequest(final TimeOffRequestType type) { + TimeOffRequest request = new TimeOffRequest(); + request.setCategory(type); + return request; + } + + private boolean isVacationExpired(final TimeOffRequest request) { + Vacation vacation = vacationService.findVacationByCategory(request.getCategory()); + + if (vacation != null && vacation.getMonthOfYear() != null && vacation.getDayOfMonth() != null) { + int vacationMonth = vacation.getMonthOfYear(); + int vacationDay = vacation.getDayOfMonth(); + int currentMonth = LocalDate.now().getMonthValue(); + int currentDay = LocalDate.now().getDayOfMonth(); + + return vacationMonth < currentMonth || (vacationMonth == currentMonth && vacationDay < currentDay); + } + + return false; + } + + private boolean shouldIncludeRequest(final TimeOffRequestType type) { + List existingRequest = requestService.findByEmployeeAndCategory(employeeId, type); + return existingRequest.isEmpty(); + } + @Override public void setParameter(final BeforeEvent event, final String parameter) { employeeId = UUID.fromString(parameter); diff --git a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java index ac733ed..e5fa896 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java @@ -63,6 +63,7 @@ public class RequestRegisterView extends VerticalLayout { } private void initializeView() { + requestService.updateRequestStatuses(); configureFormFields(); configureButtons(); configureBinder(); @@ -84,8 +85,6 @@ public class RequestRegisterView extends VerticalLayout { employeeComboBox.setItemLabelGenerator(emp -> emp.getFirstName() + " " + emp.getLastName()); employeeComboBox.addValueChangeListener(event -> { employee = event.getValue(); - System.out.println("Clearing form..." + employee); - handleEmployeeSelection(event.getValue()); }); categoryComboBox.addValueChangeListener(event -> { @@ -136,15 +135,15 @@ public class RequestRegisterView extends VerticalLayout { List availableCategories = allCategories.stream() .filter(category -> isCategoryAvailable(employeeRequests, category)) .filter(category -> isCategoryAllowedByGender(category, employee.getGender())) - .filter(category -> category != TimeOffRequestType.VACACION_GESTION_ANTERIOR - && category != TimeOffRequestType.TODOS) + .filter(category -> category != TimeOffRequestType.TODOS) .toList(); categoryComboBox.setItems(availableCategories); } private void onCategoryChange(final TimeOffRequestType selectedCategory) { - if (selectedCategory == TimeOffRequestType.VACACION_GESTION_ACTUAL) { + if (selectedCategory == TimeOffRequestType.VACACION_GESTION_ACTUAL + || selectedCategory == TimeOffRequestType.VACACION_GESTION_ANTERIOR) { startDatePicker.setEnabled(true); endDatePicker.setEnabled(true); } else { @@ -167,13 +166,21 @@ public class RequestRegisterView extends VerticalLayout { .max(Comparator.comparing(TimeOffRequest::getStartDate)) .orElse(null); - if (category == TimeOffRequestType.PERMISOS_DE_SALUD + boolean isSpecialCategory = category == TimeOffRequestType.PERMISOS_DE_SALUD || category == TimeOffRequestType.VACACION_GESTION_ACTUAL - || category == TimeOffRequestType.VACACION_GESTION_ANTERIOR) { - return latestRequest.getState() == TimeOffRequestStatus.VENCIDO - || (latestRequest.getState() == TimeOffRequestStatus.TOMADO && latestRequest.getDaysBalance() > 0); + || category == TimeOffRequestType.VACACION_GESTION_ANTERIOR; + + if (isSpecialCategory) { + return (latestRequest.getState() == TimeOffRequestStatus.TOMADO + && latestRequest.getDaysBalance() > 0) + || latestRequest.getState() == TimeOffRequestStatus.RECHAZADO + || (latestRequest.getState() == TimeOffRequestStatus.TOMADO + && latestRequest.getDaysBalance() == 0 + && latestRequest.getExpiration().isBefore(LocalDate.now())); } else { - return latestRequest.getState() == TimeOffRequestStatus.VENCIDO; + return (latestRequest.getState() == TimeOffRequestStatus.TOMADO + && latestRequest.getExpiration().isBefore(LocalDate.now())) + || latestRequest.getState() == TimeOffRequestStatus.RECHAZADO; } } @@ -199,30 +206,40 @@ public class RequestRegisterView extends VerticalLayout { vacation = vacationService.findVacationByCategory(selectedCategory); UUID employeeId = employeeComboBox.getValue().getId(); List requests = requestService.findByEmployeeAndCategory(employeeId, selectedCategory); + if (vacation != null) { TimeOffRequest requestWithBalance = requests.stream() .filter(request -> request.getDaysBalance() > 0 - && request.getState() != TimeOffRequestStatus.VENCIDO) + && request.getState() != TimeOffRequestStatus.VENCIDO + && request.getState() != TimeOffRequestStatus.RECHAZADO) .max(Comparator.comparing(TimeOffRequest::getStartDate)) .orElse(null); + if (requestWithBalance != null) { - if (requestWithBalance.getState() == TimeOffRequestStatus.TOMADO) { + if (requestWithBalance.getState() == TimeOffRequestStatus.TOMADO + && requestWithBalance.getDaysBalance() > 0) { availableDaysField.setValue(requestWithBalance.getDaysBalance()); - } else if (requestWithBalance.getState() == TimeOffRequestStatus.VENCIDO) { + } else { availableDaysField.setValue(vacation.getDuration()); } - } else if (vacation.getCategory() == TimeOffRequestType.VACACION_GESTION_ACTUAL) { + } else if (selectedCategory == TimeOffRequestType.VACACION_GESTION_ACTUAL + || selectedCategory == TimeOffRequestType.VACACION_GESTION_ANTERIOR) { LocalDate dateOfEntry = employeeComboBox.getValue().getDateOfEntry(); LocalDate currentDate = LocalDate.now(); long yearsOfService = ChronoUnit.YEARS.between(dateOfEntry, currentDate); + + if (selectedCategory == TimeOffRequestType.VACACION_GESTION_ANTERIOR) { + yearsOfService -= 1; + } + if (yearsOfService > 10) { - availableDaysField.setValue((double) 30); + availableDaysField.setValue(30.0); } else if (yearsOfService > 5) { - availableDaysField.setValue((double) 20); + availableDaysField.setValue(20.0); } else if (yearsOfService > 1) { - availableDaysField.setValue((double) 15); + availableDaysField.setValue(15.0); } else { - availableDaysField.setValue((double) 0); + availableDaysField.setValue(0.0); } } else { availableDaysField.setValue(vacation.getDuration()); @@ -248,7 +265,11 @@ public class RequestRegisterView extends VerticalLayout { } if (startDate != null) { - endDate = startDate.plusDays(vacation.getExpiration().intValue() - 1); + if (vacation.getExpiration() != null) { + endDate = startDate.plusDays(vacation.getExpiration().intValue() - 1); + } else { + endDate = LocalDate.of(startDate.getYear(), 12, 31); + } } else { startDate = LocalDate.now(); } @@ -267,19 +288,23 @@ public class RequestRegisterView extends VerticalLayout { .map(request -> request.getStartDate().getYear()) .orElse(LocalDate.now().getYear()); + if (previousRequests.getLast().getState() != TimeOffRequestStatus.RECHAZADO) { + lastRequestYear = lastRequestYear + 1; + } + int currentYear = LocalDate.now().getYear(); - return Math.max(lastRequestYear + 1, currentYear); + return Math.max(lastRequestYear, currentYear); } private LocalDate determineStartDate(final Vacation vacation, final int startYear) { - if (vacation.getMonthOfYear() != null && vacation.getDayOfMonth() != null) { - return LocalDate.of(startYear, vacation.getMonthOfYear().intValue(), vacation.getDayOfMonth().intValue()); - } - if (vacation.getCategory() == TimeOffRequestType.CUMPLEAÑOS && employee.getBirthday() != null) { return LocalDate.of(startYear, employee.getBirthday().getMonth(), employee.getBirthday().getDayOfMonth()); } + if (vacation.getMonthOfYear() != null && vacation.getDayOfMonth() != null) { + return LocalDate.of(startYear, vacation.getMonthOfYear().intValue(), vacation.getDayOfMonth().intValue()); + } + if (vacation.getCategory() == TimeOffRequestType.PERMISOS_DE_SALUD) { return LocalDate.now(); } @@ -291,7 +316,9 @@ public class RequestRegisterView extends VerticalLayout { startDatePicker.setValue(startDate); if ((vacation.getDuration() != null && vacation.getDuration() == 0.5) - || vacation.getCategory() == TimeOffRequestType.PERMISOS_DE_SALUD) { + || vacation.getCategory() == TimeOffRequestType.PERMISOS_DE_SALUD + || vacation.getCategory() == TimeOffRequestType.CUMPLEAÑOS) { + endDatePicker.setValue(startDate); } else { int durationDays = (vacation.getDuration() != null ? vacation.getDuration().intValue() - 1 : 0); @@ -330,7 +357,7 @@ public class RequestRegisterView extends VerticalLayout { double balanceDays = calculateBalanceDays(availableDays, daysToBeTakenField.getValue()); balanceDaysField.setValue(balanceDays); - if (balanceDays < 0) { + if (balanceDays < 0.0) { clearFields(); } } @@ -345,7 +372,12 @@ public class RequestRegisterView extends VerticalLayout { } private void setDaysToBeTakenField(final double daysToBeTaken) { - if (vacation.getCategory() == TimeOffRequestType.PERMISOS_DE_SALUD) { + if (vacation.getCategory() == TimeOffRequestType.PERMISOS_DE_SALUD + || vacation.getCategory() == TimeOffRequestType.CUMPLEAÑOS + || vacation.getCategory() == TimeOffRequestType.DIA_DEL_PADRE + || vacation.getCategory() == TimeOffRequestType.DIA_DE_LA_MADRE + || vacation.getCategory() == TimeOffRequestType.DIA_DE_LA_MUJER_INTERNACIONAL + || vacation.getCategory() == TimeOffRequestType.DIA_DE_LA_MUJER_NACIONAL) { daysToBeTakenField.setValue(0.5); } else { daysToBeTakenField.setValue(daysToBeTaken); @@ -410,7 +442,7 @@ public class RequestRegisterView extends VerticalLayout { request.setStartDate(startDatePicker.getValue()); request.setAvailableDays(availableDaysField.getValue()); request.setExpiration(endDate != null ? endDate : endDatePicker.getValue()); - request.setState(TimeOffRequestStatus.PENDIENTE); + request.setState(TimeOffRequestStatus.SOLICITADO); return request; } @@ -418,7 +450,10 @@ public class RequestRegisterView extends VerticalLayout { List existingRequests = requestService.findByEmployeeAndCategory(employee.getId(), request.getCategory()); - int maxRequests = request.getCategory() == TimeOffRequestType.PERMISOS_DE_SALUD ? 4 : 2; + int maxRequests = request.getCategory() != TimeOffRequestType.PERMISOS_DE_SALUD + && !request.getCategory().name().startsWith("VACACION") + && request.getCategory() != TimeOffRequestType.CUMPLEAÑOS + ? 2 : 1; if (existingRequests.size() >= maxRequests) { existingRequests.stream() diff --git a/src/main/java/com/primefactorsolutions/views/RequestsListView.java b/src/main/java/com/primefactorsolutions/views/RequestsListView.java index 73c2cbf..4b770e1 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestsListView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestsListView.java @@ -57,6 +57,7 @@ public class RequestsListView extends Main { } private void initializeView() { + requestService.updateRequestStatuses(); setupFilters(); setupRequestGrid(); add(requestGrid); @@ -163,8 +164,29 @@ public class RequestsListView extends Main { private String getGeneralTotal(final Employee employee) { List employeeRequests = requestService.findRequestsByEmployeeId(employee.getId()); List vacations = vacationService.findVacations(); + + List vacationDays = calculateVacationDays(employee); + + double utilizedVacationCurrentDays = vacationDays.get(1); + List vacationCurrentRequests = requestService + .findByEmployeeAndCategory(employee.getId(), TimeOffRequestType.VACACION_GESTION_ACTUAL); + if (vacationCurrentRequests != null && !vacationCurrentRequests.isEmpty()) { + utilizedVacationCurrentDays = vacationCurrentRequests.getLast().getDaysBalance(); + } + double totalVacationCurrentDays = vacationDays.get(1) - (vacationDays.get(1) - utilizedVacationCurrentDays); + + double utilizedVacationPreviousDays = vacationDays.get(0); + List vacationPreviousRequests = requestService + .findByEmployeeAndCategory(employee.getId(), TimeOffRequestType.VACACION_GESTION_ANTERIOR); + if (vacationPreviousRequests != null && !vacationPreviousRequests.isEmpty()) { + utilizedVacationPreviousDays = vacationPreviousRequests.getLast().getDaysBalance(); + } + double totalVacationPreviousDays = vacationDays.getFirst() + - (vacationDays.getFirst() - utilizedVacationPreviousDays); + + double totalUtilized = calculateTotalUtilized(employeeRequests); - double totalVacations = calculateVacationDays(employee); + double totalVacations = totalVacationCurrentDays + totalVacationPreviousDays; double totalAvailable = calculateTotalAvailable(vacations, employeeRequests, employee); double generalTotal = totalAvailable + totalVacations - totalUtilized; @@ -191,13 +213,24 @@ public class RequestsListView extends Main { } private double calculateTotalUtilized(final List employeeRequests) { + int currentYear = LocalDate.now().getYear(); return employeeRequests.stream() .filter(Objects::nonNull) + .filter(request -> request.getCategory() != TimeOffRequestType.PERMISOS_DE_SALUD) + .filter(request -> request.getCategory() != TimeOffRequestType.VACACION_GESTION_ACTUAL) + .filter(request -> request.getCategory() != TimeOffRequestType.VACACION_GESTION_ANTERIOR) + .filter(request -> request.getStartDate() != null && ( + request.getStartDate().getYear() == currentYear + || (request.getCategory().name().startsWith("VACACION") + && request.getStartDate().getYear() == currentYear - 1) + )) .mapToDouble(request -> request.getDaysToBeTake() != null ? request.getDaysToBeTake() : 0.0) .sum(); } - private double calculateVacationDays(final Employee employee) { + private List calculateVacationDays(final Employee employee) { + List vacationDays = new ArrayList<>(); + if (employee.getDateOfEntry() != null) { LocalDate entryDate = employee.getDateOfEntry(); LocalDate today = LocalDate.now(); @@ -233,11 +266,13 @@ public class RequestsListView extends Main { ); } - return calculateVacationDaysSinceEntry(entryDate, previousVacationYearDate) - + calculateVacationDaysSinceEntry(entryDate, currentVacationYearDate); + vacationDays.add(calculateVacationDaysSinceEntry(entryDate, previousVacationYearDate)); + vacationDays.add(calculateVacationDaysSinceEntry(entryDate, currentVacationYearDate)); } else { - return 0.0; + vacationDays.add(0.0); + vacationDays.add(0.0); } + return vacationDays; } private double calculateTotalAvailable(final List vacations, final List employeeRequests, @@ -248,8 +283,16 @@ public class RequestsListView extends Main { .map(TimeOffRequest::getCategory) .collect(Collectors.toSet()); - return vacations.stream() + double healthLicence = 2; + List healthRequests = requestService + .findByEmployeeAndCategory(employee.getId(), TimeOffRequestType.PERMISOS_DE_SALUD); + if (healthRequests != null && !healthRequests.isEmpty()) { + healthLicence = healthRequests.getLast().getDaysBalance(); + } + + double totalAvailable = vacations.stream() .filter(Objects::nonNull) + .filter(vacation -> vacation.getCategory() != TimeOffRequestType.PERMISOS_DE_SALUD) .filter(vacation -> shouldIncludeVacation( vacation, excludedCategories, @@ -258,6 +301,8 @@ public class RequestsListView extends Main { )) .mapToDouble(vacation -> vacation.getDuration() != null ? vacation.getDuration() : 0.0) .sum(); + + return totalAvailable + healthLicence; } private double calculateVacationDaysSinceEntry(final LocalDate dateOfEntry, final LocalDate date) { diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index 5833a0e..7f3c3f6 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -32,7 +32,7 @@ INSERT INTO vacation (id, version, category, month_of_year, day_of_month, durati INSERT INTO vacation (id, version, category, month_of_year, day_of_month, duration, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417400b', 1, 'DIA_DE_TODOS_LOS_DIFUNTOS', 11, 2, 1, 30, 'MOVABLE'); -INSERT INTO vacation (id, version, category, duration, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417400c', 1, 'CUMPLEAÑOS', 0.5, 365, 'OTHER'); +INSERT INTO vacation (id, version, category, month_of_year, day_of_month, duration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417400c', 1, 'CUMPLEAÑOS', 12, 31, 0.5, 'OTHER'); INSERT INTO vacation (id, version, category, duration, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417400d', 1, 'MATERNIDAD', 90, 90, 'OTHER'); INSERT INTO vacation (id, version, category, duration, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417400e', 1, 'PATERNIDAD', 3, 3, 'OTHER'); INSERT INTO vacation (id, version, category, duration, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417400f', 1, 'MATRIMONIO', 3, 3, 'OTHER'); @@ -68,25 +68,25 @@ insert into employee (id, version, username, first_name, last_name, status, team insert into time_off_request (id, version, employee_id, category, state, available_days, expiration, start_date, end_date, days_to_be_take, days_balance) -values ('9d6f12ba-e341-4e7a-b8a6-cab0982bd8c1', 1, '5c6f11fe-c341-4be7-a9a6-bba0081ad7c6', 'PATERNIDAD', 'VENCIDO', 3, '2024-10-03', '2024-10-01', '2024-10-03', 3, 0); +values ('9d6f12ba-e341-4e7a-b8a6-cab0982bd8c1', 1, '5c6f11fe-c341-4be7-a9a6-bba0081ad7c6', 'PATERNIDAD', 'APROBADO', 3, '2024-10-03', '2024-10-01', '2024-10-03', 3, 0); insert into time_off_request (id, version, employee_id, category, state, available_days, expiration, start_date, end_date, days_to_be_take, days_balance) -values ('9a6f12ba-e111-4e7a-b8a6-caa0982bd8a1', 1, '5c6f11fe-c341-4be7-a9a6-bba0081ad7c6', 'AÑO_NUEVO', 'VENCIDO', 1, '2024-01-01', '2024-01-01', '2024-01-01', 1, 0); +values ('9a6f12ba-e111-4e7a-b8a6-caa0982bd8a1', 1, '5c6f11fe-c341-4be7-a9a6-bba0081ad7c6', 'AÑO_NUEVO', 'APROBADO', 1, '2024-01-01', '2024-01-01', '2024-01-01', 1, 0); insert into time_off_request (id, version, employee_id, category, state, available_days, expiration, start_date, end_date, days_to_be_take, days_balance) values ('9b6f12ba-e222-4e7a-b8a6-caa0982bd8b2', 1, '5c6f11fe-c341-4be7-a9a6-bba0081ad7c6', 'LUNES_CARNAVAL', 'APROBADO', 1, '2025-02-12', '2025-02-12', '2025-02-12', 1, 0); insert into time_off_request (id, version, employee_id, category, state, available_days, expiration, start_date, end_date, days_to_be_take, days_balance) -values ('9c6f12ba-e333-4e7a-b8a6-caa0982bd8c3', 1, '5c6f11fe-c341-4be7-a9a6-bba0081ad7c6', 'MARTES_CARNAVAL', 'EN_REVISION', 1, '2025-02-13', '2025-02-13', '2025-02-13', 1, 0); +values ('9c6f12ba-e333-4e7a-b8a6-caa0982bd8c3', 1, '5c6f11fe-c341-4be7-a9a6-bba0081ad7c6', 'MARTES_CARNAVAL', 'SOLICITADO', 1, '2025-02-13', '2025-02-13', '2025-02-13', 1, 0); insert into time_off_request (id, version, employee_id, category, state, available_days, expiration, start_date, end_date, days_to_be_take, days_balance) values ('9d6f12ba-e444-4e7a-b8a6-caa0982bd8d4', 1, '5c6f11fe-c341-4be7-a9a6-bba0081ad7c6', 'VIERNES_SANTO', 'APROBADO', 1, '2024-03-29', '2024-03-29', '2024-03-29', 1, 0); insert into time_off_request (id, version, employee_id, category, state, available_days, expiration, start_date, end_date, days_to_be_take, days_balance) -values ('9e6f12ba-e555-4e7a-b8a6-caa0982bd8e5', 1, '5c6f11fe-c341-4be7-a9a6-bba0081ad7c6', 'VACACION_GESTION_ACTUAL', 'APROBADO', 30, '2024-11-01', '2022-11-01', '2022-11-30', 30, 0); +values ('9e6f12ba-e555-4e7a-b8a6-caa0982bd8e5', 1, '5c6f11fe-c341-4be7-a9a6-bba0081ad7c6', 'VACACION_GESTION_ACTUAL', 'APROBADO', 30, '2025-11-01', '2023-11-01', '2023-11-30', 30, 0); insert into time_off_request (id, version, employee_id, category, state, available_days, expiration, start_date, end_date, days_to_be_take, days_balance) values ('8c653f2a-f9a3-4d67-b3b6-12ad98fe0983', 1, 'f6ab3c6d-7078-45f6-9b22-4e37637bfec6', 'DIA_DEL_TRABAJADOR', 'APROBADO', 1, '2024-05-01', '2024-05-01', '2024-05-01', 1, 0); insert into time_off_request (id, version, employee_id, category, state, available_days, expiration, start_date, end_date, days_to_be_take, days_balance) values ('fb9d9d75-b2ab-4ea4-b8b3-0a8f89e5c123', 1, '2e2293b1-3f9a-4f3d-abc8-32639b0a5e15', 'DIA_DE_LA_INDEPENDENCIA', 'APROBADO', 1, '2024-08-06', '2024-08-06', '2024-08-06', 1, 0); insert into time_off_request (id, version, employee_id, category, state, available_days, expiration, start_date, end_date, days_to_be_take, days_balance) -values ('6fdc47a8-127b-41c4-8d12-7fc12098ab12', 1, 'b2436b82-7b9f-4f0d-9463-f2c3173a45c3', 'DIA_DE_TODOS_LOS_DIFUNTOS', 'PENDIENTE', 1, '2025-12-01', '2025-11-02', '2025-11-02', 1, 0); +values ('6fdc47a8-127b-41c4-8d12-7fc12098ab12', 1, 'b2436b82-7b9f-4f0d-9463-f2c3173a45c3', 'DIA_DE_TODOS_LOS_DIFUNTOS', 'SOLICITADO', 1, '2025-12-01', '2025-11-02', '2025-11-02', 1, 0); insert into time_off_request (id, version, employee_id, category, state, available_days, expiration, start_date, end_date, days_to_be_take, days_balance) -values ('12ec8b74-983d-4a17-b67e-134f45ae904c', 1, '5c1a7b82-832d-4f24-8377-54b77b91b6a8', 'AÑO_NUEVO', 'PENDIENTE', 1, '2025-01-01', '2025-01-01', '2025-01-01', 1, 0); +values ('12ec8b74-983d-4a17-b67e-134f45ae904c', 1, '5c1a7b82-832d-4f24-8377-54b77b91b6a8', 'AÑO_NUEVO', 'SOLICITADO', 1, '2025-01-01', '2025-01-01', '2025-01-01', 1, 0); insert into time_off_request (id, version, employee_id, category, state, available_days, expiration, start_date, end_date, days_to_be_take, days_balance) values ('89bc4b2a-943f-487c-a9f3-bacf78145e67', 1, 'cba3efb7-32bc-44be-9fdc-fc5e4f211254', 'LUNES_CARNAVAL', 'APROBADO', 1, '2024-02-12', '2024-02-12', '2024-02-12', 1, 0);