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..f2302bc 100644 --- a/src/main/java/com/primefactorsolutions/service/TimeOffRequestService.java +++ b/src/main/java/com/primefactorsolutions/service/TimeOffRequestService.java @@ -3,8 +3,10 @@ package com.primefactorsolutions.service; import com.primefactorsolutions.model.*; import com.primefactorsolutions.repositories.TimeOffRequestRepository; import lombok.AllArgsConstructor; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; +import java.time.LocalDate; import java.util.List; import java.util.UUID; import java.util.Optional; @@ -50,4 +52,28 @@ public class TimeOffRequestService { public List findByEmployeeAndCategory(final UUID employeeId, final TimeOffRequestType category) { return timeOffRequestRepository.findByEmployeeIdAndCategory(employeeId, category); } + + @Scheduled(cron = "0 0 0 * * ?") + private void updateRequestStatuses() { + List requests = findAllTimeOffRequests(); + LocalDate now = LocalDate.now(); + + for (TimeOffRequest request : requests) { + if (request.getState() != TimeOffRequestStatus.RECHAZADO) { + 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); + } + } + } + + 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/RequestEmployeeView.java b/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java index 50445d4..ea74641 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java @@ -107,24 +107,49 @@ public class RequestEmployeeView extends Div implements HasUrlParameter } private VerticalLayout createSummaryLayout() { - double totalHoliday = requests.stream() + List vacations = vacationService.findVacations(); + + double holiday = vacations.stream() + .filter(req -> req.getType() != Vacation.Type.OTHER) + .mapToDouble(Vacation::getDuration) + .sum(); + + double personalDays = vacations.stream() + .filter(req -> req.getType() == Vacation.Type.OTHER) + .filter(req -> !req.getCategory().name().startsWith("VACACION")) + + .mapToDouble(Vacation::getDuration) + .sum(); + + double vacation = calculateVacationDays(employeeService.getEmployee(employeeId)); + + double holidayUtilized = requests.stream() .filter(this::verificationIsHoliday) .mapToDouble(TimeOffRequest::getAvailableDays) .sum(); - double totalVacations = calculateVacationDays(employeeService.getEmployee(employeeId)); - double totalPersonalDays = requests.stream() + + double vacationUtilized = requests.stream() + .filter(req -> req.getCategory() == TimeOffRequestType.VACACION_GESTION_ACTUAL) + .mapToDouble(TimeOffRequest::getAvailableDays) + .sum(); + + double personalDaysUtilized = requests.stream() .filter(req -> !verificationIsHoliday(req)) .filter(req -> !req.getCategory().name().startsWith("VACACION")) .mapToDouble(TimeOffRequest::getAvailableDays) .sum(); + double totalHoliday = holiday - holidayUtilized; + double totalPersonalDays = personalDays - personalDaysUtilized; + double totalVacations = vacation - vacationUtilized; + 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) + new Span("Total feriados fijos y movibles: " + totalHoliday), + new Span("Total días libres personales: " + totalPersonalDays), + new Span("Total vacaciones pendientes de uso: " + totalVacations), + new Span("Total general de días disponibles: " + totalAvailableDays) ); } diff --git a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java index ac733ed..d5d677a 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java @@ -84,8 +84,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 -> { @@ -173,7 +171,7 @@ public class RequestRegisterView extends VerticalLayout { return latestRequest.getState() == TimeOffRequestStatus.VENCIDO || (latestRequest.getState() == TimeOffRequestStatus.TOMADO && latestRequest.getDaysBalance() > 0); } else { - return latestRequest.getState() == TimeOffRequestStatus.VENCIDO; + return latestRequest.getState() == TimeOffRequestStatus.VENCIDO || latestRequest.getState() == TimeOffRequestStatus.RECHAZADO; } } @@ -248,11 +246,29 @@ 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(); } + System.out.println(vacation); + System.out.println(vacation); + System.out.println(vacation); + System.out.println(vacation); + System.out.println(vacation); + System.out.println(vacation); + + System.out.println(startDate); + System.out.println(startDate); + System.out.println(startDate); + System.out.println(startDate); + System.out.println(startDate); + System.out.println(startDate); + setPickerValues(vacation, startDate); setPickerLimits(startDate, endDate); } @@ -267,19 +283,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(); } @@ -290,8 +310,24 @@ public class RequestRegisterView extends VerticalLayout { private void setPickerValues(final Vacation vacation, final LocalDate startDate) { startDatePicker.setValue(startDate); + System.out.println(vacation); + System.out.println(vacation); + System.out.println(vacation); + System.out.println(vacation); + System.out.println(vacation); + System.out.println(vacation); + + System.out.println(startDate); + System.out.println(startDate); + System.out.println(startDate); + System.out.println(startDate); + System.out.println(startDate); + System.out.println(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 +366,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 +381,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); @@ -418,7 +459,7 @@ 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().name().startsWith("VACACION") ? 2 : 1; if (existingRequests.size() >= maxRequests) { existingRequests.stream() diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index 5833a0e..f93ac0a 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');