use of org.broadleafcommerce.core.offer.service.discount.PromotionQualifier in project BroadleafCommerce by BroadleafCommerce.
the class OrderItemTest method testGetQuantityAvailableToBeUsedAsTarget.
public void testGetQuantityAvailableToBeUsedAsTarget() throws Exception {
int quantity = priceDetail1.getQuantityAvailableToBeUsedAsTarget(candidateOffer);
// no previous qualifiers, so all quantity is available
assertTrue(quantity == 2);
PromotionDiscount discount = new PromotionDiscount();
discount.setPromotion(offer);
discount.setQuantity(1);
priceDetail1.getPromotionDiscounts().add(discount);
quantity = priceDetail1.getQuantityAvailableToBeUsedAsTarget(candidateOffer);
// items that have already received this promotion cannot get it again
assertTrue(quantity == 1);
Offer tempOffer = new OfferImpl();
tempOffer.setCombinableWithOtherOffers(true);
tempOffer.setOfferItemQualifierRuleType(OfferItemRestrictionRuleType.NONE);
tempOffer.setOfferItemTargetRuleType(OfferItemRestrictionRuleType.NONE);
discount.setPromotion(tempOffer);
quantity = priceDetail1.getQuantityAvailableToBeUsedAsTarget(candidateOffer);
// this item received a different promotion, but the restriction rule is NONE, so this item cannot be a qualifier
// for this promotion
assertTrue(quantity == 1);
tempOffer.setOfferItemTargetRuleType(OfferItemRestrictionRuleType.TARGET);
quantity = priceDetail1.getQuantityAvailableToBeUsedAsTarget(candidateOffer);
// this item received a different promotion, but the restriction rule is TARGET,
// so this item can be a target of this promotion but since the "candidateOffer"
// is set to NONE, the quantity can only be 1
assertTrue(quantity == 1);
// Now set the candidateOffer to be "TARGET" and we can use the quantity
// for both promotions.
candidateOffer.getOffer().setOfferItemTargetRuleType(OfferItemRestrictionRuleType.TARGET);
quantity = priceDetail1.getQuantityAvailableToBeUsedAsTarget(candidateOffer);
// this item received a different promotion, but the restriction rule is TARGET,
// so this item can be a target of this promotion but since the "candidateOffer"
// is set to NONE, the quantity can only be 1
assertTrue(quantity == 2);
priceDetail1.getPromotionDiscounts().clear();
// rest candidate offer
candidateOffer.getOffer().setOfferItemTargetRuleType(OfferItemRestrictionRuleType.NONE);
PromotionQualifier qualifier = new PromotionQualifier();
qualifier.setPromotion(offer);
qualifier.setQuantity(1);
priceDetail1.getPromotionQualifiers().add(qualifier);
quantity = priceDetail1.getQuantityAvailableToBeUsedAsTarget(candidateOffer);
// items that have already qualified for this promotion cannot qualify again
assertTrue(quantity == 1);
qualifier.setPromotion(tempOffer);
quantity = priceDetail1.getQuantityAvailableToBeUsedAsTarget(candidateOffer);
// this item qualified for a different promotion, but the restriction rule is NONE,
// so this item cannot be a qualifier for this promotion
assertTrue(quantity == 1);
tempOffer.setOfferItemQualifierRuleType(OfferItemRestrictionRuleType.TARGET);
candidateOffer.getOffer().setOfferItemTargetRuleType(OfferItemRestrictionRuleType.QUALIFIER);
quantity = priceDetail1.getQuantityAvailableToBeUsedAsTarget(candidateOffer);
// this item qualified for a different promotion, but the restriction rule is QUALIFIER,
// so this item can be a qualifier for this promotion
// dpc disabling this test for now assertTrue(quantity==2);
}
use of org.broadleafcommerce.core.offer.service.discount.PromotionQualifier in project BroadleafCommerce by BroadleafCommerce.
the class OrderItemTest method testGetQuantityAvailableToBeUsedAsQualifier.
public void testGetQuantityAvailableToBeUsedAsQualifier() throws Exception {
int quantity = priceDetail1.getQuantityAvailableToBeUsedAsQualifier(candidateOffer);
// no previous qualifiers, so all quantity is available
assertTrue(quantity == 2);
PromotionDiscount discount = new PromotionDiscount();
discount.setPromotion(offer);
discount.setQuantity(1);
priceDetail1.getPromotionDiscounts().add(discount);
quantity = priceDetail1.getQuantityAvailableToBeUsedAsQualifier(candidateOffer);
// items that have already received this promotion cannot get it again
assertTrue(quantity == 1);
Offer testOffer = new OfferImpl();
testOffer.setOfferItemQualifierRuleType(OfferItemRestrictionRuleType.NONE);
testOffer.setOfferItemTargetRuleType(OfferItemRestrictionRuleType.NONE);
discount.setPromotion(testOffer);
quantity = priceDetail1.getQuantityAvailableToBeUsedAsQualifier(candidateOffer);
// this item received a different promotion, but the restriction rule is NONE, so this item cannot be a qualifier for this promotion
assertTrue(quantity == 1);
testOffer.setOfferItemTargetRuleType(OfferItemRestrictionRuleType.QUALIFIER);
candidateOffer.getOffer().setOfferItemQualifierRuleType(OfferItemRestrictionRuleType.TARGET);
quantity = priceDetail1.getQuantityAvailableToBeUsedAsQualifier(candidateOffer);
// this item received a different promotion, but the restriction rule is QUALIFIER, so this item can be a qualifier
// for this promotion
// dpc disabling this test for now assertTrue(quantity==2);
priceDetail1.getPromotionDiscounts().clear();
PromotionQualifier qualifier = new PromotionQualifier();
qualifier.setPromotion(offer);
qualifier.setQuantity(1);
priceDetail1.getPromotionQualifiers().add(qualifier);
quantity = priceDetail1.getQuantityAvailableToBeUsedAsQualifier(candidateOffer);
// items that have already qualified for this promotion cannot qualify again
assertTrue(quantity == 1);
qualifier.setPromotion(testOffer);
quantity = priceDetail1.getQuantityAvailableToBeUsedAsQualifier(candidateOffer);
// this item qualified for a different promotion, but the restriction rule is NONE, so this item cannot be a qualifier for this promotion
assertTrue(quantity == 1);
testOffer.setOfferItemQualifierRuleType(OfferItemRestrictionRuleType.QUALIFIER);
candidateOffer.getOffer().setOfferItemQualifierRuleType(OfferItemRestrictionRuleType.QUALIFIER);
quantity = priceDetail1.getQuantityAvailableToBeUsedAsQualifier(candidateOffer);
// this item qualified for a different promotion, but the restriction rule is QUALIFIER,
// so this item can be a qualifier for this promotion
// dpc disabling this test for now assertTrue(quantity==2);
}
use of org.broadleafcommerce.core.offer.service.discount.PromotionQualifier in project BroadleafCommerce by BroadleafCommerce.
the class OrderOfferProcessorImpl method synchronizeItemQualifiers.
protected void synchronizeItemQualifiers(OrderItem orderItem, PromotableOrderItem promotableOrderItem) {
Map<Long, PromotionQualifier> qualifiersMap = buildPromotableQualifiersMap(promotableOrderItem);
Map<Long, OrderItemQualifier> unmatchedQualifiersMap = new HashMap<Long, OrderItemQualifier>();
for (OrderItemQualifier orderItemQualifier : orderItem.getOrderItemQualifiers()) {
PromotionQualifier promotableQualifier = qualifiersMap.remove(orderItemQualifier.getOffer().getId());
if (promotableQualifier != null) {
// Offer was used as a qualifier on previous run. Update quantity if needed.
if (orderItemQualifier.getQuantity() != promotableQualifier.getQuantity()) {
orderItemQualifier.setQuantity(new Long(promotableQualifier.getQuantity()));
}
} else {
unmatchedQualifiersMap.put(orderItemQualifier.getId(), orderItemQualifier);
}
}
Iterator<OrderItemQualifier> unmatchedQualifiersIterator = unmatchedQualifiersMap.values().iterator();
for (PromotionQualifier qualifier : qualifiersMap.values()) {
if (unmatchedQualifiersIterator.hasNext()) {
// Reuse an existing qualifier
OrderItemQualifier existingQualifier = unmatchedQualifiersIterator.next();
existingQualifier.setOffer(qualifier.getPromotion());
existingQualifier.setQuantity(Long.valueOf(qualifier.getQuantity()));
unmatchedQualifiersIterator.remove();
} else {
// Create a new qualifier
OrderItemQualifier newQualifier = orderItemDao.createOrderItemQualifier();
newQualifier.setOrderItem(orderItem);
newQualifier.setOffer(qualifier.getPromotion());
newQualifier.setQuantity(Long.valueOf(qualifier.getQuantity()));
orderItem.getOrderItemQualifiers().add(newQualifier);
}
}
// Remove any unmatched qualifiers
Iterator<OrderItemQualifier> qIterator = orderItem.getOrderItemQualifiers().iterator();
offerServiceUtilities.removeUnmatchedQualifiers(unmatchedQualifiersMap, qIterator);
}
use of org.broadleafcommerce.core.offer.service.discount.PromotionQualifier in project BroadleafCommerce by BroadleafCommerce.
the class PromotableOrderItemImpl method initializePriceDetails.
protected void initializePriceDetails() {
if (includeAdjustments) {
for (OrderItemPriceDetail detail : orderItem.getOrderItemPriceDetails()) {
PromotableOrderItemPriceDetail poid = itemFactory.createPromotableOrderItemPriceDetail(this, detail.getQuantity());
itemPriceDetails.add(poid);
for (OrderItemPriceDetailAdjustment adjustment : detail.getOrderItemPriceDetailAdjustments()) {
PromotableOrderItemPriceDetailAdjustment poidAdj = new PromotableOrderItemPriceDetailAdjustmentImpl(adjustment, poid);
poid.addCandidateItemPriceDetailAdjustment(poidAdj);
}
poid.chooseSaleOrRetailAdjustments();
List<OrderItemQualifier> oiqs = poid.getPromotableOrderItem().getOrderItem().getOrderItemQualifiers();
if (CollectionUtils.isNotEmpty(oiqs)) {
for (OrderItemQualifier oiq : oiqs) {
PromotionQualifier pq = new PromotionQualifier();
pq.setPromotion(oiq.getOffer());
pq.setQuantity(oiq.getQuantity().intValue());
pq.setFinalizedQuantity(oiq.getQuantity().intValue());
pq.setPrice(oiq.getOrderItem().getPriceBeforeAdjustments(oiq.getOffer().getApplyDiscountToSalePrice()));
poid.getPromotionQualifiers().add(pq);
}
}
}
} else {
PromotableOrderItemPriceDetail poid = itemFactory.createPromotableOrderItemPriceDetail(this, orderItem.getQuantity());
itemPriceDetails.add(poid);
}
}
use of org.broadleafcommerce.core.offer.service.discount.PromotionQualifier in project BroadleafCommerce by BroadleafCommerce.
the class PromotableOrderItemPriceDetailImpl method getQuantityAvailableToBeUsedAsTarget.
@Override
public int getQuantityAvailableToBeUsedAsTarget(PromotableCandidateItemOffer itemOffer) {
int qtyAvailable = quantity;
Offer promotion = itemOffer.getOffer();
// 1. Any quantities of this item that have already received the promotion are not eligible.
// 2. If this promotion is not combinable then any quantities that have received discounts
// from other promotions cannot receive this discount
// 3. If this promotion is combinable then any quantities that have received discounts from
// other combinable promotions are eligible to receive this discount as well
boolean combinable = promotion.isCombinableWithOtherOffers();
if (!combinable && isNonCombinableOfferApplied()) {
return 0;
}
// if this promotion cannot be combined with another discount.
for (PromotionDiscount promotionDiscount : promotionDiscounts) {
if (promotionDiscount.getPromotion().equals(promotion) || restrictTarget(promotion, true)) {
qtyAvailable = qtyAvailable - promotionDiscount.getQuantity();
} else {
// it to be reused as a target.
if (restrictTarget(promotionDiscount.getPromotion(), true)) {
qtyAvailable = qtyAvailable - promotionDiscount.getQuantity();
}
}
}
// not allow the qualifier to be reused must be deduced from the qtyAvailable.
for (PromotionQualifier promotionQualifier : promotionQualifiers) {
if (promotionQualifier.getPromotion().equals(promotion) || restrictQualifier(promotion, true)) {
qtyAvailable = qtyAvailable - promotionQualifier.getQuantity();
} else {
if (restrictTarget(promotionQualifier.getPromotion(), false)) {
qtyAvailable = qtyAvailable - promotionQualifier.getQuantity();
}
}
}
return qtyAvailable;
}
Aggregations