Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * #%L
   * BroadleafCommerce Framework
   * %%
   * Copyright (C) 2009 - 2013 Broadleaf Commerce
   * %%
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * 
  *       http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * #L%
  */
 package org.broadleafcommerce.core.offer.service.discount.domain;
 
 
 import java.util.List;
 
 
 
     protected int quantity;
     protected boolean useSaleAdjustments = false;
     protected boolean adjustmentsFinalized = false;
     protected Money adjustedTotal;
 
     public PromotableOrderItemPriceDetailImpl(PromotableOrderItem promotableOrderItemint quantity) {
         this. = promotableOrderItem;
         this. = quantity;
     }
     
     @Override
     public boolean isAdjustmentsFinalized() {
         return ;
     }
     
     @Override
     public void setAdjustmentsFinalized(boolean adjustmentsFinalized) {
         this. = adjustmentsFinalized;
     }
 
     @Override
         .add(itemAdjustment);
     }
 
     @Override
         return Collections.unmodifiableList();
     }
 
     @Override
     public boolean hasNonCombinableAdjustments() {
             if (!adjustment.isCombinable()) {
                 return true;
             }
         }
         return false;
     }
 
     protected boolean hasOrderItemAdjustments() {
         return .size() > 0;
     }
 
     @Override
     public boolean isTotalitarianOfferApplied() {
             if (adjustment.isTotalitarian()) {
                 return true;
             }
         }
         return false;
     }
 
     @Override
     public boolean isNonCombinableOfferApplied() {
             if (!adjustment.isCombinable()) {
                 return true;
            }
        }
        return false;
    }
    
        Money returnPrice = .getSalePriceBeforeAdjustments();
        if (returnPrice == null) {
            returnPrice = .getRetailPriceBeforeAdjustments();
        }
            returnPrice = returnPrice.subtract(adjustment.getSaleAdjustmentValue());
        }
        return returnPrice;
    }
    
            returnPrice = returnPrice.subtract(adjustment.getRetailAdjustmentValue());
        }
        return returnPrice;
    }

    
This method will check to see if the salePriceAdjustments or retailPriceAdjustments are better and remove those that should not apply.

Returns:
    public void chooseSaleOrRetailAdjustments() {
         = true;
         = null;
        this. = .;
        Money salePriceBeforeAdjustments = .getSalePriceBeforeAdjustments();
        Money retailPriceBeforeAdjustments = .getRetailPriceBeforeAdjustments();
        if (hasOrderItemAdjustments()) {
            Money saleAdjustmentPrice = calculateSaleAdjustmentUnitPrice();
            Money retailAdjustmentPrice = calculateRetailAdjustmentUnitPrice();
            
            if (.isOnSale()) {
                if (saleAdjustmentPrice.lessThanOrEqual(retailAdjustmentPrice)) {
                    this. = .;
                     = saleAdjustmentPrice;
                } else {
                     = retailAdjustmentPrice;
                }
                if (!.lessThan(salePriceBeforeAdjustments)) {
                    // Adjustments are not as good as the sale price.  So, clear them and use the sale price.                    
                    .clear();
                     = salePriceBeforeAdjustments;
                }
            } else {
                if (!retailAdjustmentPrice.lessThan(.getRetailPriceBeforeAdjustments())) {
                    // Adjustments are not as good as the retail price.
                    .clear();
                     = retailPriceBeforeAdjustments;
                } else {
                     = retailAdjustmentPrice;
                }
            }
            if () {
                removeRetailOnlyAdjustments();
            }
            finalizeAdjustments();
        }
        if ( == null) {
            if (salePriceBeforeAdjustments != null) {
                this. = true;
                 = salePriceBeforeAdjustments;
            } else {
                 = retailPriceBeforeAdjustments;
            }
        }
    }
    public void removeAllAdjustments() {
    }
    protected void finalizeAdjustments(boolean useSaleAdjustments) {
            adjustment.finalizeAdjustment(useSaleAdjustments);
        }
    }

    
