Author: jonesde
Date: Fri Nov 12 00:32:18 2010 New Revision: 1034223 URL: http://svn.apache.org/viewvc?rev=1034223&view=rev Log: Added a new condition to get the difference between the list price and the base price for the order/cart item as either an amount or a percent; this condition is different from existing conditions because it operates on cart items and not the entire cart as all of the other ones do, or in other words these conditions are used to qualify cart items for other conditions and actions on the same rule Modified: ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/product/ProductPromoWorker.java ofbiz/trunk/applications/product/data/ProductTypeData.xml Modified: ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/product/ProductPromoWorker.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/product/ProductPromoWorker.java?rev=1034223&r1=1034222&r2=1034223&view=diff ============================================================================== --- ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/product/ProductPromoWorker.java (original) +++ ofbiz/trunk/applications/order/src/org/ofbiz/order/shoppingcart/product/ProductPromoWorker.java Fri Nov 12 00:32:18 2010 @@ -834,7 +834,8 @@ public class ProductPromoWorker { // only include if it is in the productId Set for this check and if it is not a Promo (GWP) item GenericValue product = cartItem.getProduct(); String parentProductId = cartItem.getParentProductId(); - if (!cartItem.getIsPromo() && + boolean passedItemConds = checkConditionsForItem(productPromoCond, cart, cartItem, delegator, dispatcher, nowTimestamp); + if (passedItemConds && !cartItem.getIsPromo() && (productIds.contains(cartItem.getProductId()) || (parentProductId != null && productIds.contains(parentProductId))) && (product == null || !"N".equals(product.getString("includeInPromotions")))) { @@ -878,7 +879,8 @@ public class ProductPromoWorker { // only include if it is in the productId Set for this check and if it is not a Promo (GWP) item GenericValue product = cartItem.getProduct(); String parentProductId = cartItem.getParentProductId(); - if (!cartItem.getIsPromo() && + boolean passedItemConds = checkConditionsForItem(productPromoCond, cart, cartItem, delegator, dispatcher, nowTimestamp); + if (passedItemConds && !cartItem.getIsPromo() && (productIds.contains(cartItem.getProductId()) || (parentProductId != null && productIds.contains(parentProductId))) && (product == null || !"N".equals(product.getString("includeInPromotions")))) { @@ -909,7 +911,8 @@ public class ProductPromoWorker { // only include if it is in the productId Set for this check and if it is not a Promo (GWP) item GenericValue product = cartItem.getProduct(); String parentProductId = cartItem.getParentProductId(); - if (!cartItem.getIsPromo() && + boolean passedItemConds = checkConditionsForItem(productPromoCond, cart, cartItem, delegator, dispatcher, nowTimestamp); + if (passedItemConds && !cartItem.getIsPromo() && (productIds.contains(cartItem.getProductId()) || (parentProductId != null && productIds.contains(parentProductId))) && (product == null || !"N".equals(product.getString("includeInPromotions")))) { // reduce quantity still needed to qualify for promo (quantityNeeded) @@ -1175,6 +1178,12 @@ public class ProductPromoWorker { if (Debug.verboseOn()) { Debug.logVerbose("Doing order total Shipping compare: ordertotalShipping=" + orderTotalShipping, module); } compareBase = orderTotalShipping.compareTo(new BigDecimal(condValue)); } + } else if ("PPIP_LPMUP_AMT".equals(inputParamEnumId)) { + // does nothing on order level, only checked on item level, so ignore by always considering passed + return true; + } else if ("PPIP_LPMUP_PER".equals(inputParamEnumId)) { + // does nothing on order level, only checked on item level, so ignore by always considering passed + return true; } else { Debug.logWarning(UtilProperties.getMessage(resource_error,"OrderAnUnSupportedProductPromoCondInputParameterLhs", UtilMisc.toMap("inputParamEnumId",productPromoCond.getString("inputParamEnumId")), cart.getLocale()), module); return false; @@ -1205,6 +1214,82 @@ public class ProductPromoWorker { return false; } + protected static boolean checkConditionsForItem(GenericValue productPromoActionOrCond, ShoppingCart cart, ShoppingCartItem cartItem, Delegator delegator, LocalDispatcher dispatcher, Timestamp nowTimestamp) throws GenericEntityException { + GenericValue productPromoRule = productPromoActionOrCond.getRelatedOneCache("ProductPromoRule"); + + List<GenericValue> productPromoConds = delegator.findByAndCache("ProductPromoCond", UtilMisc.toMap("productPromoId", productPromoRule.get("productPromoId")), UtilMisc.toList("productPromoCondSeqId")); + productPromoConds = EntityUtil.filterByAnd(productPromoConds, UtilMisc.toMap("productPromoRuleId", productPromoRule.get("productPromoRuleId"))); + for (GenericValue productPromoCond: productPromoConds) { + boolean passed = checkConditionForItem(productPromoCond, cart, cartItem, delegator, dispatcher, nowTimestamp); + if (!passed) return false; + } + return true; + } + + protected static boolean checkConditionForItem(GenericValue productPromoCond, ShoppingCart cart, ShoppingCartItem cartItem, Delegator delegator, LocalDispatcher dispatcher, Timestamp nowTimestamp) throws GenericEntityException { + String condValue = productPromoCond.getString("condValue"); + String otherValue = productPromoCond.getString("otherValue"); + String inputParamEnumId = productPromoCond.getString("inputParamEnumId"); + String operatorEnumId = productPromoCond.getString("operatorEnumId"); + + // don't get list price from cart because it may have tax included whereas the base price does not: BigDecimal listPrice = cartItem.getListPrice(); + Map<String, String> priceFindMap = UtilMisc.toMap("productId", cartItem.getProductId(), + "productPriceTypeId", "LIST_PRICE", "productPricePurposeId", "PURCHASE"); + List<GenericValue> listProductPriceList = delegator.findByAnd("ProductPrice", priceFindMap, UtilMisc.toList("-fromDate")); + listProductPriceList = EntityUtil.filterByDate(listProductPriceList, true); + GenericValue listProductPrice = (listProductPriceList != null && listProductPriceList.size() > 0) ? listProductPriceList.get(0): null; + BigDecimal listPrice = (listProductPrice != null) ? listProductPrice.getBigDecimal("price") : null; + + if (listPrice == null) { + // can't find a list price so this condition is meaningless, consider it passed + return true; + } + + BigDecimal basePrice = cartItem.getBasePrice(); + BigDecimal amountOff = listPrice.subtract(basePrice); + BigDecimal percentOff = amountOff.divide(listPrice, 2, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100L)); + + BigDecimal condValueBigDecimal = new BigDecimal(condValue); + + Integer compareBase = null; + + if ("PPIP_LPMUP_AMT".equals(inputParamEnumId)) { + compareBase = Integer.valueOf(amountOff.compareTo(condValueBigDecimal)); + } else if ("PPIP_LPMUP_PER".equals(inputParamEnumId)) { + compareBase = Integer.valueOf(percentOff.compareTo(condValueBigDecimal)); + } else { + // condition doesn't apply to individual item, always passes + return true; + } + + Debug.logInfo("Checking condition for item productId=" + cartItem.getProductId() + ", listPrice=" + listPrice + ", basePrice=" + basePrice + ", amountOff=" + amountOff + ", percentOff=" + percentOff + ", condValueBigDecimal=" + condValueBigDecimal + ", compareBase=" + compareBase + ", productPromoCond=" + productPromoCond, module); + + if (compareBase != null) { + int compare = compareBase.intValue(); + if ("PPC_EQ".equals(operatorEnumId)) { + if (compare == 0) return true; + } else if ("PPC_NEQ".equals(operatorEnumId)) { + if (compare != 0) return true; + } else if ("PPC_LT".equals(operatorEnumId)) { + if (compare < 0) return true; + } else if ("PPC_LTE".equals(operatorEnumId)) { + if (compare <= 0) return true; + } else if ("PPC_GT".equals(operatorEnumId)) { + if (compare > 0) return true; + } else if ("PPC_GTE".equals(operatorEnumId)) { + if (compare >= 0) return true; + } else { + Debug.logWarning(UtilProperties.getMessage(resource_error,"OrderAnUnSupportedProductPromoCondCondition", UtilMisc.toMap("operatorEnumId",operatorEnumId) , cart.getLocale()), module); + return false; + } + // was a compareBase and nothing returned above, so condition didn't pass, return false + return false; + } + + // no compareBase, this condition doesn't apply + return true; + } + private static int checkConditionPartyHierarchy(Delegator delegator, Timestamp nowTimestamp, String groupPartyId, String partyId) throws GenericEntityException{ List<GenericValue> partyRelationshipList = delegator.findByAndCache("PartyRelationship", UtilMisc.toMap("partyIdTo", partyId, "partyRelationshipTypeId", "GROUP_ROLLUP")); partyRelationshipList = EntityUtil.filterByDate(partyRelationshipList, nowTimestamp, null, null, true); @@ -1451,7 +1536,9 @@ public class ProductPromoWorker { // only include if it is in the productId Set for this check and if it is not a Promo (GWP) item GenericValue product = cartItem.getProduct(); String parentProductId = cartItem.getParentProductId(); - if (!cartItem.getIsPromo() && + boolean passedItemConds = checkConditionsForItem(productPromoAction, cart, cartItem, delegator, dispatcher, nowTimestamp); + // Debug.logInfo("Running promo action for cartItem " + cartItem.getName() + ", passedItemConds=" + passedItemConds + ", productPromoAction=" + productPromoAction, module); + if (passedItemConds && !cartItem.getIsPromo() && (productIds.contains(cartItem.getProductId()) || (parentProductId != null && productIds.contains(parentProductId))) && (product == null || !"N".equals(product.getString("includeInPromotions")))) { // reduce quantity still needed to qualify for promo (quantityNeeded) @@ -1496,7 +1583,8 @@ public class ProductPromoWorker { // only include if it is in the productId Set for this check and if it is not a Promo (GWP) item String parentProductId = cartItem.getParentProductId(); GenericValue product = cartItem.getProduct(); - if (!cartItem.getIsPromo() && + boolean passedItemConds = checkConditionsForItem(productPromoAction, cart, cartItem, delegator, dispatcher, nowTimestamp); + if (passedItemConds && !cartItem.getIsPromo() && (productIds.contains(cartItem.getProductId()) || (parentProductId != null && productIds.contains(parentProductId))) && (product == null || !"N".equals(product.getString("includeInPromotions")))) { // reduce quantity still needed to qualify for promo (quantityNeeded) @@ -1542,7 +1630,8 @@ public class ProductPromoWorker { // only include if it is in the productId Set for this check and if it is not a Promo (GWP) item String parentProductId = cartItem.getParentProductId(); GenericValue product = cartItem.getProduct(); - if (!cartItem.getIsPromo() && (productIds.contains(cartItem.getProductId()) || (parentProductId != null && productIds.contains(parentProductId))) && + boolean passedItemConds = checkConditionsForItem(productPromoAction, cart, cartItem, delegator, dispatcher, nowTimestamp); + if (passedItemConds && !cartItem.getIsPromo() && (productIds.contains(cartItem.getProductId()) || (parentProductId != null && productIds.contains(parentProductId))) && (product == null || !"N".equals(product.getString("includeInPromotions")))) { // reduce quantity still needed to qualify for promo (quantityNeeded) BigDecimal quantityUsed = cartItem.addPromoQuantityCandidateUse(quantityDesired, productPromoAction, false); Modified: ofbiz/trunk/applications/product/data/ProductTypeData.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/data/ProductTypeData.xml?rev=1034223&r1=1034222&r2=1034223&view=diff ============================================================================== --- ofbiz/trunk/applications/product/data/ProductTypeData.xml (original) +++ ofbiz/trunk/applications/product/data/ProductTypeData.xml Fri Nov 12 00:32:18 2010 @@ -370,8 +370,10 @@ under the License. <Enumeration description="Promotion Recurrence" enumCode="PROMO_RECURRENCE" enumId="PPIP_RECURRENCE" sequenceId="11" enumTypeId="PROD_PROMO_IN_PARAM"/> <Enumeration description="Order sub-total X since beginning of current year" enumCode="ORST_YEAR" enumId="PPIP_ORST_YEAR" sequenceId="12" enumTypeId="PROD_PROMO_IN_PARAM"/> <Enumeration description="Order sub-total X last year" enumCode="ORST_LAST_YEAR" enumId="PPIP_ORST_LAST_YEAR" sequenceId="13" enumTypeId="PROD_PROMO_IN_PARAM"/> - <Enumeration description="Shipping Total" enumCode="ORDER_SHIP_TOTAL" enumId="PPIP_ORDER_SHIPTOTAL" sequenceId="14" enumTypeId="PROD_PROMO_IN_PARAM"/> - <Enumeration description="Call Service" enumCode="SERVICE" enumId="PPIP_SERVICE" sequenceId="15" enumTypeId="PROD_PROMO_IN_PARAM"/> + <Enumeration description="List Price minus Unit Price (Amount)" enumCode="LPMUP_AMT" enumId="PPIP_LPMUP_AMT" sequenceId="14" enumTypeId="PROD_PROMO_IN_PARAM"/> + <Enumeration description="List Price minus Unit Price (Percent)" enumCode="LPMUP_PER" enumId="PPIP_LPMUP_PER" sequenceId="15" enumTypeId="PROD_PROMO_IN_PARAM"/> + <Enumeration description="Shipping Total" enumCode="ORDER_SHIP_TOTAL" enumId="PPIP_ORDER_SHIPTOTAL" sequenceId="16" enumTypeId="PROD_PROMO_IN_PARAM"/> + <Enumeration description="Call Service" enumCode="SERVICE" enumId="PPIP_SERVICE" sequenceId="17" enumTypeId="PROD_PROMO_IN_PARAM"/> <EnumerationType description="Product Promotion Condition" enumTypeId="PROD_PROMO_COND" hasTable="N" parentTypeId="PROD_PROMO"/> <!-- old style very technical ... |
Free forum by Nabble | Edit this page |