📄 productpromoworker.java
字号:
while (productIdIter.hasNext()) {
String productId = (String) productIdIter.next();
// if a ProductCategoryMember exists for this productId and the specified productCategoryId
List productCategoryMembers = delegator.findByAndCache("ProductCategoryMember", UtilMisc.toMap("productId", productId, "productCategoryId", productCategoryId));
// and from/thru date within range
productCategoryMembers = EntityUtil.filterByDate(productCategoryMembers, nowTimestamp);
if (productCategoryMembers != null && productCategoryMembers.size() > 0) {
// if any product is in category, set true and break
// then 0 (equals), otherwise 1 (not equals)
compare = 0;
break;
}
}
*/
} else if ("PPIP_NEW_ACCT".equals(inputParamEnumId)) {
Double acctDays = cart.getPartyDaysSinceCreated(nowTimestamp);
if (acctDays == null) {
// condition always fails if we don't know how many days since account created
return false;
}
compare = acctDays.compareTo(Double.valueOf(condValue));
} else if ("PPIP_PARTY_ID".equals(inputParamEnumId)) {
if (partyId != null) {
compare = partyId.compareTo(condValue);
} else {
compare = 1;
}
/* These aren't supported yet, ie TODO (low priority)
} else if ("PRIP_PARTY_GRP_MEM".equals(inputParamEnumId)) {
} else if ("PRIP_PARTY_CLASS".equals(inputParamEnumId)) {
*/
} else if ("PPIP_ROLE_TYPE".equals(inputParamEnumId)) {
if (partyId != null) {
// if a PartyRole exists for this partyId and the specified roleTypeId
GenericValue partyRole = delegator.findByPrimaryKeyCache("PartyRole",
UtilMisc.toMap("partyId", partyId, "roleTypeId", condValue));
// then 0 (equals), otherwise 1 (not equals)
if (partyRole != null) {
compare = 0;
} else {
compare = 1;
}
} else {
compare = 1;
}
} else if ("PPIP_ORDER_TOTAL".equals(inputParamEnumId)) {
Double orderSubTotal = new Double(cart.getSubTotalForPromotions());
if (Debug.verboseOn()) Debug.logVerbose("Doing order total compare: orderSubTotal=" + orderSubTotal, module);
compare = orderSubTotal.compareTo(Double.valueOf(condValue));
} else {
Debug.logWarning("An un-supported productPromoCond input parameter (lhs) was used: " + productPromoCond.getString("inputParamEnumId") + ", returning false, ie check failed", module);
return false;
}
if (Debug.verboseOn()) Debug.logVerbose("Condition compare done, compare=" + compare, module);
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("An un-supported productPromoCond condition was used: " + operatorEnumId + ", returning false, ie check failed", module);
return false;
}
return false;
}
public static class ActionResultInfo {
public boolean ranAction = false;
public double totalDiscountAmount = 0;
public double quantityLeftInAction = 0;
}
/** returns true if the cart was changed and rules need to be re-evaluted */
protected static ActionResultInfo performAction(GenericValue productPromoAction, ShoppingCart cart, GenericDelegator delegator, LocalDispatcher dispatcher, Timestamp nowTimestamp) throws GenericEntityException, CartItemModifyException {
ActionResultInfo actionResultInfo = new ActionResultInfo();
String productPromoActionEnumId = productPromoAction.getString("productPromoActionEnumId");
if ("PROMO_GWP".equals(productPromoActionEnumId)) {
// the code was in there for this, so even though I don't think we want to restrict this, just adding this flag to make it easy to change; could make option dynamic, but now implied by the use limit
boolean allowMultipleGwp = true;
Integer itemLoc = findPromoItem(productPromoAction, cart);
if (!allowMultipleGwp && itemLoc != null) {
if (Debug.verboseOn()) Debug.logVerbose("Not adding promo item, already there; action: " + productPromoAction, module);
actionResultInfo.ranAction = false;
} else {
GenericValue product = delegator.findByPrimaryKeyCache("Product", UtilMisc.toMap("productId", productPromoAction.get("productId")));
if (product == null) {
String errMsg = "GWP Product not found with ID [" + productPromoAction.get("productId") + "] for ProductPromoAction [" + productPromoAction.get("productPromoId") + ":" + productPromoAction.get("productPromoRuleId") + ":" + productPromoAction.get("productPromoActionSeqId") + "]";
Debug.logError(errMsg, module);
throw new CartItemModifyException(errMsg);
}
double quantity = productPromoAction.get("quantity") == null ? 0.0 : productPromoAction.getDouble("quantity").doubleValue();
// pass null for cartLocation to add to end of cart, pass false for doPromotions to avoid infinite recursion
ShoppingCartItem gwpItem = null;
try {
// just leave the prodCatalogId null, this line won't be associated with a catalog
String prodCatalogId = null;
gwpItem = ShoppingCartItem.makeItem(null, product, quantity, null, null, prodCatalogId, dispatcher, cart, false);
} catch (CartItemModifyException e) {
int gwpItemIndex = cart.getItemIndex(gwpItem);
cart.removeCartItem(gwpItemIndex, dispatcher);
throw e;
}
double discountAmount = -(quantity * gwpItem.getBasePrice());
doOrderItemPromoAction(productPromoAction, gwpItem, discountAmount, "amount", delegator);
// set promo after create; note that to setQuantity we must clear this flag, setQuantity, then re-set the flag
gwpItem.setIsPromo(true);
if (Debug.verboseOn()) Debug.logVerbose("gwpItem adjustments: " + gwpItem.getAdjustments(), module);
actionResultInfo.ranAction = true;
actionResultInfo.totalDiscountAmount = discountAmount;
}
} else if ("PROMO_FREE_SHIPPING".equals(productPromoActionEnumId)) {
// this may look a bit funny: on each pass all rules that do free shipping will set their own rule id for it,
// and on unapply if the promo and rule ids are the same then it will clear it; essentially on any pass
// through the promos and rules if any free shipping should be there, it will be there
cart.addFreeShippingProductPromoAction(productPromoAction);
// don't consider this as a cart change?
actionResultInfo.ranAction = true;
// should probably set the totalDiscountAmount to something, but we have no idea what it will be, so leave at 0, will still get run
} else if ("PROMO_PROD_DISC".equals(productPromoActionEnumId)) {
double quantityDesired = productPromoAction.get("quantity") == null ? 1.0 : productPromoAction.getDouble("quantity").doubleValue();
double startingQuantity = quantityDesired;
double discountAmountTotal = 0;
Set productIds = ProductPromoWorker.getPromoRuleActionProductIds(productPromoAction, delegator, nowTimestamp);
List lineOrderedByBasePriceList = cart.getLineListOrderedByBasePrice(false);
Iterator lineOrderedByBasePriceIter = lineOrderedByBasePriceList.iterator();
while (quantityDesired > 0 && lineOrderedByBasePriceIter.hasNext()) {
ShoppingCartItem cartItem = (ShoppingCartItem) lineOrderedByBasePriceIter.next();
// 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() &&
(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)
double quantityUsed = cartItem.addPromoQuantityCandidateUse(quantityDesired, productPromoAction, false);
if (quantityUsed > 0) {
quantityDesired -= quantityUsed;
// create an adjustment and add it to the cartItem that implements the promotion action
double percentModifier = productPromoAction.get("amount") == null ? 0.0 : (productPromoAction.getDouble("amount").doubleValue()/100.0);
double lineAmount = quantityUsed * cartItem.getBasePrice();
double discountAmount = -(lineAmount * percentModifier);
discountAmountTotal += discountAmount;
// not doing this any more, now distributing among conditions and actions (see call below): doOrderItemPromoAction(productPromoAction, cartItem, discountAmount, "amount", delegator);
}
}
}
if (quantityDesired == startingQuantity) {
// couldn't find any cart items to give a discount to, don't consider action run
actionResultInfo.ranAction = false;
} else {
double totalAmount = getCartItemsUsedTotalAmount(cart, productPromoAction);
if (Debug.verboseOn()) Debug.logVerbose("Applying promo [" + productPromoAction.getPrimaryKey() + "]\n totalAmount=" + totalAmount + ", discountAmountTotal=" + discountAmountTotal, module);
distributeDiscountAmount(discountAmountTotal, totalAmount, getCartItemsUsed(cart, productPromoAction), productPromoAction, delegator);
actionResultInfo.ranAction = true;
actionResultInfo.totalDiscountAmount = discountAmountTotal;
actionResultInfo.quantityLeftInAction = quantityDesired;
}
} else if ("PROMO_PROD_AMDISC".equals(productPromoActionEnumId)) {
double quantityDesired = productPromoAction.get("quantity") == null ? 1.0 : productPromoAction.getDouble("quantity").doubleValue();
double startingQuantity = quantityDesired;
double discountAmountTotal = 0;
Set productIds = ProductPromoWorker.getPromoRuleActionProductIds(productPromoAction, delegator, nowTimestamp);
List lineOrderedByBasePriceList = cart.getLineListOrderedByBasePrice(false);
Iterator lineOrderedByBasePriceIter = lineOrderedByBasePriceList.iterator();
while (quantityDesired > 0 && lineOrderedByBasePriceIter.hasNext()) {
ShoppingCartItem cartItem = (ShoppingCartItem) lineOrderedByBasePriceIter.next();
// 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))) &&
(product == null || !"N".equals(product.getString("includeInPromotions")))) {
// reduce quantity still needed to qualify for promo (quantityNeeded)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -