Search in sources :

Example 11 with PromotableOrderItemPriceDetail

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

the class OfferServiceUtilitiesImpl method markRelatedQualifiersAndTargetsForItemCriteria.

@Override
public int markRelatedQualifiersAndTargetsForItemCriteria(PromotableCandidateItemOffer itemOffer, PromotableOrder order, OrderItemHolder orderItemHolder, OfferItemCriteria itemCriteria, List<PromotableOrderItemPriceDetail> priceDetails, ItemOfferMarkTargets itemOfferMarkTargets) {
    sortQualifierItemDetails(priceDetails, itemOffer.getOffer().getApplyDiscountToSalePrice());
    // Calculate the number of qualifiers needed that will not receive the promotion.
    // These will be reserved first before the target is assigned.
    int qualifierQtyNeeded = itemCriteria.getQuantity();
    for (PromotableOrderItemPriceDetail detail : priceDetails) {
        OrderItem oi = detail.getPromotableOrderItem().getOrderItem();
        if (qualifierQtyNeeded > 0) {
            int itemQtyAvailableToBeUsedAsQualifier = detail.getQuantityAvailableToBeUsedAsQualifier(itemOffer);
            if (itemQtyAvailableToBeUsedAsQualifier > 0) {
                // We have found a qualifier that meets this offer criteria. First, we'll save some state that we
                // might need in the future.
                OfferItemCriteria previousQualifierCriteria = null;
                for (PromotionQualifier possibleQualifier : detail.getPromotionQualifiers()) {
                    if (possibleQualifier.getPromotion().equals(itemOffer.getOffer())) {
                        previousQualifierCriteria = possibleQualifier.getItemCriteria();
                        break;
                    }
                }
                // Go ahead and mark this item as a qualifier
                int qtyToMarkAsQualifier = Math.min(qualifierQtyNeeded, itemQtyAvailableToBeUsedAsQualifier);
                qualifierQtyNeeded -= qtyToMarkAsQualifier;
                PromotionQualifier pq = detail.addPromotionQualifier(itemOffer, itemCriteria, qtyToMarkAsQualifier);
                pq.setPrice(detail.getPromotableOrderItem().getPriceBeforeAdjustments(itemOffer.getOffer().getApplyDiscountToSalePrice()));
                // that there is an eligible target(s) and continue on.
                if (itemOfferMarkTargets.markTargets(itemOffer, order, oi, true)) {
                    // We found a target. Let's save this related order item used as the qualifier in case
                    // we succeed
                    orderItemHolder.setOrderItem(oi);
                } else {
                    // If we didn't find a target, we need to roll back how we marked this item as a qualifier.
                    qualifierQtyNeeded += qtyToMarkAsQualifier;
                    if (pq.getQuantity() == qtyToMarkAsQualifier) {
                        detail.getPromotionQualifiers().remove(pq);
                    } else {
                        pq.setItemCriteria(previousQualifierCriteria);
                        pq.setQuantity(pq.getQuantity() - qtyToMarkAsQualifier);
                    }
                }
            }
        }
        if (qualifierQtyNeeded == 0) {
            break;
        }
    }
    return qualifierQtyNeeded;
}
Also used : PromotionQualifier(org.broadleafcommerce.core.offer.service.discount.PromotionQualifier) PromotableOrderItem(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) OfferItemCriteria(org.broadleafcommerce.core.offer.domain.OfferItemCriteria) PromotableOrderItemPriceDetail(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItemPriceDetail)

Example 12 with PromotableOrderItemPriceDetail

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

the class OfferServiceUtilitiesImpl method applyAdjustmentsForItemPriceDetails.

@Override
public void applyAdjustmentsForItemPriceDetails(PromotableCandidateItemOffer itemOffer, List<PromotableOrderItemPriceDetail> itemPriceDetails) {
    for (PromotableOrderItemPriceDetail itemPriceDetail : itemPriceDetails) {
        for (PromotionDiscount discount : itemPriceDetail.getPromotionDiscounts()) {
            if (discount.getPromotion().equals(itemOffer.getOffer())) {
                if (itemOffer.getOffer().isTotalitarianOffer() || !itemOffer.getOffer().isCombinableWithOtherOffers()) {
                    // the value of the item
                    if (adjustmentIsNotGoodEnoughToBeApplied(itemOffer, itemPriceDetail)) {
                        break;
                    }
                }
                OrderItem orderItem = itemPriceDetail.getPromotableOrderItem().getOrderItem();
                Boolean offerCanApplyToChildOrderItems = itemOffer.getOffer().getApplyToChildItems();
                if (isAddOnOrderItem(orderItem) && !offerCanApplyToChildOrderItems) {
                    break;
                }
                applyOrderItemAdjustment(itemOffer, itemPriceDetail);
                break;
            }
        }
    }
}
Also used : PromotableOrderItem(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) PromotionDiscount(org.broadleafcommerce.core.offer.service.discount.PromotionDiscount) PromotableOrderItemPriceDetail(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItemPriceDetail)

Aggregations

PromotableOrderItemPriceDetail (org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItemPriceDetail)12 PromotableOrderItem (org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem)7 OfferItemCriteria (org.broadleafcommerce.core.offer.domain.OfferItemCriteria)4 OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)4 Money (org.broadleafcommerce.common.money.Money)3 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)3 Offer (org.broadleafcommerce.core.offer.domain.Offer)2 PromotionDiscount (org.broadleafcommerce.core.offer.service.discount.PromotionDiscount)2 PromotionQualifier (org.broadleafcommerce.core.offer.service.discount.PromotionQualifier)2 PromotableCandidateItemOffer (org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateItemOffer)2 PromotableCandidateOrderOffer (org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateOrderOffer)2 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 List (java.util.List)1 OrderItemPriceDetail (org.broadleafcommerce.core.order.domain.OrderItemPriceDetail)1 OrderItemHolder (org.broadleafcommerce.core.order.domain.dto.OrderItemHolder)1