Search in sources :

Example 11 with OfferItemCriteria

use of org.broadleafcommerce.core.offer.domain.OfferItemCriteria in project BroadleafCommerce by BroadleafCommerce.

the class CreateOfferUtility method createOffer.

public Offer createOffer(String offerName, OfferType offerType, OfferDiscountType discountType, double value, String orderRule, boolean stackable, boolean combinable, int priority, String qualifierRule) {
    Offer offer = offerDao.create();
    offer.setName(offerName);
    offer.setStartDate(SystemTime.asDate());
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DATE, -1);
    offer.setStartDate(calendar.getTime());
    calendar.add(Calendar.DATE, 2);
    offer.setEndDate(calendar.getTime());
    offer.setType(offerType);
    offer.setDiscountType(discountType);
    offer.setValue(BigDecimal.valueOf(value));
    if (stackable) {
        offer.setOfferItemTargetRuleType(OfferItemRestrictionRuleType.QUALIFIER_TARGET);
    }
    OfferItemCriteria oic = new OfferItemCriteriaImpl();
    oic.setQuantity(1);
    oic.setMatchRule(orderRule);
    OfferTargetCriteriaXref targetXref = new OfferTargetCriteriaXrefImpl();
    targetXref.setOffer(offer);
    targetXref.setOfferItemCriteria(oic);
    offer.setTargetItemCriteriaXref(Collections.singleton(targetXref));
    offer.setCombinableWithOtherOffers(combinable);
    if (qualifierRule != null) {
        OfferItemCriteria qoic = new OfferItemCriteriaImpl();
        qoic.setQuantity(1);
        qoic.setMatchRule(qualifierRule);
        OfferQualifyingCriteriaXref qualifyingXref = new OfferQualifyingCriteriaXrefImpl();
        qualifyingXref.setOffer(offer);
        qualifyingXref.setOfferItemCriteria(qoic);
        offer.setQualifyingItemCriteriaXref(Collections.singleton(qualifyingXref));
        offer.setOfferItemQualifierRuleType(OfferItemRestrictionRuleType.QUALIFIER_TARGET);
    }
    offer.setPriority(priority);
    offer = offerService.save(offer);
    offer.setMaxUsesPerOrder(50);
    return offer;
}
Also used : OfferItemCriteriaImpl(org.broadleafcommerce.core.offer.domain.OfferItemCriteriaImpl) OfferTargetCriteriaXrefImpl(org.broadleafcommerce.core.offer.domain.OfferTargetCriteriaXrefImpl) Offer(org.broadleafcommerce.core.offer.domain.Offer) Calendar(java.util.Calendar) OfferItemCriteria(org.broadleafcommerce.core.offer.domain.OfferItemCriteria) OfferQualifyingCriteriaXrefImpl(org.broadleafcommerce.core.offer.domain.OfferQualifyingCriteriaXrefImpl) OfferTargetCriteriaXref(org.broadleafcommerce.core.offer.domain.OfferTargetCriteriaXref) OfferQualifyingCriteriaXref(org.broadleafcommerce.core.offer.domain.OfferQualifyingCriteriaXref)

Example 12 with OfferItemCriteria

use of org.broadleafcommerce.core.offer.domain.OfferItemCriteria in project BroadleafCommerce by BroadleafCommerce.

the class OfferServiceUtilitiesImpl method orderMeetsProvidedSubtotalRequirement.

protected boolean orderMeetsProvidedSubtotalRequirement(Offer offer, HashMap<OfferItemCriteria, List<PromotableOrderItem>> promotableOrderItems, Money minSubTotal) {
    Money subtotal = Money.ZERO;
    for (OfferItemCriteria itemCriteria : promotableOrderItems.keySet()) {
        List<PromotableOrderItem> promotableItems = promotableOrderItems.get(itemCriteria);
        for (PromotableOrderItem item : promotableItems) {
            boolean shouldApplyDiscountToSalePrice = offer.getApplyDiscountToSalePrice();
            Money priceBeforeAdjustments = item.getPriceBeforeAdjustments(shouldApplyDiscountToSalePrice);
            int quantity = item.getQuantity();
            Money lineItemAmount = priceBeforeAdjustments.multiply(quantity);
            subtotal = subtotal.add(lineItemAmount);
            if (subtotal.greaterThanOrEqual(minSubTotal)) {
                return true;
            }
        }
    }
    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)

