Search in sources :

Example 1 with PromotionDiscount

use of org.broadleafcommerce.core.offer.service.discount.PromotionDiscount 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);
}
Also used : PromotionQualifier(org.broadleafcommerce.core.offer.service.discount.PromotionQualifier) PromotableCandidateItemOffer(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateItemOffer) Offer(org.broadleafcommerce.core.offer.domain.Offer) OfferImpl(org.broadleafcommerce.core.offer.domain.OfferImpl) PromotableCandidateItemOfferImpl(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateItemOfferImpl) PromotionDiscount(org.broadleafcommerce.core.offer.service.discount.PromotionDiscount)

Example 2 with PromotionDiscount

use of org.broadleafcommerce.core.offer.service.discount.PromotionDiscount 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);
}
Also used : PromotionQualifier(org.broadleafcommerce.core.offer.service.discount.PromotionQualifier) PromotableCandidateItemOffer(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateItemOffer) Offer(org.broadleafcommerce.core.offer.domain.Offer) OfferImpl(org.broadleafcommerce.core.offer.domain.OfferImpl) PromotableCandidateItemOfferImpl(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateItemOfferImpl) PromotionDiscount(org.broadleafcommerce.core.offer.service.discount.PromotionDiscount)

Example 3 with PromotionDiscount

use of org.broadleafcommerce.core.offer.service.discount.PromotionDiscount in project BroadleafCommerce by BroadleafCommerce.

the class ItemOfferProcessorImpl method calculatePotentialSavings.

/**
 * This method determines the potential savings for each item offer as if it was the only item offer being applied.
 * @param itemOffers
 * @param order
 */
protected void calculatePotentialSavings(List<PromotableCandidateItemOffer> itemOffers, PromotableOrder order) {
    if (itemOffers.size() > 1) {
        for (PromotableCandidateItemOffer itemOffer : itemOffers) {
            Money potentialSavings = new Money(order.getOrderCurrency());
            Offer offer = itemOffer.getOffer();
            BigDecimal calculatedWeightedPercent = new BigDecimal(0);
            markQualifiersAndTargets(order, itemOffer);
            for (PromotableOrderItemPriceDetail detail : order.getAllPromotableOrderItemPriceDetails()) {
                PromotableOrderItem item = detail.getPromotableOrderItem();
                for (PromotionDiscount discount : detail.getPromotionDiscounts()) {
                    Money itemSavings = calculatePotentialSavingsForOrderItem(itemOffer, item, discount.getQuantity());
                    potentialSavings = potentialSavings.add(itemSavings);
                    if (useCalculatePercent(offer)) {
                        BigDecimal discountPercent = calculatePercent(item.calculateTotalWithoutAdjustments(), itemSavings);
                        calculatedWeightedPercent = calculatedWeightedPercent.add(discountPercent);
                    } else if (hasQualifierAndQualifierRestricted(offer)) {
                        BigDecimal discountPercent = calculateWeightedPercent(discount, item, itemSavings);
                        calculatedWeightedPercent = calculatedWeightedPercent.add(discountPercent);
                    }
                }
                // Reset state back for next offer
                detail.getPromotionDiscounts().clear();
                detail.getPromotionQualifiers().clear();
            }
            itemOffer.setPotentialSavings(potentialSavings);
            if (usePercentOffValue(offer)) {
                itemOffer.setWeightedPercentSaved(offer.getValue());
            } else if (useCalculatePercent(offer) || hasQualifierAndQualifierRestricted(offer)) {
                itemOffer.setWeightedPercentSaved(calculatedWeightedPercent);
            }
            if (itemOffer.getUses() == 0) {
                itemOffer.setPotentialSavingsQtyOne(potentialSavings);
            } else {
                itemOffer.setPotentialSavingsQtyOne(potentialSavings.divide(itemOffer.getUses()));
            }
        }
    }
}
Also used : Money(org.broadleafcommerce.common.money.Money) PromotableCandidateItemOffer(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateItemOffer) PromotableCandidateItemOffer(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateItemOffer) PromotableCandidateOrderOffer(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateOrderOffer) Offer(org.broadleafcommerce.core.offer.domain.Offer) BigDecimal(java.math.BigDecimal) PromotableOrderItem(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem) PromotionDiscount(org.broadleafcommerce.core.offer.service.discount.PromotionDiscount) PromotableOrderItemPriceDetail(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItemPriceDetail)

Example 4 with PromotionDiscount

use of org.broadleafcommerce.core.offer.service.discount.PromotionDiscount 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;
}
Also used : PromotionQualifier(org.broadleafcommerce.core.offer.service.discount.PromotionQualifier) Offer(org.broadleafcommerce.core.offer.domain.Offer) PromotionDiscount(org.broadleafcommerce.core.offer.service.discount.PromotionDiscount)

Example 5 with PromotionDiscount

use of org.broadleafcommerce.core.offer.service.discount.PromotionDiscount in project BroadleafCommerce by BroadleafCommerce.

the class PromotableOrderItemPriceDetailImpl method getQuantityAvailableToBeUsedAsQualifier.

@Override
public int getQuantityAvailableToBeUsedAsQualifier(PromotableCandidateItemOffer itemOffer) {
    int qtyAvailable = quantity;
    Offer promotion = itemOffer.getOffer();
    // Any quantities of this item that have already received the promotion are not eligible.
    for (PromotionDiscount promotionDiscount : promotionDiscounts) {
        if (promotionDiscount.getPromotion().equals(promotion) || restrictTarget(promotion, false)) {
            qtyAvailable = qtyAvailable - promotionDiscount.getQuantity();
        } else {
            // Item's that receive other discounts might still be allowed to be qualifiers
            if (restrictQualifier(promotionDiscount.getPromotion(), true)) {
                qtyAvailable = qtyAvailable - promotionDiscount.getQuantity();
            }
        }
    }
    // another promotion that has a qualifier type of NONE or TARGET_ONLY cannot be used for this promotion
    for (PromotionQualifier promotionQualifier : promotionQualifiers) {
        if (promotionQualifier.getPromotion().equals(promotion) || restrictQualifier(promotion, false)) {
            qtyAvailable = qtyAvailable - promotionQualifier.getQuantity();
        } else {
            if (restrictQualifier(promotionQualifier.getPromotion(), false)) {
                qtyAvailable = qtyAvailable - promotionQualifier.getQuantity();
            }
        }
    }
    return qtyAvailable;
}
Also used : PromotionQualifier(org.broadleafcommerce.core.offer.service.discount.PromotionQualifier) Offer(org.broadleafcommerce.core.offer.domain.Offer) PromotionDiscount(org.broadleafcommerce.core.offer.service.discount.PromotionDiscount)

Aggregations

PromotionDiscount (org.broadleafcommerce.core.offer.service.discount.PromotionDiscount)12 Offer (org.broadleafcommerce.core.offer.domain.Offer)7 PromotionQualifier (org.broadleafcommerce.core.offer.service.discount.PromotionQualifier)7 PromotableCandidateItemOffer (org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateItemOffer)3 OfferImpl (org.broadleafcommerce.core.offer.domain.OfferImpl)2 PromotableCandidateItemOfferImpl (org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateItemOfferImpl)2 PromotableOrderItem (org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem)2 PromotableOrderItemPriceDetail (org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItemPriceDetail)2 BigDecimal (java.math.BigDecimal)1 Money (org.broadleafcommerce.common.money.Money)1 PromotableCandidateOrderOffer (org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateOrderOffer)1 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)1 OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)1