⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 priceservices.java

📁 国外的一套开源CRM
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
            }
            if (promoPriceValue == null) {
                List virtualTempPrices = EntityUtil.filterByAnd(virtualProductPrices, UtilMisc.toMap("productPriceTypeId", "PROMO_PRICE"));
                promoPriceValue = EntityUtil.getFirst(virtualTempPrices);
                if (virtualTempPrices != null && virtualTempPrices.size() > 1) {
                    if (Debug.infoOn()) Debug.logInfo("There is more than one PROMO_PRICE with the currencyUomId " + currencyUomId + " and productId " + virtualProductId + ", using the latest found with price: " + promoPriceValue.getDouble("price"), module);
                }
            }
            if (minimumPriceValue == null) {
                List virtualTempPrices = EntityUtil.filterByAnd(virtualProductPrices, UtilMisc.toMap("productPriceTypeId", "MINIMUM_PRICE"));
                minimumPriceValue = EntityUtil.getFirst(virtualTempPrices);
                if (virtualTempPrices != null && virtualTempPrices.size() > 1) {
                    if (Debug.infoOn()) Debug.logInfo("There is more than one MINIMUM_PRICE with the currencyUomId " + currencyUomId + " and productId " + virtualProductId + ", using the latest found with price: " + minimumPriceValue.getDouble("price"), module);
                }
            }
            if (maximumPriceValue == null) {
                List virtualTempPrices = EntityUtil.filterByAnd(virtualProductPrices, UtilMisc.toMap("productPriceTypeId", "MAXIMUM_PRICE"));
                maximumPriceValue = EntityUtil.getFirst(virtualTempPrices);
                if (virtualTempPrices != null && virtualTempPrices.size() > 1) {
                    if (Debug.infoOn()) Debug.logInfo("There is more than one MAXIMUM_PRICE with the currencyUomId " + currencyUomId + " and productId " + virtualProductId + ", using the latest found with price: " + maximumPriceValue.getDouble("price"), module);
                }
            }
        }
        
        // now if this is a virtual product check each price type, if doesn't exist get from variant with lowest DEFAULT_PRICE
        if ("Y".equals(product.getString("isVirtual"))) {
        	// only do this if there is no default price, consider the others optional for performance reasons
        	if (defaultPriceValue == null) {
        		// Debug.logInfo("Product isVirtual and there is no default price for ID " + productId + ", trying variant prices", module);
        		
        		//use the cache to find the variant with the lowest default price
        		try {
	        		List variantAssocList = EntityUtil.filterByDate(delegator.findByAndCache("ProductAssoc", UtilMisc.toMap("productId", product.get("productId"), "productAssocTypeId", "PRODUCT_VARIANT"), UtilMisc.toList("-fromDate")));
	        		Iterator variantAssocIter = variantAssocList.iterator();
	        		double minDefaultPrice = Double.MAX_VALUE;
	        		List variantProductPrices = null;
	        		String variantProductId = null;
	        		while (variantAssocIter.hasNext()) {
	        			GenericValue variantAssoc = (GenericValue) variantAssocIter.next();
	        			String curVariantProductId = variantAssoc.getString("productIdTo");
	        			List curVariantPriceList = EntityUtil.filterByDate(delegator.findByAndCache("ProductPrice", UtilMisc.toMap("productId", curVariantProductId), UtilMisc.toList("-fromDate")), nowTimestamp);
	        			List tempDefaultPriceList = EntityUtil.filterByAnd(curVariantPriceList, UtilMisc.toMap("productPriceTypeId", "DEFAULT_PRICE"));
	        			GenericValue curDefaultPriceValue = EntityUtil.getFirst(tempDefaultPriceList);
	        			if (curDefaultPriceValue != null) {
	        				Double curDefaultPrice = curDefaultPriceValue.getDouble("price");
	        				if (curDefaultPrice.doubleValue() < minDefaultPrice) {
	        					// check to see if the product is discontinued for sale before considering it the lowest price
	        					GenericValue curVariantProduct = delegator.findByPrimaryKeyCache("Product", UtilMisc.toMap("productId", curVariantProductId));
	        					if (curVariantProduct != null) {
	        						Timestamp salesDiscontinuationDate = curVariantProduct.getTimestamp("salesDiscontinuationDate");
	        						if (salesDiscontinuationDate == null || salesDiscontinuationDate.after(nowTimestamp)) {
			        					minDefaultPrice = curDefaultPrice.doubleValue();
			        					variantProductPrices = curVariantPriceList;
			        					variantProductId = curVariantProductId;
			        					// Debug.logInfo("Found new lowest price " + minDefaultPrice + " for variant with ID " + variantProductId, module);
	        						}
	        					}
	        				}
	        			}
	        		}
	        		
	        		if (variantProductPrices != null) {
	        			// we have some other options, give 'em a go...
	                    if (listPriceValue == null) {
	                        List virtualTempPrices = EntityUtil.filterByAnd(variantProductPrices, UtilMisc.toMap("productPriceTypeId", "LIST_PRICE"));
	                        listPriceValue = EntityUtil.getFirst(virtualTempPrices);
	                        if (virtualTempPrices != null && virtualTempPrices.size() > 1) {
	                            if (Debug.infoOn()) Debug.logInfo("There is more than one LIST_PRICE with the currencyUomId " + currencyUomId + " and productId " + variantProductId + ", using the latest found with price: " + listPriceValue.getDouble("price"), module);
	                        }
	                    }
	                    if (defaultPriceValue == null) {
	                        List virtualTempPrices = EntityUtil.filterByAnd(variantProductPrices, UtilMisc.toMap("productPriceTypeId", "DEFAULT_PRICE"));
	                        defaultPriceValue = EntityUtil.getFirst(virtualTempPrices);
	                        if (virtualTempPrices != null && virtualTempPrices.size() > 1) {
	                            if (Debug.infoOn()) Debug.logInfo("There is more than one DEFAULT_PRICE with the currencyUomId " + currencyUomId + " and productId " + variantProductId + ", using the latest found with price: " + defaultPriceValue.getDouble("price"), module);
	                        }
	                    }
	                    if (averageCostValue == null) {
	                        List virtualTempPrices = EntityUtil.filterByAnd(variantProductPrices, UtilMisc.toMap("productPriceTypeId", "AVERAGE_COST"));
	                        averageCostValue = EntityUtil.getFirst(virtualTempPrices);
	                        if (virtualTempPrices != null && virtualTempPrices.size() > 1) {
	                            if (Debug.infoOn()) Debug.logInfo("There is more than one AVERAGE_COST with the currencyUomId " + currencyUomId + " and productId " + variantProductId + ", using the latest found with price: " + averageCostValue.getDouble("price"), module);
	                        }
	                    }
	                    if (promoPriceValue == null) {
	                        List virtualTempPrices = EntityUtil.filterByAnd(variantProductPrices, UtilMisc.toMap("productPriceTypeId", "PROMO_PRICE"));
	                        promoPriceValue = EntityUtil.getFirst(virtualTempPrices);
	                        if (virtualTempPrices != null && virtualTempPrices.size() > 1) {
	                            if (Debug.infoOn()) Debug.logInfo("There is more than one PROMO_PRICE with the currencyUomId " + currencyUomId + " and productId " + variantProductId + ", using the latest found with price: " + promoPriceValue.getDouble("price"), module);
	                        }
	                    }
	                    if (minimumPriceValue == null) {
	                        List virtualTempPrices = EntityUtil.filterByAnd(variantProductPrices, UtilMisc.toMap("productPriceTypeId", "MINIMUM_PRICE"));
	                        minimumPriceValue = EntityUtil.getFirst(virtualTempPrices);
	                        if (virtualTempPrices != null && virtualTempPrices.size() > 1) {
	                            if (Debug.infoOn()) Debug.logInfo("There is more than one MINIMUM_PRICE with the currencyUomId " + currencyUomId + " and productId " + variantProductId + ", using the latest found with price: " + minimumPriceValue.getDouble("price"), module);
	                        }
	                    }
	                    if (maximumPriceValue == null) {
	                        List virtualTempPrices = EntityUtil.filterByAnd(variantProductPrices, UtilMisc.toMap("productPriceTypeId", "MAXIMUM_PRICE"));
	                        maximumPriceValue = EntityUtil.getFirst(virtualTempPrices);
	                        if (virtualTempPrices != null && virtualTempPrices.size() > 1) {
	                            if (Debug.infoOn()) Debug.logInfo("There is more than one MAXIMUM_PRICE with the currencyUomId " + currencyUomId + " and productId " + variantProductId + ", using the latest found with price: " + maximumPriceValue.getDouble("price"), module);
	                        }
	                    }
	        		}
        		} catch (GenericEntityException e) {
                    Debug.logError(e, "An error occurred while getting the product prices", module);
        		}
        	}
        }

        double promoPrice = (promoPriceValue != null && promoPriceValue.get("price") != null) ? promoPriceValue.getDouble("price").doubleValue() : 0;
        double defaultPrice = (defaultPriceValue != null && defaultPriceValue.get("price") != null) ? defaultPriceValue.getDouble("price").doubleValue() : 0;
        Double listPriceDbl = listPriceValue != null ? listPriceValue.getDouble("price") : null;

        if (listPriceDbl == null) {
            // no list price, use defaultPrice for the final price

            // ========= ensure calculated price is not below minSalePrice or above maxSalePrice =========
            Double maxSellPrice = maximumPriceValue != null ? maximumPriceValue.getDouble("price") : null;

            if (maxSellPrice != null && defaultPrice > maxSellPrice.doubleValue()) {
                defaultPrice = maxSellPrice.doubleValue();
            }
            // min price second to override max price, safety net
            Double minSellPrice = minimumPriceValue != null ? minimumPriceValue.getDouble("price") : null;

            if (minSellPrice != null && defaultPrice < minSellPrice.doubleValue()) {
                defaultPrice = minSellPrice.doubleValue();
            }

            result.put("price", new Double(defaultPrice));
            result.put("defaultPrice", new Double(defaultPrice));
            result.put("averageCost", averageCostValue != null ? averageCostValue.getDouble("price") : null);
        } else {
            try {
                // get some of the base values to calculate with
                double listPrice = listPriceDbl.doubleValue();
                double averageCost = (averageCostValue != null && averageCostValue.get("price") != null) ? averageCostValue.getDouble("price").doubleValue() : listPrice;
                double margin = listPrice - averageCost;

                // calculate running sum based on listPrice and rules found
                double price = listPrice;

                Collection productPriceRules = null;

                // At this point we have two options: optimize for large ruleset, or optimize for small ruleset
                // NOTE: This only effects the way that the rules to be evaluated are selected.
                // For large rule sets we can do a cached pre-filter to limit the rules that need to be evaled for a specific product.
                // Genercally I don't think that rule sets will get that big though, so the default is optimize for smaller rule set.
                if (optimizeForLargeRuleSet) {
                    // ========= find all rules that must be run for each input type; this is kind of like a pre-filter to slim down the rules to run =========
                    // utilTimer.timerString("Before create rule id list", module);
                    TreeSet productPriceRuleIds = new TreeSet();

                    // ------- These are all of the conditions that DON'T depend on the current inputs -------

                    // by productCategoryId
                    // for we will always include any rules that go by category, shouldn't be too many to iterate through each time and will save on cache entries
                    // note that we always want to put the category, quantity, etc ones that find all rules with these conditions in separate cache lists so that they can be easily cleared
                    Collection productCategoryIdConds = delegator.findByAndCache("ProductPriceCond",
                            UtilMisc.toMap("inputParamEnumId", "PRIP_PROD_CAT_ID"));

                    if (productCategoryIdConds != null && productCategoryIdConds.size() > 0) {
                        Iterator productCategoryIdCondsIter = productCategoryIdConds.iterator();

                        while (productCategoryIdCondsIter.hasNext()) {
                            GenericValue productCategoryIdCond = (GenericValue) productCategoryIdCondsIter.next();

                            productPriceRuleIds.add(productCategoryIdCond.getString("productPriceRuleId"));
                        }
                    }

                    // by quantity -- should we really do this one, ie is it necessary?
                    // we could say that all rules with quantity on them must have one of these other values
                    // but, no we'll do it the other way, any that have a quantity will always get compared
                    Collection quantityConds = delegator.findByAndCache("ProductPriceCond",
                            UtilMisc.toMap("inputParamEnumId", "PRIP_QUANTITY"));
                    if (quantityConds != null && quantityConds.size() > 0) {
                        Iterator quantityCondsIter = quantityConds.iterator();
                        while (quantityCondsIter.hasNext()) {
                            GenericValue quantityCond = (GenericValue) quantityCondsIter.next();
                            productPriceRuleIds.add(quantityCond.getString("productPriceRuleId"));
                        }
                    }

                    // by roleTypeId
                    Collection roleTypeIdConds = delegator.findByAndCache("ProductPriceCond",
                            UtilMisc.toMap("inputParamEnumId", "PRIP_ROLE_TYPE"));
                    if (roleTypeIdConds != null && roleTypeIdConds.size() > 0) {
                        Iterator roleTypeIdCondsIter = roleTypeIdConds.iterator();
                        while (roleTypeIdCondsIter.hasNext()) {
                            GenericValue roleTypeIdCond = (GenericValue) roleTypeIdCondsIter.next();
                            productPriceRuleIds.add(roleTypeIdCond.getString("productPriceRuleId"));
                        }
                    }

                    // TODO, not supported yet: by groupPartyId
                    // TODO, not supported yet: by partyClassificationGroupId
                    // later: (by partyClassificationTypeId)

                    // by listPrice
                    Collection listPriceConds = delegator.findByAndCache("ProductPriceCond",
                            UtilMisc.toMap("inputParamEnumId", "PRIP_LIST_PRICE"));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -