"primer commit"

This commit is contained in:
Melina Gutierrez 2024-11-13 15:41:30 -04:00
parent 3c626e3c85
commit 5f569333ba
6 changed files with 196 additions and 27 deletions

View File

@ -0,0 +1,81 @@
package com.primefactorsolutions.model;
import jakarta.persistence.*;
import java.time.LocalDate;
import java.util.UUID;
@Entity
@Table(name = "Actividades_hours")
public class ActividadesHours {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private UUID id;
@ManyToOne
@JoinColumn(name = "employee_id", nullable = false)
private Employee employee;
@ManyToOne
@JoinColumn(name = "actividad_id", nullable = false)
private Actividad actividad;
@Column(nullable = false)
private double totalHours;
@Column(nullable = false)
private int weekNumber;
@Column(nullable = false)
private LocalDate fecha;
// Getters y Setters
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public Actividad getActividad() {
return actividad;
}
public void setActividad(Actividad actividad) {
this.actividad = actividad;
}
public double getTotalHours() {
return totalHours;
}
public void setTotalHours(double totalHours) {
this.totalHours = totalHours;
}
public int getWeekNumber() {
return weekNumber;
}
public void setWeekNumber(int weekNumber) {
this.weekNumber = weekNumber;
}
public LocalDate getFecha() {
return fecha;
}
public void setFecha(LocalDate fecha) {
this.fecha = fecha;
}
}

View File

@ -5,8 +5,11 @@ import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType; import jakarta.persistence.GenerationType;
import jakarta.persistence.Id; import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne; import jakarta.persistence.ManyToOne;
import jakarta.persistence.CascadeType;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.temporal.WeekFields;
import java.util.Locale;
import java.util.UUID; import java.util.UUID;
@Entity @Entity
@ -18,25 +21,16 @@ public class HoursWorked extends BaseEntity {
@ManyToOne @ManyToOne
private Employee employee; private Employee employee;
@ManyToOne @ManyToOne(cascade = CascadeType.PERSIST) // Añadimos cascade para que persista la actividad automáticamente
private Actividad actividad; private Actividad actividad;
private int weekNumber; private int weekNumber;
private double totalHours; private double totalHours;
private LocalDate fecha; private LocalDate fecha;
public HoursWorked() {} public HoursWorked() {}
public Actividad getActividad() { // Getters y Setters
return this.actividad;
}
public void setActividad(Actividad actividad){
this.actividad = actividad;
}
public UUID getId() { public UUID getId() {
return id; return id;
} }
@ -49,8 +43,16 @@ public class HoursWorked extends BaseEntity {
return employee; return employee;
} }
public void setEmployee(final Employee value) { public void setEmployee(final Employee employee) {
this.employee = value; this.employee = employee;
}
public Actividad getActividad() {
return actividad;
}
public void setActividad(final Actividad actividad) {
this.actividad = actividad;
} }
public int getWeekNumber() { public int getWeekNumber() {
@ -61,8 +63,6 @@ public class HoursWorked extends BaseEntity {
this.weekNumber = weekNumber; this.weekNumber = weekNumber;
} }
public LocalDate getFecha() { return this.fecha;}
public double getTotalHours() { public double getTotalHours() {
return totalHours; return totalHours;
} }
@ -70,4 +70,34 @@ public class HoursWorked extends BaseEntity {
public void setTotalHours(final double totalHours) { public void setTotalHours(final double totalHours) {
this.totalHours = totalHours; this.totalHours = totalHours;
} }
public LocalDate getFecha() {
return fecha;
}
public void setFecha(final LocalDate fecha) {
this.fecha = fecha;
// Actualiza el número de semana automáticamente al establecer la fecha
if (fecha != null) {
this.weekNumber = calculateWeekNumber(fecha);
}
}
// Método adicional para calcular el número de semana basado en la fecha
private int calculateWeekNumber(LocalDate date) {
WeekFields weekFields = WeekFields.of(Locale.getDefault());
return date.get(weekFields.weekOfWeekBasedYear());
}
@Override
public String toString() {
return "HoursWorked{" +
"id=" + id +
", employee=" + (employee != null ? employee.getFirstName() : "N/A") +
", actividad=" + (actividad != null ? actividad.getNombre() : "N/A") +
", weekNumber=" + weekNumber +
", totalHours=" + totalHours +
", fecha=" + fecha +
'}';
}
} }