Removes retail only adjustments.
    protected void removeRetailOnlyAdjustments() {
        while (adjustments.hasNext()) {
            PromotableOrderItemPriceDetailAdjustment adjustment = adjustments.next();
            if (adjustment.getOffer().getApplyDiscountToSalePrice() == false) {
                adjustments.remove();
            }
        }
    }

    
If removeUnusedAdjustments is s

Parameters:
useSaleAdjustments
    protected void removeZeroDollarAdjustments(boolean useSalePrice) {
        while (adjustments.hasNext()) {
            PromotableOrderItemPriceDetailAdjustment adjustment = adjustments.next();
            if (useSalePrice) {
                if (adjustment.getSaleAdjustmentValue().isZero()) {
                    adjustments.remove();
                }
            } else {
                if (adjustment.getRetailAdjustmentValue().isZero()) {
                    adjustments.remove();
                }
            }            
        }
    }
    
        return ;
    }
    @Override
        return ;
    }
    @Override
        return ;
    }
    @Override
    public int getQuantity() {
        return ;
    }
    @Override
    public void setQuantity(int quantity) {
        this. = quantity;
    }
    private boolean restrictTarget(Offer offerboolean targetType) {
        OfferItemRestrictionRuleType qualifierType;
        if (targetType) {
            qualifierType = offer.getOfferItemTargetRuleType();
        } else {
            qualifierType = offer.getOfferItemQualifierRuleType();
        }
        return ..equals(qualifierType) ||
                ..equals(qualifierType);
    }
    private boolean restrictQualifier(Offer offerboolean targetType) {
        OfferItemRestrictionRuleType qualifierType;
        if (targetType) {
            qualifierType = offer.getOfferItemTargetRuleType();
        } else {
            qualifierType = offer.getOfferItemQualifierRuleType();
        }
        return ..equals(qualifierType) ||
                ..equals(qualifierType);
    }
    @Override
        int qtyAvailable = ;
        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;
        }
        // Any quantities of this item that have already received the promotion are not eligible.
        // Also, any quantities of this item that have received another promotion are not eligible
        // if this promotion cannot be combined with another discount.
        for (PromotionDiscount promotionDiscount : ) {
            if (promotionDiscount.getPromotion().equals(promotion) || restrictTarget(promotiontrue)) {
                qtyAvailable = qtyAvailable - promotionDiscount.getQuantity();
            } else {
                // The other promotion is Combinable, but we must make sure that the item qualifier also allows
                // it to be reused as a target.                   
                if (restrictTarget(promotionDiscount.getPromotion(), true)) {
                    qtyAvailable = qtyAvailable - promotionDiscount.getQuantity();
                }
            }
        }
        // 4.  Any quantities of this item that have been used as a qualifier for this promotion are not eligible as targets
        // 5.  Any quantities of this item that have been used as a qualifier for another promotion that does 
        //     not allow the qualifier to be reused must be deduced from the qtyAvailable.
        for (PromotionQualifier promotionQualifier : ) {
            if (promotionQualifier.getPromotion().equals(promotion) || restrictQualifier(promotiontrue)) {
                qtyAvailable = qtyAvailable - promotionQualifier.getQuantity();
            } else {
                if (restrictTarget(promotionQualifier.getPromotion(), false)) {
                    qtyAvailable = qtyAvailable - promotionQualifier.getQuantity();
                }
            }
        }
        return qtyAvailable;
    }
        Offer promotion = candidatePromotion.getOffer();
        for (PromotionQualifier pq : ) {
            if (pq.getPromotion().equals(promotion)) {
                return pq;
            }
        }
        PromotionQualifier pq = new PromotionQualifier();
        pq.setPromotion(promotion);
        .add(pq);
        return pq;
    }
        Offer promotion = candidatePromotion.getOffer();
        for (PromotionDiscount pd : ) {
            if (pd.getPromotion().equals(promotion)) {
                return pd;
            }
        }
        PromotionDiscount pd = new PromotionDiscount();
        pd.setPromotion(promotion);
        .add(pd);
        return pd;
    }
    @Override
    public PromotionQualifier addPromotionQualifier(PromotableCandidateItemOffer itemOfferOfferItemCriteria itemCriteriaint qtyToMarkAsQualifier) {
        PromotionQualifier pq = lookupOrCreatePromotionQualifier(itemOffer);
        pq.incrementQuantity(qtyToMarkAsQualifier);
        pq.setItemCriteria(itemCriteria);
        return pq;
    }
    @Override
    public void addPromotionDiscount(PromotableCandidateItemOffer itemOfferOfferItemCriteria itemCriteriaint qtyToMarkAsTarget) {
        PromotionDiscount pd = lookupOrCreatePromotionDiscount(itemOffer);
        if (pd == null) {
            return;
        }
        pd.incrementQuantity(qtyToMarkAsTarget);
        pd.setItemCriteria(itemCriteria);
        pd.setCandidateItemOffer(itemOffer);
    }
    @Override
    public void finalizeQuantities() {
        for (PromotionDiscount promotionDiscount : ) {
            promotionDiscount.setFinalizedQuantity(promotionDiscount.getQuantity());
        }
        for (PromotionQualifier promotionQualifier : ) {
            promotionQualifier.setFinalizedQuantity(promotionQualifier.getQuantity());
        }
    }
    @Override
    public void clearAllNonFinalizedQuantities() {
        Iterator<PromotionQualifierpromotionQualifierIterator = .iterator();
        while (promotionQualifierIterator.hasNext()) {
            PromotionQualifier promotionQualifier = promotionQualifierIterator.next();
            if (promotionQualifier.getFinalizedQuantity() == 0) {
                // If there are no quantities of this item that are finalized, then remove the item.
                promotionQualifierIterator.remove();
            } else {
                // Otherwise, set the quantity to the number of finalized items.
                promotionQualifier.setQuantity(promotionQualifier.getFinalizedQuantity());
            }
        }
        Iterator<PromotionDiscountpromotionDiscountIterator = .iterator();
        while (promotionDiscountIterator.hasNext()) {
            PromotionDiscount promotionDiscount = promotionDiscountIterator.next();
            if (promotionDiscount.getFinalizedQuantity() == 0) {
                // If there are no quantities of this item that are finalized, then remove the item.
                promotionDiscountIterator.remove();
            } else {
                // Otherwise, set the quantity to the number of finalized items.
                promotionDiscount.setQuantity(promotionDiscount.getFinalizedQuantity());
            }
        }
    }
    @Override
        int qtyAvailable = ;
        Offer promotion = itemOffer.getOffer();
        // Any quantities of this item that have already received the promotion are not eligible.
        for (PromotionDiscount promotionDiscount : ) {
            if (promotionDiscount.getPromotion().equals(promotion) || restrictTarget(promotionfalse)) {
                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();
                }
            }
        }
        // Any quantities of this item that have already been used as a qualifier for this promotion or for 
        // another promotion that has a qualifier type of NONE or TARGET_ONLY cannot be used for this promotion
        for (PromotionQualifier promotionQualifier : ) {
            if (promotionQualifier.getPromotion().equals(promotion) || restrictQualifier(promotionfalse)) {
                qtyAvailable = qtyAvailable - promotionQualifier.getQuantity();
            } else {
                if (restrictQualifier(promotionQualifier.getPromotion(), false)) {
                    qtyAvailable = qtyAvailable - promotionQualifier.getQuantity();
                }
            }
        }
        return qtyAvailable;
    }
    @Override
    public Money calculateItemUnitPriceWithAdjustments(boolean allowSalePrice) {
        Money priceWithAdjustments = null;
        if (allowSalePrice) {
            priceWithAdjustments = .getSalePriceBeforeAdjustments();
            if (priceWithAdjustments == null) {
                return .getRetailPriceBeforeAdjustments();
            }
        } else {
            priceWithAdjustments = .getRetailPriceBeforeAdjustments();
        }
            if (allowSalePrice) {
                priceWithAdjustments = priceWithAdjustments.subtract(adjustment.getSaleAdjustmentValue());
            } else {
                priceWithAdjustments = priceWithAdjustments.subtract(adjustment.getRetailAdjustmentValue());
            }
        }
        return priceWithAdjustments;
    }
    protected Money calculateAdjustmentsUnitValue() {
        Money adjustmentUnitValue = new Money(.getCurrency());
            adjustmentUnitValue = adjustmentUnitValue.add(adjustment.getAdjustmentValue());
        }
        return adjustmentUnitValue;
    }

    
