#46 Perfil de Empleado - Registro Semanal mas reporte, falta mejorar el reporte semanal
All checks were successful
PR Builder / Build-PR (pull_request) Successful in 2m31s

This commit is contained in:
Melina Gutierrez 2024-10-29 14:55:57 -04:00
parent 4ad745145f
commit c5484de9a0
11 changed files with 289 additions and 177 deletions

View File

@ -1,16 +1,14 @@
name: Builder name: PR Builder
run-name: ${{ gitea.actor }} building run-name: ${{ gitea.actor }} building PR
on: on: [pull_request]
push:
branches:
- main
jobs: jobs:
Build-Project: Build-PR:
runs-on: ubuntu-22.04 runs-on: ubuntu-22.04
steps: steps:
- run: echo "The job was automatically triggered by a ${{ gitea.event_name }} event on branch is ${{ gitea.ref }} and your repository is ${{ gitea.repository }}." - run: echo "The job was automatically triggered by a ${{ gitea.event_name }} event on branch ${{ gitea.head_ref }} and ref is ${{ gitea.ref }} and your repository is ${{ gitea.repository }}."
- name: Build package - name: Build PR
if: gitea.base_ref == 'main'
run: | run: |
git clone --single-branch --branch main https://git.primefactorsolutions.com/PFS/pfs-intra.git && cd pfs-intra && ./mvnw clean package -Pproduction && unlink /home/ubuntu/pfs-intra/app.jar && cp target/*.jar /home/ubuntu/pfs-intra/app.jar && sudo systemctl restart pfs-intra git clone --single-branch --branch "${{ gitea.head_ref }}" https://git.primefactorsolutions.com/PFS/pfs-intra.git && cd pfs-intra && ./mvnw clean package -Pproduction
- run: echo "This job's status is ${{ job.status }}." - run: echo "This job's status is ${{ job.status }}."

41
pom.xml
View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<!-- Project from https://start.vaadin.com/project/6f77deb3-8326-4444-8c05-0398f79d7fee --> <!-- Project from https://start.vaadin.com/project/6f77deb3-8326-4444-8c05-0398f79d7fee -->
<groupId>com.primefactorsolutions</groupId> <groupId>com.primefactorsolutions</groupId>
@ -72,12 +72,6 @@
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.29</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId> <artifactId>spring-boot-starter-data-jpa</artifactId>
@ -187,6 +181,21 @@
<artifactId>viritin</artifactId> <artifactId>viritin</artifactId>
<version>2.8.22</version> <version>2.8.22</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
<dependency> <dependency>
<groupId>com.flowingcode.addons</groupId> <groupId>com.flowingcode.addons</groupId>
<artifactId>simple-timer</artifactId> <artifactId>simple-timer</artifactId>
@ -386,22 +395,8 @@
</plugin> </plugin>
<!-- Runs the integration tests (*IT) after the server is started --> <!-- Runs the integration tests (*IT) after the server is started -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<trimStackTrace>false</trimStackTrace>
<enableAssertions>true</enableAssertions>
</configuration>
</plugin>
</plugins> </plugins>
</build> </build>
</profile> </profile>

View File

