Search in sources :

Example 1 with PromotableOrderItem

use of org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem in project BroadleafCommerce by BroadleafCommerce.

the class CandidateItemOfferTest method setUp.

@Override
protected void setUp() throws Exception {
    OfferDataItemProvider dataProvider = new OfferDataItemProvider();
    CandidateItemOfferImpl candidate = new CandidateItemOfferImpl();
    Category category1 = new CategoryImpl();
    category1.setName("test1");
    category1.setId(1L);
    Product product1 = new ProductImpl();
    Sku sku1 = new SkuImpl();
    sku1.setName("test1");
    sku1.setDiscountable(true);
    sku1.setRetailPrice(new Money(19.99D));
    product1.setDefaultSku(sku1);
    CategoryProductXref xref1 = new CategoryProductXrefImpl();
    xref1.setProduct(product1);
    xref1.setCategory(category1);
    category1.getAllProductXrefs().add(xref1);
    Category category2 = new CategoryImpl();
    category2.setName("test2");
    category2.setId(2L);
    Product product2 = new ProductImpl();
    Sku sku2 = new SkuImpl();
    sku2.setName("test2");
    sku2.setDiscountable(true);
    sku2.setRetailPrice(new Money(29.99D));
    product2.setDefaultSku(sku2);
    CategoryProductXref xref2 = new CategoryProductXrefImpl();
    xref2.setProduct(product2);
    xref2.setCategory(category2);
    category2.getAllProductXrefs().add(xref2);
    DiscreteOrderItemImpl orderItem1 = new DiscreteOrderItemImpl();
    orderItem1.setCategory(category1);
    orderItem1.setName("test1");
    orderItem1.setOrderItemType(OrderItemType.DISCRETE);
    orderItem1.setProduct(product1);
    orderItem1.setQuantity(2);
    orderItem1.setSku(sku1);
    Order order = new OrderImpl();
    orderItem1.setOrder(order);
    promotableOrder = new PromotableOrderImpl(order, new PromotableItemFactoryImpl(), false);
    offer = dataProvider.createItemBasedOfferWithItemCriteria("order.subTotal.getAmount()>20", OfferDiscountType.PERCENT_OFF, "([MVEL.eval(\"toUpperCase()\",\"test1\"), MVEL.eval(\"toUpperCase()\",\"test2\")] contains MVEL.eval(\"toUpperCase()\", discreteOrderItem.category.name))", "([MVEL.eval(\"toUpperCase()\",\"test1\"), MVEL.eval(\"toUpperCase()\",\"test2\")] contains MVEL.eval(\"toUpperCase()\", discreteOrderItem.category.name))").get(0);
    candidateOffer = new PromotableCandidateItemOfferImpl(promotableOrder, offer);
    promotableOrderItem = new PromotableOrderItemImpl(orderItem1, null, new PromotableItemFactoryImpl(), false);
    OrderItemPriceDetail pdetail = new OrderItemPriceDetailImpl();
    pdetail.setOrderItem(orderItem1);
    pdetail.setQuantity(2);
    priceDetail = new PromotableOrderItemPriceDetailImpl(promotableOrderItem, 2);
    List<PromotableOrderItem> items = new ArrayList<PromotableOrderItem>();
    items.add(promotableOrderItem);
    promotableCandidate = new PromotableCandidateItemOfferImpl(promotableOrder, offer);
    OfferTargetCriteriaXref xref = offer.getTargetItemCriteriaXref().iterator().next();
    promotableCandidate.getCandidateTargetsMap().put(xref.getOfferItemCriteria(), items);
}
Also used : Category(org.broadleafcommerce.core.catalog.domain.Category) PromotableOrderImpl(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderImpl) CategoryProductXref(org.broadleafcommerce.core.catalog.domain.CategoryProductXref) DiscreteOrderItemImpl(org.broadleafcommerce.core.order.domain.DiscreteOrderItemImpl) OrderItemPriceDetailImpl(org.broadleafcommerce.core.order.domain.OrderItemPriceDetailImpl) PromotableOrderItemPriceDetailImpl(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItemPriceDetailImpl) ArrayList(java.util.ArrayList) Product(org.broadleafcommerce.core.catalog.domain.Product) PromotableCandidateItemOfferImpl(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateItemOfferImpl) PromotableOrderItem(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem) Money(org.broadleafcommerce.common.money.Money) PromotableItemFactoryImpl(org.broadleafcommerce.core.offer.service.discount.domain.PromotableItemFactoryImpl) CategoryProductXrefImpl(org.broadleafcommerce.core.catalog.domain.CategoryProductXrefImpl) PromotableOrder(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrder) Order(org.broadleafcommerce.core.order.domain.Order) PromotableOrderItemImpl(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItemImpl) CategoryImpl(org.broadleafcommerce.core.catalog.domain.CategoryImpl) SkuImpl(org.broadleafcommerce.core.catalog.domain.SkuImpl) ProductImpl(org.broadleafcommerce.core.catalog.domain.ProductImpl) PromotableOrderImpl(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderImpl) OrderImpl(org.broadleafcommerce.core.order.domain.OrderImpl) PromotableCandidateItemOfferImpl(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateItemOfferImpl) OfferDataItemProvider(org.broadleafcommerce.core.offer.service.OfferDataItemProvider) OrderItemPriceDetail(org.broadleafcommerce.core.order.domain.OrderItemPriceDetail) PromotableOrderItemPriceDetail(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItemPriceDetail) Sku(org.broadleafcommerce.core.catalog.domain.Sku) PromotableOrderItemPriceDetailImpl(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItemPriceDetailImpl)