Creates a key that represents a unique priceDetail

Returns:
    @Override
    public String buildDetailKey() {
        List<LongofferIds = new ArrayList<Long>();
            Long offerId = adjustment.getOffer().getId();
            offerIds.add(offerId);
        }
        Collections.sort(offerIds);
        return .getOrderItem().toString() + offerIds.toString() + ;
    }
    @Override
        return ;
    }
    @Override
    }
    @Override
        return copyDetail;
    }
    @Override
        for (PromotionDiscount existingDiscount : ) {
            if (existingDiscount.isFinalized()) {
                PromotionDiscount newDiscount = existingDiscount.copy();
                copyDetail.getPromotionDiscounts().add(newDiscount);
            }
        }
        for (PromotionQualifier existingQualifier : ) {
            if (existingQualifier.isFinalized()) {
                PromotionQualifier newQualifier = existingQualifier.copy();
                copyDetail.getPromotionQualifiers().add(newQualifier);
            }
        }
            PromotableOrderItemPriceDetailAdjustment newAdjustment = existingAdjustment.copy();
            copyDetail.addCandidateItemPriceDetailAdjustment(newAdjustment);
        }
        return copyDetail;
    }
    protected PromotableOrderItemPriceDetail split(int discountQtyLong offerId) {
        int originalQty = ;
         = discountQty;
        int splitItemQty = originalQty - discountQty;
        // Create the new item with the correct quantity
        PromotableOrderItemPriceDetail newDetail = .createNewDetail(splitItemQty);
        // copy discounts
        for (PromotionDiscount existingDiscount : ) {
            PromotionDiscount newDiscount = existingDiscount.split(discountQty);
            if (newDiscount != null) {
                newDetail.getPromotionDiscounts().add(newDiscount);
            }
        }
        Iterator<PromotionQualifierqualifiers = .iterator();
        while (qualifiers.hasNext()) {
            PromotionQualifier currentQualifier = qualifiers.next();
            Long qualifierOfferId = currentQualifier.getPromotion().getId();
            if (qualifierOfferId.equals(offerId) && currentQualifier.getQuantity() <= splitItemQty) {
                // Remove this one from the original detail
                qualifiers.remove();
                newDetail.getPromotionQualifiers().add(currentQualifier);
            } else {
                PromotionQualifier newQualifier = currentQualifier.split(splitItemQty);
                newDetail.getPromotionQualifiers().add(newQualifier);
            }            
        }
            PromotableOrderItemPriceDetailAdjustment newAdjustment = existingAdjustment.copy();
            newDetail.addCandidateItemPriceDetailAdjustment(newAdjustment);
        }
        return newDetail;
    }
    @Override
        PromotableOrderItemPriceDetail returnDetail = null;
        for (PromotionDiscount discount : ) {
            if (discount.getQuantity() != ) {
                Long offerId = discount.getCandidateItemOffer().getOffer().getId();
                return this.split(discount.getQuantity(), offerId);
            }
        }
        return returnDetail;
    }
    @Override
    public boolean useSaleAdjustments() {
        return ;
    }
New to GrepCode? Check out our FAQ X