@ -1,6 +1,6 @@
package com.primefactorsolutions.model; package com.primefactorsolutions.model;
final public class Actividad { public final class Actividad {
private String nombre; private String nombre;
private double lunes; private double lunes;
private double martes; private double martes;

View File

@ -1,26 +1,32 @@
package com.primefactorsolutions.model; package com.primefactorsolutions.model;
import com.vaadin.flow.component.template.Id;
import jakarta.persistence.Entity; 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.ManyToOne;
import java.util.UUID; import java.util.UUID;
@Entity @Entity
public class HoursWorked extends BaseEntity { public class HoursWorked extends BaseEntity {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.UUID)
private Long id; private UUID id;
@ManyToOne
private Employee employee; private Employee employee;
private int weekNumber; private int weekNumber;
private double totalHours; private double totalHours;
public HoursWorked() {} public HoursWorked() { }
public void setId(Long id) { public UUID getId() {
return id;
}
public void setId(final UUID id) {
this.id = id; this.id = id;
} }
@ -28,15 +34,15 @@ public class HoursWorked extends BaseEntity {
return employee; return employee;
} }
public void setEmployee(Employee value) { public void setEmployee(final Employee value) {
this.employee = value; // Asignar el objeto Employee al campo this.employee = value;
} }
public int getWeekNumber() { public int getWeekNumber() {
return weekNumber; return weekNumber;
} }
public void setWeekNumber(int weekNumber) { public void setWeekNumber(final int weekNumber) {
this.weekNumber = weekNumber; this.weekNumber = weekNumber;
} }
@ -44,7 +50,7 @@ public class HoursWorked extends BaseEntity {
return totalHours; return totalHours;
} }
public void setTotalHours(double totalHours) { public void setTotalHours(final double totalHours) {
this.totalHours = totalHours; this.totalHours = totalHours;
} }
} }

View File

@ -2,10 +2,12 @@ package com.primefactorsolutions.repositories;
import com.primefactorsolutions.model.Employee; import com.primefactorsolutions.model.Employee;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, UUID> { public interface EmployeeRepository extends JpaRepository<Employee, UUID> {
Optional<Employee> findByUsername(String username); Optional<Employee> findByUsername(String username);
} }

View File

@ -12,7 +12,7 @@ public class HoursWorkedService {
private final HoursWorkedRepository hoursWorkedRepository; private final HoursWorkedRepository hoursWorkedRepository;
@Autowired @Autowired
public HoursWorkedService(HoursWorkedRepository hoursWorkedRepository) { public HoursWorkedService(final HoursWorkedRepository hoursWorkedRepository) {
this.hoursWorkedRepository = hoursWorkedRepository; this.hoursWorkedRepository = hoursWorkedRepository;
} }
@ -20,15 +20,15 @@ public class HoursWorkedService {
return hoursWorkedRepository.findAll(); return hoursWorkedRepository.findAll();
} }
public HoursWorked saveHoursWorked(HoursWorked hoursWorked) { public HoursWorked saveHoursWorked(final HoursWorked hoursWorked) {
return hoursWorkedRepository.save(hoursWorked); return hoursWorkedRepository.save(hoursWorked);
} }
public HoursWorked save(HoursWorked hoursWorked) { public HoursWorked save(final HoursWorked hoursWorked) {
return hoursWorkedRepository.save(hoursWorked); return hoursWorkedRepository.save(hoursWorked);
} }
public void deleteHoursWorked(Long id) { public void deleteHoursWorked(final Long id) {
hoursWorkedRepository.deleteById(id); hoursWorkedRepository.deleteById(id);
} }
} }

View File

@ -1,72 +1,76 @@
package com.primefactorsolutions.service; package com.primefactorsolutions.service;
import com.openhtmltopdf.pdfboxout.PdfBoxRenderer; import com.primefactorsolutions.repositories.HoursWorkedRepository;
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import freemarker.template.Configuration; import org.apache.poi.ss.usermodel.*;
import freemarker.template.DefaultObjectWrapper; import org.springframework.beans.factory.annotation.Autowired;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
import lombok.SneakyThrows;
import org.apache.pdfbox.io.MemoryUsageSetting;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.*; import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets; import java.io.IOException;
import java.util.TimeZone; import java.util.List;
import java.util.Map;
@Service @Service
public class ReportService { public class ReportService {
@SneakyThrows private final HoursWorkedRepository hoursWorkedRepository;
public byte[] writeAsPdf(final String reportName, final Object model) {
try (var os = new ByteArrayOutputStream()) {
writeAsPdf(reportName, model, os);
@Autowired
public ReportService(final HoursWorkedRepository hoursWorkedRepository) {
this.hoursWorkedRepository = hoursWorkedRepository;
}
// Este método ahora solo crea el archivo Excel a partir de los datos que recibe.
public byte[] writeAsExcel(final String reportName, final List<String> headers,
final List<Map<String, Object>> data)
throws IOException {
return createExcelFile(reportName, headers, data);
}
private byte[] createExcelFile(final String reportName, final List<String> headers,
final List<Map<String, Object>> data)
throws IOException {
try (Workbook workbook = new XSSFWorkbook();
ByteArrayOutputStream os = new ByteArrayOutputStream()) {
Sheet sheet = workbook.createSheet(reportName);
// Crear encabezados
Row headerRow = sheet.createRow(0);
CellStyle headerStyle = workbook.createCellStyle();
Font headerFont = workbook.createFont();
headerFont.setBold(true);
headerStyle.setFont(headerFont);
for (int i = 0; i < headers.size(); i++) {
Cell cell = headerRow.createCell(i);
cell.setCellValue(headers.get(i));
cell.setCellStyle(headerStyle);
}
// Crear filas de datos
for (int i = 0; i < data.size(); i++) {
Row dataRow = sheet.createRow(i + 1);
Map<String, Object> rowData = data.get(i);
int cellIndex = 0;
for (String key : headers) {
Cell cell = dataRow.createCell(cellIndex++);
Object value = rowData.get(key);
if (value instanceof String) {
cell.setCellValue((String) value);
} else if (value instanceof Number) {
cell.setCellValue(((Number) value).doubleValue());
} else if (value == null) {
cell.setCellValue(""); // Manejo de valores nulos
}
}
}
workbook.write(os);
return os.toByteArray(); return os.toByteArray();
} catch (IOException e) {
System.err.println("Error al generar el archivo Excel: " + e.getMessage());
throw e; // Propagar la excepción después de registrarla
} }
} }
@SneakyThrows
public void writeAsPdf(final String reportName, final Object model, final OutputStream out) {
var in = getTemplate(reportName);
final Configuration cfg = getConfiguration();
final Reader reader = new InputStreamReader(in);
final Template temp = new Template(reportName, reader, cfg);
var wrapper = new DefaultObjectWrapper(Configuration.VERSION_2_3_32);
ByteArrayOutputStream oo = new ByteArrayOutputStream();
Writer outTemplate = new OutputStreamWriter(oo);
temp.process(wrapper.wrap(model), outTemplate);
var builder = new PdfRendererBuilder();
builder.usePDDocument(new PDDocument(MemoryUsageSetting.setupMixed(1000000)));
builder.withHtmlContent(oo.toString(StandardCharsets.UTF_8), "/test");
builder.toStream(out);
try (PdfBoxRenderer pdfBoxRenderer = builder.buildPdfRenderer()) {
pdfBoxRenderer.layout();
pdfBoxRenderer.createPDF();
}
}
public static InputStream getTemplate(final String reportName) {
return ReportService.class.getResourceAsStream(String.format("/reports/%s.html", reportName));
}
@NotNull
private static Configuration getConfiguration() {
final Configuration cfg = new Configuration(Configuration.VERSION_2_3_32);
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
cfg.setLogTemplateExceptions(false);
cfg.setWrapUncheckedExceptions(true);
cfg.setFallbackOnNullLoopVariable(false);
cfg.setSQLDateAndTimeTimeZone(TimeZone.getDefault());
return cfg;
}
} }

View File

