From 0117915b9d91915f7dfd3a2bfa1ad8414ee637b5 Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Tue, 22 Oct 2024 00:20:13 -0400 Subject: [PATCH 01/10] =?UTF-8?q?#55=20Perfil=20de=20Personal=20Administra?= =?UTF-8?q?tivo=20-=20A=C3=B1adir=20Vacaciones=20del=20Empleado=20(Configu?= =?UTF-8?q?rar=20si=20DaysBalance=20sea=20mayor=20a=20cero)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TimeOffRequestRepository.java | 2 ++ .../service/TimeOffRequestService.java | 4 +++ .../views/RequestRegisterView.java | 26 ++++++++++++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/primefactorsolutions/repositories/TimeOffRequestRepository.java b/src/main/java/com/primefactorsolutions/repositories/TimeOffRequestRepository.java index 7225b28..1485fa0 100644 --- a/src/main/java/com/primefactorsolutions/repositories/TimeOffRequestRepository.java +++ b/src/main/java/com/primefactorsolutions/repositories/TimeOffRequestRepository.java @@ -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 { List findByEmployeeId(UUID idEmployee); Optional findByEmployeeIdAndState(UUID employeeId, TimeOffRequestStatus state); + List findByEmployeeIdAndCategory(UUID employeeId, TimeOffRequestType category); } diff --git a/src/main/java/com/primefactorsolutions/service/TimeOffRequestService.java b/src/main/java/com/primefactorsolutions/service/TimeOffRequestService.java index 2f7f661..c5eba83 100644 --- a/src/main/java/com/primefactorsolutions/service/TimeOffRequestService.java +++ b/src/main/java/com/primefactorsolutions/service/TimeOffRequestService.java @@ -38,4 +38,8 @@ public class TimeOffRequestService { public Optional findByEmployeeAndState(final UUID employeeId, final TimeOffRequestStatus state) { return timeOffRequestRepository.findByEmployeeIdAndState(employeeId, state); } + + public List findByEmployeeAndCategory(final UUID employeeId, final TimeOffRequestType category) { + return timeOffRequestRepository.findByEmployeeIdAndCategory(employeeId, category); + } } \ No newline at end of file diff --git a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java index c4e0fd1..8c08c66 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java @@ -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 employeeRequests = requestService.findRequestsByEmployeeId(employee.getId()); List requestedCategories = employeeRequests.stream() + .filter(request -> request.getDaysBalance() <= 0) .map(TimeOffRequest::getCategory) .toList(); List 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 requests = requestService.findByEmployeeAndCategory(employeeComboBox.getValue().getId(), selectedCategory); if (vacation != null) { - availableDaysField.setValue(vacation.getDuration()); + 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 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)); } From 52bed61ae8bad0707a13fa8ebcd869161ed09a8d Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Wed, 23 Oct 2024 07:28:15 -0400 Subject: [PATCH 02/10] =?UTF-8?q?#55=20Perfil=20de=20Personal=20Administra?= =?UTF-8?q?tivo=20-=20A=C3=B1adir=20Vacaciones=20del=20Empleado=20(Configu?= =?UTF-8?q?rar=20categoria=20Permiso=20de=20Salud)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../views/RequestRegisterView.java | 36 +++++++++++++++---- src/main/resources/data.sql | 4 +-- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java index 8c08c66..dc1b203 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java @@ -43,6 +43,7 @@ public class RequestRegisterView extends VerticalLayout { private final Binder binder; private Vacation vacation; + private Employee employee; private LocalDate endDate; private Button saveButton; @@ -65,7 +66,7 @@ public class RequestRegisterView extends VerticalLayout { private void configureFormFields() { employeeComboBox.setItems(employeeService.findAllEmployees()); employeeComboBox.setItemLabelGenerator(emp -> emp.getFirstName() + " " + emp.getLastName()); - employeeComboBox.addValueChangeListener(event -> handleEmployeeSelection(event.getValue())); + employeeComboBox.addValueChangeListener(event -> handleEmployeeSelection(employee = event.getValue())); categoryComboBox.setEnabled(false); startDatePicker.setEnabled(false); endDatePicker.setEnabled(false); @@ -167,8 +168,15 @@ public class RequestRegisterView extends VerticalLayout { startDatePicker.setValue(startDate); endDatePicker.setValue(startDate.plusDays(vacation.getDuration().intValue() - 1)); } else { - startDate = LocalDate.now(); - endDate = null; + if (vacation.getCategory() == TimeOffRequestType.BIRTHDAY) { + startDate = employee.getBirthday(); + endDate = startDate.plusDays(vacation.getExpiration().intValue() - 1); + startDatePicker.setValue(startDate); + endDatePicker.setValue(startDate.plusDays(vacation.getDuration().intValue() - 1)); + } else { + startDate = LocalDate.now(); + endDate = null; + } } startDatePicker.setMin(startDate); startDatePicker.setMax(endDate); @@ -209,10 +217,21 @@ public class RequestRegisterView extends VerticalLayout { if (daysToBeTaken == 1 && vacation.getDuration() == 0.5) { daysToBeTaken = 0.5; } - daysToBeTakenField.setValue(daysToBeTaken); + if (vacation.getCategory() == TimeOffRequestType.HEALTH_PERMIT && daysToBeTaken == 1) { + daysToBeTakenField.setValue(0.5); + } else { + daysToBeTakenField.setValue(daysToBeTaken); + } double balanceDays = availableDays - daysToBeTaken; - if (balanceDays < 0) { + if (vacation.getCategory() == TimeOffRequestType.HEALTH_PERMIT && daysToBeTaken > 1) { + endDatePicker.clear(); + daysToBeTakenField.clear(); + balanceDaysField.clear(); + } else if (vacation.getCategory() == TimeOffRequestType.HEALTH_PERMIT && daysToBeTaken == 1) { + balanceDays = availableDays - 0.5; + balanceDaysField.setValue(balanceDays); + } else if (balanceDays < 0) { endDatePicker.clear(); daysToBeTakenField.clear(); balanceDaysField.clear(); @@ -244,7 +263,12 @@ public class RequestRegisterView extends VerticalLayout { private void saveRequest() { if (binder.validate().isOk()) { TimeOffRequest request = binder.getBean(); - request.setExpiration(endDate); + 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)); + } request.setState(TimeOffRequestStatus.REQUESTED); updateBalanceForCategory(request); requestService.saveTimeOffRequest(request); diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index d6890d2..e911f98 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -25,7 +25,7 @@ INSERT INTO vacation (id, version, category, vacation_date, duration, expiration INSERT INTO vacation (id, version, category, vacation_date, duration, expiration, type) VALUES ('723e4567-e89b-12d3-a456-426614174006', 1, 'CHRISTMAS', '2024-12-25', 1, 1, 'FIXED'); -INSERT INTO vacation (id, version, category, vacation_date, duration, expiration, type) VALUES ('823e4567-e89b-12d3-a456-426614174007', 1, 'PRURINATIONAL_STATE_DAY', '2024-01-21', 3, 30, 'MOVABLE'); +INSERT INTO vacation (id, version, category, vacation_date, duration, expiration, type) VALUES ('823e4567-e89b-12d3-a456-426614174007', 1, 'PRURINATIONAL_STATE_DAY', '2024-01-21', 1, 30, 'MOVABLE'); INSERT INTO vacation (id, version, category, vacation_date, duration, expiration, type) VALUES ('923e4567-e89b-12d3-a456-426614174008', 1, 'CORPUS_CHRISTI', '2024-05-30', 1, 30, 'MOVABLE'); INSERT INTO vacation (id, version, category, vacation_date, duration, expiration, type) VALUES ('a23e4567-e89b-12d3-a456-426614174009', 1, 'ANDEAN_NEW_YEAR', '2024-06-21', 1, 30, 'MOVABLE'); INSERT INTO vacation (id, version, category, vacation_date, duration, expiration, type) VALUES ('b23e4567-e89b-12d3-a456-42661417400a', 1, 'DEPARTMENTAL_ANNIVERSARY', '2024-09-14', 1, 30, 'MOVABLE'); @@ -45,7 +45,7 @@ INSERT INTO vacation (id, version, category, expiration, type) VALUES ('490e5fbe INSERT INTO vacation (id, version, category, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-4266141740ff', 1, 'VACATION_PREVIOUS_MANAGEMENT', 730, 'OTHER'); -insert into employee (id, version, username, first_name, last_name, status, team_id, gender) values ('5c6f11fe-c341-4be7-a9a6-bba0081ad7c6', 1, 'bob', 'Bob', 'Test', 'ACTIVE','b0e8f394-78c1-4d8a-9c57-dc6e8b36a5fa', 'MALE'); +insert into employee (id, version, username, first_name, last_name, status, team_id, gender, birthday) values ('5c6f11fe-c341-4be7-a9a6-bba0081ad7c6', 1, 'bob', 'Bob', 'Test', 'ACTIVE','b0e8f394-78c1-4d8a-9c57-dc6e8b36a5fa', 'MALE', '2024-02-20'); insert into employee (id, version, username, first_name, last_name, status, team_id, gender) values ('cba3efb7-32bc-44be-9fdc-fc5e4f211254', 1, 'ben', 'Ben', 'Test', 'ACTIVE', '6d63bc15-3f8b-46f7-9cf1-7e9b0b9a2b28', 'MALE'); insert into employee (id, version, username, first_name, last_name, status, team_id, gender) values ('e99b7af5-7d3a-4c0f-b8bc-e8d0388d8fc4', 1, 'jperez', 'Juan', 'Perez Condori', 'INACTIVE', 'c3a8a7b1-f2d9-48c0-86ea-f215c2e6b3a3', 'MALE'); insert into employee (id, version, username, first_name, last_name, status, team_id, gender) values ('f6ab3c6d-7078-45f6-9b22-4e37637bfec6', 1, 'agarcia', 'Ana', 'Garcia Rojas', 'ACTIVE', '8f6b61e7-efb2-4de7-b8ed-7438c9d8babe', 'FEMALE'); From cb3ed95c0c9fd781f90cadfca7b6e24246c21899 Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Wed, 23 Oct 2024 08:02:12 -0400 Subject: [PATCH 03/10] =?UTF-8?q?#55=20Perfil=20de=20Personal=20Administra?= =?UTF-8?q?tivo=20-=20A=C3=B1adir=20Vacaciones=20del=20Empleado=20(Configu?= =?UTF-8?q?rar=20vacacion=20por=20antiguedad=20del=20empleado)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../views/RequestRegisterView.java | 14 ++++++++++++++ src/main/resources/data.sql | 8 ++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java index dc1b203..1d64de7 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java @@ -19,6 +19,7 @@ import jakarta.annotation.security.PermitAll; import org.springframework.context.annotation.Scope; import java.time.LocalDate; +import java.time.temporal.ChronoUnit; import java.util.Arrays; import java.util.List; @@ -153,6 +154,19 @@ public class RequestRegisterView extends VerticalLayout { if (vacation != null) { if (!requests.isEmpty()) { availableDaysField.setValue(requests.getFirst().getDaysBalance()); + } else if (vacation.getCategory() == TimeOffRequestType.VACATION_CURRENT_MANAGEMENT) { + LocalDate dateOfEntry = employeeComboBox.getValue().getDateOfEntry(); + LocalDate currentDate = LocalDate.now(); + long yearsOfService = ChronoUnit.YEARS.between(dateOfEntry, currentDate); + if (yearsOfService > 10) { + availableDaysField.setValue((double)30); + } else if (yearsOfService > 5) { + availableDaysField.setValue((double)20); + } else if (yearsOfService > 1) { + availableDaysField.setValue((double)15); + } else { + availableDaysField.setValue((double)0); // O asignar un valor predeterminado + } } else { availableDaysField.setValue(vacation.getDuration()); } diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index e911f98..e783e3a 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -45,10 +45,10 @@ INSERT INTO vacation (id, version, category, expiration, type) VALUES ('490e5fbe INSERT INTO vacation (id, version, category, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-4266141740ff', 1, 'VACATION_PREVIOUS_MANAGEMENT', 730, 'OTHER'); -insert into employee (id, version, username, first_name, last_name, status, team_id, gender, birthday) values ('5c6f11fe-c341-4be7-a9a6-bba0081ad7c6', 1, 'bob', 'Bob', 'Test', 'ACTIVE','b0e8f394-78c1-4d8a-9c57-dc6e8b36a5fa', 'MALE', '2024-02-20'); -insert into employee (id, version, username, first_name, last_name, status, team_id, gender) values ('cba3efb7-32bc-44be-9fdc-fc5e4f211254', 1, 'ben', 'Ben', 'Test', 'ACTIVE', '6d63bc15-3f8b-46f7-9cf1-7e9b0b9a2b28', 'MALE'); -insert into employee (id, version, username, first_name, last_name, status, team_id, gender) values ('e99b7af5-7d3a-4c0f-b8bc-e8d0388d8fc4', 1, 'jperez', 'Juan', 'Perez Condori', 'INACTIVE', 'c3a8a7b1-f2d9-48c0-86ea-f215c2e6b3a3', 'MALE'); -insert into employee (id, version, username, first_name, last_name, status, team_id, gender) values ('f6ab3c6d-7078-45f6-9b22-4e37637bfec6', 1, 'agarcia', 'Ana', 'Garcia Rojas', 'ACTIVE', '8f6b61e7-efb2-4de7-b8ed-7438c9d8babe', 'FEMALE'); +insert into employee (id, version, username, first_name, last_name, status, team_id, gender, birthday, date_of_entry) values ('5c6f11fe-c341-4be7-a9a6-bba0081ad7c6', 1, 'bob', 'Bob', 'Test', 'ACTIVE','b0e8f394-78c1-4d8a-9c57-dc6e8b36a5fa', 'MALE', '2024-02-20', '2013-10-22'); +insert into employee (id, version, username, first_name, last_name, status, team_id, gender, date_of_entry) values ('cba3efb7-32bc-44be-9fdc-fc5e4f211254', 1, 'ben', 'Ben', 'Test', 'ACTIVE', '6d63bc15-3f8b-46f7-9cf1-7e9b0b9a2b28', 'MALE', '2016-10-23'); +insert into employee (id, version, username, first_name, last_name, status, team_id, gender, date_of_entry) values ('e99b7af5-7d3a-4c0f-b8bc-e8d0388d8fc4', 1, 'jperez', 'Juan', 'Perez Condori', 'INACTIVE', 'c3a8a7b1-f2d9-48c0-86ea-f215c2e6b3a3', 'MALE', '2022-10-22'); +insert into employee (id, version, username, first_name, last_name, status, team_id, gender, date_of_entry) values ('f6ab3c6d-7078-45f6-9b22-4e37637bfec6', 1, 'agarcia', 'Ana', 'Garcia Rojas', 'ACTIVE', '8f6b61e7-efb2-4de7-b8ed-7438c9d8babe', 'FEMALE', '2024-10-24'); insert into employee (id, version, username, first_name, last_name, status, team_id, gender) values ('2e2293b1-3f9a-4f3d-abc8-32639b0a5e15', 1, 'clopez', 'Carlos', 'Lopez Mendoza', 'INACTIVE', 'b0e8f394-78c1-4d8a-9c57-dc6e8b36a5fa', 'MALE'); insert into employee (id, version, username, first_name, last_name, status, team_id, gender) values ('4b1c6c35-4627-4b35-b6e9-dc75c68b2c31', 1, 'mfernandez', 'Maria', 'Fernandez Villca', 'ACTIVE', '6d63bc15-3f8b-46f7-9cf1-7e9b0b9a2b28', 'FEMALE'); insert into employee (id, version, username, first_name, last_name, status, team_id, gender) values ('afc5c741-f70a-4394-853b-39d51b118927', 1, 'lgutierrez', 'Luis', 'Gutierrez Mamani', 'ACTIVE', 'c3a8a7b1-f2d9-48c0-86ea-f215c2e6b3a3', 'MALE'); From 807555a2dddeef89be103bedc6f146ccfcffc61e Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Wed, 23 Oct 2024 15:09:36 -0400 Subject: [PATCH 04/10] =?UTF-8?q?#55=20Perfil=20de=20Personal=20Administra?= =?UTF-8?q?tivo=20-=20A=C3=B1adir=20Vacaciones=20del=20Empleado=20(Configu?= =?UTF-8?q?rar=20habilitacion=20categoria=20si=20previa=20solicitud=20expi?= =?UTF-8?q?ra)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../primefactorsolutions/views/RequestEmployeeView.java | 9 +++++++++ .../primefactorsolutions/views/RequestRegisterView.java | 7 ++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java b/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java index 95c9592..f45f3fc 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java @@ -20,6 +20,7 @@ import com.vaadin.flow.spring.annotation.SpringComponent; import jakarta.annotation.security.PermitAll; import org.springframework.context.annotation.Scope; +import java.time.LocalDate; import java.time.Year; import java.util.Collections; import java.util.List; @@ -154,6 +155,14 @@ public class RequestEmployeeView extends Div implements HasUrlParameter List filteredRequests = allFiltersAreNull(category, state) ? requestService.findRequestsByEmployeeId(employeeId) : fetchFilteredTimeOffRequests(category, state); + for (TimeOffRequest request : filteredRequests) { + if (request.getExpiration().isBefore(LocalDate.now())) { + request.setState(TimeOffRequestStatus.EXPIRED); + } + } + for (TimeOffRequest request : filteredRequests) { + requestService.saveTimeOffRequest(request); + } requestGrid.setItems(filteredRequests); } diff --git a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java index 1d64de7..c1ce30b 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java @@ -123,7 +123,8 @@ public class RequestRegisterView extends VerticalLayout { .map(TimeOffRequest::getCategory) .toList(); List availableCategories = Arrays.stream(TimeOffRequestType.values()) - .filter(category -> !requestedCategories.contains(category)) + .filter(category -> !requestedCategories.contains(category) || + employeeRequests.stream().anyMatch(req -> req.getCategory() == category && req.getState() == TimeOffRequestStatus.EXPIRED)) .filter(category -> { if (employee.getGender() == Employee.Gender.MALE) { return category != TimeOffRequestType.MATERNITY @@ -152,7 +153,7 @@ public class RequestRegisterView extends VerticalLayout { vacation = vacationService.findVacationByCategory(selectedCategory); List requests = requestService.findByEmployeeAndCategory(employeeComboBox.getValue().getId(), selectedCategory); if (vacation != null) { - if (!requests.isEmpty()) { + if (!requests.isEmpty() && requests.getFirst().getState() != TimeOffRequestStatus.EXPIRED) { availableDaysField.setValue(requests.getFirst().getDaysBalance()); } else if (vacation.getCategory() == TimeOffRequestType.VACATION_CURRENT_MANAGEMENT) { LocalDate dateOfEntry = employeeComboBox.getValue().getDateOfEntry(); @@ -165,7 +166,7 @@ public class RequestRegisterView extends VerticalLayout { } else if (yearsOfService > 1) { availableDaysField.setValue((double)15); } else { - availableDaysField.setValue((double)0); // O asignar un valor predeterminado + availableDaysField.setValue((double)0); } } else { availableDaysField.setValue(vacation.getDuration()); From 6a1d3e70c67f03b38e9614937594152b70906892 Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Sun, 27 Oct 2024 00:19:56 -0400 Subject: [PATCH 05/10] #37 Perfil de Personal Administrativo - Listado General de Vacaciones (Correcciones) --- .../views/RequestEmployeeView.java | 52 +++++---- .../views/RequestRegisterView.java | 25 ++-- .../views/RequestsListView.java | 109 +++++++++--------- 3 files changed, 96 insertions(+), 90 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java b/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java index f45f3fc..1408b57 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java @@ -8,10 +8,8 @@ import com.vaadin.flow.component.combobox.ComboBox; import com.vaadin.flow.component.grid.Grid; import com.vaadin.flow.component.html.Div; import com.vaadin.flow.component.html.H3; -import com.vaadin.flow.component.html.Span; import com.vaadin.flow.component.notification.Notification; import com.vaadin.flow.component.orderedlayout.HorizontalLayout; -import com.vaadin.flow.component.orderedlayout.VerticalLayout; import com.vaadin.flow.router.BeforeEvent; import com.vaadin.flow.router.HasUrlParameter; import com.vaadin.flow.router.PageTitle; @@ -21,7 +19,6 @@ import jakarta.annotation.security.PermitAll; import org.springframework.context.annotation.Scope; import java.time.LocalDate; -import java.time.Year; import java.util.Collections; import java.util.List; import java.util.UUID; @@ -37,6 +34,7 @@ public class RequestEmployeeView extends Div implements HasUrlParameter private final TimeOffRequestService requestService; private final EmployeeService employeeService; private final Grid requestGrid = new Grid<>(TimeOffRequest.class); + private final Grid summaryGrid = new Grid<>(); private List requests = Collections.emptyList(); private ComboBox categoryFilter; private ComboBox stateFilter; @@ -51,7 +49,8 @@ public class RequestEmployeeView extends Div implements HasUrlParameter private void initializeView() { setupFilters(); setupGrid(); - add(requestGrid, createSummaryLayout(), createActionButtons()); + setupSummaryGrid(); + add(requestGrid, createActionButtons(), summaryGrid); refreshRequestGrid(null, null); } @@ -81,12 +80,9 @@ public class RequestEmployeeView extends Div implements HasUrlParameter requestGrid.setColumns( "category", "state", - "availableDays", - "expiration", "startDate", "endDate", - "daysToBeTake", - "daysBalance" + "daysToBeTake" ); requestGrid.setAllRowsVisible(true); requestGrid.asSingleSelect().addValueChangeListener(event -> { @@ -97,23 +93,29 @@ public class RequestEmployeeView extends Div implements HasUrlParameter }); } - private VerticalLayout createSummaryLayout() { - int currentYear = Year.now().getValue(); - String yearCategory = "YEAR_" + currentYear; - double totalVacations = requests.stream() - .filter(req -> req.getCategory().name().equals(yearCategory)) - .mapToDouble(TimeOffRequest::getAvailableDays) - .sum(); - double totalTimeOff = requests.stream() - .filter(req -> !req.getCategory().name().startsWith("YEAR")) - .mapToDouble(TimeOffRequest::getDaysBalance) - .sum(); - double totalAvailableDays = totalVacations + totalTimeOff; - return new VerticalLayout( - new Span("TOTAL HOLIDAYS: " + totalVacations), - new Span("TOTAL TIME OFF: " + totalTimeOff), - new Span("TOTAL AVAILABLE DAYS: " + totalAvailableDays) - ); + private void setupSummaryGrid() { + summaryGrid.addColumn(this::getCategory).setHeader("Category"); + summaryGrid.addColumn(this::getStatus).setHeader("Available"); + summaryGrid.addColumn(this::getTotalRequests).setHeader("Expiration"); + summaryGrid.addColumn(this::getTotalDays).setHeader("Taken"); + summaryGrid.addColumn(this::getTotalDays).setHeader("Balance"); + summaryGrid.setAllRowsVisible(true); + } + + private Object getTotalDays(final Vacation vacation) { + return null; + } + + private Object getTotalRequests(final Vacation vacation) { + return null; + } + + private Object getStatus(final Vacation vacation) { + return null; + } + + private Object getCategory(final Vacation vacation) { + return vacation.getCategory(); } private HorizontalLayout createActionButtons() { diff --git a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java index c1ce30b..8d50167 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java @@ -22,6 +22,7 @@ import java.time.LocalDate; import java.time.temporal.ChronoUnit; import java.util.Arrays; import java.util.List; +import java.util.UUID; @SpringComponent @PermitAll @@ -67,7 +68,10 @@ public class RequestRegisterView extends VerticalLayout { private void configureFormFields() { employeeComboBox.setItems(employeeService.findAllEmployees()); employeeComboBox.setItemLabelGenerator(emp -> emp.getFirstName() + " " + emp.getLastName()); - employeeComboBox.addValueChangeListener(event -> handleEmployeeSelection(employee = event.getValue())); + employeeComboBox.addValueChangeListener(event -> { + employee = event.getValue(); + handleEmployeeSelection(employee); + }); categoryComboBox.setEnabled(false); startDatePicker.setEnabled(false); endDatePicker.setEnabled(false); @@ -123,8 +127,10 @@ public class RequestRegisterView extends VerticalLayout { .map(TimeOffRequest::getCategory) .toList(); List availableCategories = Arrays.stream(TimeOffRequestType.values()) - .filter(category -> !requestedCategories.contains(category) || - employeeRequests.stream().anyMatch(req -> req.getCategory() == category && req.getState() == TimeOffRequestStatus.EXPIRED)) + .filter(category -> !requestedCategories.contains(category) + || employeeRequests.stream() + .anyMatch(req -> req.getCategory() == category + && req.getState() == TimeOffRequestStatus.EXPIRED)) .filter(category -> { if (employee.getGender() == Employee.Gender.MALE) { return category != TimeOffRequestType.MATERNITY @@ -151,7 +157,8 @@ public class RequestRegisterView extends VerticalLayout { private void updateAvailableDays(final TimeOffRequestType selectedCategory) { vacation = vacationService.findVacationByCategory(selectedCategory); - List requests = requestService.findByEmployeeAndCategory(employeeComboBox.getValue().getId(), selectedCategory); + 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()); @@ -160,13 +167,13 @@ public class RequestRegisterView extends VerticalLayout { LocalDate currentDate = LocalDate.now(); long yearsOfService = ChronoUnit.YEARS.between(dateOfEntry, currentDate); if (yearsOfService > 10) { - availableDaysField.setValue((double)30); + availableDaysField.setValue((double) 30); } else if (yearsOfService > 5) { - availableDaysField.setValue((double)20); + availableDaysField.setValue((double) 20); } else if (yearsOfService > 1) { - availableDaysField.setValue((double)15); + availableDaysField.setValue((double) 15); } else { - availableDaysField.setValue((double)0); + availableDaysField.setValue((double) 0); } } else { availableDaysField.setValue(vacation.getDuration()); @@ -294,7 +301,7 @@ public class RequestRegisterView extends VerticalLayout { } } - private void updateBalanceForCategory(TimeOffRequest newRequest) { + private void updateBalanceForCategory(final TimeOffRequest newRequest) { List requests = requestService.findByEmployeeAndCategory( newRequest.getEmployee().getId(), newRequest.getCategory()); diff --git a/src/main/java/com/primefactorsolutions/views/RequestsListView.java b/src/main/java/com/primefactorsolutions/views/RequestsListView.java index 24207cc..f111c59 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestsListView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestsListView.java @@ -29,7 +29,7 @@ public class RequestsListView extends Main { private final TimeOffRequestService requestService; private final EmployeeService employeeService; private final TeamService teamService; - private final PagingGrid requestGrid = new PagingGrid<>(); + private final PagingGrid requestGrid = new PagingGrid<>(); private List employees = Collections.emptyList(); private ComboBox employeeFilter; @@ -67,18 +67,19 @@ public class RequestsListView extends Main { private void setupRequestGrid() { requestGrid.addColumn(this::getEmployeeFullName).setHeader("Employee"); requestGrid.addColumn(this::getTeamName).setHeader("Team"); - requestGrid.addColumn(this::getEmployeeStatus).setHeader("State"); + requestGrid.addColumn(this::getEmployeeStatus).setHeader("Employee State"); requestGrid.addColumn(this::getCategory).setHeader("Category"); + requestGrid.addColumn(this::getState).setHeader("Request Status"); requestGrid.addColumn(this::getStartDate).setHeader("Start Date"); requestGrid.addColumn(this::getEndDate).setHeader("End Date"); - requestGrid.addColumn(this::getDaysBalance).setHeader("Days Balance"); + requestGrid.addColumn(this::getDaysBalance).setHeader("Balance"); requestGrid.setPaginationBarMode(PagingGrid.PaginationBarMode.BOTTOM); requestGrid.setPageSize(5); requestGrid.asSingleSelect().addValueChangeListener(event -> { - Employee selectedRequest = event.getValue(); + TimeOffRequest selectedRequest = event.getValue(); if (selectedRequest != null) { - selectedEmployeeId = selectedRequest.getId(); + selectedEmployeeId = selectedRequest.getEmployee().getId(); } }); } @@ -106,56 +107,58 @@ public class RequestsListView extends Main { requestGrid.getDataProvider().refreshAll(); } - private List fetchFilteredEmployees(final int start, - final int pageSize, - final Employee employee, - final Team team, - final TimeOffRequestType category, - final Status state) { - List filteredEmployees = employeeService.findAllEmployees(); + private List fetchFilteredEmployees(final int start, + final int pageSize, + final Employee employee, + final Team team, + final TimeOffRequestType category, + final Status employeeState) { + List filteredRequests = requestService.findAllTimeOffRequests(); if (employee != null && !"ALL".equals(employee.getFirstName())) { - filteredEmployees = filteredEmployees.stream() - .filter(emp -> emp.getId().equals(employee.getId())) + filteredRequests = filteredRequests.stream() + .filter(emp -> emp.getEmployee().getId().equals(employee.getId())) .collect(Collectors.toList()); } if (team != null && !"ALL".equals(team.getName())) { - filteredEmployees = filteredEmployees.stream() - .filter(emp -> emp.getTeam() != null && emp.getTeam().getId().equals(team.getId())) + filteredRequests = filteredRequests.stream() + .filter(emp -> { + Team empTeam = emp.getEmployee().getTeam(); + return empTeam != null && empTeam.getId().equals(team.getId()); + }) .collect(Collectors.toList()); } if (category != null && category != TimeOffRequestType.values()[0]) { - filteredEmployees = filteredEmployees.stream() - .filter(emp -> { - Optional request = requestService - .findByEmployeeAndState(emp.getId(), TimeOffRequestStatus.TAKEN); - return request.isPresent() && request.get().getCategory() == category; - }) + filteredRequests = filteredRequests.stream() + .filter(emp -> emp.getCategory().equals(category)) .collect(Collectors.toList()); } - if (state != null && state != Status.ALL) { - filteredEmployees = filteredEmployees.stream() - .filter(emp -> { - Optional request = requestService - .findByEmployeeAndState(emp.getId(), TimeOffRequestStatus.TAKEN); - return state == Status.IDLE ? request.isPresent() : request.isEmpty(); - }) + if (employeeState != null && employeeState != Status.ALL) { + filteredRequests = filteredRequests.stream() + .filter(emp -> employeeState == Status.IDLE + ? emp.getState().equals(TimeOffRequestStatus.IN_USE) + : !emp.getState().equals(TimeOffRequestStatus.IN_USE)) .collect(Collectors.toList()); } - int end = Math.min(start + pageSize, filteredEmployees.size()); - return filteredEmployees.subList(start, end); + int end = Math.min(start + pageSize, filteredRequests.size()); + return filteredRequests.subList(start, end); } - private String getEmployeeFullName(final Employee employee) { + private String getEmployeeFullName(final TimeOffRequest request) { + Employee employee = request.getEmployee(); + return getEmployeeFullNameLabel(employee); + } + + private String getEmployeeFullNameLabel(final Employee employee) { return "ALL".equals(employee.getFirstName()) ? "ALL" : employee.getFirstName() + " " + employee.getLastName(); } - private String getTeamName(final Employee employee) { - Team team = employee.getTeam(); + private String getTeamName(final TimeOffRequest request) { + Team team = request.getEmployee().getTeam(); return team != null ? team.getName() : "Unassigned"; } @@ -164,34 +167,28 @@ public class RequestsListView extends Main { } - private String getEmployeeStatus(final Employee employee) { - Optional activeRequest = requestService - .findByEmployeeAndState(employee.getId(), TimeOffRequestStatus.TAKEN); - return activeRequest.isPresent() ? "IDLE" : "ACTIVE"; + private String getEmployeeStatus(final TimeOffRequest request) { + return request.getState() == TimeOffRequestStatus.IN_USE ? "IDLE" : "ACTIVE"; } - private String getCategory(final Employee employee) { - Optional activeRequest = requestService - .findByEmployeeAndState(employee.getId(), TimeOffRequestStatus.TAKEN); - return activeRequest.map(request -> request.getCategory().toString()).orElse(""); + private String getCategory(final TimeOffRequest request) { + return request.getCategory().toString(); } - private String getStartDate(final Employee employee) { - Optional activeRequest = requestService - .findByEmployeeAndState(employee.getId(), TimeOffRequestStatus.TAKEN); - return activeRequest.map(request -> request.getStartDate().toString()).orElse(""); + private String getState(final TimeOffRequest request) { + return request.getState().toString(); } - private String getEndDate(final Employee employee) { - Optional activeRequest = requestService - .findByEmployeeAndState(employee.getId(), TimeOffRequestStatus.TAKEN); - return activeRequest.map(request -> request.getEndDate().toString()).orElse(""); + private String getStartDate(final TimeOffRequest request) { + return request.getStartDate().toString(); } - private String getDaysBalance(final Employee employee) { - Optional activeRequest = requestService - .findByEmployeeAndState(employee.getId(), TimeOffRequestStatus.TAKEN); - return activeRequest.map(request -> request.getDaysBalance().toString()).orElse(""); + private String getEndDate(final TimeOffRequest request) { + return request.getEndDate().toString(); + } + + private String getDaysBalance(final TimeOffRequest request) { + return request.getDaysBalance().toString(); } private ComboBox createEmployeeFilter() { @@ -199,7 +196,7 @@ public class RequestsListView extends Main { List employees = new ArrayList<>(employeeService.findAllEmployees()); employees.addFirst(createAllEmployeesOption()); employeeFilter.setItems(employees); - employeeFilter.setItemLabelGenerator(this::getEmployeeFullName); + employeeFilter.setItemLabelGenerator(this::getEmployeeFullNameLabel); employeeFilter.setValue(employees.getFirst()); employeeFilter.addValueChangeListener(event -> refreshGeneralRequestGrid( @@ -246,7 +243,7 @@ public class RequestsListView extends Main { } private ComboBox createStateFilter() { - stateFilter = new ComboBox<>("State"); + stateFilter = new ComboBox<>("Employee State"); stateFilter.setItems(Status.values()); stateFilter.setValue(Status.values()[0]); stateFilter.addValueChangeListener(event -> From 73915b136499055038c1dd602996ab9c870fbca1 Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Sun, 27 Oct 2024 20:05:31 -0400 Subject: [PATCH 06/10] =?UTF-8?q?#55=20Perfil=20de=20Personal=20Administra?= =?UTF-8?q?tivo=20-=20A=C3=B1adir=20Vacaciones=20del=20Empleado=20(Correcc?= =?UTF-8?q?ion)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../primefactorsolutions/model/Vacation.java | 5 +- .../views/RequestEmployeeView.java | 29 +---- .../views/RequestRegisterView.java | 104 ++++++++---------- .../views/RequestsListView.java | 10 -- src/main/resources/data.sql | 45 +++++--- 5 files changed, 79 insertions(+), 114 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/model/Vacation.java b/src/main/java/com/primefactorsolutions/model/Vacation.java index 9f7de1c..7714c3e 100644 --- a/src/main/java/com/primefactorsolutions/model/Vacation.java +++ b/src/main/java/com/primefactorsolutions/model/Vacation.java @@ -8,8 +8,6 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; -import java.time.LocalDate; - @Data @Entity @AllArgsConstructor @@ -18,7 +16,8 @@ import java.time.LocalDate; public class Vacation extends BaseEntity { @Enumerated(EnumType.STRING) private TimeOffRequestType category; - private LocalDate vacationDate; + private Integer monthOfYear; + private Integer dayOfMonth; private Double duration; private Double expiration; @Enumerated(EnumType.STRING) diff --git a/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java b/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java index 1408b57..f1ca462 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java @@ -34,7 +34,6 @@ public class RequestEmployeeView extends Div implements HasUrlParameter private final TimeOffRequestService requestService; private final EmployeeService employeeService; private final Grid requestGrid = new Grid<>(TimeOffRequest.class); - private final Grid summaryGrid = new Grid<>(); private List requests = Collections.emptyList(); private ComboBox categoryFilter; private ComboBox stateFilter; @@ -49,8 +48,7 @@ public class RequestEmployeeView extends Div implements HasUrlParameter private void initializeView() { setupFilters(); setupGrid(); - setupSummaryGrid(); - add(requestGrid, createActionButtons(), summaryGrid); + add(requestGrid, createActionButtons()); refreshRequestGrid(null, null); } @@ -93,31 +91,6 @@ public class RequestEmployeeView extends Div implements HasUrlParameter }); } - private void setupSummaryGrid() { - summaryGrid.addColumn(this::getCategory).setHeader("Category"); - summaryGrid.addColumn(this::getStatus).setHeader("Available"); - summaryGrid.addColumn(this::getTotalRequests).setHeader("Expiration"); - summaryGrid.addColumn(this::getTotalDays).setHeader("Taken"); - summaryGrid.addColumn(this::getTotalDays).setHeader("Balance"); - summaryGrid.setAllRowsVisible(true); - } - - private Object getTotalDays(final Vacation vacation) { - return null; - } - - private Object getTotalRequests(final Vacation vacation) { - return null; - } - - private Object getStatus(final Vacation vacation) { - return null; - } - - private Object getCategory(final Vacation vacation) { - return vacation.getCategory(); - } - private HorizontalLayout createActionButtons() { Button viewButton = new Button("View", event -> { if (request != null) { diff --git a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java index 8d50167..1176231 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java @@ -21,8 +21,10 @@ import org.springframework.context.annotation.Scope; import java.time.LocalDate; import java.time.temporal.ChronoUnit; import java.util.Arrays; +import java.util.Comparator; import java.util.List; import java.util.UUID; +import java.util.stream.Collectors; @SpringComponent @PermitAll @@ -122,36 +124,46 @@ public class RequestRegisterView extends VerticalLayout { private void filterCategories(final Employee employee) { List employeeRequests = requestService.findRequestsByEmployeeId(employee.getId()); - List requestedCategories = employeeRequests.stream() - .filter(request -> request.getDaysBalance() <= 0) - .map(TimeOffRequest::getCategory) - .toList(); - List availableCategories = Arrays.stream(TimeOffRequestType.values()) - .filter(category -> !requestedCategories.contains(category) - || employeeRequests.stream() - .anyMatch(req -> req.getCategory() == category - && req.getState() == TimeOffRequestStatus.EXPIRED)) - .filter(category -> { - if (employee.getGender() == Employee.Gender.MALE) { - return category != TimeOffRequestType.MATERNITY - && category != TimeOffRequestType.MOTHERS_DAY - && category != TimeOffRequestType.INTERNATIONAL_WOMENS_DAY - && category != TimeOffRequestType.NATIONAL_WOMENS_DAY; - } else { - return category != TimeOffRequestType.FATHERS_DAY - && category != TimeOffRequestType.PATERNITY; - } - }) + List allCategories = Arrays.asList(TimeOffRequestType.values()); + List availableCategories = allCategories.stream() + .filter(category -> isCategoryAvailable(employeeRequests, category)) + .filter(category -> isCategoryAllowedByGender(category, employee.getGender())) .toList(); categoryComboBox.setItems(availableCategories); } + private boolean isCategoryAvailable(List employeeRequests, TimeOffRequestType category) { + List requestsByCategory = employeeRequests.stream() + .filter(request -> request.getCategory() == category) + .toList(); + + if (requestsByCategory.isEmpty()) { + return true; + } + + TimeOffRequest latestRequest = requestsByCategory.stream() + .max(Comparator.comparing(TimeOffRequest::getStartDate)) + .orElse(null); + + return latestRequest.getState() == TimeOffRequestStatus.EXPIRED; + } + + private boolean isCategoryAllowedByGender(TimeOffRequestType category, Employee.Gender gender) { + if (gender == Employee.Gender.MALE) { + return category != TimeOffRequestType.MATERNITY + && category != TimeOffRequestType.MOTHERS_DAY + && category != TimeOffRequestType.INTERNATIONAL_WOMENS_DAY + && category != TimeOffRequestType.NATIONAL_WOMENS_DAY; + } else { + return category != TimeOffRequestType.FATHERS_DAY + && category != TimeOffRequestType.PATERNITY; + } + } + private void handleCategorySelection(final TimeOffRequestType selectedCategory) { - clearForm(); if (selectedCategory != null) { updateAvailableDays(selectedCategory); startDatePicker.setEnabled(true); - endDatePicker.setEnabled(true); } } @@ -184,8 +196,8 @@ public class RequestRegisterView extends VerticalLayout { private void setDatePickerLimits(final Vacation vacation) { LocalDate startDate; - if (vacation.getVacationDate() != null) { - startDate = vacation.getVacationDate(); + if (vacation.getMonthOfYear() != null && vacation.getDayOfMonth() != null) { + startDate = LocalDate.of(LocalDate.now().getYear(), vacation.getMonthOfYear(), vacation.getDayOfMonth()); endDate = startDate.plusDays(vacation.getExpiration().intValue() - 1); startDatePicker.setValue(startDate); endDatePicker.setValue(startDate.plusDays(vacation.getDuration().intValue() - 1)); @@ -209,19 +221,12 @@ public class RequestRegisterView extends VerticalLayout { private void updateDatePickerMinValues() { LocalDate startDate = startDatePicker.getValue(); - if (vacation.getVacationDate() == null) { - endDate = startDate.plusDays(vacation.getExpiration().intValue() - 1); - } - if (startDate != null) { - endDatePicker.setMin(startDate); - endDatePicker.setMax(startDate.plusDays(vacation.getExpiration().intValue() - 1)); - if (vacation.getDuration() == 0.5) { - endDatePicker.setValue(startDate.plusDays(0)); - } else { - endDatePicker.setValue(startDate.plusDays(vacation.getDuration().intValue() - 1)); - } - calculateDays(); + if (vacation.getDuration() == 0.5) { + endDatePicker.setValue(startDate.plusDays(0)); + } else { + endDatePicker.setValue(startDate.plusDays(vacation.getDuration().intValue() - 1)); } + calculateDays(); } private void calculateDays() { @@ -230,11 +235,6 @@ public class RequestRegisterView extends VerticalLayout { Double availableDays = availableDaysField.getValue(); if (startDate != null && endDate != null) { - if (startDate.isAfter(endDate)) { - endDatePicker.clear(); - return; - } - double daysToBeTaken = java.time.temporal.ChronoUnit.DAYS.between(startDate, endDate) + 1; if (daysToBeTaken == 1 && vacation.getDuration() == 0.5) { daysToBeTaken = 0.5; @@ -244,22 +244,8 @@ public class RequestRegisterView extends VerticalLayout { } else { daysToBeTakenField.setValue(daysToBeTaken); } - double balanceDays = availableDays - daysToBeTaken; - if (vacation.getCategory() == TimeOffRequestType.HEALTH_PERMIT && daysToBeTaken > 1) { - endDatePicker.clear(); - daysToBeTakenField.clear(); - balanceDaysField.clear(); - } else if (vacation.getCategory() == TimeOffRequestType.HEALTH_PERMIT && daysToBeTaken == 1) { - balanceDays = availableDays - 0.5; - balanceDaysField.setValue(balanceDays); - } else if (balanceDays < 0) { - endDatePicker.clear(); - daysToBeTakenField.clear(); - balanceDaysField.clear(); - } else { - balanceDaysField.setValue(balanceDays); - } + balanceDaysField.setValue(balanceDays); } } @@ -291,6 +277,12 @@ public class RequestRegisterView extends VerticalLayout { } else if (request.getExpiration() == null) { request.setExpiration(startDatePicker.getValue().plusDays(vacation.getExpiration().intValue() - 1)); } + List existingRequests = requestService.findByEmployeeAndCategory(employee.getId(), request.getCategory()); + + if (existingRequests.size() >= 2) { + existingRequests.stream().min(Comparator.comparing(TimeOffRequest::getStartDate)).ifPresent(oldestRequest -> requestService.deleteTimeOffRequest(oldestRequest.getId())); + + } request.setState(TimeOffRequestStatus.REQUESTED); updateBalanceForCategory(request); requestService.saveTimeOffRequest(request); diff --git a/src/main/java/com/primefactorsolutions/views/RequestsListView.java b/src/main/java/com/primefactorsolutions/views/RequestsListView.java index f111c59..d5c9136 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestsListView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestsListView.java @@ -70,8 +70,6 @@ public class RequestsListView extends Main { requestGrid.addColumn(this::getEmployeeStatus).setHeader("Employee State"); requestGrid.addColumn(this::getCategory).setHeader("Category"); requestGrid.addColumn(this::getState).setHeader("Request Status"); - requestGrid.addColumn(this::getStartDate).setHeader("Start Date"); - requestGrid.addColumn(this::getEndDate).setHeader("End Date"); requestGrid.addColumn(this::getDaysBalance).setHeader("Balance"); requestGrid.setPaginationBarMode(PagingGrid.PaginationBarMode.BOTTOM); @@ -179,14 +177,6 @@ public class RequestsListView extends Main { return request.getState().toString(); } - private String getStartDate(final TimeOffRequest request) { - return request.getStartDate().toString(); - } - - private String getEndDate(final TimeOffRequest request) { - return request.getEndDate().toString(); - } - private String getDaysBalance(final TimeOffRequest request) { return request.getDaysBalance().toString(); } diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index e783e3a..a09d9cd 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -16,30 +16,30 @@ INSERT INTO team (id, version, name) VALUES ('c3a8a7b1-f2d9-48c0-86ea-f215c2e6b3 INSERT INTO team (id, version, name) VALUES ('8f6b61e7-efb2-4de7-b8ed-7438c9d8babe', 1, 'GHI'); -INSERT INTO vacation (id, version, category, vacation_date, duration, expiration, type) VALUES ('123e4567-e89b-12d3-a456-426614174000', 1, 'NEW_YEAR', '2024-01-01', 1, 1, 'FIXED'); -INSERT INTO vacation (id, version, category, vacation_date, duration, expiration, type) VALUES ('223e4567-e89b-12d3-a456-426614174001', 1, 'MONDAY_CARNIVAL', '2024-02-12', 1, 1, 'FIXED'); -INSERT INTO vacation (id, version, category, vacation_date, duration, expiration, type) VALUES ('323e4567-e89b-12d3-a456-426614174002', 1, 'TUESDAY_CARNIVAL', '2024-02-13', 1, 1, 'FIXED'); -INSERT INTO vacation (id, version, category, vacation_date, duration, expiration, type) VALUES ('423e4567-e89b-12d3-a456-426614174003', 1, 'GOOD_FRIDAY', '2024-03-29', 1, 1, 'FIXED'); -INSERT INTO vacation (id, version, category, vacation_date, duration, expiration, type) VALUES ('523e4567-e89b-12d3-a456-426614174004', 1, 'LABOR_DAY', '2024-05-01', 1, 1, 'FIXED'); -INSERT INTO vacation (id, version, category, vacation_date, duration, expiration, type) VALUES ('623e4567-e89b-12d3-a456-426614174005', 1, 'INDEPENDENCE_DAY', '2024-08-06', 1, 1, 'FIXED'); -INSERT INTO vacation (id, version, category, vacation_date, duration, expiration, type) VALUES ('723e4567-e89b-12d3-a456-426614174006', 1, 'CHRISTMAS', '2024-12-25', 1, 1, 'FIXED'); +INSERT INTO vacation (id, version, category, month_of_year, day_of_month, duration, expiration, type) VALUES ('123e4567-e89b-12d3-a456-426614174000', 1, 'NEW_YEAR', 1, 1, 1, 1, 'FIXED'); +INSERT INTO vacation (id, version, category, month_of_year, day_of_month, duration, expiration, type) VALUES ('223e4567-e89b-12d3-a456-426614174001', 1, 'MONDAY_CARNIVAL', 2, 12, 1, 1, 'FIXED'); +INSERT INTO vacation (id, version, category, month_of_year, day_of_month, duration, expiration, type) VALUES ('323e4567-e89b-12d3-a456-426614174002', 1, 'TUESDAY_CARNIVAL', 2, 13, 1, 1, 'FIXED'); +INSERT INTO vacation (id, version, category, month_of_year, day_of_month, duration, expiration, type) VALUES ('423e4567-e89b-12d3-a456-426614174003', 1, 'GOOD_FRIDAY', 3, 29, 1, 1, 'FIXED'); +INSERT INTO vacation (id, version, category, month_of_year, day_of_month, duration, expiration, type) VALUES ('523e4567-e89b-12d3-a456-426614174004', 1, 'LABOR_DAY', 5, 1, 1, 1, 'FIXED'); +INSERT INTO vacation (id, version, category, month_of_year, day_of_month, duration, expiration, type) VALUES ('623e4567-e89b-12d3-a456-426614174005', 1, 'INDEPENDENCE_DAY', 8, 6, 1, 1, 'FIXED'); +INSERT INTO vacation (id, version, category, month_of_year, day_of_month, duration, expiration, type) VALUES ('723e4567-e89b-12d3-a456-426614174006', 1, 'CHRISTMAS', 12, 25, 1, 1, 'FIXED'); -INSERT INTO vacation (id, version, category, vacation_date, duration, expiration, type) VALUES ('823e4567-e89b-12d3-a456-426614174007', 1, 'PRURINATIONAL_STATE_DAY', '2024-01-21', 1, 30, 'MOVABLE'); -INSERT INTO vacation (id, version, category, vacation_date, duration, expiration, type) VALUES ('923e4567-e89b-12d3-a456-426614174008', 1, 'CORPUS_CHRISTI', '2024-05-30', 1, 30, 'MOVABLE'); -INSERT INTO vacation (id, version, category, vacation_date, duration, expiration, type) VALUES ('a23e4567-e89b-12d3-a456-426614174009', 1, 'ANDEAN_NEW_YEAR', '2024-06-21', 1, 30, 'MOVABLE'); -INSERT INTO vacation (id, version, category, vacation_date, duration, expiration, type) VALUES ('b23e4567-e89b-12d3-a456-42661417400a', 1, 'DEPARTMENTAL_ANNIVERSARY', '2024-09-14', 1, 30, 'MOVABLE'); -INSERT INTO vacation (id, version, category, vacation_date, duration, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417400b', 1, 'ALL_SOULS_DAY', '2024-11-02', 1, 30, 'MOVABLE'); +INSERT INTO vacation (id, version, category, month_of_year, day_of_month, duration, expiration, type) VALUES ('823e4567-e89b-12d3-a456-426614174007', 1, 'PRURINATIONAL_STATE_DAY', 1, 21, 1, 30, 'MOVABLE'); +INSERT INTO vacation (id, version, category, month_of_year, day_of_month, duration, expiration, type) VALUES ('923e4567-e89b-12d3-a456-426614174008', 1, 'CORPUS_CHRISTI', 5, 30, 1, 30, 'MOVABLE'); +INSERT INTO vacation (id, version, category, month_of_year, day_of_month, duration, expiration, type) VALUES ('a23e4567-e89b-12d3-a456-426614174009', 1, 'ANDEAN_NEW_YEAR', 6, 21, 1, 30, 'MOVABLE'); +INSERT INTO vacation (id, version, category, month_of_year, day_of_month, duration, expiration, type) VALUES ('b23e4567-e89b-12d3-a456-42661417400a', 1, 'DEPARTMENTAL_ANNIVERSARY', 9, 14, 1, 30, 'MOVABLE'); +INSERT INTO vacation (id, version, category, month_of_year, day_of_month, duration, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417400b', 1, 'ALL_SOULS_DAY', 11, 2, 1, 30, 'MOVABLE'); INSERT INTO vacation (id, version, category, duration, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417400c', 1, 'BIRTHDAY', 0.5, 365, 'OTHER'); INSERT INTO vacation (id, version, category, duration, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417400d', 1, 'MATERNITY', 90, 90, 'OTHER'); INSERT INTO vacation (id, version, category, duration, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417400e', 1, 'PATERNITY', 3, 3, 'OTHER'); INSERT INTO vacation (id, version, category, duration, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417400f', 1, 'MARRIAGE', 3, 3, 'OTHER'); -INSERT INTO vacation (id, version, category, vacation_date, duration, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417401a', 1, 'FATHERS_DAY', '2024-03-19', 0.5, 30, 'OTHER'); -INSERT INTO vacation (id, version, category, vacation_date, duration, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417401b', 1, 'MOTHERS_DAY', '2024-05-27', 0.5, 30, 'OTHER'); -INSERT INTO vacation (id, version, category, vacation_date, duration, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417401c', 1, 'INTERNATIONAL_WOMENS_DAY', '2024-03-08', 0.5, 30, 'OTHER'); -INSERT INTO vacation (id, version, category, vacation_date, duration, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417401d', 1, 'NATIONAL_WOMENS_DAY', '2024-10-11', 0.5, 30, 'OTHER'); +INSERT INTO vacation (id, version, category, month_of_year, day_of_month, duration, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417401a', 1, 'FATHERS_DAY', 3, 19, 0.5, 30, 'OTHER'); +INSERT INTO vacation (id, version, category, month_of_year, day_of_month, duration, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417401b', 1, 'MOTHERS_DAY', 5, 27, 0.5, 30, 'OTHER'); +INSERT INTO vacation (id, version, category, month_of_year, day_of_month, duration, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417401c', 1, 'INTERNATIONAL_WOMENS_DAY', 3, 8, 0.5, 30, 'OTHER'); +INSERT INTO vacation (id, version, category, month_of_year, day_of_month, duration, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417401d', 1, 'NATIONAL_WOMENS_DAY', 10, 11, 0.5, 30, 'OTHER'); INSERT INTO vacation (id, version, category, duration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417401e', 1, 'HEALTH_PERMIT', 2, 'OTHER'); INSERT INTO vacation (id, version, category, expiration, type) VALUES ('490e5fbe-895b-42f8-b914-95437f7b39c0', 1, 'VACATION_CURRENT_MANAGEMENT', 730, 'OTHER'); INSERT INTO vacation (id, version, category, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-4266141740ff', 1, 'VACATION_PREVIOUS_MANAGEMENT', 730, 'OTHER'); @@ -66,7 +66,18 @@ 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', 15, '2025-12-31', '2024-10-01', '2024-10-10', 5, 10); +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); +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); +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); +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); +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); +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); insert into time_off_request (id, version, employee_id, category, state, available_days, expiration, start_date, end_date, days_to_be_take, days_balance) From 7d20703a7d92b172ec0d959e2abceb6237e1f122 Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Mon, 28 Oct 2024 00:55:41 -0400 Subject: [PATCH 07/10] =?UTF-8?q?#55=20Perfil=20de=20Personal=20Administra?= =?UTF-8?q?tivo=20-=20A=C3=B1adir=20Vacaciones=20del=20Empleado=20(Verific?= =?UTF-8?q?acion=20de=20validaciones)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../views/RequestRegisterView.java | 62 +++++++++++-------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java index 1176231..d380fed 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java @@ -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 employeeRequests, TimeOffRequestType category) { + private boolean isCategoryAvailable(final List employeeRequests, + final TimeOffRequestType category) { List 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 existingRequests = requestService.findByEmployeeAndCategory(employee.getId(), request.getCategory()); + request.setAvailableDays(availableDaysField.getValue()); + request.setExpiration(endDate); + request.setState(TimeOffRequestStatus.REQUESTED); + + List 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."); From f0c0811f0ddc7c055f693edcbc8317b8136483b3 Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Mon, 28 Oct 2024 18:16:44 -0400 Subject: [PATCH 08/10] =?UTF-8?q?#55=20Perfil=20de=20Personal=20Administra?= =?UTF-8?q?tivo=20-=20A=C3=B1adir=20Vacaciones=20del=20Empleado=20(Verific?= =?UTF-8?q?acion=20obtencion=20de=20datos)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../views/RequestEmployeeView.java | 3 +- .../views/RequestRegisterView.java | 75 ++++++++++++++----- src/main/resources/data.sql | 38 +++------- 3 files changed, 70 insertions(+), 46 deletions(-) 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); From 476884408a090e73f1e3b9769bb0ee2a324db6c9 Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Mon, 28 Oct 2024 19:12:24 -0400 Subject: [PATCH 09/10] #37 Perfil de Personal Administrativo - Listado General de Vacaciones (Agregar total general) --- .../service/VacationService.java | 5 + .../views/RequestsListView.java | 134 ++++++++---------- 2 files changed, 67 insertions(+), 72 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/service/VacationService.java b/src/main/java/com/primefactorsolutions/service/VacationService.java index 15db4b0..084c610 100644 --- a/src/main/java/com/primefactorsolutions/service/VacationService.java +++ b/src/main/java/com/primefactorsolutions/service/VacationService.java @@ -6,6 +6,8 @@ import com.primefactorsolutions.repositories.VacationRepository; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; +import java.util.List; + @Service @AllArgsConstructor public class VacationService { @@ -15,4 +17,7 @@ public class VacationService { return vacationRepository.findByCategory(category); } + public List findVacations() { + return vacationRepository.findAll(); + } } diff --git a/src/main/java/com/primefactorsolutions/views/RequestsListView.java b/src/main/java/com/primefactorsolutions/views/RequestsListView.java index d5c9136..fe61116 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestsListView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestsListView.java @@ -4,6 +4,7 @@ import com.primefactorsolutions.model.*; import com.primefactorsolutions.service.EmployeeService; import com.primefactorsolutions.service.TeamService; import com.primefactorsolutions.service.TimeOffRequestService; +import com.primefactorsolutions.service.VacationService; import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.combobox.ComboBox; import com.vaadin.flow.component.html.Main; @@ -29,7 +30,8 @@ public class RequestsListView extends Main { private final TimeOffRequestService requestService; private final EmployeeService employeeService; private final TeamService teamService; - private final PagingGrid requestGrid = new PagingGrid<>(); + private final VacationService vacationService; + private final PagingGrid requestGrid = new PagingGrid<>(); private List employees = Collections.emptyList(); private ComboBox employeeFilter; @@ -41,10 +43,12 @@ public class RequestsListView extends Main { public RequestsListView(final TimeOffRequestService requestService, final EmployeeService employeeService, - final TeamService teamService) { + final TeamService teamService, + final VacationService vacationService) { this.requestService = requestService; this.employeeService = employeeService; this.teamService = teamService; + this.vacationService = vacationService; this.employees = employeeService.findAllEmployees(); initializeView(); refreshGeneralRequestGrid(null, null, null, null); @@ -60,7 +64,6 @@ public class RequestsListView extends Main { private void setupFilters() { add(createEmployeeFilter()); add(createTeamFilter()); - add(createCategoryFilter()); add(createStateFilter()); } @@ -68,16 +71,14 @@ public class RequestsListView extends Main { requestGrid.addColumn(this::getEmployeeFullName).setHeader("Employee"); requestGrid.addColumn(this::getTeamName).setHeader("Team"); requestGrid.addColumn(this::getEmployeeStatus).setHeader("Employee State"); - requestGrid.addColumn(this::getCategory).setHeader("Category"); - requestGrid.addColumn(this::getState).setHeader("Request Status"); - requestGrid.addColumn(this::getDaysBalance).setHeader("Balance"); + requestGrid.addColumn(this::getGeneralTotal).setHeader("General Total"); requestGrid.setPaginationBarMode(PagingGrid.PaginationBarMode.BOTTOM); requestGrid.setPageSize(5); requestGrid.asSingleSelect().addValueChangeListener(event -> { - TimeOffRequest selectedRequest = event.getValue(); + Employee selectedRequest = event.getValue(); if (selectedRequest != null) { - selectedEmployeeId = selectedRequest.getEmployee().getId(); + selectedEmployeeId = selectedRequest.getId(); } }); } @@ -100,63 +101,50 @@ public class RequestsListView extends Main { final Status state) { requestGrid.setPagingDataProvider((page, pageSize) -> { int start = (int) (page * requestGrid.getPageSize()); - return fetchFilteredEmployees(start, pageSize, employee, team, category, state); + return fetchFilteredEmployees(start, pageSize, employee, team, state); }); requestGrid.getDataProvider().refreshAll(); } - private List fetchFilteredEmployees(final int start, - final int pageSize, - final Employee employee, - final Team team, - final TimeOffRequestType category, - final Status employeeState) { - List filteredRequests = requestService.findAllTimeOffRequests(); + private List fetchFilteredEmployees(final int start, + final int pageSize, + final Employee employee, + final Team team, + final Status employeeState) { + List filteredEmployees = employeeService.findAllEmployees(); if (employee != null && !"ALL".equals(employee.getFirstName())) { - filteredRequests = filteredRequests.stream() - .filter(emp -> emp.getEmployee().getId().equals(employee.getId())) + filteredEmployees = filteredEmployees.stream() + .filter(emp -> emp.getId().equals(employee.getId())) .collect(Collectors.toList()); } if (team != null && !"ALL".equals(team.getName())) { - filteredRequests = filteredRequests.stream() - .filter(emp -> { - Team empTeam = emp.getEmployee().getTeam(); - return empTeam != null && empTeam.getId().equals(team.getId()); - }) - .collect(Collectors.toList()); - } - - if (category != null && category != TimeOffRequestType.values()[0]) { - filteredRequests = filteredRequests.stream() - .filter(emp -> emp.getCategory().equals(category)) + filteredEmployees = filteredEmployees.stream() + .filter(emp -> emp.getTeam() != null && emp.getTeam().getId().equals(team.getId())) .collect(Collectors.toList()); } if (employeeState != null && employeeState != Status.ALL) { - filteredRequests = filteredRequests.stream() - .filter(emp -> employeeState == Status.IDLE - ? emp.getState().equals(TimeOffRequestStatus.IN_USE) - : !emp.getState().equals(TimeOffRequestStatus.IN_USE)) + filteredEmployees = filteredEmployees.stream() + .filter(emp -> { + Optional request = requestService + .findByEmployeeAndState(emp.getId(), TimeOffRequestStatus.TAKEN); + return employeeState == Status.IDLE ? request.isPresent() : request.isEmpty(); + }) .collect(Collectors.toList()); } - int end = Math.min(start + pageSize, filteredRequests.size()); - return filteredRequests.subList(start, end); + int end = Math.min(start + pageSize, filteredEmployees.size()); + return filteredEmployees.subList(start, end); } - private String getEmployeeFullName(final TimeOffRequest request) { - Employee employee = request.getEmployee(); - return getEmployeeFullNameLabel(employee); - } - - private String getEmployeeFullNameLabel(final Employee employee) { + private String getEmployeeFullName(final Employee employee) { return "ALL".equals(employee.getFirstName()) ? "ALL" : employee.getFirstName() + " " + employee.getLastName(); } - private String getTeamName(final TimeOffRequest request) { - Team team = request.getEmployee().getTeam(); + private String getTeamName(final Employee employee) { + Team team = employee.getTeam(); return team != null ? team.getName() : "Unassigned"; } @@ -164,21 +152,38 @@ public class RequestsListView extends Main { return "ALL".equals(team.getName()) ? "ALL" : team.getName(); } - - private String getEmployeeStatus(final TimeOffRequest request) { - return request.getState() == TimeOffRequestStatus.IN_USE ? "IDLE" : "ACTIVE"; + private String getEmployeeStatus(final Employee employee) { + Optional activeRequest = requestService + .findByEmployeeAndState(employee.getId(), TimeOffRequestStatus.TAKEN); + return activeRequest.isPresent() ? "IDLE" : "ACTIVE"; } - private String getCategory(final TimeOffRequest request) { - return request.getCategory().toString(); - } - - private String getState(final TimeOffRequest request) { - return request.getState().toString(); - } - - private String getDaysBalance(final TimeOffRequest request) { - return request.getDaysBalance().toString(); + private String getGeneralTotal(final Employee employee) { + List employeeRequests = requestService.findRequestsByEmployeeId(employee.getId()); + List vacations = vacationService.findVacations(); + double totalUtilized = employeeRequests.stream() + .filter(Objects::nonNull) + .mapToDouble(request -> { + Double daysBalance = request.getAvailableDays(); + return daysBalance != null ? daysBalance : 0.0; + }) + .sum(); + double totalUnused = employeeRequests.stream() + .filter(Objects::nonNull) + .mapToDouble(request -> { + Double daysBalance = request.getDaysBalance(); + return daysBalance != null ? daysBalance : 0.0; + }) + .sum(); + double totalAvailable = vacations.stream() + .filter(Objects::nonNull) + .mapToDouble(request -> { + Double daysBalance = request.getDuration(); + return daysBalance != null ? daysBalance : 0.0; + }) + .sum(); + double generalTotal = (totalAvailable - totalUtilized) + totalUnused; + return String.valueOf(generalTotal); } private ComboBox createEmployeeFilter() { @@ -186,7 +191,7 @@ public class RequestsListView extends Main { List employees = new ArrayList<>(employeeService.findAllEmployees()); employees.addFirst(createAllEmployeesOption()); employeeFilter.setItems(employees); - employeeFilter.setItemLabelGenerator(this::getEmployeeFullNameLabel); + employeeFilter.setItemLabelGenerator(this::getEmployeeFullName); employeeFilter.setValue(employees.getFirst()); employeeFilter.addValueChangeListener(event -> refreshGeneralRequestGrid( @@ -217,21 +222,6 @@ public class RequestsListView extends Main { return teamFilter; } - private ComboBox createCategoryFilter() { - categoryFilter = new ComboBox<>("Category"); - categoryFilter.setItems(TimeOffRequestType.values()); - categoryFilter.setValue(TimeOffRequestType.values()[0]); - categoryFilter.addValueChangeListener(event -> - refreshGeneralRequestGrid( - employeeFilter.getValue(), - teamFilter.getValue(), - event.getValue(), - stateFilter.getValue() - ) - ); - return categoryFilter; - } - private ComboBox createStateFilter() { stateFilter = new ComboBox<>("Employee State"); stateFilter.setItems(Status.values()); From 98ed64dfda50e85272411deed7a34cc02afba69a Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Tue, 29 Oct 2024 15:28:20 -0400 Subject: [PATCH 10/10] Listado Solicitudes de Empleado - Calcular totales --- .../service/VacationService.java | 2 +- .../views/RequestEmployeeView.java | 39 ++++++++++++++++++- .../views/RequestRegisterView.java | 20 +++++++--- 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/service/VacationService.java b/src/main/java/com/primefactorsolutions/service/VacationService.java index 084c610..94cb763 100644 --- a/src/main/java/com/primefactorsolutions/service/VacationService.java +++ b/src/main/java/com/primefactorsolutions/service/VacationService.java @@ -20,4 +20,4 @@ public class VacationService { public List findVacations() { return vacationRepository.findAll(); } -} +} \ No newline at end of file diff --git a/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java b/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java index f5f8e10..26872a7 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestEmployeeView.java @@ -3,13 +3,16 @@ package com.primefactorsolutions.views; import com.primefactorsolutions.model.*; import com.primefactorsolutions.service.EmployeeService; import com.primefactorsolutions.service.TimeOffRequestService; +import com.primefactorsolutions.service.VacationService; import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.combobox.ComboBox; import com.vaadin.flow.component.grid.Grid; import com.vaadin.flow.component.html.Div; import com.vaadin.flow.component.html.H3; +import com.vaadin.flow.component.html.Span; import com.vaadin.flow.component.notification.Notification; import com.vaadin.flow.component.orderedlayout.HorizontalLayout; +import com.vaadin.flow.component.orderedlayout.VerticalLayout; import com.vaadin.flow.router.BeforeEvent; import com.vaadin.flow.router.HasUrlParameter; import com.vaadin.flow.router.PageTitle; @@ -33,6 +36,7 @@ public class RequestEmployeeView extends Div implements HasUrlParameter private final TimeOffRequestService requestService; private final EmployeeService employeeService; + private final VacationService vacationService; private final Grid requestGrid = new Grid<>(TimeOffRequest.class); private List requests = Collections.emptyList(); private ComboBox categoryFilter; @@ -40,15 +44,18 @@ public class RequestEmployeeView extends Div implements HasUrlParameter private UUID employeeId; private TimeOffRequest request; - public RequestEmployeeView(final TimeOffRequestService requestService, final EmployeeService employeeService) { + public RequestEmployeeView(final TimeOffRequestService requestService, + final EmployeeService employeeService, + final VacationService vacationService) { this.requestService = requestService; this.employeeService = employeeService; + this.vacationService = vacationService; } private void initializeView() { setupFilters(); setupGrid(); - add(requestGrid, createActionButtons()); + add(requestGrid, createActionButtons(), createSummaryLayout()); refreshRequestGrid(null, null); } @@ -92,6 +99,34 @@ public class RequestEmployeeView extends Div implements HasUrlParameter }); } + private VerticalLayout createSummaryLayout() { + double totalHoliday = requests.stream() + .filter(this::verificationIsHoliday) + .mapToDouble(TimeOffRequest::getAvailableDays) + .sum(); + double totalVacations = requests.stream() + .filter(req -> req.getCategory().toString().startsWith("VACATION")) + .mapToDouble(TimeOffRequest::getAvailableDays) + .sum(); + double totalPersonalDays = requests.stream() + .filter(req -> !verificationIsHoliday(req)) // Solo los de tipo OTHER + .mapToDouble(TimeOffRequest::getAvailableDays) + .sum(); + double totalAvailableDays = totalHoliday + totalVacations + totalPersonalDays; + + return new VerticalLayout( + new Span("TOTAL HOLIDAYS: " + totalHoliday), + new Span("TOTAL VACATIONS: " + totalVacations), + new Span("TOTAL PERSONAL DAYS: " + totalPersonalDays), + new Span("TOTAL GENERAL: " + totalAvailableDays) + ); + } + + private Boolean verificationIsHoliday(final TimeOffRequest request) { + Vacation vacation = vacationService.findVacationByCategory(request.getCategory()); + return vacation.getType() != Vacation.Type.OTHER; + } + private HorizontalLayout createActionButtons() { Button viewButton = new Button("View", event -> { if (request != null) { diff --git a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java index b81047c..a3b1f54 100644 --- a/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java +++ b/src/main/java/com/primefactorsolutions/views/RequestRegisterView.java @@ -148,8 +148,8 @@ public class RequestRegisterView extends VerticalLayout { 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); + return latestRequest.getState() == TimeOffRequestStatus.EXPIRED + || (latestRequest.getState() == TimeOffRequestStatus.TAKEN && latestRequest.getDaysBalance() > 0); } else { return latestRequest.getState() == TimeOffRequestStatus.EXPIRED; } @@ -180,7 +180,8 @@ public class RequestRegisterView extends VerticalLayout { List requests = requestService.findByEmployeeAndCategory(employeeId, selectedCategory); if (vacation != null) { TimeOffRequest requestWithBalance = requests.stream() - .filter(request -> request.getDaysBalance() > 0 && request.getState() != TimeOffRequestStatus.EXPIRED) + .filter(request -> request.getDaysBalance() > 0 + && request.getState() != TimeOffRequestStatus.EXPIRED) .max(Comparator.comparing(TimeOffRequest::getStartDate)) .orElse(null); if (requestWithBalance != null) { @@ -214,7 +215,8 @@ public class RequestRegisterView extends VerticalLayout { endDate = null; UUID employeeId = employee.getId(); - List previousRequests = requestService.findByEmployeeAndCategory(employeeId, vacation.getCategory()); + List previousRequests + = requestService.findByEmployeeAndCategory(employeeId, vacation.getCategory()); int startYear; if (previousRequests.isEmpty()) { @@ -236,11 +238,17 @@ public class RequestRegisterView extends VerticalLayout { } if (vacation.getMonthOfYear() != null && vacation.getDayOfMonth() != null) { - startDate = LocalDate.of(startYear, 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 = LocalDate.of(startYear, employee.getBirthday().getMonth(), employee.getBirthday().getDayOfMonth()); + 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();