Search in sources :

Example 6 with OfferQualifyingCriteriaXref

use of org.broadleafcommerce.core.offer.domain.OfferQualifyingCriteriaXref 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 7 with OfferQualifyingCriteriaXref

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

the class ItemOfferProcessorTest method testCouldOrderItemMeetOfferRequirement.

public void testCouldOrderItemMeetOfferRequirement() 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))");
    OfferQualifyingCriteriaXref xref = offers.get(0).getQualifyingItemCriteriaXref().iterator().next();
    boolean couldApply = itemProcessor.couldOrderItemMeetOfferRequirement(xref.getOfferItemCriteria(), order.getDiscountableOrderItems().get(0));
    // test that the valid order item offer is included
    assertTrue(couldApply);
    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))");
    xref = offers.get(0).getQualifyingItemCriteriaXref().iterator().next();
    couldApply = itemProcessor.couldOrderItemMeetOfferRequirement(xref.getOfferItemCriteria(), order.getDiscountableOrderItems().get(0));
    // test that the invalid order item offer is excluded
    assertFalse(couldApply);
    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) PromotableOrder(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrder) OfferQualifyingCriteriaXref(org.broadleafcommerce.core.offer.domain.OfferQualifyingCriteriaXref)

Example 8 with OfferQualifyingCriteriaXref

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

the class ItemOfferProcessorTest method testApplyAdjustments.

public void testApplyAdjustments() throws Exception {
    replay();
    Order order = dataProvider.createBasicOrder();
    Offer offer1 = 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);
    offer1.setId(1L);
    OfferQualifyingCriteriaXref xref = offer1.getQualifyingItemCriteriaXref().iterator().next();
    xref.getOfferItemCriteria().setQuantity(2);
    offer1.setCombinableWithOtherOffers(false);
    Offer offer2 = 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);
    offer2.setId(2L);
    List<Offer> offerListWithOneOffer = new ArrayList<Offer>();
    offerListWithOneOffer.add(offer1);
    List<Offer> offerListWithTwoOffers = new ArrayList<Offer>();
    offerListWithTwoOffers.add(offer1);
    offerListWithTwoOffers.add(offer2);
    order.updatePrices();
    offerService.applyAndSaveOffersToOrder(offerListWithOneOffer, order);
    assertTrue(checkOrderItemOfferAppliedCount(order) == 1);
    assertTrue(checkOrderItemOfferAppliedQuantity(order, offer1) == 1);
    // Add the second offer.   The first was nonCombinable so it should still be 1
    order = dataProvider.createBasicOrder();
    order.updatePrices();
    offerService.applyAndSaveOffersToOrder(offerListWithTwoOffers, order);
    assertTrue(checkOrderItemOfferAppliedCount(order) == 2);
    assertTrue(checkOrderItemOfferAppliedQuantity(order, offer2) == 2);
    assertTrue(checkOrderItemOfferAppliedQuantity(order, offer1) == 0);
    // Reset offer1 to combinable.   Now both should be applied.
    offer1.setCombinableWithOtherOffers(true);
    order = dataProvider.createBasicOrder();
    order.updatePrices();
    offerService.applyAndSaveOffersToOrder(offerListWithTwoOffers, order);
    assertTrue(checkOrderItemOfferAppliedCount(order) == 2);
    assertTrue(checkOrderItemOfferAppliedQuantity(order, offer2) == 2);
    assertTrue(checkOrderItemOfferAppliedQuantity(order, offer1) == 0);
    // Offer 1 back to nonCombinable but don't allow discount to the sale price
    // and make the sale price a better overall offer
    offer1.setCombinableWithOtherOffers(false);
    offer1.setApplyDiscountToSalePrice(false);
    order = dataProvider.createBasicOrder();
    ((DiscreteOrderItem) order.getOrderItems().get(1)).getSku().setSalePrice(new Money(10D));
    order.updatePrices();
    offerService.applyAndSaveOffersToOrder(offerListWithOneOffer, order);
    assertTrue(checkOrderItemOfferAppliedCount(order) == 0);
    // Try again with two offers.   The second should be applied.
    order.updatePrices();
    offerService.applyAndSaveOffersToOrder(offerListWithTwoOffers, order);
    assertTrue(checkOrderItemOfferAppliedCount(order) == 2);
    // Trying with 2nd offer as nonCombinable.
    offer1.setCombinableWithOtherOffers(true);
    ((DiscreteOrderItem) order.getOrderItems().get(1)).getSku().setSalePrice(null);
    offer2.setCombinableWithOtherOffers(false);
    order.updatePrices();
    offerService.applyAndSaveOffersToOrder(offerListWithOneOffer, order);
    assertTrue(checkOrderItemOfferAppliedCount(order) == 1);
    order.updatePrices();
    offerService.applyAndSaveOffersToOrder(offerListWithTwoOffers, order);
    assertTrue(checkOrderItemOfferAppliedQuantity(order, offer2) == 2);
    assertTrue(checkOrderItemOfferAppliedQuantity(order, offer1) == 0);
    // Set qualifying criteria quantity to 1
    // Set qualifying target criteria to 2
    order = dataProvider.createBasicOrder();
    xref = offer1.getQualifyingItemCriteriaXref().iterator().next();
    xref.getOfferItemCriteria().setQuantity(1);
    OfferTargetCriteriaXref targetXref = offer1.getTargetItemCriteriaXref().iterator().next();
    targetXref.getOfferItemCriteria().setQuantity(2);
    order.updatePrices();
    offerService.applyAndSaveOffersToOrder(offerListWithOneOffer, order);
    assertTrue(checkOrderItemOfferAppliedQuantity(order, offer1) == 2);
    // Reset both offers to combinable and the qualifiers as allowing duplicate QUALIFIERs
    // and Targets
    offer1.setCombinableWithOtherOffers(true);
    offer2.setCombinableWithOtherOffers(true);
    offer1.setOfferItemQualifierRuleType(OfferItemRestrictionRuleType.QUALIFIER_TARGET);
    offer1.setOfferItemTargetRuleType(OfferItemRestrictionRuleType.QUALIFIER_TARGET);
    offer2.setOfferItemQualifierRuleType(OfferItemRestrictionRuleType.QUALIFIER_TARGET);
    offer2.setOfferItemTargetRuleType(OfferItemRestrictionRuleType.QUALIFIER_TARGET);
    order = dataProvider.createBasicOrder();
    order.updatePrices();
    offerService.applyAndSaveOffersToOrder(offerListWithTwoOffers, order);
    assertTrue(checkOrderItemOfferAppliedCount(order) == 4);
    assertTrue(checkOrderItemOfferAppliedQuantity(order, offer2) == 2);
    assertTrue(checkOrderItemOfferAppliedQuantity(order, offer1) == 2);
    verify();
}
Also used : PromotableOrder(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrder) Order(org.broadleafcommerce.core.order.domain.Order) Money(org.broadleafcommerce.common.money.Money) PromotableCandidateItemOffer(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateItemOffer) CandidateItemOffer(org.broadleafcommerce.core.offer.domain.CandidateItemOffer) Offer(org.broadleafcommerce.core.offer.domain.Offer) ArrayList(java.util.ArrayList) OfferTargetCriteriaXref(org.broadleafcommerce.core.offer.domain.OfferTargetCriteriaXref) OfferQualifyingCriteriaXref(org.broadleafcommerce.core.offer.domain.OfferQualifyingCriteriaXref)

