hoursworked #75

Closed
alex wants to merge 25 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.GenerationType;
import jakarta.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.util.UUID;
@Data
@Entity
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class Actividad extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.UUID)
@ -129,7 +134,6 @@ public class Actividad extends BaseEntity {
return horas;
}
// Builder para crear instancias de Actividad
public static class Builder {
private String nombre;
private double lunes;
@ -139,7 +143,7 @@ public class Actividad extends BaseEntity {
private double viernes;
private double sabado;
private double domingo;
private String tarea; // Cambié 'tarea' por 'descripcion'
private String tarea;
private 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.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.CascadeType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.time.LocalDate;
import java.time.temporal.WeekFields;
import java.util.Locale;
import java.util.UUID;
@Data
@Entity
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class HoursWorked extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.UUID)
@ -18,16 +27,20 @@ public class HoursWorked extends BaseEntity {
@ManyToOne
private Employee employee;
@ManyToOne
@ManyToOne(cascade = CascadeType.PERSIST) // Añadimos cascade para que persista la actividad automáticamente
private Actividad actividad;
private int weekNumber;
private double totalHours;
private LocalDate fecha;
public HoursWorked() {}
public HoursWorked(Employee employee) {
this.employee = employee;
this.weekNumber = 0;
this.totalHours = 0;
this.fecha = LocalDate.now();
}
public Actividad getActividad() {
return this.actividad;
@ -49,8 +62,8 @@ public class HoursWorked extends BaseEntity {
return employee;
}
public void setEmployee(final Employee value) {
this.employee = value;
public void setEmployee(final Employee employee) {
this.employee = employee;
}
public int getWeekNumber() {
@ -61,8 +74,6 @@ public class HoursWorked extends BaseEntity {
this.weekNumber = weekNumber;
}
public LocalDate getFecha() { return this.fecha;}
public double getTotalHours() {
return totalHours;
}
@ -70,4 +81,34 @@ public class HoursWorked extends BaseEntity {
public void setTotalHours(final double 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> findByPersonalEmail(String personalEmail);
Optional<Employee> findByTeamId(UUID teamId);
Optional<Employee> findByTeamIdAndLeadManagerTrue(UUID teamId);
List<Employee> findByTeamName(String teamName);
}

View File

@ -50,7 +50,7 @@ public class EmployeeService {
public String getTeamLeadName(final UUID teamId) {
// 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())
.orElse("No asignado");

View File

@ -176,6 +176,7 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
editButton.setVisible(true);
reportButton.setVisible(true);
birthday.addValueChangeListener(event -> calculateAge());
birthday.setMax(java.time.LocalDate.now());
reportButton.addClickListener((ComponentEventListener<ClickEvent<Button>>) buttonClickEvent -> {
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);
}
} else {
// Si no existe, se agrega como nueva actividad
switch (dia) {
case MONDAY -> nuevaActividad.setLunes(horas);
case TUESDAY -> nuevaActividad.setMartes(horas);
@ -318,9 +317,8 @@ public class HoursWorkedView extends VerticalLayout {
}
actividades.add(nuevaActividad);
}
// Actualiza el Grid
grid.setItems(actividades);
guardarActividades();
}
@ -437,22 +435,29 @@ public class HoursWorkedView extends VerticalLayout {
return;
}
if (actividades.isEmpty()) {
Notification.show("No hay actividades para guardar.");
return;
}
double totalHorasSemana = actividades.stream()
.mapToDouble(this::calcularTotalPorDia)
.sum();
List<HoursWorked> savedHoursWorked = new ArrayList<>(); // Para almacenar los objetos guardados
actividades.forEach(actividad -> {
HoursWorked hoursWorked = new HoursWorked();
hoursWorked.setActividad(actividad);
hoursWorked.setFecha(fechaPicker.getValue());
hoursWorked.setEmployee(selectedEmployee);
hoursWorked.setWeekNumber(weekNumber);
hoursWorked.setTotalHours(totalHorasSemana);
try {
hoursWorkedService.saveHoursWorked(hoursWorked); // Usa saveHoursWorked directamente
hoursWorkedService.saveHoursWorked(hoursWorked);
});
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) {
@ -468,4 +473,5 @@ public class HoursWorkedView extends VerticalLayout {
private void closeView() {
getUI().ifPresent(ui -> ui.navigate(HoursWorkedView.class));
}
}