Example 13 with OfferItemCriteria

use of org.broadleafcommerce.core.offer.domain.OfferItemCriteria in project BroadleafCommerce by BroadleafCommerce.

the class OfferServiceUtilitiesImpl method markRelatedQualifiersAndTargetsForItemCriteria.

@Override
public int markRelatedQualifiersAndTargetsForItemCriteria(PromotableCandidateItemOffer itemOffer, PromotableOrder order, OrderItemHolder orderItemHolder, OfferItemCriteria itemCriteria, List<PromotableOrderItemPriceDetail> priceDetails, ItemOfferMarkTargets itemOfferMarkTargets) {
    sortQualifierItemDetails(priceDetails, itemOffer.getOffer().getApplyDiscountToSalePrice());
    // Calculate the number of qualifiers needed that will not receive the promotion.
    // These will be reserved first before the target is assigned.
    int qualifierQtyNeeded = itemCriteria.getQuantity();
    for (PromotableOrderItemPriceDetail detail : priceDetails) {
        OrderItem oi = detail.getPromotableOrderItem().getOrderItem();
        if (qualifierQtyNeeded > 0) {
            int itemQtyAvailableToBeUsedAsQualifier = detail.getQuantityAvailableToBeUsedAsQualifier(itemOffer);
            if (itemQtyAvailableToBeUsedAsQualifier > 0) {
                // We have found a qualifier that meets this offer criteria. First, we'll save some state that we
                // might need in the future.
                OfferItemCriteria previousQualifierCriteria = null;
                for (PromotionQualifier possibleQualifier : detail.getPromotionQualifiers()) {
                    if (possibleQualifier.getPromotion().equals(itemOffer.getOffer())) {
                        previousQualifierCriteria = possibleQualifier.getItemCriteria();
                        break;
                    }
                }
                // Go ahead and mark this item as a qualifier
                int qtyToMarkAsQualifier = Math.min(qualifierQtyNeeded, itemQtyAvailableToBeUsedAsQualifier);
                qualifierQtyNeeded -= qtyToMarkAsQualifier;
                PromotionQualifier pq = detail.addPromotionQualifier(itemOffer, itemCriteria, qtyToMarkAsQualifier);
                pq.setPrice(detail.getPromotableOrderItem().getPriceBeforeAdjustments(itemOffer.getOffer().getApplyDiscountToSalePrice()));
                // that there is an eligible target(s) and continue on.
                if (itemOfferMarkTargets.markTargets(itemOffer, order, oi, true)) {
                    // We found a target. Let's save this related order item used as the qualifier in case
                    // we succeed
                    orderItemHolder.setOrderItem(oi);
                } else {
                    // If we didn't find a target, we need to roll back how we marked this item as a qualifier.
                    qualifierQtyNeeded += qtyToMarkAsQualifier;
                    if (pq.getQuantity() == qtyToMarkAsQualifier) {
                        detail.getPromotionQualifiers().remove(pq);
                    } else {
                        pq.setItemCriteria(previousQualifierCriteria);
                        pq.setQuantity(pq.getQuantity() - qtyToMarkAsQualifier);
                    }
                }
            }
        }
        if (qualifierQtyNeeded == 0) {
            break;
        }
    }
    return qualifierQtyNeeded;
}
Also used : PromotionQualifier(org.broadleafcommerce.core.offer.service.discount.PromotionQualifier) PromotableOrderItem(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) OfferItemCriteria(org.broadleafcommerce.core.offer.domain.OfferItemCriteria) PromotableOrderItemPriceDetail(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItemPriceDetail)

Example 14 with OfferItemCriteria

use of org.broadleafcommerce.core.offer.domain.OfferItemCriteria in project BroadleafCommerce by BroadleafCommerce.

the class OrderOfferProcessorTest method testQualifyingQuantity.