Example 9 with OfferQualifyingCriteriaXref

use of org.broadleafcommerce.core.offer.domain.OfferQualifyingCriteriaXref 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

Offer (org.broadleafcommerce.core.offer.domain.Offer)9 OfferQualifyingCriteriaXref (org.broadleafcommerce.core.offer.domain.OfferQualifyingCriteriaXref)9 OfferItemCriteria (org.broadleafcommerce.core.offer.domain.OfferItemCriteria)5 OfferItemCriteriaImpl (org.broadleafcommerce.core.offer.domain.OfferItemCriteriaImpl)5 OfferQualifyingCriteriaXrefImpl (org.broadleafcommerce.core.offer.domain.OfferQualifyingCriteriaXrefImpl)5 PromotableOrder (org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrder)5 HashSet (java.util.HashSet)4 CandidateItemOffer (org.broadleafcommerce.core.offer.domain.CandidateItemOffer)3 ArrayList (java.util.ArrayList)2 OfferTargetCriteriaXref (org.broadleafcommerce.core.offer.domain.OfferTargetCriteriaXref)2 PromotableCandidateItemOffer (org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateItemOffer)2 PromotableCandidateOrderOffer (org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateOrderOffer)2 Calendar (java.util.Calendar)1 Money (org.broadleafcommerce.common.money.Money)1 CandidateFulfillmentGroupOffer (org.broadleafcommerce.core.offer.domain.CandidateFulfillmentGroupOffer)1 OfferTargetCriteriaXrefImpl (org.broadleafcommerce.core.offer.domain.OfferTargetCriteriaXrefImpl)1 CandidatePromotionItems (org.broadleafcommerce.core.offer.service.discount.CandidatePromotionItems)1 PromotableCandidateFulfillmentGroupOffer (org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateFulfillmentGroupOffer)1 PromotableOrderItem (org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem)1 Order (org.broadleafcommerce.core.order.domain.Order)1