@ -4,9 +4,7 @@ import com.primefactorsolutions.model.Employee;
import com.primefactorsolutions.service.EmployeeService; import com.primefactorsolutions.service.EmployeeService;
import com.primefactorsolutions.service.ReportService; import com.primefactorsolutions.service.ReportService;
import com.vaadin.componentfactory.pdfviewer.PdfViewer; import com.vaadin.componentfactory.pdfviewer.PdfViewer;
import com.vaadin.flow.component.ClickEvent;
import com.vaadin.flow.component.Component; import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.ComponentEventListener;
import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant; import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.combobox.ComboBox; import com.vaadin.flow.component.combobox.ComboBox;
@ -24,14 +22,12 @@ import com.vaadin.flow.component.upload.Upload;
import com.vaadin.flow.component.upload.receivers.MemoryBuffer; import com.vaadin.flow.component.upload.receivers.MemoryBuffer;
import com.vaadin.flow.data.value.ValueChangeMode; import com.vaadin.flow.data.value.ValueChangeMode;
import com.vaadin.flow.router.*; import com.vaadin.flow.router.*;
import com.vaadin.flow.server.StreamResource;
import com.vaadin.flow.spring.annotation.SpringComponent; import com.vaadin.flow.spring.annotation.SpringComponent;
import jakarta.annotation.security.PermitAll; import jakarta.annotation.security.PermitAll;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import org.vaadin.firitin.components.datepicker.VDatePicker; import org.vaadin.firitin.components.datepicker.VDatePicker;
import org.vaadin.firitin.form.BeanValidationForm; import org.vaadin.firitin.form.BeanValidationForm;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Base64; import java.util.Base64;
@ -156,13 +152,13 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
reportButton.setVisible(true); reportButton.setVisible(true);
birthday.addValueChangeListener(event -> calculateAge()); birthday.addValueChangeListener(event -> calculateAge());
reportButton.addClickListener((ComponentEventListener<ClickEvent<Button>>) buttonClickEvent -> { // reportButton.addClickListener((ComponentEventListener<ClickEvent<Button>>) buttonClickEvent -> {
var employee = getEntity(); // var employee = getEntity();
byte[] pdfContent = reportService.writeAsPdf("ficha", employee); // byte[] pdfContent = reportService.writeAsPdf("ficha", employee);
var resource = new StreamResource("ficha.pdf", () -> new ByteArrayInputStream(pdfContent)); // var resource = new StreamResource("ficha.pdf", () -> new ByteArrayInputStream(pdfContent));
pdfViewer.setSrc(resource); // pdfViewer.setSrc(resource);
dialog.open(); // dialog.open();
}); // });
initDialog(); initDialog();
} }

View File

