Vacaciones #61

Merged
jesus.pelaez merged 10 commits from Vacaciones into En-desarrollo 2024-10-30 18:31:56 +00:00
3 changed files with 96 additions and 90 deletions
Showing only changes of commit 6a1d3e70c6 - Show all commits

View File

@ -8,10 +8,8 @@ import com.vaadin.flow.component.combobox.ComboBox;
import com.vaadin.flow.component.grid.Grid; import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.html.Div; import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.H3; 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.notification.Notification;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout; 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.BeforeEvent;
import com.vaadin.flow.router.HasUrlParameter; import com.vaadin.flow.router.HasUrlParameter;
import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.PageTitle;
@ -21,7 +19,6 @@ import jakarta.annotation.security.PermitAll;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.Year;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@ -37,6 +34,7 @@ public class RequestEmployeeView extends Div implements HasUrlParameter<String>
private final TimeOffRequestService requestService; private final TimeOffRequestService requestService;
private final EmployeeService employeeService; private final EmployeeService employeeService;
private final Grid<TimeOffRequest> requestGrid = new Grid<>(TimeOffRequest.class); private final Grid<TimeOffRequest> requestGrid = new Grid<>(TimeOffRequest.class);
private final Grid<Vacation> summaryGrid = new Grid<>();
private List<TimeOffRequest> requests = Collections.emptyList(); private List<TimeOffRequest> requests = Collections.emptyList();
private ComboBox<TimeOffRequestType> categoryFilter; private ComboBox<TimeOffRequestType> categoryFilter;
private ComboBox<TimeOffRequestStatus> stateFilter; private ComboBox<TimeOffRequestStatus> stateFilter;
@ -51,7 +49,8 @@ public class RequestEmployeeView extends Div implements HasUrlParameter<String>
private void initializeView() { private void initializeView() {
setupFilters(); setupFilters();
setupGrid(); setupGrid();
add(requestGrid, createSummaryLayout(), createActionButtons()); setupSummaryGrid();
add(requestGrid, createActionButtons(), summaryGrid);
refreshRequestGrid(null, null); refreshRequestGrid(null, null);
} }
@ -81,12 +80,9 @@ public class RequestEmployeeView extends Div implements HasUrlParameter<String>
requestGrid.setColumns( requestGrid.setColumns(
"category", "category",
"state", "state",
"availableDays",
"expiration",
"startDate", "startDate",
"endDate", "endDate",
"daysToBeTake", "daysToBeTake"
"daysBalance"
); );
requestGrid.setAllRowsVisible(true); requestGrid.setAllRowsVisible(true);
requestGrid.asSingleSelect().addValueChangeListener(event -> { requestGrid.asSingleSelect().addValueChangeListener(event -> {
@ -97,23 +93,29 @@ public class RequestEmployeeView extends Div implements HasUrlParameter<String>
}); });
} }
private VerticalLayout createSummaryLayout() { private void setupSummaryGrid() {
int currentYear = Year.now().getValue(); summaryGrid.addColumn(this::getCategory).setHeader("Category");
String yearCategory = "YEAR_" + currentYear; summaryGrid.addColumn(this::getStatus).setHeader("Available");
double totalVacations = requests.stream() summaryGrid.addColumn(this::getTotalRequests).setHeader("Expiration");
.filter(req -> req.getCategory().name().equals(yearCategory)) summaryGrid.addColumn(this::getTotalDays).setHeader("Taken");
.mapToDouble(TimeOffRequest::getAvailableDays) summaryGrid.addColumn(this::getTotalDays).setHeader("Balance");
.sum(); summaryGrid.setAllRowsVisible(true);
double totalTimeOff = requests.stream() }
.filter(req -> !req.getCategory().name().startsWith("YEAR"))
.mapToDouble(TimeOffRequest::getDaysBalance) private Object getTotalDays(final Vacation vacation) {
.sum(); return null;
double totalAvailableDays = totalVacations + totalTimeOff; }
return new VerticalLayout(
new Span("TOTAL HOLIDAYS: " + totalVacations), private Object getTotalRequests(final Vacation vacation) {
new Span("TOTAL TIME OFF: " + totalTimeOff), return null;
new Span("TOTAL AVAILABLE DAYS: " + totalAvailableDays) }
);
private Object getStatus(final Vacation vacation) {
return null;
}
private Object getCategory(final Vacation vacation) {
return vacation.getCategory();
} }
private HorizontalLayout createActionButtons() { private HorizontalLayout createActionButtons() {

View File

@ -22,6 +22,7 @@ import java.time.LocalDate;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.UUID;
@SpringComponent @SpringComponent
@PermitAll @PermitAll
@ -67,7 +68,10 @@ public class RequestRegisterView extends VerticalLayout {
private void configureFormFields() { private void configureFormFields() {
employeeComboBox.setItems(employeeService.findAllEmployees()); employeeComboBox.setItems(employeeService.findAllEmployees());
employeeComboBox.setItemLabelGenerator(emp -> emp.getFirstName() + " " + emp.getLastName()); 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); categoryComboBox.setEnabled(false);
startDatePicker.setEnabled(false); startDatePicker.setEnabled(false);
endDatePicker.setEnabled(false); endDatePicker.setEnabled(false);
@ -123,8 +127,10 @@ public class RequestRegisterView extends VerticalLayout {
.map(TimeOffRequest::getCategory) .map(TimeOffRequest::getCategory)
.toList(); .toList();
List<TimeOffRequestType> availableCategories = Arrays.stream(TimeOffRequestType.values()) List<TimeOffRequestType> 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)) || employeeRequests.stream()
.anyMatch(req -> req.getCategory() == category
&& req.getState() == TimeOffRequestStatus.EXPIRED))
.filter(category -> { .filter(category -> {
if (employee.getGender() == Employee.Gender.MALE) { if (employee.getGender() == Employee.Gender.MALE) {
return category != TimeOffRequestType.MATERNITY return category != TimeOffRequestType.MATERNITY
@ -151,7 +157,8 @@ public class RequestRegisterView extends VerticalLayout {
private void updateAvailableDays(final TimeOffRequestType selectedCategory) { private void updateAvailableDays(final TimeOffRequestType selectedCategory) {
vacation = vacationService.findVacationByCategory(selectedCategory); vacation = vacationService.findVacationByCategory(selectedCategory);
List<TimeOffRequest> requests = requestService.findByEmployeeAndCategory(employeeComboBox.getValue().getId(), selectedCategory); UUID employeeId = employeeComboBox.getValue().getId();
List<TimeOffRequest> requests = requestService.findByEmployeeAndCategory(employeeId, selectedCategory);
if (vacation != null) { if (vacation != null) {
if (!requests.isEmpty() && requests.getFirst().getState() != TimeOffRequestStatus.EXPIRED) { if (!requests.isEmpty() && requests.getFirst().getState() != TimeOffRequestStatus.EXPIRED) {
availableDaysField.setValue(requests.getFirst().getDaysBalance()); availableDaysField.setValue(requests.getFirst().getDaysBalance());
@ -294,7 +301,7 @@ public class RequestRegisterView extends VerticalLayout {
} }
} }
private void updateBalanceForCategory(TimeOffRequest newRequest) { private void updateBalanceForCategory(final TimeOffRequest newRequest) {
List<TimeOffRequest> requests = requestService.findByEmployeeAndCategory( List<TimeOffRequest> requests = requestService.findByEmployeeAndCategory(
newRequest.getEmployee().getId(), newRequest.getCategory()); newRequest.getEmployee().getId(), newRequest.getCategory());

View File

@ -29,7 +29,7 @@ public class RequestsListView extends Main {
private final TimeOffRequestService requestService; private final TimeOffRequestService requestService;
private final EmployeeService employeeService; private final EmployeeService employeeService;
private final TeamService teamService; private final TeamService teamService;
private final PagingGrid<Employee> requestGrid = new PagingGrid<>(); private final PagingGrid<TimeOffRequest> requestGrid = new PagingGrid<>();
private List<Employee> employees = Collections.emptyList(); private List<Employee> employees = Collections.emptyList();
private ComboBox<Employee> employeeFilter; private ComboBox<Employee> employeeFilter;
@ -67,18 +67,19 @@ public class RequestsListView extends Main {
private void setupRequestGrid() { private void setupRequestGrid() {
requestGrid.addColumn(this::getEmployeeFullName).setHeader("Employee"); requestGrid.addColumn(this::getEmployeeFullName).setHeader("Employee");
requestGrid.addColumn(this::getTeamName).setHeader("Team"); 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::getCategory).setHeader("Category");
requestGrid.addColumn(this::getState).setHeader("Request Status");
requestGrid.addColumn(this::getStartDate).setHeader("Start Date"); requestGrid.addColumn(this::getStartDate).setHeader("Start Date");
requestGrid.addColumn(this::getEndDate).setHeader("End 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.setPaginationBarMode(PagingGrid.PaginationBarMode.BOTTOM);
requestGrid.setPageSize(5); requestGrid.setPageSize(5);
requestGrid.asSingleSelect().addValueChangeListener(event -> { requestGrid.asSingleSelect().addValueChangeListener(event -> {
Employee selectedRequest = event.getValue(); TimeOffRequest selectedRequest = event.getValue();
if (selectedRequest != null) { if (selectedRequest != null) {
selectedEmployeeId = selectedRequest.getId(); selectedEmployeeId = selectedRequest.getEmployee().getId();
} }
}); });
} }
@ -106,56 +107,58 @@ public class RequestsListView extends Main {
requestGrid.getDataProvider().refreshAll(); requestGrid.getDataProvider().refreshAll();
} }
private List<Employee> fetchFilteredEmployees(final int start, private List<TimeOffRequest> fetchFilteredEmployees(final int start,
final int pageSize, final int pageSize,
final Employee employee, final Employee employee,
final Team team, final Team team,
final TimeOffRequestType category, final TimeOffRequestType category,
final Status state) { final Status employeeState) {
List<Employee> filteredEmployees = employeeService.findAllEmployees(); List<TimeOffRequest> filteredRequests = requestService.findAllTimeOffRequests();
if (employee != null && !"ALL".equals(employee.getFirstName())) { if (employee != null && !"ALL".equals(employee.getFirstName())) {
filteredEmployees = filteredEmployees.stream() filteredRequests = filteredRequests.stream()
.filter(emp -> emp.getId().equals(employee.getId())) .filter(emp -> emp.getEmployee().getId().equals(employee.getId()))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
if (team != null && !"ALL".equals(team.getName())) { if (team != null && !"ALL".equals(team.getName())) {
filteredEmployees = filteredEmployees.stream() filteredRequests = filteredRequests.stream()
.filter(emp -> emp.getTeam() != null && emp.getTeam().getId().equals(team.getId())) .filter(emp -> {
Team empTeam = emp.getEmployee().getTeam();
return empTeam != null && empTeam.getId().equals(team.getId());
})
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
if (category != null && category != TimeOffRequestType.values()[0]) { if (category != null && category != TimeOffRequestType.values()[0]) {
filteredEmployees = filteredEmployees.stream() filteredRequests = filteredRequests.stream()
.filter(emp -> { .filter(emp -> emp.getCategory().equals(category))
Optional<TimeOffRequest> request = requestService
.findByEmployeeAndState(emp.getId(), TimeOffRequestStatus.TAKEN);
return request.isPresent() && request.get().getCategory() == category;
})
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
if (state != null && state != Status.ALL) { if (employeeState != null && employeeState != Status.ALL) {
filteredEmployees = filteredEmployees.stream() filteredRequests = filteredRequests.stream()
.filter(emp -> { .filter(emp -> employeeState == Status.IDLE
Optional<TimeOffRequest> request = requestService ? emp.getState().equals(TimeOffRequestStatus.IN_USE)
.findByEmployeeAndState(emp.getId(), TimeOffRequestStatus.TAKEN); : !emp.getState().equals(TimeOffRequestStatus.IN_USE))
return state == Status.IDLE ? request.isPresent() : request.isEmpty();
})
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
int end = Math.min(start + pageSize, filteredEmployees.size()); int end = Math.min(start + pageSize, filteredRequests.size());
return filteredEmployees.subList(start, end); 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(); return "ALL".equals(employee.getFirstName()) ? "ALL" : employee.getFirstName() + " " + employee.getLastName();
} }
private String getTeamName(final Employee employee) { private String getTeamName(final TimeOffRequest request) {
Team team = employee.getTeam(); Team team = request.getEmployee().getTeam();
return team != null ? team.getName() : "Unassigned"; return team != null ? team.getName() : "Unassigned";
} }
@ -164,34 +167,28 @@ public class RequestsListView extends Main {
} }
private String getEmployeeStatus(final Employee employee) { private String getEmployeeStatus(final TimeOffRequest request) {
Optional<TimeOffRequest> activeRequest = requestService return request.getState() == TimeOffRequestStatus.IN_USE ? "IDLE" : "ACTIVE";
.findByEmployeeAndState(employee.getId(), TimeOffRequestStatus.TAKEN);
return activeRequest.isPresent() ? "IDLE" : "ACTIVE";
} }
private String getCategory(final Employee employee) { private String getCategory(final TimeOffRequest request) {
Optional<TimeOffRequest> activeRequest = requestService return request.getCategory().toString();
.findByEmployeeAndState(employee.getId(), TimeOffRequestStatus.TAKEN);
return activeRequest.map(request -> request.getCategory().toString()).orElse("");
} }
private String getStartDate(final Employee employee) { private String getState(final TimeOffRequest request) {
Optional<TimeOffRequest> activeRequest = requestService return request.getState().toString();
.findByEmployeeAndState(employee.getId(), TimeOffRequestStatus.TAKEN);
return activeRequest.map(request -> request.getStartDate().toString()).orElse("");
} }
private String getEndDate(final Employee employee) { private String getStartDate(final TimeOffRequest request) {
Optional<TimeOffRequest> activeRequest = requestService return request.getStartDate().toString();
.findByEmployeeAndState(employee.getId(), TimeOffRequestStatus.TAKEN);
return activeRequest.map(request -> request.getEndDate().toString()).orElse("");
} }
private String getDaysBalance(final Employee employee) { private String getEndDate(final TimeOffRequest request) {
Optional<TimeOffRequest> activeRequest = requestService return request.getEndDate().toString();
.findByEmployeeAndState(employee.getId(), TimeOffRequestStatus.TAKEN); }
return activeRequest.map(request -> request.getDaysBalance().toString()).orElse("");
private String getDaysBalance(final TimeOffRequest request) {
return request.getDaysBalance().toString();
} }
private ComboBox<Employee> createEmployeeFilter() { private ComboBox<Employee> createEmployeeFilter() {
@ -199,7 +196,7 @@ public class RequestsListView extends Main {
List<Employee> employees = new ArrayList<>(employeeService.findAllEmployees()); List<Employee> employees = new ArrayList<>(employeeService.findAllEmployees());
employees.addFirst(createAllEmployeesOption()); employees.addFirst(createAllEmployeesOption());
employeeFilter.setItems(employees); employeeFilter.setItems(employees);
employeeFilter.setItemLabelGenerator(this::getEmployeeFullName); employeeFilter.setItemLabelGenerator(this::getEmployeeFullNameLabel);
employeeFilter.setValue(employees.getFirst()); employeeFilter.setValue(employees.getFirst());
employeeFilter.addValueChangeListener(event -> employeeFilter.addValueChangeListener(event ->
refreshGeneralRequestGrid( refreshGeneralRequestGrid(
@ -246,7 +243,7 @@ public class RequestsListView extends Main {
} }
private ComboBox<Status> createStateFilter() { private ComboBox<Status> createStateFilter() {
stateFilter = new ComboBox<>("State"); stateFilter = new ComboBox<>("Employee State");
stateFilter.setItems(Status.values()); stateFilter.setItems(Status.values());
stateFilter.setValue(Status.values()[0]); stateFilter.setValue(Status.values()[0]);
stateFilter.addValueChangeListener(event -> stateFilter.addValueChangeListener(event ->