diff --git a/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java b/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java index f1ca462..f5f8e10 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java @@ -80,7 +80,8 @@ public class RequestEmployeeView extends Div implements HasUrlParameter "state", "startDate", "endDate", - "daysToBeTake" + "daysToBeTake", + "daysBalance" ); requestGrid.setAllRowsVisible(true); requestGrid.asSingleSelect().addValueChangeListener(event -> { diff --git a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java index d380fed..b81047c 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java @@ -145,7 +145,14 @@ public class RequestRegisterView extends VerticalLayout { .max(Comparator.comparing(TimeOffRequest::getStartDate)) .orElse(null); - return latestRequest.getState() == TimeOffRequestStatus.EXPIRED; + if (category == TimeOffRequestType.HEALTH_PERMIT + || category == TimeOffRequestType.VACATION_CURRENT_MANAGEMENT + || category == TimeOffRequestType.VACATION_PREVIOUS_MANAGEMENT) { + return latestRequest.getState() == TimeOffRequestStatus.EXPIRED || + (latestRequest.getState() == TimeOffRequestStatus.TAKEN && latestRequest.getDaysBalance() > 0); + } else { + return latestRequest.getState() == TimeOffRequestStatus.EXPIRED; + } } private boolean isCategoryAllowedByGender(final TimeOffRequestType category, final Employee.Gender gender) { @@ -172,8 +179,16 @@ public class RequestRegisterView extends VerticalLayout { UUID employeeId = employeeComboBox.getValue().getId(); List requests = requestService.findByEmployeeAndCategory(employeeId, selectedCategory); if (vacation != null) { - if (!requests.isEmpty() && requests.getFirst().getState() != TimeOffRequestStatus.EXPIRED) { - availableDaysField.setValue(requests.getFirst().getDaysBalance()); + TimeOffRequest requestWithBalance = requests.stream() + .filter(request -> request.getDaysBalance() > 0 && request.getState() != TimeOffRequestStatus.EXPIRED) + .max(Comparator.comparing(TimeOffRequest::getStartDate)) + .orElse(null); + if (requestWithBalance != null) { + if (requestWithBalance.getState() == TimeOffRequestStatus.TAKEN) { + availableDaysField.setValue(requestWithBalance.getDaysBalance()); + } else if (requestWithBalance.getState() == TimeOffRequestStatus.EXPIRED) { + availableDaysField.setValue(vacation.getDuration()); + } } else if (vacation.getCategory() == TimeOffRequestType.VACATION_CURRENT_MANAGEMENT) { LocalDate dateOfEntry = employeeComboBox.getValue().getDateOfEntry(); LocalDate currentDate = LocalDate.now(); @@ -198,20 +213,38 @@ public class RequestRegisterView extends VerticalLayout { LocalDate startDate; endDate = null; + UUID employeeId = employee.getId(); + List previousRequests = requestService.findByEmployeeAndCategory(employeeId, vacation.getCategory()); + + int startYear; + if (previousRequests.isEmpty()) { + startYear = LocalDate.now().getYear(); + } else { + int lastRequestYear = previousRequests.stream() + .max(Comparator.comparing(TimeOffRequest::getStartDate)) + .map(request -> request.getStartDate().getYear()) + .orElse(LocalDate.now().getYear()); + + int proposedYear = lastRequestYear + 1; + int currentYear = LocalDate.now().getYear(); + + if (proposedYear < currentYear) { + startYear = currentYear; + } else { + startYear = proposedYear; + } + } + if (vacation.getMonthOfYear() != null && vacation.getDayOfMonth() != null) { - startDate = LocalDate.of( - LocalDate.now().getYear(), - vacation.getMonthOfYear().intValue(), - vacation.getDayOfMonth().intValue() - ); + startDate = LocalDate.of(startYear, vacation.getMonthOfYear().intValue(), vacation.getDayOfMonth().intValue()); endDate = startDate.plusDays(vacation.getExpiration().intValue() - 1); } else { if (vacation.getCategory() == TimeOffRequestType.BIRTHDAY && employee.getBirthday() != null) { - startDate = employee.getBirthday(); + startDate = LocalDate.of(startYear, employee.getBirthday().getMonth(), employee.getBirthday().getDayOfMonth()); endDate = startDate.plusDays(vacation.getExpiration().intValue() - 1); } else if (vacation.getCategory() == TimeOffRequestType.HEALTH_PERMIT) { startDate = LocalDate.now(); - endDate = LocalDate.of(LocalDate.now().getYear(), 12, 31); + endDate = LocalDate.of(startYear, 12, 31); } else { startDate = LocalDate.now(); } @@ -284,19 +317,24 @@ public class RequestRegisterView extends VerticalLayout { TimeOffRequest request = binder.getBean(); request.setStartDate(startDatePicker.getValue()); request.setAvailableDays(availableDaysField.getValue()); - request.setExpiration(endDate); + if (endDate == null) { + request.setExpiration(endDatePicker.getValue()); + } else { + request.setExpiration(endDate); + } request.setState(TimeOffRequestStatus.REQUESTED); List existingRequests = requestService.findByEmployeeAndCategory(employee.getId(), request.getCategory()); - if (existingRequests.size() >= 2) { + int maxRequests = request.getCategory() == TimeOffRequestType.HEALTH_PERMIT ? 4 : 2; + + if (existingRequests.size() >= maxRequests) { existingRequests.stream() .min(Comparator.comparing(TimeOffRequest::getStartDate)) .ifPresent(oldestRequest -> requestService.deleteTimeOffRequest(oldestRequest.getId())); } - updateBalanceForCategory(request); requestService.saveTimeOffRequest(request); Notification.show("Request saved successfully."); closeForm(); @@ -309,10 +347,13 @@ public class RequestRegisterView extends VerticalLayout { List requests = requestService.findByEmployeeAndCategory( newRequest.getEmployee().getId(), newRequest.getCategory()); - for (TimeOffRequest request : requests) { - double newBalance = request.getDaysBalance() - newRequest.getDaysToBeTake(); - request.setDaysBalance(newBalance); - requestService.saveTimeOffRequest(request); + if (vacation.getCategory() == TimeOffRequestType.HEALTH_PERMIT + && vacation.getCategory() == TimeOffRequestType.VACATION_CURRENT_MANAGEMENT) { + for (TimeOffRequest request : requests) { + double newBalance = request.getDaysBalance() - newRequest.getDaysToBeTake(); + request.setDaysBalance(newBalance); + requestService.saveTimeOffRequest(request); + } } } diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index a09d9cd..4a4a6bd 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -66,43 +66,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', 'PATERNITY', 'TAKEN', 0.5, '2025-12-31', '2024-10-01', '2024-10-10', 0.5, 0); +values ('9d6f12ba-e341-4e7a-b8a6-cab0982bd8c1', 1, '5c6f11fe-c341-4be7-a9a6-bba0081ad7c6', 'PATERNITY', 'TAKEN', 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', 'NEW_YEAR', 'EXPIRED', 1, '2024-01-01', '2023-12-31', '2024-01-01', 1, 0); +values ('9a6f12ba-e111-4e7a-b8a6-caa0982bd8a1', 1, '5c6f11fe-c341-4be7-a9a6-bba0081ad7c6', 'NEW_YEAR', 'EXPIRED', 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', 'MONDAY_CARNIVAL', 'APPROVED', 1, '2025-02-12', '2024-02-12', '2024-02-12', 1, 0); +values ('9b6f12ba-e222-4e7a-b8a6-caa0982bd8b2', 1, '5c6f11fe-c341-4be7-a9a6-bba0081ad7c6', 'MONDAY_CARNIVAL', 'APPROVED', 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', 'TUESDAY_CARNIVAL', 'UNDER_REVIEW', 1, '2025-02-13', '2024-02-13', '2024-02-13', 1, 0); +values ('9c6f12ba-e333-4e7a-b8a6-caa0982bd8c3', 1, '5c6f11fe-c341-4be7-a9a6-bba0081ad7c6', 'TUESDAY_CARNIVAL', 'UNDER_REVIEW', 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', 'GOOD_FRIDAY', 'COMPLETED', 1, '2024-03-29', '2024-03-28', '2024-03-29', 1, 0); +values ('9d6f12ba-e444-4e7a-b8a6-caa0982bd8d4', 1, '5c6f11fe-c341-4be7-a9a6-bba0081ad7c6', 'GOOD_FRIDAY', 'COMPLETED', 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', 'LABOR_DAY', 'REJECTED', 1, '2025-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 ('2fa314bc-f547-4b12-a8b6-bb789feabc12', 1, '19b5a76e-d7b1-4b76-8b02-4d0748e85809', 'BIRTHDAY', 'APPROVED', 15, '2025-12-31', '2024-12-01', '2024-12-15', 7, 8); +values ('8c653f2a-f9a3-4d67-b3b6-12ad98fe0983', 1, 'f6ab3c6d-7078-45f6-9b22-4e37637bfec6', 'LABOR_DAY', 'REQUESTED', 1, '2025-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 ('d5f6341a-913d-4e7f-a0b2-cfe0786acd34', 1, 'cba3efb7-32bc-44be-9fdc-fc5e4f211254', 'HEALTH_PERMIT', 'IN_USE', 20, '2025-11-30', '2024-11-10', '2024-11-20', 10, 10); +values ('fb9d9d75-b2ab-4ea4-b8b3-0a8f89e5c123', 1, '2e2293b1-3f9a-4f3d-abc8-32639b0a5e15', 'INDEPENDENCE_DAY', 'IN_USE', 1, '2025-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 ('4f913b23-ff23-4527-bcd6-adfe01234567', 1, 'e99b7af5-7d3a-4c0f-b8bc-e8d0388d8fc4', 'MATERNITY', 'IN_USE', 18, '2025-06-30', '2024-07-01', '2024-07-15', 10, 8); +values ('6fdc47a8-127b-41c4-8d12-7fc12098ab12', 1, 'b2436b82-7b9f-4f0d-9463-f2c3173a45c3', 'ALL_SOULS_DAY', 'PENDING', 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 ('8c653f2a-f9a3-4d67-b3b6-12ad98fe0983', 1, 'f6ab3c6d-7078-45f6-9b22-4e37637bfec6', 'LABOR_DAY', 'REQUESTED', 10, '2025-10-31', '2024-09-15', '2024-09-20', 5, 5); +values ('12ec8b74-983d-4a17-b67e-134f45ae904c', 1, '5c1a7b82-832d-4f24-8377-54b77b91b6a8', 'NEW_YEAR', 'PENDING', 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 ('fb9d9d75-b2ab-4ea4-b8b3-0a8f89e5c123', 1, '2e2293b1-3f9a-4f3d-abc8-32639b0a5e15', 'INDEPENDENCE_DAY', 'IN_USE', 12, '2025-08-31', '2024-08-05', '2024-08-15', 6, 6); -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 ('1c913a12-46e9-47b7-9e31-ab903fedc789', 1, '4b1c6c35-4627-4b35-b6e9-dc75c68b2c31', 'BIRTHDAY', 'APPROVED', 14, '2025-12-31', '2024-10-20', '2024-10-25', 5, 9); -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 ('b1249d3a-cc34-4954-88d9-1e4f67fe2436', 1, 'afc5c741-f70a-4394-853b-39d51b118927', 'MATERNITY', 'APPROVED', 20, '2025-11-30', '2024-11-05', '2024-11-12', 7, 13); -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', 'ALL_SOULS_DAY', 'PENDING', 18, '2025-06-30', '2024-07-10', '2024-07-20', 8, 10); -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', 'NEW_YEAR', 'PENDING', 15, '2025-12-31', '2024-09-01', '2024-09-05', 4, 11); -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', 'MONDAY_CARNIVAL', 'APPROVED', 20, '2025-11-30', '2024-10-25', '2024-11-05', 9, 11); -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 ('37adfc2a-7463-4b2d-a7c1-fae04567cdef', 1, 'e99b7af5-7d3a-4c0f-b8bc-e8d0388d8fc4', 'BIRTHDAY', 'REQUESTED', 18, '2025-06-30', '2024-06-01', '2024-06-10', 6, 12); -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 ('2bc138ea-12db-4b89-a0b4-78e045e34b4e', 1, 'f6ab3c6d-7078-45f6-9b22-4e37637bfec6', 'MATERNITY', 'REQUESTED', 10, '2025-10-31', '2024-10-01', '2024-10-10', 3, 7); -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 ('14de1a56-6893-4e12-90f3-4faec457f002', 1, 'cd80e1d0-9a08-44a6-bd63-2c63eaa003d4', 'HEALTH_PERMIT', 'PENDING', 22, '2025-08-31', '2024-07-15', '2024-07-25', 8, 14); -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 ('fb08a6c9-cd17-42e8-b9e2-734ec834cae2', 1, '4b1c6c35-4627-4b35-b6e9-dc75c68b2c31', 'HEALTH_PERMIT', 'PENDING', 16, '2025-12-31', '2024-09-30', '2024-10-05', 4, 12); +values ('89bc4b2a-943f-487c-a9f3-bacf78145e67', 1, 'cba3efb7-32bc-44be-9fdc-fc5e4f211254', 'MONDAY_CARNIVAL', 'APPROVED', 1, '2025-02-12', '2024-02-12', '2024-02-12', 1, 0);