Search in sources :

Example 1 with DayShiftHolder

use of com.qcadoo.mes.productionPerShift.report.print.utils.DayShiftHolder in project mes by qcadoo.

the class PPSReportXlsService method addSeriesForChangeOver.

private void addSeriesForChangeOver(final HSSFSheet sheet, final Entity entity, final HSSFRow row, final Entity changeover, final Entity order, final PPSReportXlsStyleContainer styleContainer, final List<ReportColumn> columns) {
    Map<Integer, DayShiftHolder> mapCells = Maps.newHashMap();
    List<Entity> shifts = shiftsService.getShifts();
    DateTime dateFrom = new DateTime(entity.getDateField(PPSReportFields.DATE_FROM));
    DateTime dateTo = new DateTime(entity.getDateField(PPSReportFields.DATE_TO));
    List<DateTime> days = shiftsService.getDaysBetweenGivenDates(dateFrom, dateTo);
    Date startDateOrder = order.getDateField(OrderFields.START_DATE);
    Shift shiftFirst = new Shift(shifts.get(0));
    List<TimeRange> ranges = shiftFirst.findWorkTimeAt(days.get(0).toLocalDate());
    if (ranges.isEmpty()) {
        return;
    }
    LocalTime startTime = ranges.get(0).getFrom();
    DateTime firstStartShitTime = days.get(0);
    firstStartShitTime = firstStartShitTime.withHourOfDay(startTime.getHourOfDay());
    firstStartShitTime = firstStartShitTime.withMinuteOfHour(startTime.getMinuteOfHour());
    int columnNumber = columns.size();
    if (new DateTime(startDateOrder).minusSeconds(1).toDate().before(firstStartShitTime.toDate())) {
        for (DateTime day : days) {
            for (Entity shift : shifts) {
                HSSFCell cellDailyProgress = row.createCell(columnNumber);
                cellDailyProgress.setCellValue("");
                cellDailyProgress.setCellStyle(styleContainer.getStyles().get(PPSReportXlsStyleContainer.I_ChangeoverDataStyle));
                columnNumber++;
                sheet.autoSizeColumn((short) columnNumber);
            }
        }
    } else {
        for (DateTime day : days) {
            for (Entity shift : shifts) {
                HSSFCell cell = row.createCell(columnNumber);
                cell.setCellValue("");
                DayShiftHolder holder = new DayShiftHolder(shift, day, cell);
                cell.setCellStyle(styleContainer.getStyles().get(PPSReportXlsStyleContainer.I_ChangeoverDataStyle));
                mapCells.put(columnNumber, holder);
                columnNumber++;
                sheet.autoSizeColumn((short) columnNumber);
            }
        }
        columnNumber = columns.size();
        for (DateTime day : days) {
            for (Entity shift : shifts) {
                Optional<DateTime> maybeShiftStart = getShiftStartDate(day, shift);
                Optional<DateTime> maybeShiftEnd = getShiftEndDate(day, shift);
                HSSFCell cell = mapCells.get(columnNumber).getCell();
                if (!maybeShiftStart.isPresent() || !maybeShiftEnd.isPresent()) {
                    cell.setCellValue("");
                    columnNumber++;
                    continue;
                }
                DateTime shiftEnd = maybeShiftEnd.get();
                DateTime shiftStart = maybeShiftStart.get();
                if (shiftStart.toDate().after(shiftEnd.toDate())) {
                    shiftEnd = shiftEnd.plusDays(1);
                }
                boolean isChangeover = betweenDates(shiftStart.toDate(), shiftEnd.toDate(), new DateTime(startDateOrder).plusSeconds(1).toDate());
                if (isChangeover) {
                    int duration = changeover.getIntegerField(LineChangeoverNormsFields.DURATION);
                    int changeoverOnShift = Seconds.secondsBetween(shiftStart, new DateTime(startDateOrder)).getSeconds();
                    if (duration - changeoverOnShift > 0) {
                        if (changeoverOnShift > 0) {
                            cell.setCellValue(DurationFormatUtils.formatDuration(changeoverOnShift * 1000, "HH:mm"));
                        }
                        int durationToMark = duration - changeoverOnShift;
                        int currentIndex = columnNumber - 1;
                        if (currentIndex >= columns.size()) {
                            while (durationToMark > 0) {
                                HSSFCell cellBefore = mapCells.get(currentIndex).getCell();
                                Optional<DateTime> maybeStart = getShiftStartDate(day, shift);
                                Optional<DateTime> maybeEnd = getShiftEndDate(day, shift);
                                if (!maybeStart.isPresent() || !maybeEnd.isPresent()) {
                                    cell.setCellValue("");
                                    continue;
                                }
                                DateTime start = maybeStart.get();
                                DateTime end = maybeEnd.get();
                                if (start.toDate().after(end.toDate())) {
                                    end = end.plusDays(1);
                                }
                                int seconds = Seconds.secondsBetween(start, end).getSeconds();
                                if (durationToMark > seconds) {
                                    cellBefore.setCellValue(DurationFormatUtils.formatDuration(seconds * 1000, "HH:mm"));
                                    durationToMark = durationToMark - seconds;
                                    currentIndex = currentIndex - 1;
                                    if (currentIndex < columns.size()) {
                                        break;
                                    }
                                } else {
                                    cellBefore.setCellValue(DurationFormatUtils.formatDuration(durationToMark * 1000, "HH:mm"));
                                    durationToMark = durationToMark - seconds;
                                }
                            }
                        }
                    } else {
                        cell.setCellValue(DurationFormatUtils.formatDuration(duration * 1000, "HH:mm"));
                    }
                }
                columnNumber++;
            }
        }
    }
    for (int i = 0; i < columnNumber; i++) {
        row.getCell(i).setCellStyle(styleContainer.getStyles().get(PPSReportXlsStyleContainer.I_ChangeoverDataStyle));
    }
}
Also used : Shift(com.qcadoo.mes.basic.shift.Shift) Entity(com.qcadoo.model.api.Entity) LocalTime(org.joda.time.LocalTime) DateTime(org.joda.time.DateTime) Date(java.util.Date) LocalDate(org.joda.time.LocalDate) TimeRange(com.qcadoo.commons.dateTime.TimeRange) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) DayShiftHolder(com.qcadoo.mes.productionPerShift.report.print.utils.DayShiftHolder)

Aggregations

TimeRange (com.qcadoo.commons.dateTime.TimeRange)1 Shift (com.qcadoo.mes.basic.shift.Shift)1 DayShiftHolder (com.qcadoo.mes.productionPerShift.report.print.utils.DayShiftHolder)1 Entity (com.qcadoo.model.api.Entity)1 Date (java.util.Date)1 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)1 DateTime (org.joda.time.DateTime)1 LocalDate (org.joda.time.LocalDate)1 LocalTime (org.joda.time.LocalTime)1