Search in sources :

Example 16 with Shift

use of com.qcadoo.mes.basic.shift.Shift 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 17 with Shift

use of com.qcadoo.mes.basic.shift.Shift 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 18 with Shift

use of com.qcadoo.mes.basic.shift.Shift 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)

Example 19 with Shift

use of com.qcadoo.mes.basic.shift.Shift in project mes by qcadoo.

the class OrderRealizationDaysResolverTest method shouldProduceStreamOfRealizationDays2.

@Test
public final void shouldProduceStreamOfRealizationDays2() {
    // given
    DateTime startDate = new DateTime(2014, 8, 14, 14, 0, 0);
    List<Shift> shifts = ImmutableList.of(shift1, shift2, shift3);
    // when
    LazyStream<OrderRealizationDay> stream = orderRealizationDaysResolver.asStreamFrom(startDate, shifts);
    // then
    OrderRealizationDay[] streamVals = FluentIterable.from(stream).limit(5).toArray(OrderRealizationDay.class);
    assertRealizationDayState(streamVals[0], 1, startDate.toLocalDate(), shifts);
    assertRealizationDayState(streamVals[1], 2, startDate.toLocalDate().plusDays(1), shifts);
    assertRealizationDayState(streamVals[2], 5, startDate.toLocalDate().plusDays(4), shifts);
    assertRealizationDayState(streamVals[3], 6, startDate.toLocalDate().plusDays(5), shifts);
    assertRealizationDayState(streamVals[4], 7, startDate.toLocalDate().plusDays(6), shifts);
}
Also used : Shift(com.qcadoo.mes.basic.shift.Shift) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 20 with Shift

use of com.qcadoo.mes.basic.shift.Shift in project mes by qcadoo.

the class OrderRealizationDaysResolverTest method shouldResolveRealizationDay3.

@Test
public final void shouldResolveRealizationDay3() {
    // given
    DateTime startDate = new DateTime(2014, 8, 14, 3, 0, 0);
    List<Shift> shifts = ImmutableList.of(shift1, shift2, shift3);
    // when
    // - 1st order realization day is Thursday,
    // - shift working at given time doesn't work at this date but it's not a first day of order realization,
    OrderRealizationDay realizationDay = orderRealizationDaysResolver.find(startDate, 1, false, shifts);
    // then
    assertRealizationDayState(realizationDay, 1, startDate.toLocalDate(), shifts);
}
Also used : Shift(com.qcadoo.mes.basic.shift.Shift) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Aggregations

Shift (com.qcadoo.mes.basic.shift.Shift)37 DateTime (org.joda.time.DateTime)30 Entity (com.qcadoo.model.api.Entity)12 Test (org.junit.Test)11 TimeRange (com.qcadoo.commons.dateTime.TimeRange)9 DateTimeRange (com.qcadoo.mes.basic.util.DateTimeRange)8 Date (java.util.Date)7 LocalTime (org.joda.time.LocalTime)7 LocalDate (org.joda.time.LocalDate)6 BigDecimal (java.math.BigDecimal)3 DailyProgressContainer (com.qcadoo.mes.productionPerShift.domain.DailyProgressContainer)2 ErrorMessage (com.qcadoo.model.api.validators.ErrorMessage)2 Calendar (java.util.Calendar)2 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)2 Function (com.google.common.base.Function)1 Optional (com.google.common.base.Optional)1 Predicate (com.google.common.base.Predicate)1 FluentIterable (com.google.common.collect.FluentIterable)1 Lists (com.google.common.collect.Lists)1 DateRange (com.qcadoo.commons.dateTime.DateRange)1