hoursworked #74

Closed
melina.gutierrez wants to merge 24 commits from hoursworked into main
6 changed files with 78 additions and 26 deletions
Showing only changes of commit 3f17eb780b - Show all commits

View File

@ -4,11 +4,16 @@ import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue; import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType; import jakarta.persistence.GenerationType;
import jakarta.persistence.Id; import jakarta.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.util.UUID; import java.util.UUID;
@Data
@Entity @Entity
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class Actividad extends BaseEntity { public class Actividad extends BaseEntity {
@Id @Id
@GeneratedValue(strategy = GenerationType.UUID) @GeneratedValue(strategy = GenerationType.UUID)
@ -129,7 +134,6 @@ public class Actividad extends BaseEntity {
return horas; return horas;
} }
// Builder para crear instancias de Actividad
public static class Builder { public static class Builder {
private String nombre; private String nombre;
private double lunes; private double lunes;
@ -139,7 +143,7 @@ public class Actividad extends BaseEntity {
private double viernes; private double viernes;
private double sabado; private double sabado;
private double domingo; private double domingo;
private String tarea; // Cambié 'tarea' por 'descripcion' private String tarea;
private double horas; private double horas;
public Builder tarea(final String tarea, final double horas) { public Builder tarea(final String tarea, final double horas) {

View File

@ -5,11 +5,20 @@ 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 lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
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;
@Data
@Entity @Entity
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class HoursWorked extends BaseEntity { public class HoursWorked extends BaseEntity {
@Id @Id
@GeneratedValue(strategy = GenerationType.UUID) @GeneratedValue(strategy = GenerationType.UUID)
@ -18,16 +27,20 @@ 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 HoursWorked(Employee employee) {
this.employee = employee;
this.weekNumber = 0;
this.totalHours = 0;
this.fecha = LocalDate.now();
}
public Actividad getActividad() { public Actividad getActividad() {
return this.actividad; return this.actividad;
@ -49,8 +62,8 @@ 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 int getWeekNumber() { public int getWeekNumber() {
@ -61,8 +74,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 +81,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

@ -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

@ -176,6 +176,7 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
editButton.setVisible(true); editButton.setVisible(true);
reportButton.setVisible(true); reportButton.setVisible(true);
birthday.addValueChangeListener(event -> calculateAge()); birthday.addValueChangeListener(event -> calculateAge());
birthday.setMax(java.time.LocalDate.now());
reportButton.addClickListener((ComponentEventListener<ClickEvent<Button>>) buttonClickEvent -> { reportButton.addClickListener((ComponentEventListener<ClickEvent<Button>>) buttonClickEvent -> {
var employee = getEntity(); var employee = getEntity();

View File

@ -305,7 +305,6 @@ public class HoursWorkedView extends VerticalLayout {
default -> throw new IllegalArgumentException("Día seleccionado no válido: " + dia); default -> throw new IllegalArgumentException("Día seleccionado no válido: " + dia);
} }
} else { } else {
// Si no existe, se agrega como nueva actividad
switch (dia) { switch (dia) {
case MONDAY -> nuevaActividad.setLunes(horas); case MONDAY -> nuevaActividad.setLunes(horas);
case TUESDAY -> nuevaActividad.setMartes(horas); case TUESDAY -> nuevaActividad.setMartes(horas);
@ -318,9 +317,8 @@ public class HoursWorkedView extends VerticalLayout {
} }
actividades.add(nuevaActividad); actividades.add(nuevaActividad);
} }
// Actualiza el Grid
grid.setItems(actividades); grid.setItems(actividades);
guardarActividades();
} }
@ -437,22 +435,29 @@ public class HoursWorkedView extends VerticalLayout {
return; return;
} }
if (actividades.isEmpty()) {
Notification.show("No hay actividades para guardar.");
return;
}
double totalHorasSemana = actividades.stream() double totalHorasSemana = actividades.stream()
.mapToDouble(this::calcularTotalPorDia) .mapToDouble(this::calcularTotalPorDia)
.sum(); .sum();
List<HoursWorked> savedHoursWorked = new ArrayList<>(); // Para almacenar los objetos guardados
actividades.forEach(actividad -> {
HoursWorked hoursWorked = new HoursWorked(); HoursWorked hoursWorked = new HoursWorked();
hoursWorked.setActividad(actividad);
hoursWorked.setFecha(fechaPicker.getValue());
hoursWorked.setEmployee(selectedEmployee); hoursWorked.setEmployee(selectedEmployee);
hoursWorked.setWeekNumber(weekNumber); hoursWorked.setWeekNumber(weekNumber);
hoursWorked.setTotalHours(totalHorasSemana); hoursWorked.setTotalHours(totalHorasSemana);
try { hoursWorkedService.saveHoursWorked(hoursWorked);
hoursWorkedService.saveHoursWorked(hoursWorked); // Usa saveHoursWorked directamente
});
Notification.show("Actividades guardadas correctamente."); Notification.show("Actividades guardadas correctamente.");
System.out.println(hoursWorked);
} catch (Exception e) {
Notification.show("Error al guardar actividades: " + e.getMessage());
}
} }
private double calcularTotalHoras(final List<HoursWorked> listaDeHorasTrabajadas) { private double calcularTotalHoras(final List<HoursWorked> listaDeHorasTrabajadas) {
@ -468,4 +473,5 @@ public class HoursWorkedView extends VerticalLayout {
private void closeView() { private void closeView() {
getUI().ifPresent(ui -> ui.navigate(HoursWorkedView.class)); getUI().ifPresent(ui -> ui.navigate(HoursWorkedView.class));
} }
} }