@ -19,8 +19,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@SpringComponent @SpringComponent
@ -45,7 +43,7 @@ public class HoursWorkedMonthView extends VerticalLayout {
private LocalDate selectedMonth; private LocalDate selectedMonth;
@Autowired @Autowired
public HoursWorkedMonthView(EmployeeService employeeService) { public HoursWorkedMonthView(final EmployeeService employeeService) {
this.employeeService = employeeService; this.employeeService = employeeService;
configurarVista(); configurarVista();
} }
@ -55,7 +53,7 @@ public class HoursWorkedMonthView extends VerticalLayout {
monthPicker.setValue(LocalDate.now()); monthPicker.setValue(LocalDate.now());
monthPicker.addValueChangeListener(event -> { monthPicker.addValueChangeListener(event -> {
selectedMonth = event.getValue().withDayOfMonth(1); selectedMonth = event.getValue().withDayOfMonth(1);
cargarDatosMes(selectedMonth); //cargarDatosMes(selectedMonth);
}); });
equipoDropdown.setItems("Equipo 1", "Equipo 2", "Equipo 3"); equipoDropdown.setItems("Equipo 1", "Equipo 2", "Equipo 3");
@ -96,56 +94,56 @@ public class HoursWorkedMonthView extends VerticalLayout {
grid.addColumn(this::calcularTotalPorDia).setHeader("Total Semanal").setKey("totalSemanal"); grid.addColumn(this::calcularTotalPorDia).setHeader("Total Semanal").setKey("totalSemanal");
} }
private void cargarDatosMes(LocalDate month) { // private void cargarDatosMes(final LocalDate month) {
List<Actividad> actividadesDelMes = obtenerActividadesDelMes(month); // List<Actividad> actividadesDelMes = obtenerActividadesDelMes(month);
grid.setItems(actividadesDelMes); // grid.setItems(actividadesDelMes);
//
// double totalCompletado = calcularTotalCompletado(actividadesDelMes);
// double horasPendientes = calcularHorasPendientes(totalCompletado);
// double totalAcumuladas = 166;
// double horasAdeudadas = 2;
//
// totalCompletadoLabel.setText("Prom. Hrs/Semana Completadas: " + totalCompletado);
// horasPendientesLabel.setText("Prom. Hrs/Semana Pendientes: " + horasPendientes);
// totalAcumuladasLabel.setText("Total Hrs./Mes Acumuladas: " + totalAcumuladas);
// horasAdeudadasLabel.setText("Total Hrs./Mes Adeudadas: " + horasAdeudadas);
// }
double totalCompletado = calcularTotalCompletado(actividadesDelMes); // private List<Actividad> obtenerActividadesDelMes(final LocalDate month) {
double horasPendientes = calcularHorasPendientes(totalCompletado); // LocalDate startOfMonth = month.with(TemporalAdjusters.firstDayOfMonth());
double totalAcumuladas = 166; // LocalDate endOfMonth = month.with(TemporalAdjusters.lastDayOfMonth());
double horasAdeudadas = 2; //
// List<Actividad> actividadesDelMes = new ArrayList<>();
//
// for (LocalDate date = startOfMonth; date.isBefore(endOfMonth.plusDays(1)); date = date.plusDays(1)) {
// Actividad actividad = new Actividad.Builder()
// .lunes(0)
// .martes(0)
// .miercoles(0)
// .jueves(0)
// .viernes(0)
// .sabado(0)
// .domingo(0)
// .build();
// actividadesDelMes.add(actividad);
// }
//
// return actividadesDelMes;
// }
totalCompletadoLabel.setText("Prom. Hrs/Semana Completadas: " + totalCompletado); private double calcularTotalCompletado(final List<Actividad> actividades) {
horasPendientesLabel.setText("Prom. Hrs/Semana Pendientes: " + horasPendientes);
totalAcumuladasLabel.setText("Total Hrs./Mes Acumuladas: " + totalAcumuladas);
horasAdeudadasLabel.setText("Total Hrs./Mes Adeudadas: " + horasAdeudadas);
}
private List<Actividad> obtenerActividadesDelMes(final LocalDate month) {
LocalDate startOfMonth = month.with(TemporalAdjusters.firstDayOfMonth());
LocalDate endOfMonth = month.with(TemporalAdjusters.lastDayOfMonth());
List<Actividad> actividadesDelMes = new ArrayList<>();
for (LocalDate date = startOfMonth; date.isBefore(endOfMonth.plusDays(1)); date = date.plusDays(1)) {
Actividad actividad = new Actividad.Builder()
.lunes(0)
.martes(0)
.miercoles(0)
.jueves(0)
.viernes(0)
.sabado(0)
.domingo(0)
.build();
actividadesDelMes.add(actividad);
}
return actividadesDelMes;
}
private double calcularTotalCompletado(List<Actividad> actividades) {
return actividades.stream() return actividades.stream()
.mapToDouble(this::calcularTotalPorDia) .mapToDouble(this::calcularTotalPorDia)
.sum(); .sum();
} }
private double calcularTotalPorDia(final Actividad actividad) { private double calcularTotalPorDia(final Actividad actividad) {
return actividad.getLunes() + actividad.getMartes() + actividad.getMiercoles() + return actividad.getLunes() + actividad.getMartes() + actividad.getMiercoles()
actividad.getJueves() + actividad.getViernes() + actividad.getSabado() + + actividad.getJueves() + actividad.getViernes() + actividad.getSabado()
actividad.getDomingo(); + actividad.getDomingo();
} }
private double calcularHorasPendientes(double totalCompletado) { private double calcularHorasPendientes(final double totalCompletado) {
return 40 - totalCompletado; return 40 - totalCompletado;
} }

View File

@ -37,6 +37,7 @@ import java.util.Locale;
public class HoursWorkedView extends VerticalLayout { public class HoursWorkedView extends VerticalLayout {
private final List<Actividad> actividades = new ArrayList<>(); private final List<Actividad> actividades = new ArrayList<>();
private final Grid<Actividad> grid = new Grid<>(Actividad.class); private final Grid<Actividad> grid = new Grid<>(Actividad.class);
private final ComboBox<Employee> employeeComboBox = new ComboBox<>("Employee"); private final ComboBox<Employee> employeeComboBox = new ComboBox<>("Employee");
private LocalDate selectedStartOfWeek; private LocalDate selectedStartOfWeek;
private int weekNumber; private int weekNumber;
@ -78,13 +79,14 @@ public class HoursWorkedView extends VerticalLayout {
employeeComboBox.addValueChangeListener(event -> { employeeComboBox.addValueChangeListener(event -> {
Employee selectedEmployee = event.getValue(); Employee selectedEmployee = event.getValue();
if (selectedEmployee != null) { if (selectedEmployee != null) {
Notification.show("Empleado seleccionado: " + Notification.show("Empleado seleccionado: "
selectedEmployee.getFirstName() + " " + selectedEmployee.getLastName()); + selectedEmployee.getFirstName() + " "
+ selectedEmployee.getLastName());
} }
}); });
} }
private int getWeekOfYear(LocalDate date) { private int getWeekOfYear(final LocalDate date) {
return date.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR); return date.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR);
} }
@ -120,7 +122,8 @@ public class HoursWorkedView extends VerticalLayout {
Button guardarButton = new Button("Guardar", event -> guardarActividades()); Button guardarButton = new Button("Guardar", event -> guardarActividades());
Button cerrarButton = new Button("Cerrar", event -> this.closeView()); Button cerrarButton = new Button("Cerrar", event -> this.closeView());
HorizontalLayout buttonsLayout = new HorizontalLayout(actualizarButton, guardarButton, cerrarButton, verMesButton); HorizontalLayout buttonsLayout = new HorizontalLayout(actualizarButton, guardarButton,
cerrarButton, verMesButton);
VerticalLayout totalesLayout = new VerticalLayout(totalCompletadoLabel, horasPendientesLabel); VerticalLayout totalesLayout = new VerticalLayout(totalCompletadoLabel, horasPendientesLabel);
totalesLayout.setSpacing(true); totalesLayout.setSpacing(true);
@ -173,6 +176,15 @@ public class HoursWorkedView extends VerticalLayout {
grid.setItems(actividades); grid.setItems(actividades);
actualizarTotales(); actualizarTotales();
Notification.show("Actividad agregada correctamente"); Notification.show("Actividad agregada correctamente");
// Limpiar los campos de entrada
actividadNombre.clear();
lunesHoras.clear();
martesHoras.clear();
miercolesHoras.clear();
juevesHoras.clear();
viernesHoras.clear();
sabadoHoras.clear();
domingoHoras.clear();
} catch (NumberFormatException ex) { } catch (NumberFormatException ex) {
Notification.show("Error: Por favor ingresa números válidos para las horas."); Notification.show("Error: Por favor ingresa números válidos para las horas.");
} }
@ -183,14 +195,14 @@ public class HoursWorkedView extends VerticalLayout {
juevesHoras, viernesHoras, sabadoHoras, domingoHoras, agregarActividadButton); juevesHoras, viernesHoras, sabadoHoras, domingoHoras, agregarActividadButton);
} }
private TextField crearCampoHora(String placeholder) { private TextField crearCampoHora(final String placeholder) {
TextField field = new TextField(placeholder); TextField field = new TextField(placeholder);
field.setWidth("80px"); field.setWidth("80px");
field.setPlaceholder("0.0"); field.setPlaceholder("0.0");
return field; return field;
} }
private double parseHoras(String value) { private double parseHoras(final String value) {
if (value == null || value.trim().isEmpty()) { if (value == null || value.trim().isEmpty()) {
return 0.0; return 0.0;
} }
@ -218,17 +230,31 @@ public class HoursWorkedView extends VerticalLayout {
} }
private void guardarActividades() { private void guardarActividades() {
HoursWorked hoursWorked = new HoursWorked(); Employee selectedEmployee = employeeComboBox.getValue();
hoursWorked.setEmployee(employeeComboBox.getValue());
hoursWorked.setWeekNumber(weekNumber);
hoursWorked.setTotalHours((actividades.stream()
.mapToDouble(this::calcularTotalPorDia).sum()));
hoursWorkedService.save(hoursWorked);
Notification.show("Actividades guardadas correctamente."); if (selectedEmployee == null) {
Notification.show("Por favor, selecciona un empleado antes de guardar.");
return;
}
double totalHorasSemana = actividades.stream()
.mapToDouble(this::calcularTotalPorDia)
.sum();
HoursWorked hoursWorked = new HoursWorked();
hoursWorked.setEmployee(selectedEmployee);
hoursWorked.setWeekNumber(weekNumber);
hoursWorked.setTotalHours(totalHorasSemana);
try {
hoursWorkedService.saveHoursWorked(hoursWorked); // Usa saveHoursWorked directamente
Notification.show("Actividades guardadas correctamente.");
} catch (Exception e) {
Notification.show("Error al guardar actividades: " + e.getMessage());
}
} }
private double calcularTotalHoras(List<HoursWorked> listaDeHorasTrabajadas) { private double calcularTotalHoras(final List<HoursWorked> listaDeHorasTrabajadas) {
return listaDeHorasTrabajadas.stream() return listaDeHorasTrabajadas.stream()
.mapToDouble(HoursWorked::getTotalHours) .mapToDouble(HoursWorked::getTotalHours)
.sum(); .sum();

View File

@ -0,0 +1,87 @@
package com.primefactorsolutions.views;
import com.primefactorsolutions.model.HoursWorked;
import com.primefactorsolutions.service.HoursWorkedService;
import com.primefactorsolutions.service.ReportService;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.html.Anchor;
import com.vaadin.flow.component.html.H2;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.StreamResource;
import jakarta.annotation.security.PermitAll;
import org.springframework.beans.factory.annotation.Autowired;
import com.vaadin.flow.component.notification.Notification;
import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@PermitAll
@Route(value = "/reportes", layout = MainLayout.class)
@PageTitle("Reporte de Horas Trabajadas")
public class ReporteView extends VerticalLayout {
private final HoursWorkedService hoursWorkedService;
private final ReportService reportService;
@Autowired
public ReporteView(final HoursWorkedService hoursWorkedService, final ReportService reportService) {
this.hoursWorkedService = hoursWorkedService;
this.reportService = reportService;
H2 title = new H2("Reporte de Horas Trabajadas");
add(title);
Button reportButton = new Button("Generar Reporte de Horas Trabajadas", event -> generateHoursWorkedReport());
add(reportButton);
}
private void generateHoursWorkedReport() {
List<HoursWorked> hoursWorkedList = hoursWorkedService.findAll(); // Obtener la lista de HoursWorked
if (hoursWorkedList.isEmpty()) {
Notification.show("No hay horas trabajadas disponibles para generar el reporte.",
3000, Notification.Position.MIDDLE);
return;
}
try {
List<String> headers = List.of("ID", "Employee ID", "Week Number", "Total Hours");
List<Map<String, Object>> data = hoursWorkedList.stream()
.map(hoursWorked -> {
Map<String, Object> map = new HashMap<>();
map.put("ID", hoursWorked.getId().toString());
map.put("Employee ID", hoursWorked.getEmployee().getId().toString());
map.put("Week Number", hoursWorked.getWeekNumber());
map.put("Total Hours", hoursWorked.getTotalHours());
return map;
})
.collect(Collectors.toList());
byte[] excelBytes = reportService.writeAsExcel("hours_worked_report", headers, data);
StreamResource excelResource = new StreamResource("hours_worked_report.xlsx",
() -> new ByteArrayInputStream(excelBytes));
excelResource.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
excelResource.setCacheTime(0);
Anchor downloadLink = new Anchor(excelResource, "Descargar Reporte de Horas Trabajadas");
downloadLink.getElement().setAttribute("download", true);
add(downloadLink);
Notification.show("Reporte de horas trabajadas generado exitosamente.",
3000, Notification.Position.MIDDLE);
} catch (Exception e) {
Notification.show("Error al generar el reporte de horas trabajadas. Inténtalo de nuevo.",
3000, Notification.Position.MIDDLE);
e.printStackTrace();
}
}
}