Example 2 with PromotableOrderItem

use of org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem in project BroadleafCommerce by BroadleafCommerce.

the class ItemOfferProcessorTest method testCouldOfferApplyToOrderItems.

public void testCouldOfferApplyToOrderItems() throws Exception {
    replay();
    PromotableOrder order = dataProvider.createBasicPromotableOrder();
    List<Offer> offers = dataProvider.createItemBasedOfferWithItemCriteria("order.subTotal.getAmount()>20", OfferDiscountType.PERCENT_OFF, "([MVEL.eval(\"toUpperCase()\",\"test1\"), MVEL.eval(\"toUpperCase()\",\"test2\")] contains MVEL.eval(\"toUpperCase()\", discreteOrderItem.category.name))", "([MVEL.eval(\"toUpperCase()\",\"test1\"), MVEL.eval(\"toUpperCase()\",\"test2\")] contains MVEL.eval(\"toUpperCase()\", discreteOrderItem.category.name))");
    List<PromotableOrderItem> orderItems = new ArrayList<PromotableOrderItem>();
    for (PromotableOrderItem orderItem : order.getDiscountableOrderItems()) {
        orderItems.add(orderItem);
    }
    CandidatePromotionItems candidates = itemProcessor.couldOfferApplyToOrderItems(offers.get(0), orderItems);
    // test that the valid order item offer is included
    // both cart items are valid for qualification and target
    assertTrue(candidates.isMatchedQualifier() && candidates.getCandidateQualifiersMap().size() == 1 && candidates.getCandidateQualifiersMap().values().iterator().next().size() == 2 && candidates.isMatchedTarget() && candidates.getCandidateTargetsMap().size() == 1 && candidates.getCandidateTargetsMap().values().iterator().next().size() == 2);
    offers = dataProvider.createItemBasedOfferWithItemCriteria("order.subTotal.getAmount()>20", OfferDiscountType.PERCENT_OFF, "([MVEL.eval(\"toUpperCase()\",\"test5\"), MVEL.eval(\"toUpperCase()\",\"test6\")] contains MVEL.eval(\"toUpperCase()\", discreteOrderItem.category.name))", "([MVEL.eval(\"toUpperCase()\",\"test5\"), MVEL.eval(\"toUpperCase()\",\"test6\")] contains MVEL.eval(\"toUpperCase()\", discreteOrderItem.category.name))");
    candidates = itemProcessor.couldOfferApplyToOrderItems(offers.get(0), orderItems);
    // test that the invalid order item offer is excluded because there are no qualifying items
    assertFalse(candidates.isMatchedQualifier() && candidates.getCandidateQualifiersMap().size() == 1);
    verify();
}
Also used : PromotableCandidateItemOffer(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateItemOffer) CandidateItemOffer(org.broadleafcommerce.core.offer.domain.CandidateItemOffer) Offer(org.broadleafcommerce.core.offer.domain.Offer) CandidatePromotionItems(org.broadleafcommerce.core.offer.service.discount.CandidatePromotionItems) ArrayList(java.util.ArrayList) PromotableOrder(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrder) PromotableOrderItem(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem)

Example 3 with PromotableOrderItem

use of org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem in project BroadleafCommerce by BroadleafCommerce.

the class OrderOfferProcessorTest method testCouldOfferApplyToOrderItems.

