Search in sources :

Example 1 with ProductPromoUseInfo

use of org.apache.ofbiz.order.shoppingcart.ShoppingCart.ProductPromoUseInfo in project ofbiz-framework by apache.

the class ProductPromoWorker method doPromotions.

public static void doPromotions(ShoppingCart cart, List<GenericValue> productPromoList, LocalDispatcher dispatcher) {
    if (!cart.getDoPromotions()) {
        return;
    }
    Delegator delegator = cart.getDelegator();
    Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
    // start out by clearing all existing promotions, then we can just add all that apply
    cart.clearAllPromotionInformation();
    // there will be a ton of db access, so just do a big catch entity exception block
    try {
        if (productPromoList == null) {
            if ("SALES_ORDER".equals(cart.getOrderType())) {
                productPromoList = ProductPromoWorker.getProductStorePromotions(cart, nowTimestamp, dispatcher);
            } else {
                productPromoList = ProductPromoWorker.getAgreementPromotions(cart, nowTimestamp, dispatcher);
            }
        }
        // do a calculate only run through the promotions, then order by descending totalDiscountAmount for each promotion
        // NOTE: on this run, with isolatedTestRun passed as false it should not apply any adjustments
        // or track which cart items are used for which promotions, but it will track ProductPromoUseInfo and
        // useLimits; we are basically just trying to run each promo "independently" to see how much each is worth
        runProductPromos(productPromoList, cart, delegator, dispatcher, nowTimestamp, true);
        // NOTE: we can easily recognize the promos for the order total: they are the ones with usage set to 0
        Iterator<ProductPromoUseInfo> promoUses = cart.getProductPromoUseInfoIter();
        List<ProductPromoUseInfo> sortedPromoUses = new ArrayList<>();
        while (promoUses.hasNext()) {
            ProductPromoUseInfo promoUse = promoUses.next();
            sortedPromoUses.add(promoUse);
        }
        Collections.sort(sortedPromoUses);
        List<GenericValue> sortedExplodedProductPromoList = new ArrayList<>(sortedPromoUses.size());
        Map<String, Long> usesPerPromo = new HashMap<>();
        int indexOfFirstOrderTotalPromo = -1;
        for (ProductPromoUseInfo promoUse : sortedPromoUses) {
            GenericValue productPromo = EntityQuery.use(delegator).from("ProductPromo").where("productPromoId", promoUse.getProductPromoId()).cache().queryOne();
            GenericValue newProductPromo = (GenericValue) productPromo.clone();
            if (!usesPerPromo.containsKey(promoUse.getProductPromoId())) {
                usesPerPromo.put(promoUse.getProductPromoId(), 0l);
            }
            long uses = usesPerPromo.get(promoUse.getProductPromoId());
            uses = uses + 1;
            long useLimitPerOrder = (newProductPromo.get("useLimitPerOrder") != null ? newProductPromo.getLong("useLimitPerOrder") : -1);
            if (useLimitPerOrder == -1 || uses < useLimitPerOrder) {
                newProductPromo.set("useLimitPerOrder", uses);
            }
            usesPerPromo.put(promoUse.getProductPromoId(), uses);
            sortedExplodedProductPromoList.add(newProductPromo);
            if (indexOfFirstOrderTotalPromo == -1 && BigDecimal.ZERO.equals(promoUse.getUsageWeight())) {
                indexOfFirstOrderTotalPromo = sortedExplodedProductPromoList.size() - 1;
            }
        }
        if (indexOfFirstOrderTotalPromo == -1) {
            indexOfFirstOrderTotalPromo = sortedExplodedProductPromoList.size() - 1;
        }
        for (GenericValue productPromo : productPromoList) {
            if (hasOrderTotalCondition(productPromo, delegator)) {
                if (!usesPerPromo.containsKey(productPromo.getString("productPromoId"))) {
                    sortedExplodedProductPromoList.add(productPromo);
                }
            } else {
                if (!usesPerPromo.containsKey(productPromo.getString("productPromoId"))) {
                    if (indexOfFirstOrderTotalPromo != -1) {
                        sortedExplodedProductPromoList.add(indexOfFirstOrderTotalPromo, productPromo);
                    } else {
                        sortedExplodedProductPromoList.add(0, productPromo);
                    }
                }
            }
        }
        // okay, all ready, do the real run, clearing the temporary result first...
        cart.clearAllPromotionInformation();
        runProductPromos(sortedExplodedProductPromoList, cart, delegator, dispatcher, nowTimestamp, false);
    } catch (NumberFormatException e) {
        Debug.logError(e, "Number not formatted correctly in promotion rules, not completed...", module);
    } catch (GenericEntityException e) {
        Debug.logError(e, "Error looking up promotion data while doing promotions", module);
    } catch (GeneralException e) {
        Debug.logError(e, "Error running promotions, will ignore: " + e.toString(), module);
    }
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GeneralException(org.apache.ofbiz.base.util.GeneralException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProductPromoUseInfo(org.apache.ofbiz.order.shoppingcart.ShoppingCart.ProductPromoUseInfo) Timestamp(java.sql.Timestamp) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Aggregations

Timestamp (java.sql.Timestamp)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 GeneralException (org.apache.ofbiz.base.util.GeneralException)1 Delegator (org.apache.ofbiz.entity.Delegator)1 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)1 GenericValue (org.apache.ofbiz.entity.GenericValue)1 ProductPromoUseInfo (org.apache.ofbiz.order.shoppingcart.ShoppingCart.ProductPromoUseInfo)1