Vacaciones #61

Merged
jesus.pelaez merged 10 commits from Vacaciones into En-desarrollo 2024-10-30 18:31:56 +00:00
2 changed files with 67 additions and 72 deletions
Showing only changes of commit 476884408a - Show all commits

View File

@ -6,6 +6,8 @@ import com.primefactorsolutions.repositories.VacationRepository;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List;
@Service @Service
@AllArgsConstructor @AllArgsConstructor
public class VacationService { public class VacationService {
@ -15,4 +17,7 @@ public class VacationService {
return vacationRepository.findByCategory(category); return vacationRepository.findByCategory(category);
} }
public List<Vacation> findVacations() {
return vacationRepository.findAll();
}
} }

View File

@ -4,6 +4,7 @@ import com.primefactorsolutions.model.*;
import com.primefactorsolutions.service.EmployeeService; import com.primefactorsolutions.service.EmployeeService;
import com.primefactorsolutions.service.TeamService; import com.primefactorsolutions.service.TeamService;
import com.primefactorsolutions.service.TimeOffRequestService; import com.primefactorsolutions.service.TimeOffRequestService;
import com.primefactorsolutions.service.VacationService;
import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.combobox.ComboBox; import com.vaadin.flow.component.combobox.ComboBox;
import com.vaadin.flow.component.html.Main; import com.vaadin.flow.component.html.Main;
@ -29,7 +30,8 @@ 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<TimeOffRequest> requestGrid = new PagingGrid<>(); private final VacationService vacationService;
private final PagingGrid<Employee> requestGrid = new PagingGrid<>();
private List<Employee> employees = Collections.emptyList(); private List<Employee> employees = Collections.emptyList();
private ComboBox<Employee> employeeFilter; private ComboBox<Employee> employeeFilter;
@ -41,10 +43,12 @@ public class RequestsListView extends Main {
public RequestsListView(final TimeOffRequestService requestService, public RequestsListView(final TimeOffRequestService requestService,
final EmployeeService employeeService, final EmployeeService employeeService,
final TeamService teamService) { final TeamService teamService,
final VacationService vacationService) {
this.requestService = requestService; this.requestService = requestService;
this.employeeService = employeeService; this.employeeService = employeeService;
this.teamService = teamService; this.teamService = teamService;
this.vacationService = vacationService;
this.employees = employeeService.findAllEmployees(); this.employees = employeeService.findAllEmployees();
initializeView(); initializeView();
refreshGeneralRequestGrid(null, null, null, null); refreshGeneralRequestGrid(null, null, null, null);
@ -60,7 +64,6 @@ public class RequestsListView extends Main {
private void setupFilters() { private void setupFilters() {
add(createEmployeeFilter()); add(createEmployeeFilter());
add(createTeamFilter()); add(createTeamFilter());
add(createCategoryFilter());
add(createStateFilter()); add(createStateFilter());
} }
@ -68,16 +71,14 @@ public class RequestsListView extends Main {
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("Employee State"); requestGrid.addColumn(this::getEmployeeStatus).setHeader("Employee State");
requestGrid.addColumn(this::getCategory).setHeader("Category"); requestGrid.addColumn(this::getGeneralTotal).setHeader("General Total");
requestGrid.addColumn(this::getState).setHeader("Request Status");
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 -> {
TimeOffRequest selectedRequest = event.getValue(); Employee selectedRequest = event.getValue();
if (selectedRequest != null) { if (selectedRequest != null) {
selectedEmployeeId = selectedRequest.getEmployee().getId(); selectedEmployeeId = selectedRequest.getId();
} }
}); });
} }
@ -100,63 +101,50 @@ public class RequestsListView extends Main {
final Status state) { final Status state) {
requestGrid.setPagingDataProvider((page, pageSize) -> { requestGrid.setPagingDataProvider((page, pageSize) -> {
int start = (int) (page * requestGrid.getPageSize()); int start = (int) (page * requestGrid.getPageSize());
return fetchFilteredEmployees(start, pageSize, employee, team, category, state); return fetchFilteredEmployees(start, pageSize, employee, team, state);
}); });
requestGrid.getDataProvider().refreshAll(); requestGrid.getDataProvider().refreshAll();
} }
private List<TimeOffRequest> fetchFilteredEmployees(final int start, private List<Employee> 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 Status employeeState) { final Status employeeState) {
List<TimeOffRequest> filteredRequests = requestService.findAllTimeOffRequests(); List<Employee> filteredEmployees = employeeService.findAllEmployees();
if (employee != null && !"ALL".equals(employee.getFirstName())) { if (employee != null && !"ALL".equals(employee.getFirstName())) {
filteredRequests = filteredRequests.stream() filteredEmployees = filteredEmployees.stream()
.filter(emp -> emp.getEmployee().getId().equals(employee.getId())) .filter(emp -> emp.getId().equals(employee.getId()))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
if (team != null && !"ALL".equals(team.getName())) { if (team != null && !"ALL".equals(team.getName())) {
filteredRequests = filteredRequests.stream() filteredEmployees = filteredEmployees.stream()
.filter(emp -> { .filter(emp -> emp.getTeam() != null && emp.getTeam().getId().equals(team.getId()))
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))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
if (employeeState != null && employeeState != Status.ALL) { if (employeeState != null && employeeState != Status.ALL) {
filteredRequests = filteredRequests.stream() filteredEmployees = filteredEmployees.stream()
.filter(emp -> employeeState == Status.IDLE .filter(emp -> {
? emp.getState().equals(TimeOffRequestStatus.IN_USE) Optional<TimeOffRequest> request = requestService
: !emp.getState().equals(TimeOffRequestStatus.IN_USE)) .findByEmployeeAndState(emp.getId(), TimeOffRequestStatus.TAKEN);
return employeeState == Status.IDLE ? request.isPresent() : request.isEmpty();
})
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
int end = Math.min(start + pageSize, filteredRequests.size()); int end = Math.min(start + pageSize, filteredEmployees.size());
return filteredRequests.subList(start, end); return filteredEmployees.subList(start, end);
} }
private String getEmployeeFullName(final TimeOffRequest request) { private String getEmployeeFullName(final Employee employee) {
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 TimeOffRequest request) { private String getTeamName(final Employee employee) {
Team team = request.getEmployee().getTeam(); Team team = employee.getTeam();
return team != null ? team.getName() : "Unassigned"; return team != null ? team.getName() : "Unassigned";
} }
@ -164,21 +152,38 @@ public class RequestsListView extends Main {
return "ALL".equals(team.getName()) ? "ALL" : team.getName(); return "ALL".equals(team.getName()) ? "ALL" : team.getName();
} }
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 TimeOffRequest request) { private String getGeneralTotal(final Employee employee) {
return request.getCategory().toString(); List<TimeOffRequest> employeeRequests = requestService.findRequestsByEmployeeId(employee.getId());
} List<Vacation> vacations = vacationService.findVacations();
double totalUtilized = employeeRequests.stream()
private String getState(final TimeOffRequest request) { .filter(Objects::nonNull)
return request.getState().toString(); .mapToDouble(request -> {
} Double daysBalance = request.getAvailableDays();
return daysBalance != null ? daysBalance : 0.0;
private String getDaysBalance(final TimeOffRequest request) { })
return request.getDaysBalance().toString(); .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<Employee> createEmployeeFilter() { private ComboBox<Employee> createEmployeeFilter() {
@ -186,7 +191,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::getEmployeeFullNameLabel); employeeFilter.setItemLabelGenerator(this::getEmployeeFullName);
employeeFilter.setValue(employees.getFirst()); employeeFilter.setValue(employees.getFirst());
employeeFilter.addValueChangeListener(event -> employeeFilter.addValueChangeListener(event ->
refreshGeneralRequestGrid( refreshGeneralRequestGrid(
@ -217,21 +222,6 @@ public class RequestsListView extends Main {
return teamFilter; return teamFilter;
} }
private ComboBox<TimeOffRequestType> 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<Status> createStateFilter() { private ComboBox<Status> createStateFilter() {
stateFilter = new ComboBox<>("Employee State"); stateFilter = new ComboBox<>("Employee State");
stateFilter.setItems(Status.values()); stateFilter.setItems(Status.values());