public void testCouldOfferApplyToOrderItems() throws Exception {
    replay();
    PromotableOrder order = dataProvider.createBasicPromotableOrder();
    List<Offer> offers = dataProvider.createOrderBasedOfferWithItemCriteria("order.subTotal.getAmount()>20", OfferDiscountType.PERCENT_OFF, "([MVEL.eval(\"toUpperCase()\",\"test1\"), MVEL.eval(\"toUpperCase()\",\"test2\")] contains MVEL.eval(\"toUpperCase()\", discreteOrderItem.category.name))");
    List<PromotableOrderItem> orderItems = new ArrayList<PromotableOrderItem>();
    for (PromotableOrderItem orderItem : order.getDiscountableOrderItems()) {
        orderItems.add(orderItem);
    }
    CandidatePromotionItems candidates = orderProcessor.couldOfferApplyToOrderItems(offers.get(0), orderItems);
    // test that the valid order offer is included
    assertTrue(candidates.isMatchedQualifier() && candidates.getCandidateQualifiersMap().size() == 1);
    offers = dataProvider.createOrderBasedOfferWithItemCriteria("order.subTotal.getAmount()>20", OfferDiscountType.PERCENT_OFF, "([MVEL.eval(\"toUpperCase()\",\"test5\"), MVEL.eval(\"toUpperCase()\",\"test6\")] contains MVEL.eval(\"toUpperCase()\", discreteOrderItem.category.name))");
    candidates = orderProcessor.couldOfferApplyToOrderItems(offers.get(0), orderItems);
    // test that the invalid order offer is excluded because there are no qualifying items
    assertFalse(candidates.isMatchedQualifier() && candidates.getCandidateQualifiersMap().size() == 1);
    verify();
}
Also used : PromotableCandidateOrderOffer(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateOrderOffer) Offer(org.broadleafcommerce.core.offer.domain.Offer) CandidatePromotionItems(org.broadleafcommerce.core.offer.service.discount.CandidatePromotionItems) ArrayList(java.util.ArrayList) PromotableOrder(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrder) PromotableOrderItem(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem)

Example 4 with PromotableOrderItem

use of org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem in project BroadleafCommerce by BroadleafCommerce.

the class AbstractBaseProcessor method meetsItemQualifierSubtotal.

protected boolean meetsItemQualifierSubtotal(Offer offer, CandidatePromotionItems candidateItem) {
    Money qualifyingSubtotal = offer.getQualifyingItemSubTotal();
    if (!hasPositiveValue(qualifyingSubtotal)) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("Offer " + offer.getName() + " does not have an item subtotal requirement.");
        }
        return true;
    }
    if (isEmpty(offer.getQualifyingItemCriteriaXref())) {
        if (OfferType.ORDER_ITEM.equals(offer.getType())) {
            if (LOG.isWarnEnabled()) {
                LOG.warn("Offer " + offer.getName() + " has a subtotal item requirement but no item qualification criteria.");
            }
            return false;
        } else {
            // Checking if targets meet subtotal for item offer with no item criteria.
            Money accumulatedTotal = null;
            for (PromotableOrderItem orderItem : candidateItem.getAllCandidateTargets()) {
                Money itemPrice = orderItem.getCurrentBasePrice().multiply(orderItem.getQuantity());
                accumulatedTotal = accumulatedTotal == null ? itemPrice : accumulatedTotal.add(itemPrice);
                if (accumulatedTotal.greaterThan(qualifyingSubtotal)) {
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("Offer " + offer.getName() + " meets qualifying item subtotal.");
                    }
                    return true;
                }
            }
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Offer " + offer.getName() + " does not meet qualifying item subtotal.");
        }
    } else {
        if (candidateItem.getCandidateQualifiersMap() != null) {
            Money accumulatedTotal = null;
            Set<PromotableOrderItem> usedItems = new HashSet<PromotableOrderItem>();
            for (OfferItemCriteria criteria : candidateItem.getCandidateQualifiersMap().keySet()) {
                List<PromotableOrderItem> promotableItems = candidateItem.getCandidateQualifiersMap().get(criteria);
                if (promotableItems != null) {
                    for (PromotableOrderItem item : promotableItems) {
                        if (!usedItems.contains(item)) {
                            usedItems.add(item);
                            Money itemPrice = item.getCurrentBasePrice().multiply(item.getQuantity());
                            accumulatedTotal = accumulatedTotal == null ? itemPrice : accumulatedTotal.add(itemPrice);
                            if (accumulatedTotal.greaterThan(qualifyingSubtotal)) {
                                if (LOG.isTraceEnabled()) {
                                    LOG.trace("Offer " + offer.getName() + " meets the item subtotal requirement.");
                                }
                                return true;
                            }
                        }
                    }
                }
            }
        }
    }
    if (LOG.isTraceEnabled()) {
        LOG.trace("Offer " + offer.getName() + " does not meet the item subtotal qualifications.");
    }
    return false;
}
Also used : Money(org.broadleafcommerce.common.money.Money) OfferItemCriteria(org.broadleafcommerce.core.offer.domain.OfferItemCriteria) PromotableOrderItem(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem) HashSet(java.util.HashSet)

