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);
offer1.getQualifyingItemCriteria().iterator().next().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);
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();
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();
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();
order.getOrderItems().get(1).setSalePrice(new Money(10D));
offerService.applyAndSaveOffersToOrder(offerListWithOneOffer, order);
assertTrue(checkOrderItemOfferAppliedCount(order) == 0);
// Try again with two offers. The second should be applied.
offerService.applyAndSaveOffersToOrder(offerListWithTwoOffers, order);
assertTrue(checkOrderItemOfferAppliedCount(order) == 2);
// Trying with 2nd offer as nonCombinable.
offer1.setCombinableWithOtherOffers(true);
order.getOrderItems().get(1).setSalePrice(null);
offer2.setCombinableWithOtherOffers(false);
offerService.applyAndSaveOffersToOrder(offerListWithOneOffer, order);
assertTrue(checkOrderItemOfferAppliedCount(order) == 1);
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();
offer1.getQualifyingItemCriteria().iterator().next().setQuantity(1);
offer1.getTargetItemCriteria().iterator().next().setQuantity(2);
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();
offerService.applyAndSaveOffersToOrder(offerListWithTwoOffers, order);
assertTrue(checkOrderItemOfferAppliedCount(order) == 4);
assertTrue(checkOrderItemOfferAppliedQuantity(order, offer2) == 2);
assertTrue(checkOrderItemOfferAppliedQuantity(order, offer1) == 2);