📄 productpromoworker.java
字号:
long productPromoCodeUseSize = delegator.findCountByCondition("ProductPromoUseCheck", checkCondition, null);
long perCodeThisOrder = codeUseLimitPerCode.longValue() - productPromoCodeUseSize;
if (codeUseLimit == null || codeUseLimit.longValue() > perCodeThisOrder) {
codeUseLimit = new Long(perCodeThisOrder);
}
}
return codeUseLimit;
}
public static String checkCanUsePromoCode(String productPromoCodeId, String partyId, GenericDelegator delegator) {
try {
GenericValue productPromoCode = delegator.findByPrimaryKey("ProductPromoCode", UtilMisc.toMap("productPromoCodeId", productPromoCodeId));
if (productPromoCode == null) {
return "The promotion code [" + productPromoCodeId + "] is not valid.";
}
if ("Y".equals(productPromoCode.getString("requireEmailOrParty"))) {
boolean hasEmailOrParty = false;
// check partyId
if (UtilValidate.isNotEmpty(partyId)) {
if (delegator.findByPrimaryKey("ProductPromoCodeParty", UtilMisc.toMap("productPromoCodeId", productPromoCodeId, "partyId", partyId)) != null) {
// found party associated with the code, looks good...
return null;
}
// check email address in ProductPromoCodeEmail
Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
List validEmailCondList = new ArrayList();
validEmailCondList.add(new EntityExpr("partyId", EntityOperator.EQUALS, partyId));
validEmailCondList.add(new EntityExpr("productPromoCodeId", EntityOperator.EQUALS, productPromoCodeId));
validEmailCondList.add(new EntityExpr("fromDate", EntityOperator.LESS_THAN_EQUAL_TO, nowTimestamp));
validEmailCondList.add(new EntityExpr(new EntityExpr("thruDate", EntityOperator.GREATER_THAN_EQUAL_TO, nowTimestamp),
EntityOperator.OR, new EntityExpr("thruDate", EntityOperator.EQUALS, null)));
EntityCondition validEmailCondition = new EntityConditionList(validEmailCondList, EntityOperator.AND);
long validEmailCount = delegator.findCountByCondition("ProductPromoCodeEmailParty", validEmailCondition, null);
if (validEmailCount > 0) {
// there was an email in the list, looks good...
return null;
}
}
if (!hasEmailOrParty) {
return "This promotion code [" + productPromoCodeId + "] requires you to be associated with it by account or email address and you are not associated with it.";
}
}
// check per customer and per promotion code use limits
Long useLimit = getProductPromoCodeUseLimit(productPromoCode, partyId, delegator);
if (useLimit != null && useLimit.longValue() <= 0) {
return "This promotion code [" + productPromoCodeId + "] has reached it's maximum use limit for you and can no longer be used.";
}
return null;
} catch (GenericEntityException e) {
Debug.logError(e, "Error looking up ProductPromoCode", module);
return "Error looking up code [" + productPromoCodeId + "]:" + e.toString();
}
}
public static String makeAutoDescription(GenericValue productPromo, GenericDelegator delegator, Locale locale) throws GenericEntityException {
if (productPromo == null) {
return "";
}
StringBuffer promoDescBuf = new StringBuffer();
List productPromoRules = productPromo.getRelatedCache("ProductPromoRule", null, null);
Iterator promoRulesIter = productPromoRules.iterator();
while (promoRulesIter != null && promoRulesIter.hasNext()) {
GenericValue productPromoRule = (GenericValue) promoRulesIter.next();
List productPromoConds = delegator.findByAndCache("ProductPromoCond", UtilMisc.toMap("productPromoId", productPromo.get("productPromoId")), UtilMisc.toList("productPromoCondSeqId"));
productPromoConds = EntityUtil.filterByAnd(productPromoConds, UtilMisc.toMap("productPromoRuleId", productPromoRule.get("productPromoRuleId")));
// using the other method to consolodate cache entries because the same cache is used elsewhere: List productPromoConds = productPromoRule.getRelatedCache("ProductPromoCond", null, UtilMisc.toList("productPromoCondSeqId"));
Iterator productPromoCondIter = UtilMisc.toIterator(productPromoConds);
while (productPromoCondIter != null && productPromoCondIter.hasNext()) {
GenericValue productPromoCond = (GenericValue) productPromoCondIter.next();
String equalityOperator = UtilProperties.getMessage("promotext", "operator.equality." + productPromoCond.getString("operatorEnumId"), locale);
String quantityOperator = UtilProperties.getMessage("promotext", "operator.quantity." + productPromoCond.getString("operatorEnumId"), locale);
String condValue = "invalid";
if (UtilValidate.isNotEmpty(productPromoCond.getString("condValue"))) {
condValue = productPromoCond.getString("condValue");
}
Map messageContext = UtilMisc.toMap("condValue", condValue, "equalityOperator", equalityOperator, "quantityOperator", quantityOperator);
String msgProp = UtilProperties.getMessage("promotext", "condition." + productPromoCond.getString("inputParamEnumId"), messageContext, locale);
promoDescBuf.append(msgProp);
promoDescBuf.append(" ");
if (promoRulesIter.hasNext()) {
promoDescBuf.append(" and ");
}
}
List productPromoActions = productPromoRule.getRelatedCache("ProductPromoAction", null, UtilMisc.toList("productPromoActionSeqId"));
Iterator productPromoActionIter = UtilMisc.toIterator(productPromoActions);
while (productPromoActionIter != null && productPromoActionIter.hasNext()) {
GenericValue productPromoAction = (GenericValue) productPromoActionIter.next();
String productId = productPromoAction.getString("productId");
Map messageContext = UtilMisc.toMap("quantity", productPromoAction.get("quantity"), "amount", productPromoAction.get("amount"), "productId", productId, "partyId", productPromoAction.get("partyId"));
if (UtilValidate.isEmpty((String) messageContext.get("productId"))) messageContext.put("productId", "any");
if (UtilValidate.isEmpty((String) messageContext.get("partyId"))) messageContext.put("partyId", "any");
GenericValue product = delegator.findByPrimaryKeyCache("Product", UtilMisc.toMap("productId", productId));
if (product != null) {
messageContext.put("productName", ProductContentWrapper.getProductContentAsText(product, "PRODUCT_NAME", locale));
}
String msgProp = UtilProperties.getMessage("promotext", "action." + productPromoAction.getString("productPromoActionEnumId"), messageContext, locale);
promoDescBuf.append(msgProp);
promoDescBuf.append(" ");
if (promoRulesIter.hasNext()) {
promoDescBuf.append(" and ");
}
}
if (promoRulesIter.hasNext()) {
promoDescBuf.append(" or ");
}
}
if (promoDescBuf.length() > 0) {
// remove any trailing space
if (promoDescBuf.charAt(promoDescBuf.length() - 1) == ' ') promoDescBuf.deleteCharAt(promoDescBuf.length() - 1);
// add a period
promoDescBuf.append(". ");
// capitalize the first letter
promoDescBuf.setCharAt(0, Character.toUpperCase(promoDescBuf.charAt(0)));
}
if ("Y".equals(productPromo.getString("requireCode"))) {
promoDescBuf.append("Requires code to use. ");
}
if (productPromo.getLong("useLimitPerOrder") != null) {
promoDescBuf.append("Limit ");
promoDescBuf.append(productPromo.getLong("useLimitPerOrder"));
promoDescBuf.append(" per order. ");
}
if (productPromo.getLong("useLimitPerCustomer") != null) {
promoDescBuf.append("Limit ");
promoDescBuf.append(productPromo.getLong("useLimitPerCustomer"));
promoDescBuf.append(" per customer. ");
}
if (productPromo.getLong("useLimitPerPromotion") != null) {
promoDescBuf.append("Limit ");
promoDescBuf.append(productPromo.getLong("useLimitPerPromotion"));
promoDescBuf.append(" per promotion. ");
}
return promoDescBuf.toString();
}
protected static boolean runProductPromoRules(ShoppingCart cart, boolean cartChanged, Long useLimit, boolean requireCode, String productPromoCodeId, Long codeUseLimit, long maxUseLimit,
GenericValue productPromo, List productPromoRules, LocalDispatcher dispatcher, GenericDelegator delegator, Timestamp nowTimestamp) throws GenericEntityException, UseLimitException {
String productPromoId = productPromo.getString("productPromoId");
while ((useLimit == null || useLimit.longValue() > cart.getProductPromoUseCount(productPromoId)) &&
(!requireCode || UtilValidate.isNotEmpty(productPromoCodeId)) &&
(codeUseLimit == null || codeUseLimit.longValue() > cart.getProductPromoCodeUse(productPromoCodeId))) {
boolean promoUsed = false;
double totalDiscountAmount = 0;
double quantityLeftInActions = 0;
Iterator promoRulesIter = productPromoRules.iterator();
while (promoRulesIter != null && promoRulesIter.hasNext()) {
GenericValue productPromoRule = (GenericValue) promoRulesIter.next();
// if apply then performActions when no conditions are false, so default to true
boolean performActions = true;
// loop through conditions for rule, if any false, set allConditionsTrue to false
List productPromoConds = delegator.findByAndCache("ProductPromoCond", UtilMisc.toMap("productPromoId", productPromo.get("productPromoId")), UtilMisc.toList("productPromoCondSeqId"));
productPromoConds = EntityUtil.filterByAnd(productPromoConds, UtilMisc.toMap("productPromoRuleId", productPromoRule.get("productPromoRuleId")));
// using the other method to consolodate cache entries because the same cache is used elsewhere: List productPromoConds = productPromoRule.getRelatedCache("ProductPromoCond", null, UtilMisc.toList("productPromoCondSeqId"));
if (Debug.verboseOn()) Debug.logVerbose("Checking " + productPromoConds.size() + " conditions for rule " + productPromoRule, module);
Iterator productPromoCondIter = UtilMisc.toIterator(productPromoConds);
while (productPromoCondIter != null && productPromoCondIter.hasNext()) {
GenericValue productPromoCond = (GenericValue) productPromoCondIter.next();
boolean condResult = checkCondition(productPromoCond, cart, delegator, nowTimestamp);
// any false condition will cause it to NOT perform the action
if (condResult == false) {
performActions = false;
break;
}
}
if (performActions) {
// perform all actions, either apply or unapply
List productPromoActions = productPromoRule.getRelatedCache("ProductPromoAction", null, UtilMisc.toList("productPromoActionSeqId"));
if (Debug.verboseOn()) Debug.logVerbose("Performing " + productPromoActions.size() + " actions for rule " + productPromoRule, module);
Iterator productPromoActionIter = UtilMisc.toIterator(productPromoActions);
while (productPromoActionIter != null && productPromoActionIter.hasNext()) {
GenericValue productPromoAction = (GenericValue) productPromoActionIter.next();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -