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');