From 6fbf6e8fa71c20b089ae65a5376c86fe83311284 Mon Sep 17 00:00:00 2001 From: Melina Gutierrez Date: Fri, 15 Nov 2024 10:23:40 -0400 Subject: [PATCH] Formulario de registro de horas trabajadas mas el formulario --- .../model/HoursWorked.java | 19 ++++++++++++++++++- .../service/HoursWorkedService.java | 10 ++++++++++ .../views/HoursWorkedListView.java | 3 +++ .../views/HoursWorkedView.java | 15 +++++++++++++-- .../views/ReporteView.java | 2 ++ src/main/resources/data.sql | 2 +- 6 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/model/HoursWorked.java b/src/main/java/com/primefactorsolutions/model/HoursWorked.java index 32f465b..e98a29a 100644 --- a/src/main/java/com/primefactorsolutions/model/HoursWorked.java +++ b/src/main/java/com/primefactorsolutions/model/HoursWorked.java @@ -7,6 +7,9 @@ import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import java.time.LocalDate; +import java.time.temporal.WeekFields; +import java.util.List; +import java.util.Locale; @Data @Entity @@ -31,6 +34,17 @@ public class HoursWorked extends BaseEntity { private double horaspendientes; private double totalHours; + public static double calculateTotalHours(List activities) { + return activities.stream() + .mapToDouble(activity -> activity.hours + activity.horasTareasEspecificas) + .sum(); + } + + public static double calculatePendingHours(List activities) { + double totalHoursWorked = calculateTotalHours(activities); + return Math.max(0, 40 - totalHoursWorked); + } + public Employee getEmployee() { return employee; } @@ -46,13 +60,16 @@ public class HoursWorked extends BaseEntity { public void setWeekNumber(final int weekNumber) { this.weekNumber = weekNumber; } - public LocalDate getDate() { return date; } public void setDate(final LocalDate date) { this.date = date; + if (date != null) { + WeekFields weekFields = WeekFields.of(Locale.getDefault()); + this.weekNumber = date.get(weekFields.weekOfWeekBasedYear()); + } } public String getActividad() { diff --git a/src/main/java/com/primefactorsolutions/service/HoursWorkedService.java b/src/main/java/com/primefactorsolutions/service/HoursWorkedService.java index 72960d9..4ab66d2 100644 --- a/src/main/java/com/primefactorsolutions/service/HoursWorkedService.java +++ b/src/main/java/com/primefactorsolutions/service/HoursWorkedService.java @@ -44,6 +44,16 @@ public class HoursWorkedService { return hoursWorkedRepository.save(hoursWorked); } + public double getTotalHoursForEmployee(UUID employeeId, int weekNumber) { + List activities = hoursWorkedRepository.findByEmployeeIdAndWeekNumber(employeeId, weekNumber); + return HoursWorked.calculateTotalHours(activities); + } + + public double getPendingHoursForEmployee(UUID employeeId, int weekNumber) { + List activities = hoursWorkedRepository.findByEmployeeIdAndWeekNumber(employeeId, weekNumber); + return HoursWorked.calculatePendingHours(activities); + } + public List findByWeekNumber(final int weekNumber) { return hoursWorkedRepository.findByWeekNumber(weekNumber); } diff --git a/src/main/java/com/primefactorsolutions/views/HoursWorkedListView.java b/src/main/java/com/primefactorsolutions/views/HoursWorkedListView.java index be01789..fb7f45d 100644 --- a/src/main/java/com/primefactorsolutions/views/HoursWorkedListView.java +++ b/src/main/java/com/primefactorsolutions/views/HoursWorkedListView.java @@ -125,6 +125,9 @@ public class HoursWorkedListView extends Main { hoursWorkedGrid.addColumn(hw -> hw.getDate() != null ? hw.getDate().toString() : "") .setHeader("Fecha") .setSortable(true); + hoursWorkedGrid.addColumn(hw -> hw.getWeekNumber()) + .setHeader("Semana") + .setSortable(true); hoursWorkedGrid.addColumn(hw -> hw.getEmployee().getFirstName() + " " + hw.getEmployee().getLastName()) .setHeader("Empleado"); hoursWorkedGrid.addColumn(hw -> hw.getEmployee().getTeam() != null ? hw.getEmployee().getTeam().getName() : "Sin asignar") diff --git a/src/main/java/com/primefactorsolutions/views/HoursWorkedView.java b/src/main/java/com/primefactorsolutions/views/HoursWorkedView.java index 752bad9..1fc1696 100644 --- a/src/main/java/com/primefactorsolutions/views/HoursWorkedView.java +++ b/src/main/java/com/primefactorsolutions/views/HoursWorkedView.java @@ -21,6 +21,7 @@ import org.vaadin.firitin.form.BeanValidationForm; import java.time.LocalDate; import java.time.YearMonth; +import java.time.temporal.IsoFields; import java.util.ArrayList; import java.util.List; import java.util.UUID; @@ -175,7 +176,16 @@ public class HoursWorkedView extends BeanValidationForm implements dateField.setMax(maxSelectableDate); dateField.setValue(today); - dateField.setLabel("Fecha"); + dateField.addValueChangeListener(event -> { + LocalDate selectedDate = event.getValue(); + if (selectedDate != null) { + int weekNumber = selectedDate.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR); + Notification.show("Número de la semana: " + weekNumber, 3000, Notification.Position.BOTTOM_CENTER); + if (hoursWorked != null) { + hoursWorked.setWeekNumber(weekNumber); + } + } + }); } private void saveHoursWorked() { @@ -200,11 +210,12 @@ public class HoursWorkedView extends BeanValidationForm implements Notification.show("Por favor, ingrese un número válido para las horas."); } if ("Otros".equals(tareasEspecificasDropdown.getValue())) { - // Maneja horas y actividad específica adicional hoursWorked.setTareasEspecificas(tareaEspecificaInput.getValue()); try { double horasEspecifica = Double.parseDouble(horasTareaEspecificaField.getValue()); hoursWorked.setHorasTareasEspecificas(horasEspecifica); + double totalHoras = hoursWorked.getHours() + horasEspecifica; + hoursWorked.setTotalHours(totalHoras); } catch (NumberFormatException e) { Notification.show("Por favor, ingrese un número válido para las horas de la tarea específica."); } diff --git a/src/main/java/com/primefactorsolutions/views/ReporteView.java b/src/main/java/com/primefactorsolutions/views/ReporteView.java index 3e10457..e1a962b 100644 --- a/src/main/java/com/primefactorsolutions/views/ReporteView.java +++ b/src/main/java/com/primefactorsolutions/views/ReporteView.java @@ -142,7 +142,9 @@ public class ReporteView extends VerticalLayout { .getId()) && hw.getWeekNumber() == weekNumber) .collect(Collectors.toList()); + System.out.println(hoursWorkedList); if (hoursWorkedList.isEmpty()) { + Notification.show("No hay horas trabajadas disponibles para generar el reporte.", 3000, Notification.Position.MIDDLE); return; diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index cb9a79f..ac5f389 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -92,4 +92,4 @@ insert into time_off_request (id, version, employee_id, category, state, availab values ('89bc4b2a-943f-487c-a9f3-bacf78145e67', 1, 'cba3efb7-32bc-44be-9fdc-fc5e4f211254', 'LUNES_CARNAVAL', 'APROBADO', 1, '2024-02-12', '2024-02-12', '2024-02-12', 1, 0); INSERT INTO HOURS_WORKED (ID, VERSION, ACTIVIDAD, DATE, HORAS_TAREAS_ESPECIFICAS, HORASPENDIENTES, HOURS, TAREAS_ESPECIFICAS, TOTAL_HOURS, WEEK_NUMBER, EMPLOYEE_ID, TEAM_ID) -VALUES ('6d6b3a71-9b11-4526-8335-b089627a8cd6', 0, 'Scrum Meeting', '2024-11-15', 0.0, 0.0, 2.0, NULL, 0.0, 0, '5c6f11fe-c341-4be7-a9a6-bba0081ad7c6', 'b0e8f394-78c1-4d8a-9c57-dc6e8b36a5fa'); +VALUES ('6d6b3a71-9b11-4526-8335-b089627a8cd6', 0, 'Scrum Meeting', '2024-11-15', 0.0, 0.0, 2.0, NULL, 0.0, 46, '5c6f11fe-c341-4be7-a9a6-bba0081ad7c6', 'b0e8f394-78c1-4d8a-9c57-dc6e8b36a5fa');