From f9e1d651ad26691197751fd5769b6a4f76f63939 Mon Sep 17 00:00:00 2001 From: Melina Gutierrez Date: Thu, 28 Nov 2024 08:00:37 -0400 Subject: [PATCH] corregir que cuando selecciona un equipo este solo muestra los empleados de este equipo --- .../views/HoursWorkedView.java | 38 +++++++++++++------ 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/views/HoursWorkedView.java b/src/main/java/com/primefactorsolutions/views/HoursWorkedView.java index 80f4a13..e05b001 100644 --- a/src/main/java/com/primefactorsolutions/views/HoursWorkedView.java +++ b/src/main/java/com/primefactorsolutions/views/HoursWorkedView.java @@ -26,6 +26,7 @@ import java.time.temporal.IsoFields; import java.util.ArrayList; import java.util.List; import java.util.UUID; +import java.util.stream.Collectors; @SpringComponent @PermitAll @@ -142,14 +143,25 @@ public class HoursWorkedView extends BeanValidationForm implements List teams = new ArrayList<>(teamService.findAllTeams()); teamField.setItems(teamService.findAllTeams()); teamField.setItemLabelGenerator(Team::getName); - teamField.setValue(teams.getFirst()); + teamField.setValue(null); teamField.addValueChangeListener(event -> { - if (teams != null) { - employeeField.getValue(); - event.getValue(); - } - } - ); + Team selectedTeam = event.getValue(); + updateEmployeeField(selectedTeam); + }); + } + + private void updateEmployeeField(Team selectedTeam) { + if (selectedTeam != null) { + List employeesInTeam = employeeService.findAllEmployees().stream() + .filter(employee -> employee.getTeam() != null && employee.getTeam().equals(selectedTeam)) + .collect(Collectors.toList()); + employeeField.setItems(employeesInTeam); + if (!employeesInTeam.isEmpty()) { + employeeField.setValue(employeesInTeam.get(0)); + } else { + employeeField.clear(); + } + } } private ComboBox initializeEmployeeField() { @@ -157,7 +169,7 @@ public class HoursWorkedView extends BeanValidationForm implements List employees = new ArrayList<>(employeeService.findAllEmployees()); employeeField.setItems(employees); employeeField.setItemLabelGenerator(this::getEmployeeFullName); - employeeField.setValue(employees.getFirst()); + employeeField.setValue(null); return employeeField; } @@ -168,11 +180,16 @@ public class HoursWorkedView extends BeanValidationForm implements private void initializeDateField() { LocalDate today = LocalDate.now(); - LocalDate lastWeekMonday = today.minusWeeks(1).with(java.time.DayOfWeek.MONDAY); + YearMonth currentMonth = YearMonth.of(today.getYear(), today.getMonth()); + + LocalDate startOfMonth = currentMonth.atDay(1); + LocalDate maxSelectableDate = today; - dateField.setMin(lastWeekMonday); + + dateField.setMin(startOfMonth); dateField.setMax(maxSelectableDate); dateField.setValue(today); + dateField.addValueChangeListener(event -> { LocalDate selectedDate = event.getValue(); if (selectedDate != null) { @@ -186,7 +203,6 @@ public class HoursWorkedView extends BeanValidationForm implements }); } - private void saveHoursWorked() { if (isFormValid()) { HoursWorked hoursWorked = getEntity();