Search in sources :

Example 1 with ConfigOption

use of org.apache.ofbiz.product.config.ProductConfigWrapper.ConfigOption in project ofbiz-framework by apache.

the class ProductWorker method isProductInventoryAvailableByFacility.

/**
 * Invokes the getInventoryAvailableByFacility service, returns true if specified quantity is available for all the selected parts, else false.
 * Also, set the available flag for all the product configuration's options.
 */
public static boolean isProductInventoryAvailableByFacility(ProductConfigWrapper productConfig, String inventoryFacilityId, BigDecimal quantity, LocalDispatcher dispatcher) {
    boolean available = true;
    List<ConfigOption> options = productConfig.getSelectedOptions();
    for (ConfigOption ci : options) {
        List<GenericValue> products = ci.getComponents();
        for (GenericValue product : products) {
            String productId = product.getString("productId");
            BigDecimal cmpQuantity = product.getBigDecimal("quantity");
            BigDecimal neededQty = BigDecimal.ZERO;
            if (cmpQuantity != null) {
                neededQty = quantity.multiply(cmpQuantity);
            }
            if (!isProductInventoryAvailableByFacility(productId, inventoryFacilityId, neededQty, dispatcher)) {
                ci.setAvailable(false);
            }
        }
        if (!ci.isAvailable()) {
            available = false;
        }
    }
    return available;
}
Also used : ConfigOption(org.apache.ofbiz.product.config.ProductConfigWrapper.ConfigOption) GenericValue(org.apache.ofbiz.entity.GenericValue) BigDecimal(java.math.BigDecimal)

Example 2 with ConfigOption

use of org.apache.ofbiz.product.config.ProductConfigWrapper.ConfigOption in project ofbiz-framework by apache.

the class ProductionRunServices method createProductionRunFromConfiguration.

