use of org.apache.ofbiz.order.shoppingcart.ShoppingCart in project ofbiz-framework by apache.
the class OrderServices method shoppingCartRemoteTest.
public static Map<String, Object> shoppingCartRemoteTest(DispatchContext dctx, Map<String, ? extends Object> context) {
ShoppingCart cart = (ShoppingCart) context.get("cart");
Debug.logInfo("Product ID : " + cart.findCartItem(0).getProductId(), module);
return ServiceUtil.returnSuccess();
}
use of org.apache.ofbiz.order.shoppingcart.ShoppingCart in project ofbiz-framework by apache.
the class ProductDisplayWorker method getQuickReorderProducts.
public static Map<String, Object> getQuickReorderProducts(ServletRequest request) {
Delegator delegator = (Delegator) request.getAttribute("delegator");
HttpServletRequest httpRequest = (HttpServletRequest) request;
GenericValue userLogin = (GenericValue) httpRequest.getSession().getAttribute("userLogin");
Map<String, Object> results = new HashMap<>();
if (userLogin == null) {
userLogin = (GenericValue) httpRequest.getSession().getAttribute("autoUserLogin");
}
if (userLogin == null) {
return results;
}
try {
Map<String, GenericValue> products = UtilGenerics.checkMap(httpRequest.getSession().getAttribute("_QUICK_REORDER_PRODUCTS_"));
Map<String, BigDecimal> productQuantities = UtilGenerics.checkMap(httpRequest.getSession().getAttribute("_QUICK_REORDER_PRODUCT_QUANTITIES_"));
Map<String, Integer> productOccurances = UtilGenerics.checkMap(httpRequest.getSession().getAttribute("_QUICK_REORDER_PRODUCT_OCCURANCES_"));
if (products == null || productQuantities == null || productOccurances == null) {
products = new HashMap<>();
productQuantities = new HashMap<>();
// keep track of how many times a product occurs in order to find averages and rank by purchase amount
productOccurances = new HashMap<>();
// get all order role entities for user by customer role type : PLACING_CUSTOMER
List<GenericValue> orderRoles = EntityQuery.use(delegator).from("OrderRole").where("partyId", userLogin.get("partyId"), "roleTypeId", "PLACING_CUSTOMER").queryList();
Iterator<GenericValue> ordersIter = UtilMisc.toIterator(orderRoles);
while (ordersIter != null && ordersIter.hasNext()) {
GenericValue orderRole = ordersIter.next();
// for each order role get all order items
List<GenericValue> orderItems = orderRole.getRelated("OrderItem", null, null, false);
Iterator<GenericValue> orderItemsIter = UtilMisc.toIterator(orderItems);
while (orderItemsIter != null && orderItemsIter.hasNext()) {
GenericValue orderItem = orderItemsIter.next();
String productId = orderItem.getString("productId");
if (UtilValidate.isNotEmpty(productId)) {
// for each order item get the associated product
GenericValue product = orderItem.getRelatedOne("Product", true);
products.put(product.getString("productId"), product);
BigDecimal curQuant = productQuantities.get(product.get("productId"));
if (curQuant == null) {
curQuant = BigDecimal.ZERO;
}
BigDecimal orderQuant = orderItem.getBigDecimal("quantity");
if (orderQuant == null) {
orderQuant = BigDecimal.ZERO;
}
productQuantities.put(product.getString("productId"), curQuant.add(orderQuant));
Integer curOcc = productOccurances.get(product.get("productId"));
if (curOcc == null) {
curOcc = Integer.valueOf(0);
}
productOccurances.put(product.getString("productId"), Integer.valueOf(curOcc.intValue() + 1));
}
}
}
// go through each product quantity and divide it by the occurances to get the average
for (Map.Entry<String, BigDecimal> entry : productQuantities.entrySet()) {
String prodId = entry.getKey();
BigDecimal quantity = entry.getValue();
Integer occs = productOccurances.get(prodId);
BigDecimal nqint = quantity.divide(new BigDecimal(occs), new MathContext(10));
if (nqint.compareTo(BigDecimal.ONE) < 0) {
nqint = BigDecimal.ONE;
}
productQuantities.put(prodId, nqint);
}
httpRequest.getSession().setAttribute("_QUICK_REORDER_PRODUCTS_", new HashMap<>(products));
httpRequest.getSession().setAttribute("_QUICK_REORDER_PRODUCT_QUANTITIES_", new HashMap<>(productQuantities));
httpRequest.getSession().setAttribute("_QUICK_REORDER_PRODUCT_OCCURANCES_", new HashMap<>(productOccurances));
} else {
// make a copy since we are going to change them
products = new HashMap<>(products);
productQuantities = new HashMap<>(productQuantities);
productOccurances = new HashMap<>(productOccurances);
}
// remove all products that are already in the cart
ShoppingCart cart = (ShoppingCart) httpRequest.getSession().getAttribute("shoppingCart");
if (UtilValidate.isNotEmpty(cart)) {
for (ShoppingCartItem item : cart) {
String productId = item.getProductId();
products.remove(productId);
productQuantities.remove(productId);
productOccurances.remove(productId);
}
}
// if desired check view allow category
String currentCatalogId = CatalogWorker.getCurrentCatalogId(request);
String viewProductCategoryId = CatalogWorker.getCatalogViewAllowCategoryId(delegator, currentCatalogId);
if (viewProductCategoryId != null) {
for (Map.Entry<String, GenericValue> entry : products.entrySet()) {
String productId = entry.getKey();
if (!CategoryWorker.isProductInCategory(delegator, productId, viewProductCategoryId)) {
products.remove(productId);
productQuantities.remove(productId);
productOccurances.remove(productId);
}
}
}
List<GenericValue> reorderProds = new LinkedList<>();
reorderProds.addAll(products.values());
// sort descending by new metric...
BigDecimal occurancesModifier = BigDecimal.ONE;
BigDecimal quantityModifier = BigDecimal.ONE;
Map<String, Object> newMetric = new HashMap<>();
for (Map.Entry<String, Integer> entry : productOccurances.entrySet()) {
String prodId = entry.getKey();
Integer quantity = entry.getValue();
BigDecimal occs = productQuantities.get(prodId);
// For quantity we should test if we allow to add decimal quantity for this product an productStore : if not then round to 0
if (!ProductWorker.isDecimalQuantityOrderAllowed(delegator, prodId, cart.getProductStoreId())) {
occs = occs.setScale(0, UtilNumber.getRoundingMode("order.rounding"));
} else {
occs = occs.setScale(UtilNumber.getBigDecimalScale("order.decimals"), UtilNumber.getRoundingMode("order.rounding"));
}
productQuantities.put(prodId, occs);
BigDecimal nqdbl = quantityModifier.multiply(new BigDecimal(quantity)).add(occs.multiply(occurancesModifier));
newMetric.put(prodId, nqdbl);
}
reorderProds = productOrderByMap(reorderProds, newMetric, true);
// remove extra products - only return 5
while (reorderProds.size() > 5) {
reorderProds.remove(reorderProds.size() - 1);
}
results.put("products", reorderProds);
results.put("quantities", productQuantities);
} catch (GenericEntityException e) {
Debug.logWarning(e, module);
}
return results;
}
use of org.apache.ofbiz.order.shoppingcart.ShoppingCart in project ofbiz-framework by apache.
the class ProductDisplayWorker method getRandomCartProductAssoc.
/* ========================================================================================*/
/* ============================= Special Data Retrieval Methods ===========================*/
public static List<GenericValue> getRandomCartProductAssoc(ServletRequest request, boolean checkViewAllow) {
Delegator delegator = (Delegator) request.getAttribute("delegator");
HttpServletRequest httpRequest = (HttpServletRequest) request;
ShoppingCart cart = (ShoppingCart) httpRequest.getSession().getAttribute("shoppingCart");
if (cart == null || cart.size() <= 0) {
return null;
}
List<GenericValue> cartAssocs = null;
try {
Map<String, GenericValue> products = new HashMap<>();
Iterator<ShoppingCartItem> cartiter = cart.iterator();
while (cartiter != null && cartiter.hasNext()) {
ShoppingCartItem item = cartiter.next();
// since ProductAssoc records have a fromDate and thruDate, we can filter by now so that only assocs in the date range are included
List<GenericValue> complementProducts = EntityQuery.use(delegator).from("ProductAssoc").where("productId", item.getProductId(), "productAssocTypeId", "PRODUCT_COMPLEMENT").cache(true).filterByDate().queryList();
List<GenericValue> productsCategories = EntityQuery.use(delegator).from("ProductCategoryMember").where("productId", item.getProductId()).cache(true).filterByDate().queryList();
if (productsCategories != null) {
for (GenericValue productsCategoryMember : productsCategories) {
GenericValue productsCategory = productsCategoryMember.getRelatedOne("ProductCategory", true);
if ("CROSS_SELL_CATEGORY".equals(productsCategory.getString("productCategoryTypeId"))) {
List<GenericValue> curPcms = productsCategory.getRelated("ProductCategoryMember", null, null, true);
if (curPcms != null) {
for (GenericValue curPcm : curPcms) {
if (!products.containsKey(curPcm.getString("productId"))) {
GenericValue product = curPcm.getRelatedOne("Product", true);
products.put(product.getString("productId"), product);
}
}
}
}
}
}
if (UtilValidate.isNotEmpty(complementProducts)) {
for (GenericValue productAssoc : complementProducts) {
if (!products.containsKey(productAssoc.getString("productIdTo"))) {
GenericValue product = productAssoc.getRelatedOne("AssocProduct", true);
products.put(product.getString("productId"), product);
}
}
}
}
// remove all products that are already in the cart
cartiter = cart.iterator();
while (cartiter != null && cartiter.hasNext()) {
ShoppingCartItem item = cartiter.next();
products.remove(item.getProductId());
}
// if desired check view allow category
if (checkViewAllow) {
String currentCatalogId = CatalogWorker.getCurrentCatalogId(request);
String viewProductCategoryId = CatalogWorker.getCatalogViewAllowCategoryId(delegator, currentCatalogId);
if (viewProductCategoryId != null) {
List<GenericValue> tempList = new LinkedList<>();
tempList.addAll(products.values());
tempList = CategoryWorker.filterProductsInCategory(delegator, tempList, viewProductCategoryId, "productId");
cartAssocs = new LinkedList<>();
cartAssocs.addAll(tempList);
}
}
if (cartAssocs == null) {
cartAssocs = new LinkedList<>();
cartAssocs.addAll(products.values());
}
// randomly remove products while there are more than 3
while (cartAssocs.size() > 3) {
int toRemove = (int) (Math.random() * cartAssocs.size());
cartAssocs.remove(toRemove);
}
} catch (GenericEntityException e) {
Debug.logWarning(e, module);
}
if (UtilValidate.isNotEmpty(cartAssocs)) {
return cartAssocs;
}
return null;
}
use of org.apache.ofbiz.order.shoppingcart.ShoppingCart in project ofbiz-framework by apache.
the class ProductPromoWorker method getStoreProductPromos.
public static List<GenericValue> getStoreProductPromos(Delegator delegator, LocalDispatcher dispatcher, ServletRequest request) {
List<GenericValue> productPromos = new LinkedList<>();
Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
// get the ShoppingCart out of the session.
HttpServletRequest req = null;
ShoppingCart cart = null;
try {
req = (HttpServletRequest) request;
cart = ShoppingCartEvents.getCartObject(req);
} catch (ClassCastException cce) {
Debug.logError("Not a HttpServletRequest, no shopping cart found.", module);
return null;
} catch (IllegalArgumentException e) {
Debug.logError(e, module);
return null;
}
boolean condResult = true;
try {
String productStoreId = cart.getProductStoreId();
GenericValue productStore = null;
try {
productStore = EntityQuery.use(delegator).from("ProductStore").where("productStoreId", productStoreId).cache().queryOne();
} catch (GenericEntityException e) {
Debug.logError(e, "Error looking up store with id " + productStoreId, module);
}
if (productStore == null) {
Debug.logWarning(UtilProperties.getMessage(resource_error, "OrderNoStoreFoundWithIdNotDoingPromotions", UtilMisc.toMap("productStoreId", productStoreId), cart.getLocale()), module);
return productPromos;
}
Iterator<GenericValue> productStorePromoAppls = UtilMisc.toIterator(EntityUtil.filterByDate(productStore.getRelated("ProductStorePromoAppl", UtilMisc.toMap("productStoreId", productStoreId), UtilMisc.toList("sequenceNum"), true), true));
while (productStorePromoAppls != null && productStorePromoAppls.hasNext()) {
GenericValue productStorePromoAppl = productStorePromoAppls.next();
if (UtilValidate.isNotEmpty(productStorePromoAppl.getString("manualOnly")) && "Y".equals(productStorePromoAppl.getString("manualOnly"))) {
// manual only promotions are not automatically evaluated (they must be explicitly selected by the user)
if (Debug.verboseOn()) {
Debug.logVerbose("Skipping promotion with id [" + productStorePromoAppl.getString("productPromoId") + "] because it is applied to the store with ID " + productStoreId + " as a manual only promotion.", module);
}
continue;
}
GenericValue productPromo = productStorePromoAppl.getRelatedOne("ProductPromo", true);
List<GenericValue> productPromoRules = productPromo.getRelated("ProductPromoRule", null, null, true);
if (productPromoRules != null) {
Iterator<GenericValue> promoRulesItr = productPromoRules.iterator();
while (condResult && promoRulesItr != null && promoRulesItr.hasNext()) {
GenericValue promoRule = promoRulesItr.next();
Iterator<GenericValue> productPromoConds = UtilMisc.toIterator(promoRule.getRelated("ProductPromoCond", null, UtilMisc.toList("productPromoCondSeqId"), true));
while (condResult && productPromoConds != null && productPromoConds.hasNext()) {
GenericValue productPromoCond = productPromoConds.next();
// evaluate the party related conditions; so we don't show the promo if it doesn't apply.
if ("PPIP_PARTY_ID".equals(productPromoCond.getString("inputParamEnumId"))) {
condResult = checkCondition(productPromoCond, cart, delegator, dispatcher, nowTimestamp);
} else if ("PPIP_PARTY_GRP_MEM".equals(productPromoCond.getString("inputParamEnumId"))) {
condResult = checkCondition(productPromoCond, cart, delegator, dispatcher, nowTimestamp);
} else if ("PPIP_PARTY_CLASS".equals(productPromoCond.getString("inputParamEnumId"))) {
condResult = checkCondition(productPromoCond, cart, delegator, dispatcher, nowTimestamp);
} else if ("PPIP_ROLE_TYPE".equals(productPromoCond.getString("inputParamEnumId"))) {
condResult = checkCondition(productPromoCond, cart, delegator, dispatcher, nowTimestamp);
}
}
}
if (!condResult) {
productPromo = null;
}
}
if (productPromo != null) {
productPromos.add(productPromo);
}
}
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
return productPromos;
}
use of org.apache.ofbiz.order.shoppingcart.ShoppingCart in project ofbiz-framework by apache.
the class ShoppingListEvents method addListToCart.
public static String addListToCart(HttpServletRequest request, HttpServletResponse response) {
Delegator delegator = (Delegator) request.getAttribute("delegator");
LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
ShoppingCart cart = ShoppingCartEvents.getCartObject(request);
String shoppingListId = request.getParameter("shoppingListId");
String includeChild = request.getParameter("includeChild");
String prodCatalogId = CatalogWorker.getCurrentCatalogId(request);
try {
addListToCart(delegator, dispatcher, cart, prodCatalogId, shoppingListId, (includeChild != null), true, true);
} catch (IllegalArgumentException e) {
request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
return "error";
}
return "success";
}
Aggregations