Example 5 with PromotableOrderItem

use of org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem in project BroadleafCommerce by BroadleafCommerce.

the class ItemOfferProcessorImpl method calculatePotentialSavings.

/**
 * This method determines the potential savings for each item offer as if it was the only item offer being applied.
 * @param itemOffers
 * @param order
 */
protected void calculatePotentialSavings(List<PromotableCandidateItemOffer> itemOffers, PromotableOrder order) {
    if (itemOffers.size() > 1) {
        for (PromotableCandidateItemOffer itemOffer : itemOffers) {
            Money potentialSavings = new Money(order.getOrderCurrency());
            Offer offer = itemOffer.getOffer();
            BigDecimal calculatedWeightedPercent = new BigDecimal(0);
            markQualifiersAndTargets(order, itemOffer);
            for (PromotableOrderItemPriceDetail detail : order.getAllPromotableOrderItemPriceDetails()) {
                PromotableOrderItem item = detail.getPromotableOrderItem();
                for (PromotionDiscount discount : detail.getPromotionDiscounts()) {
                    Money itemSavings = calculatePotentialSavingsForOrderItem(itemOffer, item, discount.getQuantity());
                    potentialSavings = potentialSavings.add(itemSavings);
                    if (useCalculatePercent(offer)) {
                        BigDecimal discountPercent = calculatePercent(item.calculateTotalWithoutAdjustments(), itemSavings);
                        calculatedWeightedPercent = calculatedWeightedPercent.add(discountPercent);
                    } else if (hasQualifierAndQualifierRestricted(offer)) {
                        BigDecimal discountPercent = calculateWeightedPercent(discount, item, itemSavings);
                        calculatedWeightedPercent = calculatedWeightedPercent.add(discountPercent);
                    }
                }
                // Reset state back for next offer
                detail.getPromotionDiscounts().clear();
                detail.getPromotionQualifiers().clear();
            }
            itemOffer.setPotentialSavings(potentialSavings);
            if (usePercentOffValue(offer)) {
                itemOffer.setWeightedPercentSaved(offer.getValue());
            } else if (useCalculatePercent(offer) || hasQualifierAndQualifierRestricted(offer)) {
                itemOffer.setWeightedPercentSaved(calculatedWeightedPercent);
            }
            if (itemOffer.getUses() == 0) {
                itemOffer.setPotentialSavingsQtyOne(potentialSavings);
            } else {
                itemOffer.setPotentialSavingsQtyOne(potentialSavings.divide(itemOffer.getUses()));
            }
        }
    }
}
Also used : Money(org.broadleafcommerce.common.money.Money) PromotableCandidateItemOffer(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateItemOffer) PromotableCandidateItemOffer(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateItemOffer) PromotableCandidateOrderOffer(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateOrderOffer) Offer(org.broadleafcommerce.core.offer.domain.Offer) BigDecimal(java.math.BigDecimal) PromotableOrderItem(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem) PromotionDiscount(org.broadleafcommerce.core.offer.service.discount.PromotionDiscount) PromotableOrderItemPriceDetail(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItemPriceDetail)

Aggregations

PromotableOrderItem (org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem)19 ArrayList (java.util.ArrayList)8 CandidatePromotionItems (org.broadleafcommerce.core.offer.service.discount.CandidatePromotionItems)7 PromotableOrder (org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrder)7 Money (org.broadleafcommerce.common.money.Money)6 Offer (org.broadleafcommerce.core.offer.domain.Offer)6 OfferItemCriteria (org.broadleafcommerce.core.offer.domain.OfferItemCriteria)6 PromotableOrderItemPriceDetail (org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItemPriceDetail)6 PromotableCandidateItemOffer (org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateItemOffer)5 PromotableCandidateOrderOffer (org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateOrderOffer)5 PromotableFulfillmentGroup (org.broadleafcommerce.core.offer.service.discount.domain.PromotableFulfillmentGroup)3 OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)3 HashSet (java.util.HashSet)2 List (java.util.List)2 CandidateItemOffer (org.broadleafcommerce.core.offer.domain.CandidateItemOffer)2 OfferDataItemProvider (org.broadleafcommerce.core.offer.service.OfferDataItemProvider)2 PromotableCandidateFulfillmentGroupOffer (org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateFulfillmentGroupOffer)2 PromotableCandidateItemOfferImpl (org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateItemOfferImpl)2 PromotableItemFactoryImpl (org.broadleafcommerce.core.offer.service.discount.domain.PromotableItemFactoryImpl)2 PromotableOrderImpl (org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderImpl)2