public static Map<String, Object> createProductionRunFromConfiguration(DispatchContext ctx, Map<String, ? extends Object> context) {
    Map<String, Object> result = new HashMap<String, Object>();
    Delegator delegator = ctx.getDelegator();
    LocalDispatcher dispatcher = ctx.getDispatcher();
    Locale locale = (Locale) context.get("locale");
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    // Mandatory input fields
    String facilityId = (String) context.get("facilityId");
    // Optional input fields
    String configId = (String) context.get("configId");
    ProductConfigWrapper config = (ProductConfigWrapper) context.get("config");
    BigDecimal quantity = (BigDecimal) context.get("quantity");
    String orderId = (String) context.get("orderId");
    String orderItemSeqId = (String) context.get("orderItemSeqId");
    if (config == null && configId == null) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ManufacturingConfigurationNotAvailable", locale));
    }
    if (config == null && configId != null) {
        // TODO: load the configuration
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ManufacturingProductionRunFromConfigurationNotYetImplemented", locale));
    }
    if (!config.isCompleted()) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ManufacturingProductionRunFromConfigurationNotValid", locale));
    }
    if (quantity == null) {
        quantity = BigDecimal.ONE;
    }
    String instanceProductId = null;
    try {
        instanceProductId = ProductWorker.getAggregatedInstanceId(delegator, config.getProduct().getString("productId"), config.getConfigId());
    } catch (Exception e) {
        return ServiceUtil.returnError(e.getMessage());
    }
    Map<String, Object> serviceContext = new HashMap<String, Object>();
    serviceContext.clear();
    serviceContext.put("productId", instanceProductId);
    serviceContext.put("pRQuantity", quantity);
    serviceContext.put("startDate", UtilDateTime.nowTimestamp());
    serviceContext.put("facilityId", facilityId);
    serviceContext.put("userLogin", userLogin);
    Map<String, Object> serviceResult = null;
    try {
        serviceResult = dispatcher.runSync("createProductionRun", serviceContext);
        if (ServiceUtil.isError(serviceResult)) {
            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ManufacturingProductionRunNotCreated", locale));
        }
    } catch (GenericServiceException e) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ManufacturingProductionRunNotCreated", locale));
    }
    String productionRunId = (String) serviceResult.get("productionRunId");
    result.put("productionRunId", productionRunId);
    Map<String, BigDecimal> components = new HashMap<String, BigDecimal>();
    for (ConfigOption co : config.getSelectedOptions()) {
        for (GenericValue selComponent : co.getComponents()) {
            BigDecimal componentQuantity = null;
            if (selComponent.get("quantity") != null) {
                componentQuantity = selComponent.getBigDecimal("quantity");
            }
            if (componentQuantity == null) {
                componentQuantity = BigDecimal.ONE;
            }
            String componentProductId = selComponent.getString("productId");
            if (co.isVirtualComponent(selComponent)) {
                Map<String, String> componentOptions = co.getComponentOptions();
                if (UtilValidate.isNotEmpty(componentOptions) && UtilValidate.isNotEmpty(componentOptions.get(componentProductId))) {
                    componentProductId = componentOptions.get(componentProductId);
                }
            }
            componentQuantity = quantity.multiply(componentQuantity);
            if (components.containsKey(componentProductId)) {
                BigDecimal totalQuantity = components.get(componentProductId);
                componentQuantity = totalQuantity.add(componentQuantity);
            }
            // check if a bom exists
            List<GenericValue> bomList = null;
            try {
                bomList = EntityQuery.use(delegator).from("ProductAssoc").where("productId", componentProductId, "productAssocTypeId", "MANUF_COMPONENT").filterByDate().queryList();
            } catch (GenericEntityException e) {
                return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ManufacturingProductionRunTryToGetBomListError", locale));
            }
            // if so create a mandatory predecessor to this production run
            if (UtilValidate.isNotEmpty(bomList)) {
                serviceContext.clear();
                serviceContext.put("productId", componentProductId);
                serviceContext.put("quantity", componentQuantity);
                serviceContext.put("startDate", UtilDateTime.nowTimestamp());
                serviceContext.put("facilityId", facilityId);
                serviceContext.put("userLogin", userLogin);
                serviceResult = null;
                try {
                    serviceResult = dispatcher.runSync("createProductionRunsForProductBom", serviceContext);
                    if (ServiceUtil.isError(serviceResult)) {
                        return ServiceUtil.returnError(ServiceUtil.getErrorMessage(serviceResult));
                    }
                    GenericValue workEffortPreDecessor = delegator.makeValue("WorkEffortAssoc", UtilMisc.toMap("workEffortIdTo", productionRunId, "workEffortIdFrom", serviceResult.get("productionRunId"), "workEffortAssocTypeId", "WORK_EFF_PRECEDENCY", "fromDate", UtilDateTime.nowTimestamp()));
                    workEffortPreDecessor.create();
                } catch (GenericServiceException e) {
                    return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ManufacturingProductionRunNotCreated", locale));
                } catch (GenericEntityException e) {
                    return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ManufacturingProductionRunTryToCreateWorkEffortAssoc", locale));
                }
            } else {
                components.put(componentProductId, componentQuantity);
            }
            // create production run notes from comments
            String comments = co.getComments();
            if (UtilValidate.isNotEmpty(comments)) {
                serviceResult.clear();
                serviceContext.clear();
                serviceContext.put("workEffortId", productionRunId);
                serviceContext.put("internalNote", "Y");
                serviceContext.put("noteInfo", comments);
                serviceContext.put("noteName", co.getDescription());
                serviceContext.put("userLogin", userLogin);
                serviceContext.put("noteParty", userLogin.getString("partyId"));
                try {
                    serviceResult = dispatcher.runSync("createWorkEffortNote", serviceContext);
                    if (ServiceUtil.isError(serviceResult)) {
                        return ServiceUtil.returnError(ServiceUtil.getErrorMessage(serviceResult));
                    }
                } catch (GenericServiceException e) {
                    Debug.logWarning(e.getMessage(), module);
                    return ServiceUtil.returnError(e.getMessage());
                }
            }
        }
    }
    for (Map.Entry<String, BigDecimal> component : components.entrySet()) {
        String productId = component.getKey();
        BigDecimal componentQuantity = component.getValue();
        if (componentQuantity == null) {
            componentQuantity = BigDecimal.ONE;
        }
        serviceResult = null;
        serviceContext = new HashMap<String, Object>();
        serviceContext.put("productionRunId", productionRunId);
        serviceContext.put("productId", productId);
        serviceContext.put("estimatedQuantity", componentQuantity);
        serviceContext.put("userLogin", userLogin);
        try {
            serviceResult = dispatcher.runSync("addProductionRunComponent", serviceContext);
            if (ServiceUtil.isError(serviceResult)) {
                return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ManufacturingProductionRunNotCreated", locale));
            }
        } catch (GenericServiceException e) {
            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ManufacturingProductionRunNotCreated", locale));
        }
    }
    try {
        if (productionRunId != null && orderId != null && orderItemSeqId != null) {
            delegator.create("WorkOrderItemFulfillment", UtilMisc.toMap("workEffortId", productionRunId, "orderId", orderId, "orderItemSeqId", orderItemSeqId));
        }
    } catch (GenericEntityException e) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ManufacturingRequirementNotDeleted", locale));
    }
    result.put(ModelService.SUCCESS_MESSAGE, UtilProperties.getMessage(resource, "ManufacturingProductionRunCreated", UtilMisc.toMap("productionRunId", productionRunId), locale));
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) HashMap(java.util.HashMap) ProductConfigWrapper(org.apache.ofbiz.product.config.ProductConfigWrapper) BigDecimal(java.math.BigDecimal) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GeneralException(org.apache.ofbiz.base.util.GeneralException) ConfigOption(org.apache.ofbiz.product.config.ProductConfigWrapper.ConfigOption) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Example 3 with ConfigOption

