Compare commits

..

No commits in common. "9a29f91994443f759c649acc2c0ca7b5347c1057" and "81acc686e66ddf1d34372ea9df0498bce454ffa2" have entirely different histories.

2 changed files with 16 additions and 134 deletions

View File

@ -2,7 +2,6 @@ package com.primefactorsolutions.model;
import com.google.common.collect.Lists;
import jakarta.persistence.*;
import jakarta.validation.constraints.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
@ -19,45 +18,30 @@ import java.util.Collection;
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class Employee extends BaseEntity implements UserDetails {
private String username;
@NotNull(message = "El nombre no puede estar vacío")
@Pattern(regexp = "^[a-zA-Z ]+$", message = "El nombre solo debe contener letras")
private String firstName;
@NotNull(message = "El apellido no puede estar vacío")
@Pattern(regexp = "^[a-zA-Z ]+$", message = "El apellido solo debe contener letras")
private String lastName;
private LocalDate birthday;
@Pattern(regexp = "^[a-zA-Z ]+$", message = "La ciudad de nacimiento solo debe contener letras")
private String birthCity;
private String age;
@Size(max = 100, message = "La dirección de residencia no debe exceder 100 caracteres")
private String residenceAddress;
@Size(max = 100, message = "La dirección local no debe exceder 100 caracteres")
private String localAddress;
@Pattern(regexp = "^[0-9]+$", message = "El número de teléfono debe contener solo números")
private String phoneNumber;
@Email(message = "El correo personal no tiene un formato válido")
private String personalEmail;
@Pattern(regexp = "^[a-zA-Z ]+$", message = "El cargo solo debe contener letras")
private String position;
@ManyToOne
@JoinColumn(name = "team_id", nullable = false)
private Team team;
@Size(max = 100, message = "El nombre de contacto de emergencia no debe exceder 100 caracteres")
private String emergencyCName;
@Size(max = 100, message = "La dirección de contacto de emergencia no debe exceder 100 caracteres")
private String emergencyCAddress;
@Pattern(regexp = "^[0-9]+$", message = "El teléfono de contacto de emergencia debe contener solo números")
private String emergencyCPhone;
@Email(message = "El correo de contacto de emergencia no tiene un formato válido")
private String emergencyCEmail;
@Pattern(regexp = "^[0-9]+$", message = "La cantidad de hijos debe contener solo números")
private String numberOfChildren;
@Pattern(regexp = "^[0-9]+$", message = "El CI debe contener solo números")
private String ci;
private String issuedIn;
@Size(max = 100, message = "El título no debe exceder 100 caracteres")
private String pTitle1;
private String pTitle2;
private String pTitle3;
@ -70,36 +54,29 @@ public class Employee extends BaseEntity implements UserDetails {
private String certification2;
private String certification3;
private String certification4;
@Size(max = 255, message = "El reconocimiento no debe exceder 255 caracteres")
private String recognition;
@Size(max = 500, message = "Los logros no deben exceder 500 caracteres")
private String achievements;
private String language;
private String languageLevel;
@Pattern(regexp = "^[A-Za-z0-9]+$", message = "El código debe contener solo letras y números")
private String cod;
@Pattern(regexp = "^[a-zA-Z ]+$", message = "El lead manager solo debe contener letras")
private String leadManager;
@Pattern(regexp = "^[a-zA-Z ]+$", message = "El proyecto solo debe contener letras")
private String project;
private LocalDate dateOfEntry;
private LocalDate dateOfExit;
@Pattern(regexp = "^[a-zA-Z ]+$", message = "El tipo de contrato solo debe contener letras")
private String contractType;
@Pattern(regexp = "^[0-9]+$", message = "La antigüedad debe contener solo números")
private String seniority;
@Pattern(regexp = "^[0-9]+(\\.[0-9]{1,2})?$", message = "El salario debe ser un número con hasta dos decimales")
private String salary;
@Pattern(regexp = "^[a-zA-Z ]+$", message = "El nombre del banco solo debe contener letras")
private String bankName;
@Pattern(regexp = "^[0-9]+$", message = "El número de cuenta debe contener solo números")
private String accountNumber;
private String gpss;
private String sss;
@Pattern(regexp = "^[a-zA-Z ]+$", message = "Los derechohabientes solo deben contener letras")
private String beneficiaries;
@Column(columnDefinition = "TEXT")

View File

@ -1,10 +1,10 @@
package com.primefactorsolutions.views;
import com.primefactorsolutions.model.*;
import com.primefactorsolutions.model.Employee;
import com.primefactorsolutions.model.Team;
import com.primefactorsolutions.service.EmployeeService;
import com.primefactorsolutions.service.ReportService;
import com.primefactorsolutions.service.TeamService;
import com.primefactorsolutions.service.TimeOffRequestService;
import com.vaadin.componentfactory.pdfviewer.PdfViewer;
import com.vaadin.flow.component.ClickEvent;
import com.vaadin.flow.component.Component;
@ -39,8 +39,6 @@ import org.vaadin.firitin.form.BeanValidationForm;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Base64;
import java.util.List;
import java.util.UUID;
@ -54,10 +52,8 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
private final EmployeeService employeeService;
private final ReportService reportService;
private final TimeOffRequestService requestService;
private final TeamService teamService;
// TODO: campo usado para registrar al empleado en LDAP. Este campo podria estar en otro form eventualmente.
private final TextField username = createTextField("Username: ", 30, true);
private final TextField firstName = createTextField("Nombres: ", 30, true);
@ -147,12 +143,10 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
public EmployeeView(final EmployeeService employeeService,
final ReportService reportService,
final TeamService teamService,
final TimeOffRequestService requestService) {
final TeamService teamService) {
super(Employee.class);
this.employeeService = employeeService;
this.reportService = reportService;
this.requestService = requestService;
this.teamService = teamService;
saveButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
@ -177,18 +171,12 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
phoneNumber.addValueChangeListener(e -> validatePhoneNumber(phoneNumber, e.getValue()));
emergencyCPhone.setValueChangeMode(ValueChangeMode.EAGER);
emergencyCPhone.addValueChangeListener(e -> validatePhoneNumber(emergencyCPhone, e.getValue()));
firstName.setValueChangeMode(ValueChangeMode.EAGER);
firstName.addValueChangeListener(e -> validateNameField(firstName, e.getValue()));
lastName.setValueChangeMode(ValueChangeMode.EAGER);
lastName.addValueChangeListener(e -> validateNameField(lastName, e.getValue()));
createTeamComboBox();
configureUpload();
saveButton.setVisible(true);
editButton.setVisible(true);
reportButton.setVisible(true);
birthday.addValueChangeListener(event -> calculateAge());
createTeamComboBox();
reportButton.addClickListener((ComponentEventListener<ClickEvent<Button>>) buttonClickEvent -> {
var employee = getEntity();
@ -201,15 +189,6 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
initDialog();
}
private void validateNameField(TextField textField, String value) {
if (!value.matches("^[a-zA-ZáéíóúÁÉÍÓÚñÑ\\s]*$")) {
textField.setInvalid(true);
textField.setErrorMessage("Este campo solo debe contener letras.");
} else {
textField.setInvalid(false);
}
}
private void calculateAge() {
if (birthday.getValue() != null) {
int currentYear = java.time.LocalDate.now().getYear();
@ -280,7 +259,7 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
ComboBox<Employee.Status> comboBox = new ComboBox<>("Estado");
comboBox.setItems(Employee.Status.values());
comboBox.setItemLabelGenerator(Employee.Status::name);
comboBox.setRequiredIndicatorVisible(true);
comboBox.setRequiredIndicatorVisible(true); // Indicador de campo requerido
return comboBox;
}
@ -312,14 +291,6 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
team.setWidthFull();
}
private <T> ComboBox<T> createComboBox(final String label, final T[] items) {
ComboBox<T> comboBox = new ComboBox<>(label);
comboBox.setItems(items);
comboBox.setItemLabelGenerator(Object::toString);
comboBox.setWidthFull();
return comboBox;
}
private ComboBox<Employee.Gender> createGenderComboBox() {
ComboBox<Employee.Gender> comboBox = new ComboBox<>("Genero");
comboBox.setItems(Employee.Gender.values());
@ -332,33 +303,12 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
return !firstName.isEmpty() && !lastName.isEmpty() && status.getValue() != null;
}
private void setVacationDuration(Employee employee, TimeOffRequest request, LocalDate referenceDate) {
double yearsOfService = ChronoUnit.YEARS.between(employee.getDateOfEntry(), referenceDate);
request.setAvailableDays(calculateAvailableDays(yearsOfService));
}
private double calculateAvailableDays(double yearsOfService) {
if (yearsOfService > 10) {
return 30.0;
} else if (yearsOfService > 5) {
return 20.0;
} else if (yearsOfService > 1) {
return 15.0;
} else {
return 0.0;
}
}
private void saveEmployee() {
if (validateForm()) {
Employee employee = getEntity();
employee.setStatus(status.getValue());
employee.setAge(age.getValue());
if (employee.getDateOfEntry() != null) {
processTimeOffRequests(employee);
}
employeeService.createOrUpdate(employee);
Notification.show(NOTIFICATION_SAVE_SUCCESS);
getUI().ifPresent(ui -> ui.navigate(EmployeesListView.class));
@ -367,60 +317,13 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
}
}
private void processTimeOffRequests(Employee employee) {
if (hasTimeOffRequests(employee)) {
updateExistingTimeOffRequests(employee);
} else {
createAndSaveTimeOffRequests(employee);
}
}
private boolean hasTimeOffRequests(Employee employee) {
return !requestService.findByEmployeeAndCategory(employee.getId(), TimeOffRequestType.VACACION_GESTION_ACTUAL).isEmpty() ||
!requestService.findByEmployeeAndCategory(employee.getId(), TimeOffRequestType.VACACION_GESTION_ANTERIOR).isEmpty();
}
private void updateExistingTimeOffRequests(Employee employee) {
updateTimeOffRequest(employee, TimeOffRequestType.VACACION_GESTION_ANTERIOR, -1, 729);
updateTimeOffRequest(employee, TimeOffRequestType.VACACION_GESTION_ACTUAL, 0, 1094);
}
private void updateTimeOffRequest(Employee employee, TimeOffRequestType category, int yearOffset, int daysToAdd) {
List<TimeOffRequest> requests = requestService.findByEmployeeAndCategory(employee.getId(), category);
if (!requests.isEmpty()) {
TimeOffRequest request = requests.getLast();
LocalDate newBaseDate = LocalDate.of(LocalDate.now().getYear() + yearOffset, employee.getDateOfEntry().getMonth(), employee.getDateOfEntry().getDayOfMonth());
request.setExpiration(newBaseDate.plusDays(daysToAdd));
setVacationDuration(employee, request, newBaseDate);
requestService.saveTimeOffRequest(request);
}
}
private void createAndSaveTimeOffRequests(Employee employee) {
requestService.saveTimeOffRequest(createTimeOffRequest(employee, TimeOffRequestType.VACACION_GESTION_ANTERIOR, -1, 729));
requestService.saveTimeOffRequest(createTimeOffRequest(employee, TimeOffRequestType.VACACION_GESTION_ACTUAL, 0, 1094));
}
private TimeOffRequest createTimeOffRequest(Employee employee, TimeOffRequestType category, int yearOffset, int daysToAdd) {
LocalDate baseDate = LocalDate.of(LocalDate.now().getYear() + yearOffset, employee.getDateOfEntry().getMonth(), employee.getDateOfEntry().getDayOfMonth());
LocalDate expirationDate = baseDate.plusDays(daysToAdd);
TimeOffRequest request = new TimeOffRequest();
request.setCategory(category);
request.setState(TimeOffRequestStatus.APROBADO);
request.setEmployee(employee);
request.setExpiration(expirationDate);
setVacationDuration(employee, request, baseDate);
return request;
}
private void enableEditMode() {
setFieldsEditable();
saveButton.setVisible(true);
editButton.setVisible(false);
}
@Override
public void setParameter(final BeforeEvent beforeEvent, final String action) {
final RouteParameters params = beforeEvent.getRouteParameters();
@ -435,6 +338,7 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
UUID employeeId = UUID.fromString(s);
var employee = employeeService.getEmployee(employeeId);
setEntityWithEnabledSave(employee);
team.setValue(employee.getTeam());
if ("edit".equals(action) && !s.isEmpty()) {
saveButton.setVisible(true);
@ -601,4 +505,5 @@ public class EmployeeView extends BeanValidationForm<Employee> implements HasUrl
saveButton, editButton, reportButton, dialog
);
}
}
}