Compare commits

...

3 Commits

Author SHA1 Message Date
ca5fb122fb Lista de Solicitudes de empleado - Actualizar estado
All checks were successful
PR Builder / Build-PR (pull_request) Successful in 2m40s
2024-11-13 14:10:09 -04:00
6d60fd00cc Lista de Solicitudes de empleado - Refactorizar 2024-11-13 12:38:25 -04:00
51a1d22be9 Lista de Solicitudes de empleado - Calcular total disponibilidad de dias 2024-11-12 21:54:28 -04:00
8 changed files with 166 additions and 69 deletions

View File

@ -4,7 +4,6 @@ import com.vaadin.flow.component.page.AppShellConfigurator;
import com.vaadin.flow.theme.Theme; import com.vaadin.flow.theme.Theme;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
/** /**
* The entry point of the Spring Boot application. * The entry point of the Spring Boot application.
* *

View File

@ -3,13 +3,14 @@ package com.primefactorsolutions.model;
public enum TimeOffRequestStatus { public enum TimeOffRequestStatus {
TODOS, TODOS,
TOMADO, TOMADO,
SOLICITADO,
APROBADO, APROBADO,
EN_USO, EN_USO,
EN_REVISION,
PENDIENTE, PENDIENTE,
RECHAZADO, RECHAZADO,
VENCIDO,
SOLICITADO,
EN_REVISION,
COMPLETADO, COMPLETADO,
CANCELADO, CANCELADO,
VENCIDO
} }

View File

@ -5,6 +5,7 @@ import com.primefactorsolutions.repositories.TimeOffRequestRepository;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.Optional; import java.util.Optional;
@ -50,4 +51,23 @@ public class TimeOffRequestService {
public List<TimeOffRequest> findByEmployeeAndCategory(final UUID employeeId, final TimeOffRequestType category) { public List<TimeOffRequest> findByEmployeeAndCategory(final UUID employeeId, final TimeOffRequestType category) {
return timeOffRequestRepository.findByEmployeeIdAndCategory(employeeId, category); return timeOffRequestRepository.findByEmployeeIdAndCategory(employeeId, category);
} }
public void updateRequestStatuses() {
List<TimeOffRequest> requests = findAllTimeOffRequests();
LocalDate now = LocalDate.now();
for (TimeOffRequest request : requests) {
if (request.getState() == TimeOffRequestStatus.APROBADO) {
LocalDate startDate = request.getStartDate();
LocalDate endDate = request.getEndDate();
if (now.isAfter(endDate)) {
request.setState(TimeOffRequestStatus.TOMADO);
} else if (now.isEqual(startDate) || now.isAfter(startDate) && now.isBefore(endDate)) {
request.setState(TimeOffRequestStatus.EN_USO);
}
}
}
saveAll(requests);
}
} }

View File

@ -1,7 +1,5 @@
package com.primefactorsolutions.views; package com.primefactorsolutions.views;
import com.primefactorsolutions.model.TimeOffRequest;
import com.primefactorsolutions.model.TimeOffRequestStatus;
import com.primefactorsolutions.service.TimeOffRequestService; import com.primefactorsolutions.service.TimeOffRequestService;
import com.vaadin.flow.component.Text; import com.vaadin.flow.component.Text;
import com.vaadin.flow.component.html.Main; import com.vaadin.flow.component.html.Main;
@ -9,42 +7,12 @@ import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route; import com.vaadin.flow.router.Route;
import jakarta.annotation.security.PermitAll; import jakarta.annotation.security.PermitAll;
import java.time.LocalDate;
import java.util.List;
@PageTitle("Home") @PageTitle("Home")
@Route(value = "", layout = MainLayout.class) @Route(value = "", layout = MainLayout.class)
@PermitAll @PermitAll
public class MainView extends Main { public class MainView extends Main {
private final TimeOffRequestService requestService;
public MainView(final TimeOffRequestService requestService) { public MainView(final TimeOffRequestService requestService) {
this.requestService = requestService;
add(new Text("Welcome")); add(new Text("Welcome"));
updateRequestStatuses();
}
private void updateRequestStatuses() {
List<TimeOffRequest> requests = requestService.findAllTimeOffRequests();
LocalDate now = LocalDate.now();
for (TimeOffRequest request : requests) {
if (request.getState() == TimeOffRequestStatus.APROBADO) {
LocalDate expirationDate = request.getExpiration();
LocalDate startDate = request.getStartDate();
LocalDate endDate = request.getEndDate();
if (now.isAfter(expirationDate)) {
request.setState(TimeOffRequestStatus.VENCIDO);
} else if (now.isAfter(endDate) && now.isBefore(expirationDate)) {
request.setState(TimeOffRequestStatus.TOMADO);
} else if (now.isEqual(startDate) || now.isAfter(startDate) && now.isBefore(endDate)) {
request.setState(TimeOffRequestStatus.EN_USO);
}
}
}
requestService.saveAll(requests);
} }
} }

View File

@ -23,9 +23,7 @@ import org.vaadin.firitin.components.grid.PagingGrid;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.Period; import java.time.Period;
import java.util.Collections; import java.util.*;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@SpringComponent @SpringComponent
@ -54,6 +52,7 @@ public class RequestEmployeeView extends Div implements HasUrlParameter<String>
} }
private void initializeView() { private void initializeView() {
requestService.updateRequestStatuses();
setupFilters(); setupFilters();
setupGrid(); setupGrid();
add(requestGrid, createActionButtons(), createSummaryLayout()); add(requestGrid, createActionButtons(), createSummaryLayout());
@ -106,28 +105,70 @@ public class RequestEmployeeView extends Div implements HasUrlParameter<String>
}); });
} }
private VerticalLayout createSummaryLayout() { private Set<TimeOffRequestType> getStandardExclusions() {
double totalHoliday = requests.stream() return Set.of(
.filter(this::verificationIsHoliday) TimeOffRequestType.MATERNIDAD,
.mapToDouble(TimeOffRequest::getAvailableDays) TimeOffRequestType.PATERNIDAD,
.sum(); TimeOffRequestType.MATRIMONIO,
double totalVacations = calculateVacationDays(employeeService.getEmployee(employeeId)); TimeOffRequestType.DUELO_1ER_GRADO,
double totalPersonalDays = requests.stream() TimeOffRequestType.DUELO_2ER_GRADO,
.filter(req -> !verificationIsHoliday(req)) TimeOffRequestType.DIA_DEL_PADRE,
.filter(req -> !req.getCategory().name().startsWith("VACACION")) TimeOffRequestType.DIA_DE_LA_MADRE
.mapToDouble(TimeOffRequest::getAvailableDays) );
.sum(); }
double totalAvailableDays = totalHoliday + totalVacations + totalPersonalDays; private Set<TimeOffRequestType> getMaleSpecificExclusions() {
return Set.of(
TimeOffRequestType.DIA_DE_LA_MUJER_INTERNACIONAL,
TimeOffRequestType.DIA_DE_LA_MUJER_NACIONAL
);
}
private VerticalLayout createSummaryLayout() {
Employee employee = employeeService.getEmployee(employeeId);
boolean isMale = employee.getGender() == Employee.Gender.MALE;
List<Vacation> vacations = vacationService.findVacations();
double totalFixedAndMovableHolidays = calculateHolidayDays(vacations);
double totalPersonalDays = calculatePersonalDays(vacations, isMale);
double totalVacationDays = calculateVacationDays(employee);
double utilizedFixedAndMovableHolidays = calculateHolidayUtilizedDays();
double utilizedVacationDays = calculateVacationUtilizedDays();
double utilizedPersonalDays = calculatePersonalDaysUtilized(isMale);
double remainingHolidayDays = totalFixedAndMovableHolidays - utilizedFixedAndMovableHolidays;
double remainingPersonalDays = totalPersonalDays - utilizedPersonalDays;
double remainingVacationDays = totalVacationDays - utilizedVacationDays;
double totalAvailableDays = remainingHolidayDays + remainingPersonalDays + remainingVacationDays;
return new VerticalLayout( return new VerticalLayout(
new Span("Total feriados: " + totalHoliday), new Span("Total feriados fijos y movibles: " + remainingHolidayDays),
new Span("Total vacaciones: " + totalVacations), new Span("Total días libres personales: " + remainingPersonalDays),
new Span("Total días personales: " + totalPersonalDays), new Span("Total vacaciones pendientes de uso: " + remainingVacationDays),
new Span("Total general: " + totalAvailableDays) new Span("Total general de días disponibles: " + totalAvailableDays)
); );
} }
private double calculateHolidayDays(final List<Vacation> vacations) {
return vacations.stream()
.filter(req -> req.getType() != Vacation.Type.OTHER)
.mapToDouble(Vacation::getDuration)
.sum();
}
private double calculatePersonalDays(final List<Vacation> vacations, final boolean isMale) {
return vacations.stream()
.filter(req -> req.getType() == Vacation.Type.OTHER)
.filter(req -> !getStandardExclusions().contains(req.getCategory()))
.filter(req -> !(isMale && getMaleSpecificExclusions().contains(req.getCategory())))
.filter(req -> !req.getCategory().name().startsWith("VACACION"))
.mapToDouble(Vacation::getDuration)
.sum();
}
private double calculateVacationDays(final Employee employee) { private double calculateVacationDays(final Employee employee) {
if (employee.getDateOfEntry() != null) { if (employee.getDateOfEntry() != null) {
LocalDate entryDate = employee.getDateOfEntry(); LocalDate entryDate = employee.getDateOfEntry();
@ -171,6 +212,30 @@ public class RequestEmployeeView extends Div implements HasUrlParameter<String>
} }
} }
private double calculateHolidayUtilizedDays() {
return requests.stream()
.filter(this::verificationIsHoliday)
.mapToDouble(TimeOffRequest::getAvailableDays)
.sum();
}
private double calculateVacationUtilizedDays() {
return requests.stream()
.filter(req -> req.getCategory() == TimeOffRequestType.VACACION_GESTION_ACTUAL)
.mapToDouble(TimeOffRequest::getAvailableDays)
.sum();
}
private double calculatePersonalDaysUtilized(final boolean isMale) {
return requests.stream()
.filter(req -> !verificationIsHoliday(req))
.filter(req -> !getStandardExclusions().contains(req.getCategory()))
.filter(req -> !(isMale && getMaleSpecificExclusions().contains(req.getCategory())))
.filter(req -> !req.getCategory().name().startsWith("VACACION"))
.mapToDouble(TimeOffRequest::getAvailableDays)
.sum();
}
private double calculateVacationDaysSinceEntry(final LocalDate dateOfEntry, final LocalDate date) { private double calculateVacationDaysSinceEntry(final LocalDate dateOfEntry, final LocalDate date) {
int yearsOfService = dateOfEntry != null ? Period.between(dateOfEntry, date).getYears() : 0; int yearsOfService = dateOfEntry != null ? Period.between(dateOfEntry, date).getYears() : 0;
if (yearsOfService > 10) { if (yearsOfService > 10) {

View File

@ -63,6 +63,7 @@ public class RequestRegisterView extends VerticalLayout {
} }
private void initializeView() { private void initializeView() {
requestService.updateRequestStatuses();
configureFormFields(); configureFormFields();
configureButtons(); configureButtons();
configureBinder(); configureBinder();
@ -84,8 +85,6 @@ public class RequestRegisterView extends VerticalLayout {
employeeComboBox.setItemLabelGenerator(emp -> emp.getFirstName() + " " + emp.getLastName()); employeeComboBox.setItemLabelGenerator(emp -> emp.getFirstName() + " " + emp.getLastName());
employeeComboBox.addValueChangeListener(event -> { employeeComboBox.addValueChangeListener(event -> {
employee = event.getValue(); employee = event.getValue();
System.out.println("Clearing form..." + employee);
handleEmployeeSelection(event.getValue()); handleEmployeeSelection(event.getValue());
}); });
categoryComboBox.addValueChangeListener(event -> { categoryComboBox.addValueChangeListener(event -> {
@ -173,7 +172,8 @@ public class RequestRegisterView extends VerticalLayout {
return latestRequest.getState() == TimeOffRequestStatus.VENCIDO return latestRequest.getState() == TimeOffRequestStatus.VENCIDO
|| (latestRequest.getState() == TimeOffRequestStatus.TOMADO && latestRequest.getDaysBalance() > 0); || (latestRequest.getState() == TimeOffRequestStatus.TOMADO && latestRequest.getDaysBalance() > 0);
} else { } else {
return latestRequest.getState() == TimeOffRequestStatus.VENCIDO; return latestRequest.getState() == TimeOffRequestStatus.VENCIDO
|| latestRequest.getState() == TimeOffRequestStatus.RECHAZADO;
} }
} }
@ -248,11 +248,29 @@ public class RequestRegisterView extends VerticalLayout {
} }
if (startDate != null) { if (startDate != null) {
if (vacation.getExpiration() != null) {
endDate = startDate.plusDays(vacation.getExpiration().intValue() - 1); endDate = startDate.plusDays(vacation.getExpiration().intValue() - 1);
} else {
endDate = LocalDate.of(startDate.getYear(), 12, 31);
}
} else { } else {
startDate = LocalDate.now(); startDate = LocalDate.now();
} }
System.out.println(vacation);
System.out.println(vacation);
System.out.println(vacation);
System.out.println(vacation);
System.out.println(vacation);
System.out.println(vacation);
System.out.println(startDate);
System.out.println(startDate);
System.out.println(startDate);
System.out.println(startDate);
System.out.println(startDate);
System.out.println(startDate);
setPickerValues(vacation, startDate); setPickerValues(vacation, startDate);
setPickerLimits(startDate, endDate); setPickerLimits(startDate, endDate);
} }
@ -267,19 +285,23 @@ public class RequestRegisterView extends VerticalLayout {
.map(request -> request.getStartDate().getYear()) .map(request -> request.getStartDate().getYear())
.orElse(LocalDate.now().getYear()); .orElse(LocalDate.now().getYear());
if (previousRequests.getLast().getState() != TimeOffRequestStatus.RECHAZADO) {
lastRequestYear = lastRequestYear + 1;
}
int currentYear = LocalDate.now().getYear(); int currentYear = LocalDate.now().getYear();
return Math.max(lastRequestYear + 1, currentYear); return Math.max(lastRequestYear, currentYear);
} }
private LocalDate determineStartDate(final Vacation vacation, final int startYear) { private LocalDate determineStartDate(final Vacation vacation, final int startYear) {
if (vacation.getMonthOfYear() != null && vacation.getDayOfMonth() != null) {
return LocalDate.of(startYear, vacation.getMonthOfYear().intValue(), vacation.getDayOfMonth().intValue());
}
if (vacation.getCategory() == TimeOffRequestType.CUMPLEAÑOS && employee.getBirthday() != null) { if (vacation.getCategory() == TimeOffRequestType.CUMPLEAÑOS && employee.getBirthday() != null) {
return LocalDate.of(startYear, employee.getBirthday().getMonth(), employee.getBirthday().getDayOfMonth()); return LocalDate.of(startYear, employee.getBirthday().getMonth(), employee.getBirthday().getDayOfMonth());
} }
if (vacation.getMonthOfYear() != null && vacation.getDayOfMonth() != null) {
return LocalDate.of(startYear, vacation.getMonthOfYear().intValue(), vacation.getDayOfMonth().intValue());
}
if (vacation.getCategory() == TimeOffRequestType.PERMISOS_DE_SALUD) { if (vacation.getCategory() == TimeOffRequestType.PERMISOS_DE_SALUD) {
return LocalDate.now(); return LocalDate.now();
} }
@ -290,8 +312,24 @@ public class RequestRegisterView extends VerticalLayout {
private void setPickerValues(final Vacation vacation, final LocalDate startDate) { private void setPickerValues(final Vacation vacation, final LocalDate startDate) {
startDatePicker.setValue(startDate); startDatePicker.setValue(startDate);
System.out.println(vacation);
System.out.println(vacation);
System.out.println(vacation);
System.out.println(vacation);
System.out.println(vacation);
System.out.println(vacation);
System.out.println(startDate);
System.out.println(startDate);
System.out.println(startDate);
System.out.println(startDate);
System.out.println(startDate);
System.out.println(startDate);
if ((vacation.getDuration() != null && vacation.getDuration() == 0.5) if ((vacation.getDuration() != null && vacation.getDuration() == 0.5)
|| vacation.getCategory() == TimeOffRequestType.PERMISOS_DE_SALUD) { || vacation.getCategory() == TimeOffRequestType.PERMISOS_DE_SALUD
|| vacation.getCategory() == TimeOffRequestType.CUMPLEAÑOS) {
endDatePicker.setValue(startDate); endDatePicker.setValue(startDate);
} else { } else {
int durationDays = (vacation.getDuration() != null ? vacation.getDuration().intValue() - 1 : 0); int durationDays = (vacation.getDuration() != null ? vacation.getDuration().intValue() - 1 : 0);
@ -330,7 +368,7 @@ public class RequestRegisterView extends VerticalLayout {
double balanceDays = calculateBalanceDays(availableDays, daysToBeTakenField.getValue()); double balanceDays = calculateBalanceDays(availableDays, daysToBeTakenField.getValue());
balanceDaysField.setValue(balanceDays); balanceDaysField.setValue(balanceDays);
if (balanceDays < 0) { if (balanceDays < 0.0) {
clearFields(); clearFields();
} }
} }
@ -345,7 +383,12 @@ public class RequestRegisterView extends VerticalLayout {
} }
private void setDaysToBeTakenField(final double daysToBeTaken) { private void setDaysToBeTakenField(final double daysToBeTaken) {
if (vacation.getCategory() == TimeOffRequestType.PERMISOS_DE_SALUD) { if (vacation.getCategory() == TimeOffRequestType.PERMISOS_DE_SALUD
|| vacation.getCategory() == TimeOffRequestType.CUMPLEAÑOS
|| vacation.getCategory() == TimeOffRequestType.DIA_DEL_PADRE
|| vacation.getCategory() == TimeOffRequestType.DIA_DE_LA_MADRE
|| vacation.getCategory() == TimeOffRequestType.DIA_DE_LA_MUJER_INTERNACIONAL
|| vacation.getCategory() == TimeOffRequestType.DIA_DE_LA_MUJER_NACIONAL) {
daysToBeTakenField.setValue(0.5); daysToBeTakenField.setValue(0.5);
} else { } else {
daysToBeTakenField.setValue(daysToBeTaken); daysToBeTakenField.setValue(daysToBeTaken);
@ -418,7 +461,7 @@ public class RequestRegisterView extends VerticalLayout {
List<TimeOffRequest> existingRequests = List<TimeOffRequest> existingRequests =
requestService.findByEmployeeAndCategory(employee.getId(), request.getCategory()); requestService.findByEmployeeAndCategory(employee.getId(), request.getCategory());
int maxRequests = request.getCategory() == TimeOffRequestType.PERMISOS_DE_SALUD ? 4 : 2; int maxRequests = request.getCategory().name().startsWith("VACACION") ? 2 : 1;
if (existingRequests.size() >= maxRequests) { if (existingRequests.size() >= maxRequests) {
existingRequests.stream() existingRequests.stream()

View File

@ -57,6 +57,7 @@ public class RequestsListView extends Main {
} }
private void initializeView() { private void initializeView() {
requestService.updateRequestStatuses();
setupFilters(); setupFilters();
setupRequestGrid(); setupRequestGrid();
add(requestGrid); add(requestGrid);

View File

@ -32,7 +32,7 @@ INSERT INTO vacation (id, version, category, month_of_year, day_of_month, durati
INSERT INTO vacation (id, version, category, month_of_year, day_of_month, duration, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417400b', 1, 'DIA_DE_TODOS_LOS_DIFUNTOS', 11, 2, 1, 30, 'MOVABLE'); INSERT INTO vacation (id, version, category, month_of_year, day_of_month, duration, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417400b', 1, 'DIA_DE_TODOS_LOS_DIFUNTOS', 11, 2, 1, 30, 'MOVABLE');
INSERT INTO vacation (id, version, category, duration, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417400c', 1, 'CUMPLEAÑOS', 0.5, 365, 'OTHER'); INSERT INTO vacation (id, version, category, month_of_year, day_of_month, duration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417400c', 1, 'CUMPLEAÑOS', 12, 31, 0.5, 'OTHER');
INSERT INTO vacation (id, version, category, duration, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417400d', 1, 'MATERNIDAD', 90, 90, 'OTHER'); INSERT INTO vacation (id, version, category, duration, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417400d', 1, 'MATERNIDAD', 90, 90, 'OTHER');
INSERT INTO vacation (id, version, category, duration, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417400e', 1, 'PATERNIDAD', 3, 3, 'OTHER'); INSERT INTO vacation (id, version, category, duration, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417400e', 1, 'PATERNIDAD', 3, 3, 'OTHER');
INSERT INTO vacation (id, version, category, duration, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417400f', 1, 'MATRIMONIO', 3, 3, 'OTHER'); INSERT INTO vacation (id, version, category, duration, expiration, type) VALUES ('c23e4567-e89b-12d3-a456-42661417400f', 1, 'MATRIMONIO', 3, 3, 'OTHER');