Search in sources :

Example 1 with ProgressForDaysContainer

use of com.qcadoo.mes.productionPerShift.domain.ProgressForDaysContainer in project mes by qcadoo.

the class OrderHooksPPS method regenerateProductionPerShift.

public void regenerateProductionPerShift(final DataDefinition orderDD, final Entity order) {
    if (order.getId() == null) {
        return;
    }
    Entity orderFromDB = orderDD.get(order.getId());
    Entity productionPerShift = dataDefinitionService.get(ProductionPerShiftConstants.PLUGIN_IDENTIFIER, ProductionPerShiftConstants.MODEL_PRODUCTION_PER_SHIFT).find().add(SearchRestrictions.belongsTo(ProductionPerShiftFields.ORDER, order)).setMaxResults(1).uniqueResult();
    boolean generate = canGenerate(order, orderFromDB, productionPerShift);
    if (isOrderFieldsChanged(order, orderFromDB) || generate) {
        if (productionPerShift != null && automaticPpsParametersService.isAutomaticPlanForShiftOn()) {
            boolean shouldBeCorrected = OrderState.of(order).compareTo(OrderState.PENDING) != 0;
            if (!productionPerShift.getHasManyField(ProductionPerShiftFields.PROGRES_FOR_DAYS).isEmpty() || generate) {
                BigDecimal plannedQuantity = order.getDecimalField(OrderFields.PLANNED_QUANTITY);
                if (order.getBooleanField(OrderFields.FINAL_PRODUCTION_TRACKING)) {
                    plannedQuantity = basicProductionCountingService.getProducedQuantityFromBasicProductionCountings(order);
                }
                ProgressForDaysContainer progressForDaysContainer = new ProgressForDaysContainer();
                progressForDaysContainer.setShouldBeCorrected(shouldBeCorrected);
                progressForDaysContainer.setOrder(order);
                try {
                    automaticPpsExecutorService.generateProgressForDays(progressForDaysContainer, productionPerShift);
                } catch (Exception ex) {
                    for (ErrorMessage errorMessage : progressForDaysContainer.getErrors()) {
                        order.addGlobalError(errorMessage.getMessage(), false, errorMessage.getVars());
                    }
                    return;
                }
                List<Entity> progressForDays = progressForDaysContainer.getProgressForDays();
                if (progressForDaysContainer.isCalculationError()) {
                    productionPerShift.getGlobalErrors().forEach(error -> order.addGlobalError(error.getMessage(), false, error.getVars()));
                    return;
                }
                if (!progressForDaysContainer.isPartCalculation()) {
                    Date finishDate = ppsTimeHelper.calculateOrderFinishDate(order, progressForDays);
                    order.setField(OrderFields.FINISH_DATE, finishDate);
                    if (shouldBeCorrected) {
                        order.setField(OrderFields.CORRECTED_DATE_TO, finishDate);
                    } else {
                        order.setField(OrderFields.DATE_TO, finishDate);
                    }
                }
                productionPerShift.setField(ProductionPerShiftFields.PLANNED_PROGRESS_TYPE, "01planned");
                if (shouldBeCorrected) {
                    productionPerShift.setField(ProductionPerShiftFields.PLANNED_PROGRESS_TYPE, "02corrected");
                    progressForDays.addAll(productionPerShift.getHasManyField(ProductionPerShiftFields.PROGRES_FOR_DAYS).stream().filter(progressForDay -> !progressForDay.getBooleanField(ProgressForDayFields.CORRECTED)).collect(Collectors.toList()));
                }
                productionPerShift.setField(ProductionPerShiftFields.PROGRES_FOR_DAYS, progressForDays);
                productionPerShift.getDataDefinition().save(productionPerShift);
            }
        }
    }
    updateOrderData(order);
}
Also used : Entity(com.qcadoo.model.api.Entity) ProgressForDaysContainer(com.qcadoo.mes.productionPerShift.domain.ProgressForDaysContainer) ErrorMessage(com.qcadoo.model.api.validators.ErrorMessage) BigDecimal(java.math.BigDecimal) Date(java.util.Date)

Example 2 with ProgressForDaysContainer

use of com.qcadoo.mes.productionPerShift.domain.ProgressForDaysContainer in project mes by qcadoo.

the class ProductionPerShiftListeners method generateProgressForDays.

public void generateProgressForDays(final ViewDefinitionState view, final ComponentState componentState, final String[] args) {
    if (!automaticPpsParametersService.isAutomaticPlanForShiftOn()) {
        view.addMessage(new ErrorMessage("productionPerShift.automaticAlgorithm.error.ppsOff", false));
        return;
    }
    FormComponent productionPerShiftForm = (FormComponent) view.getComponentByReference(QcadooViewConstants.L_FORM);
    AwesomeDynamicListComponent progressForDaysComponent = (AwesomeDynamicListComponent) view.getComponentByReference("progressForDays");
    Entity productionPerShift = productionPerShiftForm.getPersistedEntityWithIncludedFormValues();
    ProgressForDaysContainer progressForDaysContainer = new ProgressForDaysContainer();
    try {
        automaticPpsExecutorService.generateProgressForDays(progressForDaysContainer, productionPerShift);
    } catch (Exception ex) {
        for (ErrorMessage errorMessage : progressForDaysContainer.getErrors()) {
            view.addMessage(errorMessage.getMessage(), ComponentState.MessageType.FAILURE, false, errorMessage.getVars());
        }
        if (progressForDaysContainer.getErrors().isEmpty()) {
            LOG.error("PPS generation error ", ex);
            throw new IllegalStateException();
        }
        return;
    }
    List<Entity> progressForDays = progressForDaysContainer.getProgressForDays();
    Entity order = productionPerShift.getBelongsToField(ProductionPerShiftFields.ORDER);
    Entity product = order.getBelongsToField(OrderFields.PRODUCT);
    if (progressForDaysContainer.isCalculationError()) {
        productionPerShift.getGlobalErrors().forEach(error -> view.addMessage(error.getMessage(), ComponentState.MessageType.FAILURE, false, error.getVars()));
    } else if (progressForDaysContainer.isPartCalculation()) {
        productionPerShiftForm.setEntity(productionPerShift);
        String unit = null;
        if (product != null) {
            unit = product.getStringField(ProductFields.UNIT);
        }
        progressForDaysComponent.setFieldValue(progressForDays);
        fillUnit(progressForDaysComponent, unit);
        for (PpsMessage message : progressForDaysContainer.getMessages()) {
            view.addMessage(message.getMessage(), message.getType(), false, message.getVars());
        }
        updateProgressForDays(view, componentState, args);
    } else {
        Date orderFinishDate = ppsTimeHelper.calculateOrderFinishDate(order, progressForDays);
        productionPerShift.setField(ProductionPerShiftFields.ORDER_FINISH_DATE, orderFinishDate);
        productionPerShiftForm.setEntity(productionPerShift);
        String unit = null;
        if (product != null) {
            unit = product.getStringField(ProductFields.UNIT);
        }
        progressForDaysComponent.setFieldValue(progressForDays);
        fillUnit(progressForDaysComponent, unit);
        updateProgressForDays(view, componentState, args);
    }
}
Also used : Entity(com.qcadoo.model.api.Entity) ProgressForDaysContainer(com.qcadoo.mes.productionPerShift.domain.ProgressForDaysContainer) PpsMessage(com.qcadoo.mes.productionPerShift.domain.PpsMessage) ErrorMessage(com.qcadoo.model.api.validators.ErrorMessage) Date(java.util.Date)

Aggregations

ProgressForDaysContainer (com.qcadoo.mes.productionPerShift.domain.ProgressForDaysContainer)2 Entity (com.qcadoo.model.api.Entity)2 ErrorMessage (com.qcadoo.model.api.validators.ErrorMessage)2 Date (java.util.Date)2 PpsMessage (com.qcadoo.mes.productionPerShift.domain.PpsMessage)1 BigDecimal (java.math.BigDecimal)1