📄 productworker.java
字号:
pageContext.setAttribute(assocPrefix + "upgrade", upgradeProducts); if (complementProducts != null && complementProducts.size() > 0) pageContext.setAttribute(assocPrefix + "complement", complementProducts); if (obsolescenceProducts != null && obsolescenceProducts.size() > 0) pageContext.setAttribute(assocPrefix + "obsolescence", obsolescenceProducts); if (obsoleteByProducts != null && obsoleteByProducts.size() > 0) pageContext.setAttribute(assocPrefix + "obsoleteby", obsoleteByProducts); } catch (GenericEntityException e) { Debug.logWarning(e, module); } } /** * Gets ProductFeature GenericValue for all distinguishing features of a variant product. * Distinguishing means all features that are selectable on the corresponding virtual product and standard on the variant plus all DISTINGUISHING_FEAT assoc type features on the variant. */ public static Set getVariantDistinguishingFeatures(GenericValue variantProduct) throws GenericEntityException { if (variantProduct == null) { return new HashSet(); } if (!"Y".equals(variantProduct.getString("isVariant"))) { throw new IllegalArgumentException("Cannot get distinguishing features for a product that is not a variant (ie isVariant!=Y)."); } GenericDelegator delegator = variantProduct.getDelegator(); String virtualProductId = getVariantVirtualId(variantProduct); // find all selectable features on the virtual product that are also standard features on the variant Set distFeatures = new HashSet(); List variantDistinguishingFeatures = delegator.findByAndCache("ProductFeatureAndAppl", UtilMisc.toMap("productId", variantProduct.get("productId"), "productFeatureApplTypeId", "DISTINGUISHING_FEAT")); // Debug.logInfo("Found variantDistinguishingFeatures: " + variantDistinguishingFeatures, module); Iterator variantDistinguishingFeatureIter = UtilMisc.toIterator(EntityUtil.filterByDate(variantDistinguishingFeatures)); while (variantDistinguishingFeatureIter != null && variantDistinguishingFeatureIter.hasNext()) { GenericValue variantDistinguishingFeature = (GenericValue) variantDistinguishingFeatureIter.next(); GenericValue dummyFeature = delegator.makeValue("ProductFeature", null); dummyFeature.setAllFields(variantDistinguishingFeature, true, null, null); distFeatures.add(dummyFeature); } List virtualSelectableFeatures = delegator.findByAndCache("ProductFeatureAndAppl", UtilMisc.toMap("productId", virtualProductId, "productFeatureApplTypeId", "SELECTABLE_FEATURE")); // Debug.logInfo("Found virtualSelectableFeatures: " + virtualSelectableFeatures, module); Iterator virtualSelectableFeatureIter = UtilMisc.toIterator(EntityUtil.filterByDate(virtualSelectableFeatures)); Set virtualSelectableFeatureIds = new HashSet(); while (virtualSelectableFeatureIter != null && virtualSelectableFeatureIter.hasNext()) { GenericValue virtualSelectableFeature = (GenericValue) virtualSelectableFeatureIter.next(); virtualSelectableFeatureIds.add(virtualSelectableFeature.get("productFeatureId")); } List variantStandardFeatures = delegator.findByAndCache("ProductFeatureAndAppl", UtilMisc.toMap("productId", variantProduct.get("productId"), "productFeatureApplTypeId", "STANDARD_FEATURE")); // Debug.logInfo("Found variantStandardFeatures: " + variantStandardFeatures, module); Iterator variantStandardFeatureIter = UtilMisc.toIterator(EntityUtil.filterByDate(variantStandardFeatures)); while (variantStandardFeatureIter != null && variantStandardFeatureIter.hasNext()) { GenericValue variantStandardFeature = (GenericValue) variantStandardFeatureIter.next(); if (virtualSelectableFeatureIds.contains(variantStandardFeature.get("productFeatureId"))) { GenericValue dummyFeature = delegator.makeValue("ProductFeature", null); dummyFeature.setAllFields(variantStandardFeature, true, null, null); distFeatures.add(dummyFeature); } } return distFeatures; } /** * Get the name to show to the customer for GWP alternative options. * If the alternative is a variant, find the distinguishing features and show those instead of the name; if it is not a variant then show the PRODUCT_NAME content. */ public static String getGwpAlternativeOptionName(GenericDelegator delegator, String alternativeOptionProductId, Locale locale) { try { GenericValue alternativeOptionProduct = delegator.findByPrimaryKeyCache("Product", UtilMisc.toMap("productId", alternativeOptionProductId)); if (alternativeOptionProduct != null) { if ("Y".equals(alternativeOptionProduct.getString("isVariant"))) { Set distFeatures = getVariantDistinguishingFeatures(alternativeOptionProduct); if (distFeatures != null && distFeatures.size() > 0) { // Debug.logInfo("Found distinguishing features: " + distFeatures, module); StringBuffer nameBuf = new StringBuffer(); Iterator distFeatIter = distFeatures.iterator(); while (distFeatIter.hasNext()) { GenericValue productFeature = (GenericValue) distFeatIter.next(); GenericValue productFeatureType = productFeature.getRelatedOneCache("ProductFeatureType"); if (productFeatureType != null) { nameBuf.append(productFeatureType.get("description", locale)); nameBuf.append(":"); } nameBuf.append(productFeature.get("description", locale)); if (distFeatIter.hasNext()) { nameBuf.append(", "); } } return nameBuf.toString(); } } // got to here, default to PRODUCT_NAME String alternativeProductName = ProductContentWrapper.getProductContentAsText(alternativeOptionProduct, "PRODUCT_NAME", locale); // Debug.logInfo("Using PRODUCT_NAME: " + alternativeProductName, module); return alternativeProductName; } } catch (GenericEntityException e) { Debug.logError(e, module); } catch (Exception e) { Debug.logError(e, module); } // finally fall back to the ID in square braces return "[" + alternativeOptionProductId + "]"; } /** * gets productFeatures given a productFeatureApplTypeId * @param delegator * @param productId * @param productFeatureApplTypeId - if null, returns ALL productFeatures, regardless of applType * @return List */ public static List getProductFeaturesByApplTypeId(GenericDelegator delegator, String productId, String productFeatureApplTypeId) { if (productId == null) { return null; } try { return getProductFeaturesByApplTypeId(delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", productId)), productFeatureApplTypeId); } catch (GenericEntityException e) { Debug.logError(e, module); } return null; } public static List getProductFeaturesByApplTypeId(GenericValue product, String productFeatureApplTypeId) { if (product == null) { return null; } List features = new ArrayList(); try { if (product != null) { List productAppls; if (productFeatureApplTypeId == null) { productAppls = product.getRelated("ProductFeatureAppl"); } else { productAppls = product.getRelatedByAnd("ProductFeatureAppl", UtilMisc.toMap("productFeatureApplTypeId", productFeatureApplTypeId)); } for (int i = 0; i < productAppls.size(); i++) { GenericValue productAppl = (GenericValue)productAppls.get(i); features.add(productAppl.getRelatedOne("ProductFeature")); } features = EntityUtil.orderBy(features, UtilMisc.toList("description")); } } catch (GenericEntityException e) { Debug.logError(e, module); } return features; } public static Map getOptionalProductFeatures(GenericDelegator delegator, String productId) { Map featureMap = new LinkedMap(); List productFeatureAppls = null; try { productFeatureAppls = delegator.findByAnd("ProductFeatureAndAppl", UtilMisc.toMap("productId", productId, "productFeatureApplTypeId", "OPTIONAL_FEATURE"), UtilMisc.toList("productFeatureTypeId", "sequenceNum")); } catch (GenericEntityException e) { Debug.logError(e, module); } if (productFeatureAppls != null) { Iterator i = productFeatureAppls.iterator(); while (i.hasNext()) { GenericValue appl = (GenericValue) i.next(); String featureType = appl.getString("productFeatureTypeId"); List features = (List) featureMap.get(featureType); if (features == null) { features = new LinkedList(); } features.add(appl); featureMap.put(featureType, features); } } return featureMap; } // product calc methods public static double calcOrderAdjustments(List orderHeaderAdjustments, double subTotal, boolean includeOther, boolean includeTax, boolean includeShipping) { double adjTotal = 0.0; if (orderHeaderAdjustments != null && orderHeaderAdjustments.size() > 0) { List filteredAdjs = filterOrderAdjustments(orderHeaderAdjustments, includeOther, includeTax, includeShipping, false, false); Iterator adjIt = filteredAdjs.iterator(); while (adjIt.hasNext()) { GenericValue orderAdjustment = (GenericValue) adjIt.next(); adjTotal += calcOrderAdjustment(orderAdjustment, subTotal); } } return adjTotal; } public static double calcOrderAdjustment(GenericValue orderAdjustment, double orderSubTotal) { double adjustment = 0.0; if (orderAdjustment.get("amount") != null) { adjustment += orderAdjustment.getDouble("amount").doubleValue(); } if (orderAdjustment.get("percentage") != null) { adjustment += (orderAdjustment.getDouble("percentage").doubleValue() * orderSubTotal); } return adjustment; } public static List filterOrderAdjustments(List adjustments, boolean includeOther, boolean includeTax, boolean includeShipping, boolean forTax, boolean forShipping) { List newOrderAdjustmentsList = new LinkedList(); if (adjustments != null && adjustments.size() > 0) { Iterator adjIt = adjustments.iterator(); while (adjIt.hasNext()) { GenericValue orderAdjustment = (GenericValue) adjIt.next(); boolean includeAdjustment = false; if ("SALES_TAX".equals(orderAdjustment.getString("orderAdjustmentTypeId"))) { if (includeTax) includeAdjustment = true; } else if ("SHIPPING_CHARGES".equals(orderAdjustment.getString("orderAdjustmentTypeId"))) { if (includeShipping) includeAdjustment = true; } else { if (includeOther) includeAdjustment = true; } // default to yes, include for shipping; so only exclude if includeInShipping is N, or false; if Y or null or anything else it will be included if (forTax && "N".equals(orderAdjustment.getString("includeInTax"))) { includeAdjustment = false; } // default to yes, include for shipping; so only exclude if includeInShipping is N, or false; if Y or null or anything else it will be included if (forShipping && "N".equals(orderAdjustment.getString("includeInShipping"))) { includeAdjustment = false; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -