From b765211e2719cc9ef69c0d2d90647578d7edf41d Mon Sep 17 00:00:00 2001 From: Klaus Richarz Date: Wed, 3 Jul 2024 21:22:45 +0200 Subject: [PATCH] #354 rework validation of breaks and rest time --- .../service/TimereportService.java | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/tb/dailyreport/service/TimereportService.java b/src/main/java/org/tb/dailyreport/service/TimereportService.java index 89308efb1..ea01c2be0 100644 --- a/src/main/java/org/tb/dailyreport/service/TimereportService.java +++ b/src/main/java/org/tb/dailyreport/service/TimereportService.java @@ -212,16 +212,15 @@ public List validateForRelease(Long employeeContractI .filter(timeReport -> timeReport.getOrderType().isRelevantForWorkingTimeValidation()) .collect(Collectors.groupingBy(TimereportDTO::getReferenceday, Collectors.mapping(identity(), Collectors.toList()))) .forEach((date, timeReports) -> { - Workingday workingDay = workingdayDAO.getWorkingdayByDateAndEmployeeContractId(date, employeeContractId); - WorkingDayValidationError error = validateBeginOfWorkingDay(workingDay, date); + WorkingDayValidationError error = validateBeginOfWorkingDay(date, employeeContractId); if (error != null) { errors.add(error); } - error = validateBreakTimes(timeReports, workingDay); + error = validateBreakTimes(date, employeeContractId, timeReports); if (error != null) { errors.add(error); } - error = validateRestTime(workingDay, date, employeeContractId); + error = validateRestTime(date, employeeContractId); if (error != null) { errors.add(error); } @@ -497,30 +496,30 @@ private void validateTimeReportingBusinessRules(List timereports) { }); } - private WorkingDayValidationError validateBreakTimes(List timeReports, Workingday workingDay) { + private WorkingDayValidationError validateBreakTimes(LocalDate date, long employeeContractId, List timeReports) { + Workingday workingDay = workingdayDAO.getWorkingdayByDateAndEmployeeContractId(date, employeeContractId); Duration workDurationPerDay = timeReports.stream().map(TimereportDTO::getDuration).reduce(Duration.ZERO, Duration::plus); - boolean notEnoughBreaksAfter9Hours = workingDay != null && workingDay.getBreakLengthInMinutes() < BREAK_MINUTES_AFTER_NINE_HOURS; - boolean notEnoughBreaksAfter6Hours = workingDay != null && workingDay.getBreakLengthInMinutes() < BREAK_MINUTES_AFTER_SIX_HOURS; - if (workDurationPerDay.toMinutes() > NINE_HOURS_IN_MINUTES && notEnoughBreaksAfter9Hours) { - return new WorkingDayValidationError(workingDay.getRefday(), "form.release.error.breaktime.nine.length"); - } else if (workDurationPerDay.toMinutes() > SIX_HOURS_IN_MINUTES && notEnoughBreaksAfter6Hours) { - return new WorkingDayValidationError(workingDay.getRefday(), "form.release.error.breaktime.six.length"); + boolean notEnoughBreaksAfter9Hours = workingDay == null || workingDay.getBreakLengthInMinutes() < BREAK_MINUTES_AFTER_NINE_HOURS; + boolean notEnoughBreaksAfter6Hours = workingDay == null || workingDay.getBreakLengthInMinutes() < BREAK_MINUTES_AFTER_SIX_HOURS; + if (workDurationPerDay.toMinutes() >= NINE_HOURS_IN_MINUTES && notEnoughBreaksAfter9Hours) { + return new WorkingDayValidationError(date, "form.release.error.breaktime.nine.length"); + } else if (workDurationPerDay.toMinutes() >= SIX_HOURS_IN_MINUTES && notEnoughBreaksAfter6Hours) { + return new WorkingDayValidationError(date, "form.release.error.breaktime.six.length"); } return null; } - private WorkingDayValidationError validateRestTime(Workingday workingDay, - LocalDate releaseDate, - long employeeContractId) { - Workingday theDayBefore = workingdayDAO.getWorkingdayByDateAndEmployeeContractId(releaseDate.minusDays(1), employeeContractId); + private WorkingDayValidationError validateRestTime(LocalDate date, long employeeContractId) { + Workingday workingDay = workingdayDAO.getWorkingdayByDateAndEmployeeContractId(date, employeeContractId); + Workingday theDayBefore = workingdayDAO.getWorkingdayByDateAndEmployeeContractId(date.minusDays(1), employeeContractId); if (theDayBefore == null) { return null; } - Duration workDurationPerDay = timereportDAO.getOpenTimereportsByEmployeeContractIdBeforeDate(employeeContractId, releaseDate.minusDays(1)).stream() + Duration workDurationSum = timereportDAO.getTimereportsByDateAndEmployeeContractId(employeeContractId, theDayBefore.getRefday()).stream() .filter(timeReport -> timeReport.getOrderType().isRelevantForWorkingTimeValidation()) .map(TimereportDTO::getDuration) .reduce(Duration.ZERO, Duration::plus); - LocalDateTime endOfWorkingDay = theDayBefore.getStartOfWorkingDay().plus(theDayBefore.getBreakLength()).plus(workDurationPerDay); + LocalDateTime endOfWorkingDay = theDayBefore.getStartOfWorkingDay().plus(theDayBefore.getBreakLength()).plus(workDurationSum); LocalDateTime startOfWorkingDay = workingDay.getStartOfWorkingDay(); Duration restTime = Duration.between(endOfWorkingDay, startOfWorkingDay); if (restTime.toMinutes() < REST_PERIOD_IN_MINUTES) { @@ -529,7 +528,8 @@ private WorkingDayValidationError validateRestTime(Workingday workingDay, return null; } - private WorkingDayValidationError validateBeginOfWorkingDay(Workingday workingDay, LocalDate date) { + private WorkingDayValidationError validateBeginOfWorkingDay(LocalDate date, long employeeContractId) { + Workingday workingDay = workingdayDAO.getWorkingdayByDateAndEmployeeContractId(date, employeeContractId); if (workingDay == null || workingDay.getStartOfWorkingDay() == null) { return new WorkingDayValidationError(date, "form.release.error.beginofworkingday.required"); }