use of org.apache.ofbiz.order.shoppingcart.CartItemModifyException in project ofbiz-framework by apache.
the class ProductPromoWorker method runProductPromoRules.
private static boolean runProductPromoRules(ShoppingCart cart, Long useLimit, boolean requireCode, String productPromoCodeId, Long codeUseLimit, long maxUseLimit, GenericValue productPromo, List<GenericValue> productPromoRules, LocalDispatcher dispatcher, Delegator delegator, Timestamp nowTimestamp) throws GenericEntityException, UseLimitException {
boolean cartChanged = false;
Map<ShoppingCartItem, BigDecimal> usageInfoMap = prepareProductUsageInfoMap(cart);
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;
BigDecimal totalDiscountAmount = BigDecimal.ZERO;
BigDecimal quantityLeftInActions = BigDecimal.ZERO;
Iterator<GenericValue> promoRulesIter = productPromoRules.iterator();
while (promoRulesIter != null && promoRulesIter.hasNext()) {
GenericValue productPromoRule = 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<GenericValue> productPromoConds = EntityQuery.use(delegator).from("ProductPromoCond").where("productPromoId", productPromo.get("productPromoId")).orderBy("productPromoCondSeqId").cache(true).queryList();
productPromoConds = EntityUtil.filterByAnd(productPromoConds, UtilMisc.toMap("productPromoRuleId", productPromoRule.get("productPromoRuleId")));
// using the other method to consolidate cache entries because the same cache is used elsewhere: List productPromoConds = productPromoRule.getRelated("ProductPromoCond", null, UtilMisc.toList("productPromoCondSeqId"), true);
if (Debug.verboseOn()) {
Debug.logVerbose("Checking " + productPromoConds.size() + " conditions for rule " + productPromoRule, module);
}
Iterator<GenericValue> productPromoCondIter = UtilMisc.toIterator(productPromoConds);
while (productPromoCondIter != null && productPromoCondIter.hasNext()) {
GenericValue productPromoCond = productPromoCondIter.next();
boolean conditionSatisfied = checkCondition(productPromoCond, cart, delegator, dispatcher, nowTimestamp);
// any false condition will cause it to NOT perform the action
if (!conditionSatisfied) {
performActions = false;
break;
}
}
if (performActions) {
// perform all actions, either apply or unapply
List<GenericValue> productPromoActions = productPromoRule.getRelated("ProductPromoAction", null, UtilMisc.toList("productPromoActionSeqId"), true);
Iterator<GenericValue> productPromoActionIter = UtilMisc.toIterator(productPromoActions);
while (productPromoActionIter != null && productPromoActionIter.hasNext()) {
GenericValue productPromoAction = productPromoActionIter.next();
try {
ActionResultInfo actionResultInfo = performAction(productPromoAction, cart, delegator, dispatcher, nowTimestamp);
totalDiscountAmount = totalDiscountAmount.add(actionResultInfo.totalDiscountAmount);
quantityLeftInActions = quantityLeftInActions.add(actionResultInfo.quantityLeftInAction);
// only set if true, don't set back to false: implements OR logic (ie if ANY actions change content, redo loop)
boolean actionChangedCart = actionResultInfo.ranAction;
if (actionChangedCart) {
promoUsed = true;
cartChanged = true;
}
} catch (CartItemModifyException e) {
Debug.logError(e, "Error modifying the cart while performing promotion action [" + productPromoAction.getPrimaryKey() + "]", module);
}
}
}
}
if (promoUsed) {
// Get product use information from the cart
Map<ShoppingCartItem, BigDecimal> newUsageInfoMap = prepareProductUsageInfoMap(cart);
Map<ShoppingCartItem, BigDecimal> deltaUsageInfoMap = prepareDeltaProductUsageInfoMap(usageInfoMap, newUsageInfoMap);
usageInfoMap = newUsageInfoMap;
cart.addProductPromoUse(productPromo.getString("productPromoId"), productPromoCodeId, totalDiscountAmount, quantityLeftInActions, deltaUsageInfoMap);
} else {
// the promotion was not used, don't try again until we finish a full pass and come back to see the promo conditions are now satisfied based on changes to the cart
break;
}
if (cart.getProductPromoUseCount(productPromoId) > maxUseLimit) {
throw new UseLimitException("ERROR: While calculating promotions the promotion [" + productPromoId + "] action was applied more than " + maxUseLimit + " times, so the calculation has been ended. This should generally never happen unless you have bad rule definitions.");
}
}
return cartChanged;
}
use of org.apache.ofbiz.order.shoppingcart.CartItemModifyException in project ofbiz-framework by apache.
the class ShoppingListServices method makeShoppingListCart.
/**
* Add a shoppinglist to an existing shoppingcart
*
* @param listCart the shopping cart list
* @param dispatcher the local dispatcher
* @param shoppingList a GenericValue object of the shopping list
* @param locale the locale in use
* @return the modified shopping cart adding the shopping list elements
*/
public static ShoppingCart makeShoppingListCart(ShoppingCart listCart, LocalDispatcher dispatcher, GenericValue shoppingList, Locale locale) {
Delegator delegator = dispatcher.getDelegator();
if (shoppingList != null && shoppingList.get("productStoreId") != null) {
String productStoreId = shoppingList.getString("productStoreId");
String currencyUom = shoppingList.getString("currencyUom");
if (currencyUom == null) {
GenericValue productStore = ProductStoreWorker.getProductStore(productStoreId, delegator);
if (productStore == null) {
return null;
}
currencyUom = productStore.getString("defaultCurrencyUomId");
}
if (locale == null) {
locale = Locale.getDefault();
}
List<GenericValue> items = null;
try {
items = shoppingList.getRelated("ShoppingListItem", null, UtilMisc.toList("shoppingListItemSeqId"), false);
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
if (UtilValidate.isNotEmpty(items)) {
if (listCart == null) {
listCart = new ShoppingCart(delegator, productStoreId, locale, currencyUom);
listCart.setOrderPartyId(shoppingList.getString("partyId"));
listCart.setAutoOrderShoppingListId(shoppingList.getString("shoppingListId"));
} else {
if (!listCart.getPartyId().equals(shoppingList.getString("partyId"))) {
Debug.logError("CANNOT add shoppingList: " + shoppingList.getString("shoppingListId") + " of partyId: " + shoppingList.getString("partyId") + " to a shoppingcart with a different orderPartyId: " + listCart.getPartyId(), module);
return listCart;
}
}
ProductConfigWrapper configWrapper = null;
for (GenericValue shoppingListItem : items) {
String productId = shoppingListItem.getString("productId");
BigDecimal quantity = shoppingListItem.getBigDecimal("quantity");
Timestamp reservStart = shoppingListItem.getTimestamp("reservStart");
BigDecimal reservLength = null;
String configId = shoppingListItem.getString("configId");
if (shoppingListItem.get("reservLength") != null) {
reservLength = shoppingListItem.getBigDecimal("reservLength");
}
BigDecimal reservPersons = null;
if (shoppingListItem.get("reservPersons") != null) {
reservPersons = shoppingListItem.getBigDecimal("reservPersons");
}
if (UtilValidate.isNotEmpty(productId) && quantity != null) {
if (UtilValidate.isNotEmpty(configId)) {
configWrapper = ProductConfigWorker.loadProductConfigWrapper(delegator, dispatcher, configId, productId, listCart.getProductStoreId(), null, listCart.getWebSiteId(), listCart.getCurrency(), listCart.getLocale(), listCart.getAutoUserLogin());
}
// list items are noted in the shopping cart
String listId = shoppingListItem.getString("shoppingListId");
String itemId = shoppingListItem.getString("shoppingListItemSeqId");
Map<String, Object> attributes = UtilMisc.<String, Object>toMap("shoppingListId", listId, "shoppingListItemSeqId", itemId);
try {
listCart.addOrIncreaseItem(productId, null, quantity, reservStart, reservLength, reservPersons, null, null, null, null, null, attributes, null, configWrapper, null, null, null, dispatcher);
} catch (CartItemModifyException e) {
Debug.logError(e, "Unable to add product to List Cart - " + productId, module);
} catch (ItemNotFoundException e) {
Debug.logError(e, "Product not found - " + productId, module);
}
}
}
if (listCart.size() > 0) {
if (UtilValidate.isNotEmpty(shoppingList.get("paymentMethodId"))) {
listCart.addPayment(shoppingList.getString("paymentMethodId"));
}
if (UtilValidate.isNotEmpty(shoppingList.get("contactMechId"))) {
listCart.setAllShippingContactMechId(shoppingList.getString("contactMechId"));
}
if (UtilValidate.isNotEmpty(shoppingList.get("shipmentMethodTypeId"))) {
listCart.setAllShipmentMethodTypeId(shoppingList.getString("shipmentMethodTypeId"));
}
if (UtilValidate.isNotEmpty(shoppingList.get("carrierPartyId"))) {
listCart.setAllCarrierPartyId(shoppingList.getString("carrierPartyId"));
}
if (UtilValidate.isNotEmpty(shoppingList.getString("productPromoCodeId"))) {
listCart.addProductPromoCode(shoppingList.getString("productPromoCodeId"), dispatcher);
}
}
}
}
return listCart;
}
Aggregations