Search in sources :

Example 1 with OrdersByLevel

use of com.qcadoo.mes.ordersForSubproductsGeneration.productionScheduling.OrdersByLevel in project mes by qcadoo.

the class OperationDurationDetailsInOrderListenersOFSPGOverrideAspect method aroundGenerateRealizationTime.

@Around("generateRealizationTimeE(viewDefinitionState, state, args)")
public void aroundGenerateRealizationTime(final ProceedingJoinPoint pjp, final ViewDefinitionState viewDefinitionState, final ComponentState state, final String[] args) throws Throwable {
    CheckBoxComponent component = (CheckBoxComponent) viewDefinitionState.getComponentByReference("includeOrdersForComponent");
    if (!component.isChecked()) {
        pjp.proceed();
        return;
    }
    FormComponent orderForm = (FormComponent) viewDefinitionState.getComponentByReference(QcadooViewConstants.L_FORM);
    FieldComponent startTimeField = (FieldComponent) viewDefinitionState.getComponentByReference(L_START_TIME);
    if (!StringUtils.hasText((String) startTimeField.getFieldValue())) {
        startTimeField.addMessage(L_PRODUCTION_SCHEDULING_ERROR_FIELD_REQUIRED, ComponentState.MessageType.FAILURE);
        return;
    }
    FieldComponent plannedQuantityField = (FieldComponent) viewDefinitionState.getComponentByReference(OrderFields.PLANNED_QUANTITY);
    FieldComponent productionLineLookup = (FieldComponent) viewDefinitionState.getComponentByReference(OrderFields.PRODUCTION_LINE);
    FieldComponent generatedEndDateField = (FieldComponent) viewDefinitionState.getComponentByReference(OrderFieldsPS.GENERATED_END_DATE);
    FieldComponent calculatedFinishAllOrdersField = (FieldComponent) viewDefinitionState.getComponentByReference("calculatedFinishAllOrders");
    FieldComponent calculatedStartAllOrdersField = (FieldComponent) viewDefinitionState.getComponentByReference("calculatedStartAllOrders");
    FieldComponent includeTpzField = (FieldComponent) viewDefinitionState.getComponentByReference(OrderFieldsPS.INCLUDE_TPZ);
    FieldComponent includeAdditionalTimeField = (FieldComponent) viewDefinitionState.getComponentByReference(OrderFieldsPS.INCLUDE_ADDITIONAL_TIME);
    boolean isGenerated = false;
    Entity productionLine = dataDefinitionService.get(ProductionLinesConstants.PLUGIN_IDENTIFIER, ProductionLinesConstants.MODEL_PRODUCTION_LINE).get((Long) productionLineLookup.getFieldValue());
    Entity order = dataDefinitionService.get(OrdersConstants.PLUGIN_IDENTIFIER, OrdersConstants.MODEL_ORDER).get(orderForm.getEntity().getId());
    // copy of technology from order
    Entity technology = order.getBelongsToField(OrderFields.TECHNOLOGY);
    Validate.notNull(technology, "technology is null");
    BigDecimal quantity = orderRealizationTimeService.getBigDecimalFromField(plannedQuantityField.getFieldValue(), viewDefinitionState.getLocale());
    // Included in work time
    boolean includeTpz = "1".equals(includeTpzField.getFieldValue());
    boolean includeAdditionalTime = "1".equals(includeAdditionalTimeField.getFieldValue());
    final Map<Long, BigDecimal> operationRuns = Maps.newHashMap();
    productQuantitiesService.getProductComponentQuantities(technology, quantity, operationRuns);
    operationWorkTimeService.deleteOperCompTimeCalculations(order);
    OperationWorkTime workTime = operationWorkTimeService.estimateTotalWorkTimeForOrder(order, operationRuns, includeTpz, includeAdditionalTime, true);
    List<Entity> orders = getOrderAndSubOrders(order.getId());
    Map<Integer, OrdersByLevel> ordersByLevel = productionSchedulingForComponentsService.mapToOrdersByLevel(orders);
    List<Integer> keys = Lists.newArrayList(ordersByLevel.keySet());
    keys.sort(Collections.reverseOrder());
    Date orderStartDate = order.getDateField(OrderFields.START_DATE);
    Date lastDateTo = orderStartDate;
    for (Integer key : keys) {
        OrdersByLevel ords = ordersByLevel.get(key);
        Date currentDateTo = lastDateTo;
        for (Entity o : ords.getOrders()) {
            Entity t = o.getBelongsToField(OrderFields.TECHNOLOGY);
            final Map<Long, BigDecimal> oR = Maps.newHashMap();
            productQuantitiesService.getProductComponentQuantities(t, quantity, oR);
            operationWorkTimeService.estimateTotalWorkTimeForOrder(o, oR, includeTpz, includeAdditionalTime, true);
            int maxPathTime = orderRealizationTimeService.estimateMaxOperationTimeConsumptionForWorkstation(o, t.getTreeField(TechnologyFields.OPERATION_COMPONENTS).getRoot(), quantity, includeTpz, includeAdditionalTime, productionLine);
            if (maxPathTime > OrderRealizationTimeService.MAX_REALIZATION_TIME) {
                state.addMessage("orders.validate.global.error.RealizationTimeIsToLong", ComponentState.MessageType.FAILURE);
                if (o.getId().equals(order.getId())) {
                    generatedEndDateField.setFieldValue(null);
                }
            } else {
                o.setField(OrderFieldsPS.REALIZATION_TIME, maxPathTime);
                Date startTime = o.getDateField(OrderFields.DATE_FROM);
                if (Objects.isNull(startTime)) {
                    startTime = orderStartDate;
                }
                if (startTime == null) {
                    startTimeField.addMessage("orders.validate.global.error.dateFromIsNull", ComponentState.MessageType.FAILURE);
                } else {
                    if (maxPathTime == 0) {
                        orderForm.addMessage("productionScheduling.timenorms.isZero", ComponentState.MessageType.FAILURE, false);
                        if (o.getId().equals(order.getId())) {
                            generatedEndDateField.setFieldValue(null);
                        }
                    } else {
                        lastDateTo = scheduleOperationsInOrder(o, currentDateTo);
                        isGenerated = true;
                    }
                    orderForm.addMessage("orders.dateFrom.info.dateFromSetToFirstPossible", ComponentState.MessageType.INFO, false);
                }
                o.getDataDefinition().save(o);
            }
        }
    }
    fillWorkTimeFields(viewDefinitionState, workTime);
    generatedEndDateField.requestComponentUpdateState();
    if (isGenerated) {
        order = getActualOrderWithChanges(order);
        Entity orderTimeCalculation = dataDefinitionService.get(TimeNormsConstants.PLUGIN_PRODUCTION_SCHEDULING_IDENTIFIER, TimeNormsConstants.MODEL_ORDER_TIME_CALCULATION).find().add(SearchRestrictions.belongsTo("order", order)).setMaxResults(1).uniqueResult();
        Date startTimeOrders = findCalculatedStartAllOrders(order);
        order.setField("calculatedStartAllOrders", orderRealizationTimeService.setDateToField(startTimeOrders));
        calculatedStartAllOrdersField.setFieldValue(orderRealizationTimeService.setDateToField(startTimeOrders));
        Date finishDate = orderTimeCalculation.getDateField(OrderTimeCalculationFields.EFFECTIVE_DATE_TO);
        generatedEndDateField.setFieldValue(orderRealizationTimeService.setDateToField(finishDate));
        calculatedFinishAllOrdersField.setFieldValue(orderRealizationTimeService.setDateToField(finishDate));
        order.setField("calculatedFinishAllOrders", orderRealizationTimeService.setDateToField(finishDate));
        order = order.getDataDefinition().save(order);
        orderForm.setEntity(order);
        orderForm.addMessage("productionScheduling.info.calculationGenerated", ComponentState.MessageType.SUCCESS);
        state.performEvent(viewDefinitionState, "reset");
    }
}
Also used : FormComponent(com.qcadoo.view.api.components.FormComponent) Entity(com.qcadoo.model.api.Entity) FieldComponent(com.qcadoo.view.api.components.FieldComponent) OrdersByLevel(com.qcadoo.mes.ordersForSubproductsGeneration.productionScheduling.OrdersByLevel) BigDecimal(java.math.BigDecimal) Date(java.util.Date) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) CheckBoxComponent(com.qcadoo.view.api.components.CheckBoxComponent) OperationWorkTime(com.qcadoo.mes.operationTimeCalculations.OperationWorkTime) Around(org.aspectj.lang.annotation.Around)

Aggregations

OperationWorkTime (com.qcadoo.mes.operationTimeCalculations.OperationWorkTime)1 OrdersByLevel (com.qcadoo.mes.ordersForSubproductsGeneration.productionScheduling.OrdersByLevel)1 Entity (com.qcadoo.model.api.Entity)1 CheckBoxComponent (com.qcadoo.view.api.components.CheckBoxComponent)1 FieldComponent (com.qcadoo.view.api.components.FieldComponent)1 FormComponent (com.qcadoo.view.api.components.FormComponent)1 BigDecimal (java.math.BigDecimal)1 Date (java.util.Date)1 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)1 Around (org.aspectj.lang.annotation.Around)1