public void testQualifyingQuantity() throws Exception {
    replay();
    PromotableOrder order = dataProvider.createBasicPromotableOrder();
    List<Offer> offers = dataProvider.createOrderBasedOffer("order.subTotal.getAmount()>20", OfferDiscountType.PERCENT_OFF);
    Offer firstOffer = offers.get(0);
    OfferItemCriteria qualCriteria = new OfferItemCriteriaImpl();
    int originalQuantityOnOrder = 5;
    qualCriteria.setQuantity(originalQuantityOnOrder + 1);
    qualCriteria.setMatchRule("([MVEL.eval(\"toUpperCase()\",\"test1\"), MVEL.eval(\"toUpperCase()\",\"test2\")] contains MVEL.eval(\"toUpperCase()\", discreteOrderItem.category.name))");
    Set<OfferQualifyingCriteriaXref> criterias = new HashSet<OfferQualifyingCriteriaXref>();
    OfferQualifyingCriteriaXref xref = new OfferQualifyingCriteriaXrefImpl();
    xref.setOffer(firstOffer);
    xref.setOfferItemCriteria(qualCriteria);
    criterias.add(xref);
    firstOffer.setQualifyingItemCriteriaXref(criterias);
    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 not included
    assertTrue(!candidates.isMatchedQualifier() && candidates.getCandidateQualifiersMap().size() == 1);
    int quantity = orderItems.get(0).getOrderItem().getQuantity();
    orderItems.get(0).getOrderItem().setQuantity(quantity + 1);
    candidates = orderProcessor.couldOfferApplyToOrderItems(offers.get(0), orderItems);
    // test that the valid order offer is included
    assertTrue(candidates.isMatchedQualifier() && candidates.getCandidateQualifiersMap().size() == 1);
    verify();
}
Also used : OfferItemCriteriaImpl(org.broadleafcommerce.core.offer.domain.OfferItemCriteriaImpl) CandidatePromotionItems(org.broadleafcommerce.core.offer.service.discount.CandidatePromotionItems) OfferItemCriteria(org.broadleafcommerce.core.offer.domain.OfferItemCriteria) ArrayList(java.util.ArrayList) PromotableOrder(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrder) PromotableOrderItem(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem) PromotableCandidateOrderOffer(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateOrderOffer) Offer(org.broadleafcommerce.core.offer.domain.Offer) OfferQualifyingCriteriaXrefImpl(org.broadleafcommerce.core.offer.domain.OfferQualifyingCriteriaXrefImpl) OfferQualifyingCriteriaXref(org.broadleafcommerce.core.offer.domain.OfferQualifyingCriteriaXref) HashSet(java.util.HashSet)

Aggregations

OfferItemCriteria (org.broadleafcommerce.core.offer.domain.OfferItemCriteria)14 Offer (org.broadleafcommerce.core.offer.domain.Offer)9 OfferItemCriteriaImpl (org.broadleafcommerce.core.offer.domain.OfferItemCriteriaImpl)7 PromotableOrderItem (org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem)7 HashSet (java.util.HashSet)5 OfferQualifyingCriteriaXref (org.broadleafcommerce.core.offer.domain.OfferQualifyingCriteriaXref)5 OfferQualifyingCriteriaXrefImpl (org.broadleafcommerce.core.offer.domain.OfferQualifyingCriteriaXrefImpl)5 PromotableOrderItemPriceDetail (org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItemPriceDetail)4 ArrayList (java.util.ArrayList)3 Money (org.broadleafcommerce.common.money.Money)3 OfferTargetCriteriaXref (org.broadleafcommerce.core.offer.domain.OfferTargetCriteriaXref)3 OfferTargetCriteriaXrefImpl (org.broadleafcommerce.core.offer.domain.OfferTargetCriteriaXrefImpl)3 Calendar (java.util.Calendar)2 List (java.util.List)2 PromotionQualifier (org.broadleafcommerce.core.offer.service.discount.PromotionQualifier)2 PromotableCandidateOrderOffer (org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateOrderOffer)2 OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)2 Map (java.util.Map)1 OfferImpl (org.broadleafcommerce.core.offer.domain.OfferImpl)1 CandidatePromotionItems (org.broadleafcommerce.core.offer.service.discount.CandidatePromotionItems)1