use of org.apache.ofbiz.product.config.ProductConfigWrapper.ConfigOption in project ofbiz-framework by apache.

the class ProductConfigWorker method storeProductConfigWrapper.

/**
 * First search persisted configurations and update configWrapper.configId if found.
 * Otherwise store ProductConfigWrapper to ProductConfigConfig entity and updates configWrapper.configId with new configId
 * This method persists only the selected options, price data is lost.
 * @param configWrapper the ProductConfigWrapper object
 * @param delegator the delegator
 */
public static void storeProductConfigWrapper(ProductConfigWrapper configWrapper, Delegator delegator) {
    if (configWrapper == null || (!configWrapper.isCompleted())) {
        return;
    }
    String configId = null;
    List<ConfigItem> questions = configWrapper.getQuestions();
    List<GenericValue> configsToCheck = new LinkedList<>();
    int selectedOptionSize = 0;
    for (ConfigItem ci : questions) {
        String configItemId = null;
        Long sequenceNum = null;
        List<ProductConfigWrapper.ConfigOption> selectedOptions = new LinkedList<>();
        List<ConfigOption> options = ci.getOptions();
        if (ci.isStandard()) {
            selectedOptions.addAll(options);
        } else {
            for (ConfigOption oneOption : options) {
                if (oneOption.isSelected()) {
                    selectedOptions.add(oneOption);
                }
            }
        }
        if (selectedOptions.size() > 0) {
            selectedOptionSize += selectedOptions.size();
            configItemId = ci.getConfigItemAssoc().getString("configItemId");
            sequenceNum = ci.getConfigItemAssoc().getLong("sequenceNum");
            try {
                List<GenericValue> configs = EntityQuery.use(delegator).from("ProductConfigConfig").where("configItemId", configItemId, "sequenceNum", sequenceNum).queryList();
                for (GenericValue productConfigConfig : configs) {
                    for (ConfigOption oneOption : selectedOptions) {
                        String configOptionId = oneOption.configOption.getString("configOptionId");
                        if (productConfigConfig.getString("configOptionId").equals(configOptionId)) {
                            String comments = oneOption.getComments() != null ? oneOption.getComments() : "";
                            if ((UtilValidate.isEmpty(comments) && UtilValidate.isEmpty(productConfigConfig.getString("description"))) || comments.equals(productConfigConfig.getString("description"))) {
                                configsToCheck.add(productConfigConfig);
                            }
                        }
                    }
                }
            } catch (GenericEntityException e) {
                Debug.logError(e, module);
            }
        }
    }
    if (UtilValidate.isNotEmpty(configsToCheck)) {
        for (GenericValue productConfigConfig : configsToCheck) {
            String tempConfigId = productConfigConfig.getString("configId");
            try {
                List<GenericValue> tempResult = EntityQuery.use(delegator).from("ProductConfigConfig").where("configId", tempConfigId).queryList();
                if (tempResult.size() == selectedOptionSize && configsToCheck.containsAll(tempResult)) {
                    List<GenericValue> configOptionProductOptions = EntityQuery.use(delegator).from("ConfigOptionProductOption").where("configId", tempConfigId).queryList();
                    if (UtilValidate.isNotEmpty(configOptionProductOptions)) {
                        // check for variant product equality
                        for (ConfigItem ci : questions) {
                            String configItemId = null;
                            Long sequenceNum = null;
                            List<ProductConfigWrapper.ConfigOption> selectedOptions = new LinkedList<>();
                            List<ConfigOption> options = ci.getOptions();
                            if (ci.isStandard()) {
                                selectedOptions.addAll(options);
                            } else {
                                for (ConfigOption oneOption : options) {
                                    if (oneOption.isSelected()) {
                                        selectedOptions.add(oneOption);
                                    }
                                }
                            }
                            boolean match = true;
                            for (ProductConfigWrapper.ConfigOption anOption : selectedOptions) {
                                if (match && anOption.hasVirtualComponent()) {
                                    List<GenericValue> components = anOption.getComponents();
                                    for (GenericValue aComponent : components) {
                                        if (anOption.isVirtualComponent(aComponent)) {
                                            Map<String, String> componentOptions = anOption.getComponentOptions();
                                            String optionProductId = aComponent.getString("productId");
                                            String optionProductOptionId = null;
                                            if (UtilValidate.isNotEmpty(componentOptions)) {
                                                optionProductOptionId = componentOptions.get(optionProductId);
                                            }
                                            String configOptionId = anOption.configOption.getString("configOptionId");
                                            configItemId = ci.getConfigItemAssoc().getString("configItemId");
                                            sequenceNum = ci.getConfigItemAssoc().getLong("sequenceNum");
                                            GenericValue configOptionProductOption = delegator.makeValue("ConfigOptionProductOption");
                                            configOptionProductOption.set("configId", tempConfigId);
                                            configOptionProductOption.set("configItemId", configItemId);
                                            configOptionProductOption.set("sequenceNum", sequenceNum);
                                            configOptionProductOption.set("configOptionId", configOptionId);
                                            configOptionProductOption.set("productId", optionProductId);
                                            configOptionProductOption.set("productOptionId", optionProductOptionId);
                                            if (!configOptionProductOptions.remove(configOptionProductOption)) {
                                                match = false;
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                            if (match && (UtilValidate.isEmpty(configOptionProductOptions))) {
                                configWrapper.configId = tempConfigId;
                                Debug.logInfo("Existing configuration found with configId:" + tempConfigId, module);
                                return;
                            }
                        }
                    } else {
                        configWrapper.configId = tempConfigId;
                        Debug.logInfo("Existing configuration found with configId:" + tempConfigId, module);
                        return;
                    }
                }
            } catch (GenericEntityException e) {
                Debug.logError(e, module);
            }
        }
    }
    // Current configuration is not found in ProductConfigConfig entity. So lets store this one
    boolean nextId = true;
    for (ConfigItem ci : questions) {
        String configItemId = null;
        Long sequenceNum = null;
        List<ProductConfigWrapper.ConfigOption> selectedOptions = new LinkedList<>();
        List<ConfigOption> options = ci.getOptions();
        if (ci.isStandard()) {
            selectedOptions.addAll(options);
        } else {
            for (ConfigOption oneOption : options) {
                if (oneOption.isSelected()) {
                    selectedOptions.add(oneOption);
                }
            }
        }
        if (selectedOptions.size() > 0) {
            if (nextId) {
                configId = delegator.getNextSeqId("ProductConfigConfig");
                // get next configId only once and only if there are selectedOptions
                nextId = false;
            }
            configItemId = ci.getConfigItemAssoc().getString("configItemId");
            sequenceNum = ci.getConfigItemAssoc().getLong("sequenceNum");
            for (ConfigOption oneOption : selectedOptions) {
                Map<String, String> componentOptions = oneOption.componentOptions;
                List<GenericValue> toBeStored = new LinkedList<>();
                String configOptionId = oneOption.configOption.getString("configOptionId");
                String description = oneOption.getComments();
                GenericValue productConfigConfig = delegator.makeValue("ProductConfigConfig");
                productConfigConfig.put("configId", configId);
                productConfigConfig.put("configItemId", configItemId);
                productConfigConfig.put("sequenceNum", sequenceNum);
                productConfigConfig.put("configOptionId", configOptionId);
                productConfigConfig.put("description", description);
                toBeStored.add(productConfigConfig);
                if (oneOption.hasVirtualComponent()) {
                    List<GenericValue> components = oneOption.getComponents();
                    for (GenericValue component : components) {
                        if (oneOption.isVirtualComponent(component) && UtilValidate.isNotEmpty(componentOptions)) {
                            String componentOption = componentOptions.get(component.getString("productId"));
                            GenericValue configOptionProductOption = delegator.makeValue("ConfigOptionProductOption");
                            configOptionProductOption.put("configId", configId);
                            configOptionProductOption.put("configItemId", configItemId);
                            configOptionProductOption.put("sequenceNum", sequenceNum);
                            configOptionProductOption.put("configOptionId", configOptionId);
                            configOptionProductOption.put("productId", component.getString("productId"));
                            configOptionProductOption.put("productOptionId", componentOption);
                            toBeStored.add(configOptionProductOption);
                        }
                    }
                }
                try {
                    delegator.storeAll(toBeStored);
                } catch (GenericEntityException e) {
                    configId = null;
                    Debug.logWarning(e.getMessage(), module);
                }
            }
        }
    }
    // save  configId to configWrapper, so we can use it in shopping cart operations
    configWrapper.configId = configId;
    Debug.logInfo("New configId created:" + configId, module);
    return;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) ConfigItem(org.apache.ofbiz.product.config.ProductConfigWrapper.ConfigItem) LinkedList(java.util.LinkedList) ConfigOption(org.apache.ofbiz.product.config.ProductConfigWrapper.ConfigOption) ConfigOption(org.apache.ofbiz.product.config.ProductConfigWrapper.ConfigOption) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Aggregations

GenericValue (org.apache.ofbiz.entity.GenericValue)3 ConfigOption (org.apache.ofbiz.product.config.ProductConfigWrapper.ConfigOption)3 BigDecimal (java.math.BigDecimal)2 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)2 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Locale (java.util.Locale)1 Map (java.util.Map)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 GeneralException (org.apache.ofbiz.base.util.GeneralException)1 Delegator (org.apache.ofbiz.entity.Delegator)1 ProductConfigWrapper (org.apache.ofbiz.product.config.ProductConfigWrapper)1 ConfigItem (org.apache.ofbiz.product.config.ProductConfigWrapper.ConfigItem)1 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)1 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)1