use of org.broadleafcommerce.core.offer.domain.OfferTargetCriteriaXref in project BroadleafCommerce by BroadleafCommerce.
the class OfferDataItemProvider method createItemBasedOffer.
public List<Offer> createItemBasedOffer(String orderRule, String targetRule, OfferDiscountType discountType) {
List<Offer> offers = createOrderBasedOffer(orderRule, discountType);
offers.get(0).setType(OfferType.ORDER_ITEM);
if (targetRule != null) {
Offer offer = offers.get(0);
OfferItemCriteria targetCriteria = new OfferItemCriteriaImpl();
// targetCriteria.setQualifyingOffer(offers.get(0));
targetCriteria.setQuantity(1);
targetCriteria.setMatchRule(targetRule);
OfferTargetCriteriaXref targetXref = new OfferTargetCriteriaXrefImpl();
targetXref.setOffer(offer);
targetXref.setOfferItemCriteria(targetCriteria);
offer.setTargetItemCriteriaXref(Collections.singleton(targetXref));
}
return offers;
}
use of org.broadleafcommerce.core.offer.domain.OfferTargetCriteriaXref in project BroadleafCommerce by BroadleafCommerce.
the class PromotableCandidateItemOfferImpl method calculateMaximumNumberOfUses.
/**
* Determines the maximum number of times this promotion can be used based on the
* ItemCriteria and promotion's maxQty setting.
*/
@Override
public int calculateMaximumNumberOfUses() {
// set arbitrarily high / algorithm will adjust down
int maxMatchesFound = 9999;
// iterate through the target criteria and find the least amount of max uses. This will be the overall
// max usage, since the target criteria are grouped together in "and" style.
int numberOfUsesForThisItemCriteria = maxMatchesFound;
for (OfferTargetCriteriaXref targetXref : getOffer().getTargetItemCriteriaXref()) {
int temp = calculateMaxUsesForItemCriteria(targetXref.getOfferItemCriteria(), getOffer());
numberOfUsesForThisItemCriteria = Math.min(numberOfUsesForThisItemCriteria, temp);
}
maxMatchesFound = Math.min(maxMatchesFound, numberOfUsesForThisItemCriteria);
int offerMaxUses = getOffer().isUnlimitedUsePerOrder() ? maxMatchesFound : getOffer().getMaxUsesPerOrder();
return Math.min(maxMatchesFound, offerMaxUses);
}
use of org.broadleafcommerce.core.offer.domain.OfferTargetCriteriaXref in project BroadleafCommerce by BroadleafCommerce.
the class PricingTest method createOffer.
private Offer createOffer(String offerName, OfferType offerType, OfferDiscountType discountType, double value, String customerRule, String orderRule) {
Offer offer = new OfferImpl();
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));
offer.setAutomaticallyAdded(true);
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(true);
offer = offerService.save(offer);
offer.setMaxUsesPerOrder(50);
return offer;
}
use of org.broadleafcommerce.core.offer.domain.OfferTargetCriteriaXref 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;
}
use of org.broadleafcommerce.core.offer.domain.OfferTargetCriteriaXref in project BroadleafCommerce by BroadleafCommerce.
the class ItemOfferProcessorTest method testApplyItemQualifiersAndTargets.
public void testApplyItemQualifiersAndTargets() throws Exception {
replay();
List<PromotableCandidateItemOffer> qualifiedOffers = new ArrayList<PromotableCandidateItemOffer>();
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);
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);
OfferTargetCriteriaXref targetXref = offer2.getTargetItemCriteriaXref().iterator().next();
targetXref.getOfferItemCriteria().setQuantity(4);
offer2.getQualifyingItemCriteriaXref().clear();
offer2.setOfferItemTargetRuleType(OfferItemRestrictionRuleType.TARGET);
Offer offer3 = 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);
PromotableOrder promotableOrder = dataProvider.createBasicPromotableOrder();
itemProcessor.filterItemLevelOffer(promotableOrder, qualifiedOffers, offer1);
assertTrue(qualifiedOffers.size() == 1 && qualifiedOffers.get(0).getOffer().equals(offer1) && qualifiedOffers.get(0).getCandidateQualifiersMap().size() == 1);
itemProcessor.filterItemLevelOffer(promotableOrder, qualifiedOffers, offer2);
assertTrue(qualifiedOffers.size() == 2 && qualifiedOffers.get(1).getOffer().equals(offer2) && qualifiedOffers.get(1).getCandidateQualifiersMap().size() == 0);
itemProcessor.filterItemLevelOffer(promotableOrder, qualifiedOffers, offer3);
assertTrue(qualifiedOffers.size() == 3 && qualifiedOffers.get(2).getOffer().equals(offer3) && qualifiedOffers.get(2).getCandidateQualifiersMap().size() == 1);
// Try with just the second offer. Expect to get 4 targets based on the offer having no qualifiers required
// and targeting category test1 or test2 and that the offer requires 4 target criteria.
Order order = dataProvider.createBasicOrder();
List<Offer> offerList = new ArrayList<Offer>();
offerList.add(offer2);
offerService.applyAndSaveOffersToOrder(offerList, order);
assertTrue(checkOrderItemOfferAppliedQuantity(order, offer2) == 4);
assertTrue(countPriceDetails(order) == 3);
// Now try with both offers. Since the targets can be reused, we expect to have 4 targets on offer2
// and 1 target on offer1
order = dataProvider.createBasicOrder();
// add in second offer (which happens to be offer1)
offerList.add(offer1);
offerService.applyAndSaveOffersToOrder(offerList, order);
assertTrue(checkOrderItemOfferAppliedQuantity(order, offer2) == 4);
assertTrue(countPriceDetails(order) == 3);
// All three offers - offer 3 is now higher priority so the best offer (offer 2) won't be applied
order = dataProvider.createBasicOrder();
// add in second offer (which happens to be offer1)
offerList.add(offer3);
offer3.setPriority(-1);
offerService.applyAndSaveOffersToOrder(offerList, order);
assertTrue(checkOrderItemOfferAppliedQuantity(order, offer3) == 2);
assertTrue(countPriceDetails(order) == 4);
verify();
}
Aggregations