From b5396ae373fa3dfa94fdcc400ccdf5cf5409b9fb Mon Sep 17 00:00:00 2001 From: Melina Gutierrez Date: Tue, 15 Oct 2024 10:11:14 -0400 Subject: [PATCH] #46 Perfil de Empleado - Registro Semanal de Horas trabajadas con el numero de semanas --- .../views/HoursWorkedView.java | 44 ++++++++++++++----- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/views/HoursWorkedView.java b/src/main/java/com/primefactorsolutions/views/HoursWorkedView.java index 9c172b5..5d5491e 100644 --- a/src/main/java/com/primefactorsolutions/views/HoursWorkedView.java +++ b/src/main/java/com/primefactorsolutions/views/HoursWorkedView.java @@ -18,15 +18,16 @@ import com.vaadin.flow.spring.annotation.SpringComponent; import jakarta.annotation.security.PermitAll; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; +import com.vaadin.flow.component.html.Label; + import java.time.DayOfWeek; import java.time.LocalDate; -import java.time.temporal.TemporalAdjusters; import java.time.temporal.WeekFields; import java.util.ArrayList; import java.util.List; import java.util.Locale; -import java.util.Optional; +import java.util.stream.IntStream; @SpringComponent @PermitAll @@ -42,21 +43,28 @@ public class HoursWorkedView extends VerticalLayout { @Autowired private EmployeeService employeeService; + private final Label fechasLabel = new Label("Selecciona una semana para ver las fechas."); + public HoursWorkedView() { actividades = new ArrayList<>(); grid = new Grid<>(Actividad.class, false); totalsGrid = new Grid<>(Actividad.class, false); - DatePicker datePicker = new DatePicker("Selecciona una fecha"); - datePicker.setValue(LocalDate.now()); - datePicker.addValueChangeListener(event -> { - LocalDate selectedDate = event.getValue(); - if (selectedDate != null) { - selectedStartOfWeek = selectedDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); + ComboBox semanaComboBox = new ComboBox<>("Selecciona una semana"); + semanaComboBox.setItems(getWeeksOfYear()); + semanaComboBox.addValueChangeListener(event -> { + String selectedWeek = event.getValue(); + if (selectedWeek != null) { + int weekNumber = Integer.parseInt(selectedWeek.split(" ")[1]); + selectedStartOfWeek = getStartOfWeek(weekNumber); + + LocalDate endOfWeek = selectedStartOfWeek.plusDays(6); + fechasLabel.setText(("Semana del " + selectedStartOfWeek + " al " + endOfWeek)); + mostrarActividadesDeLaSemana(); } }); - datePicker.setWidth("250px"); + semanaComboBox.setWidth("250px"); ComboBox equipoDropdown = new ComboBox<>("Equipo"); equipoDropdown.setItems("Equipo 1", "Equipo 2", "Equipo 3"); @@ -130,15 +138,29 @@ public class HoursWorkedView extends VerticalLayout { HorizontalLayout buttonsLayout = new HorizontalLayout(actualizarButton, guardarButton, cerrarButton); - add(datePicker, filtersLayout, grid, actividadFormLayout, buttonsLayout); + add(semanaComboBox, fechasLabel, filtersLayout, grid, actividadFormLayout, buttonsLayout); add(totalsGrid); configurarTablaTotales(); } + private List getWeeksOfYear() { + return IntStream.rangeClosed(1, 52) + .mapToObj(week -> "Semana " + week) + .toList(); + } + + private LocalDate getStartOfWeek(int weekNumber) { + WeekFields weekFields = WeekFields.of(Locale.getDefault()); + return LocalDate.now() + .with(weekFields.weekOfYear(), weekNumber) + .with(weekFields.dayOfWeek(), DayOfWeek.MONDAY.getValue()); + } + private void mostrarActividadesDeLaSemana() { if (selectedStartOfWeek != null) { - Notification.show("Mostrando actividades de la semana del " + selectedStartOfWeek + " al " + selectedStartOfWeek.plusDays(6)); + Notification.show("Mostrando actividades de la semana del " + + selectedStartOfWeek + " al " + selectedStartOfWeek.plusDays(6)); } }