View File

@ -0,0 +1,6 @@
package com.primefactorsolutions.repositories;
import com.primefactorsolutions.model.ActividadesHours;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.UUID;
public interface ActividadesHoursRepository extends JpaRepository<ActividadesHours,UUID> {
}

View File

@ -11,7 +11,7 @@ public interface EmployeeRepository extends JpaRepository<Employee, UUID> {
Optional<Employee> findByUsername(String username); Optional<Employee> findByUsername(String username);
Optional<Employee> findByPersonalEmail(String personalEmail); Optional<Employee> findByPersonalEmail(String personalEmail);
Optional<Employee> findByTeamId(UUID teamId); Optional<Employee> findByTeamIdAndLeadManagerTrue(UUID teamId);
List<Employee> findByTeamName(String teamName); List<Employee> findByTeamName(String teamName);
} }

View File

@ -50,7 +50,7 @@ public class EmployeeService {
public String getTeamLeadName(final UUID teamId) { public String getTeamLeadName(final UUID teamId) {
// Encuentra al empleado con el rol de lead_manager en el equipo especificado // Encuentra al empleado con el rol de lead_manager en el equipo especificado
Optional<Employee> leadManager = employeeRepository.findByTeamId(teamId); Optional<Employee> leadManager = employeeRepository.findByTeamIdAndLeadManagerTrue(teamId);
return leadManager.map(employee -> employee.getFirstName() + " " + employee.getLastName()) return leadManager.map(employee -> employee.getFirstName() + " " + employee.getLastName())
.orElse("No asignado"); .orElse("No asignado");

View File

@ -2,6 +2,7 @@ package com.primefactorsolutions.views;
import com.primefactorsolutions.model.Actividad; import com.primefactorsolutions.model.Actividad;
import com.primefactorsolutions.model.Employee; import com.primefactorsolutions.model.Employee;
import com.primefactorsolutions.model.HoursWorked; import com.primefactorsolutions.model.HoursWorked;
import com.primefactorsolutions.repositories.ActividadesHoursRepository;
import com.primefactorsolutions.service.EmployeeService; import com.primefactorsolutions.service.EmployeeService;
import com.primefactorsolutions.service.HoursWorkedService; import com.primefactorsolutions.service.HoursWorkedService;
import com.vaadin.flow.component.grid.Grid; import com.vaadin.flow.component.grid.Grid;
@ -58,6 +59,11 @@ public class HoursWorkedView extends VerticalLayout {
@Autowired @Autowired
private final HoursWorkedService hoursWorkedService; private final HoursWorkedService hoursWorkedService;
@Autowired
private ActividadesHoursRepository actividadesHoursRepository;
private final H2 equipoLabel = new H2("Tareas del Cliente/Equipo"); private final H2 equipoLabel = new H2("Tareas del Cliente/Equipo");
private final H2 empresaLabel = new H2("Tareas de la Empresa"); private final H2 empresaLabel = new H2("Tareas de la Empresa");
private final Label totalCompletadoLabel = new Label(); private final Label totalCompletadoLabel = new Label();
@ -186,6 +192,7 @@ public class HoursWorkedView extends VerticalLayout {
add(fechaPicker, filtersLayout, actividadFormLayout, add(fechaPicker, filtersLayout, actividadFormLayout,
equipoLabel, grid, empresaLabel, tareasEspecificasLayout, equipoLabel, grid, empresaLabel, tareasEspecificasLayout,
gridActividadesEspecificas, buttonsLayout, totalesLayout); gridActividadesEspecificas, buttonsLayout, totalesLayout);
} }
private void configurarGrid() { private void configurarGrid() {
@ -437,24 +444,69 @@ public class HoursWorkedView extends VerticalLayout {
return; return;
} }
// Verificar que hay actividades para guardar
if (actividades.isEmpty()) {
Notification.show("No hay actividades para guardar.");
return;
}
// Sumar el total de horas para la semana
double totalHorasSemana = actividades.stream() double totalHorasSemana = actividades.stream()
.mapToDouble(this::calcularTotalPorDia) .mapToDouble(this::calcularTotalPorDia)
.sum(); .sum();
// Crear un objeto HoursWorked por cada actividad
List<HoursWorked> savedHoursWorked = new ArrayList<>(); // Para almacenar los objetos guardados
// Recorrer todas las actividades y guardar un registro para cada una
for (Actividad actividad : actividades) {
HoursWorked hoursWorked = new HoursWorked(); HoursWorked hoursWorked = new HoursWorked();
hoursWorked.setEmployee(selectedEmployee); hoursWorked.setEmployee(selectedEmployee);
hoursWorked.setWeekNumber(weekNumber); hoursWorked.setWeekNumber(weekNumber);
hoursWorked.setTotalHours(totalHorasSemana); hoursWorked.setTotalHours(totalHorasSemana);
// Asignar cada actividad individualmente
hoursWorked.setActividad(actividad);
hoursWorked.setFecha(LocalDate.now()); // Establecer la fecha actual o la seleccionada si es otra
// Mostrar información de la actividad para depuración
System.out.println("Guardando actividad: " + actividad.getNombre() + " con " + totalHorasSemana + " horas.");
try { try {
hoursWorkedService.saveHoursWorked(hoursWorked); // Usa saveHoursWorked directamente // Guardar en el servicio
Notification.show("Actividades guardadas correctamente."); hoursWorkedService.saveHoursWorked(hoursWorked);
System.out.println(hoursWorked); savedHoursWorked.add(hoursWorked); // Agregar a la lista de registros guardados
Notification.show("Actividad guardada correctamente: " + actividad.getNombre());
} catch (Exception e) { } catch (Exception e) {
Notification.show("Error al guardar actividades: " + e.getMessage()); Notification.show("Error al guardar la actividad: " + actividad.getNombre() + " - " + e.getMessage());
} }
} }
// Mostrar los registros guardados
savedHoursWorked.forEach(hw -> System.out.println("Horas trabajadas guardadas: " + hw));
// Crear el Grid
Grid<HoursWorked> grid = new Grid<>();
// Configurar columnas para mostrar la información deseada
grid.addColumn(hw -> hw.getEmployee().getFirstName()).setHeader("Empleado");
grid.addColumn(hw -> hw.getEmployee().getId()).setHeader("ID Empleado");
grid.addColumn(hw -> hw.getActividad().getNombre()).setHeader("Actividad");
grid.addColumn(hw -> hw.getTotalHours()).setHeader("Horas Trabajadas");
grid.addColumn(hw -> hw.getWeekNumber()).setHeader("Número de Semana");
grid.addColumn(hw -> hw.getFecha().toString()).setHeader("Fecha");
// Llenar el Grid con los datos de savedHoursWorked
grid.setItems(savedHoursWorked);
// Agregar el Grid a la vista
add(grid);
}
private double calcularTotalHoras(final List<HoursWorked> listaDeHorasTrabajadas) { private double calcularTotalHoras(final List<HoursWorked> listaDeHorasTrabajadas) {
return listaDeHorasTrabajadas.stream() return listaDeHorasTrabajadas.stream()
.mapToDouble(HoursWorked::getTotalHours) .mapToDouble(HoursWorked::getTotalHours)