use of com.qcadoo.mes.basic.util.DateTimeRange in project mes by qcadoo.
the class ShiftExceptionService method getExceptionRange.
private Optional<DateTimeRange> getExceptionRange(final Entity exception, final Shift shift, boolean workingException) {
Date fromDate = exception.getDateField(ShiftTimetableExceptionFields.FROM_DATE);
Date toDate = exception.getDateField(ShiftTimetableExceptionFields.TO_DATE);
if (Objects.isNull(shift.getShiftStartDate()) || Objects.isNull(shift.getShiftEndDate())) {
if (workingException) {
return Optional.of(new DateTimeRange(fromDate, toDate));
} else {
return Optional.empty();
}
}
if (toDate.before(shift.getShiftStartDate().toDate()) || shift.getShiftEndDate().toDate().before(fromDate)) {
return Optional.empty();
}
if (fromDate.before(shift.getShiftStartDate().toDate())) {
fromDate = shift.getShiftStartDate().toDate();
}
if (toDate.after(shift.getShiftEndDate().toDate())) {
toDate = shift.getShiftEndDate().toDate();
}
return Optional.of(new DateTimeRange(fromDate, toDate));
}
use of com.qcadoo.mes.basic.util.DateTimeRange in project mes by qcadoo.
the class ShiftsServiceImpl method getShiftForNearestWorkingDate.
@Override
public Optional<Shift> getShiftForNearestWorkingDate(DateTime nearestWorkingDate, Entity productionLine) {
List<Shift> shifts = findAll(productionLine);
List<DateTimeRange> finalShiftWorkTimes = Lists.newArrayList();
DateTime currentDate = nearestWorkingDate.minusDays(1);
if (shifts.stream().noneMatch(shift -> checkShiftWorkingAfterDate(nearestWorkingDate, productionLine, shift))) {
return Optional.empty();
}
while (finalShiftWorkTimes.isEmpty()) {
for (Shift shift : shifts) {
getNearestWorkingDateForShift(shift, productionLine, nearestWorkingDate, currentDate, finalShiftWorkTimes);
if (!finalShiftWorkTimes.isEmpty()) {
shift.setShiftStartDate(nearestWorkingDate);
DateTime endDate = finalShiftWorkTimes.stream().max(Comparator.comparing(DateTimeRange::getTo)).get().getTo();
shift.setShiftEndDate(endDate);
return Optional.of(shift);
}
}
currentDate = currentDate.plusDays(1);
}
return Optional.empty();
}
use of com.qcadoo.mes.basic.util.DateTimeRange in project mes by qcadoo.
the class ShiftsServiceImpl method getTotalAvailableTimeForProductionLine.
@Override
public long getTotalAvailableTimeForProductionLine(final Date dateFrom, final Date dateTo, final Entity productionLine) {
if (getShiftDataDefinition().find().list().getTotalNumberOfEntities() == 0) {
return (dateTo.getTime() - dateFrom.getTime()) / 1000;
}
long totalAvailableTime = 0;
List<Shift> shifts = findAll(productionLine);
DateTime dateOfDay = new DateTime(dateFrom);
int loopCount = 0;
while (!dateOfDay.isAfter(new DateTime(dateTo))) {
if (loopCount > MAX_LOOPS) {
return (dateTo.getTime() - dateFrom.getTime()) / 1000;
}
for (Shift shift : shifts) {
for (DateTimeRange range : shiftExceptionService.getShiftWorkDateTimes(productionLine, shift, dateOfDay, false)) {
totalAvailableTime += range.durationMillis();
}
}
loopCount++;
dateOfDay = dateOfDay.plusDays(1);
}
return totalAvailableTime / 1000;
}
use of com.qcadoo.mes.basic.util.DateTimeRange in project mes by qcadoo.
the class ShiftsServiceImpl method getNearestWorkingDateForShift.
private void getNearestWorkingDateForShift(final Shift shift, final Entity productionLine, final DateTime dateFrom, final DateTime currentDate, final List<DateTimeRange> finalShiftWorkTimes) {
List<DateTimeRange> workTimes = Lists.newArrayList();
List<TimeRange> shiftWorkTimes = shift.findWorkTimeAt(currentDate.toLocalDate());
for (TimeRange shiftWorkTime : shiftWorkTimes) {
getNearestWorkingDateForShiftWorkTime(shiftWorkTime, dateFrom, currentDate, workTimes);
}
workTimes = shiftExceptionService.manageExceptions(workTimes, productionLine, shift, currentDate.toDate(), true);
workTimes = workTimes.stream().filter(workTime -> workTime.contains(dateFrom) || workTime.isAfter(dateFrom)).collect(Collectors.toList());
finalShiftWorkTimes.addAll(workTimes);
}
use of com.qcadoo.mes.basic.util.DateTimeRange in project mes by qcadoo.
the class ShiftsServiceImpl method findDateToForProductionLine.
@Override
public Date findDateToForProductionLine(final Date dateFrom, final long seconds, final Entity productionLine) {
if (getShiftDataDefinition().find().list().getTotalNumberOfEntities() == 0) {
return Date.from(dateFrom.toInstant().plusSeconds(seconds));
}
DateTime dateFromDT = new DateTime(dateFrom, DateTimeZone.getDefault());
List<Shift> shifts = findAll(productionLine);
DateTime dateOfDay = new DateTime(dateFrom);
dateOfDay = dateOfDay.minusDays(1);
dateOfDay = dateOfDay.toLocalDate().toDateTimeAtStartOfDay();
long leftMilliseconds = seconds * MILLS;
int loopCount = 0;
while (leftMilliseconds > 0L) {
if (loopCount > MAX_LOOPS) {
return Date.from(dateFrom.toInstant().plusSeconds(seconds));
}
for (Shift shift : shifts) {
for (DateTimeRange range : shiftExceptionService.getShiftWorkDateTimes(productionLine, shift, dateOfDay, true)) {
if (dateFrom.after(dateOfDay.toDate())) {
range = range.trimBefore(dateFromDT);
}
if (range != null) {
if (leftMilliseconds > range.durationMillis()) {
leftMilliseconds = leftMilliseconds - range.durationMillis();
} else {
return range.getFrom().plusMillis((int) leftMilliseconds).toDate();
}
}
}
}
loopCount++;
dateOfDay = dateOfDay.plusDays(1);
}
return Date.from(dateFrom.toInstant().plusSeconds(seconds));
}
Aggregations