Search in sources :

Example 6 with DateTimeRange

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));
}
Also used : DateTimeRange(com.qcadoo.mes.basic.util.DateTimeRange) Date(java.util.Date)

Example 7 with DateTimeRange

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();
}
Also used : Shift(com.qcadoo.mes.basic.shift.Shift) DateTimeRange(com.qcadoo.mes.basic.util.DateTimeRange) DateTime(org.joda.time.DateTime)

Example 8 with DateTimeRange

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;
}
Also used : Shift(com.qcadoo.mes.basic.shift.Shift) DateTimeRange(com.qcadoo.mes.basic.util.DateTimeRange) DateTime(org.joda.time.DateTime)

Example 9 with DateTimeRange

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);
}
Also used : DateTimeRange(com.qcadoo.mes.basic.util.DateTimeRange) TimeRange(com.qcadoo.commons.dateTime.TimeRange) DateTimeRange(com.qcadoo.mes.basic.util.DateTimeRange)

Example 10 with DateTimeRange

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));
}
Also used : Shift(com.qcadoo.mes.basic.shift.Shift) DateTimeRange(com.qcadoo.mes.basic.util.DateTimeRange) DateTime(org.joda.time.DateTime)

Aggregations

DateTimeRange (com.qcadoo.mes.basic.util.DateTimeRange)14 Shift (com.qcadoo.mes.basic.shift.Shift)9 DateTime (org.joda.time.DateTime)8 TimeRange (com.qcadoo.commons.dateTime.TimeRange)4 Entity (com.qcadoo.model.api.Entity)3 BigDecimal (java.math.BigDecimal)3 Date (java.util.Date)3 Interval (org.joda.time.Interval)2 Period (org.joda.time.Period)2 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 BasicConstants (com.qcadoo.mes.basic.constants.BasicConstants)1 ShiftFields (com.qcadoo.mes.basic.constants.ShiftFields)1 TimetableExceptionType (com.qcadoo.mes.basic.constants.TimetableExceptionType)1 DailyProgressContainer (com.qcadoo.mes.productionPerShift.domain.DailyProgressContainer)1 DailyProgressKey (com.qcadoo.mes.productionPerShift.domain.DailyProgressKey)1 ShiftEfficiencyCalculationHolder (com.qcadoo.mes.productionPerShift.domain.ShiftEfficiencyCalculationHolder)1 DataDefinition (com.qcadoo.model.api.DataDefinition)1 DataDefinitionService (com.qcadoo.model.api.DataDefinitionService)1 SearchRestrictions (com.qcadoo.model.api.search.SearchRestrictions)1