Search in sources :

Example 6 with NullComparator

use of org.apache.commons.collections.comparators.NullComparator in project BroadleafCommerce by BroadleafCommerce.

the class FulfillmentGroupOfferProcessorImpl method applyAllFulfillmentGroupOffers.

@Override
@SuppressWarnings("unchecked")
public boolean applyAllFulfillmentGroupOffers(List<PromotableCandidateFulfillmentGroupOffer> qualifiedFGOffers, PromotableOrder order) {
    Map<FulfillmentGroupOfferPotential, List<PromotableCandidateFulfillmentGroupOffer>> offerMap = new HashMap<FulfillmentGroupOfferPotential, List<PromotableCandidateFulfillmentGroupOffer>>();
    for (PromotableCandidateFulfillmentGroupOffer candidate : qualifiedFGOffers) {
        FulfillmentGroupOfferPotential potential = new FulfillmentGroupOfferPotential();
        potential.setOffer(candidate.getOffer());
        if (offerMap.get(potential) == null) {
            offerMap.put(potential, new ArrayList<PromotableCandidateFulfillmentGroupOffer>());
        }
        offerMap.get(potential).add(candidate);
    }
    List<FulfillmentGroupOfferPotential> potentials = new ArrayList<FulfillmentGroupOfferPotential>();
    for (FulfillmentGroupOfferPotential potential : offerMap.keySet()) {
        List<PromotableCandidateFulfillmentGroupOffer> fgOffers = offerMap.get(potential);
        Collections.sort(fgOffers, new ReverseComparator(new BeanComparator("discountedAmount", new NullComparator())));
        Collections.sort(fgOffers, new BeanComparator("priority", new NullComparator()));
        if (potential.getOffer().isLimitedUsePerOrder() && fgOffers.size() > potential.getOffer().getMaxUsesPerOrder()) {
            for (int j = potential.getOffer().getMaxUsesPerOrder(); j < fgOffers.size(); j++) {
                fgOffers.remove(j);
            }
        }
        filterOffersByQualifyingAndSubtotalRequirements(order, fgOffers);
        for (PromotableCandidateFulfillmentGroupOffer candidate : fgOffers) {
            if (potential.getTotalSavings().getAmount().equals(BankersRounding.zeroAmount())) {
                BroadleafCurrency currency = order.getOrderCurrency();
                if (currency != null) {
                    potential.setTotalSavings(new Money(BigDecimal.ZERO, currency.getCurrencyCode()));
                } else {
                    potential.setTotalSavings(new Money(BigDecimal.ZERO));
                }
            }
            Money priceBeforeAdjustments = candidate.getFulfillmentGroup().calculatePriceWithoutAdjustments();
            Money discountedPrice = candidate.getDiscountedPrice();
            potential.setTotalSavings(potential.getTotalSavings().add(priceBeforeAdjustments.subtract(discountedPrice)));
            potential.setPriority(candidate.getOffer().getPriority());
        }
        potentials.add(potential);
    }
    // Sort fg potentials by priority and discount
    Collections.sort(potentials, new BeanComparator("totalSavings", Collections.reverseOrder()));
    Collections.sort(potentials, new BeanComparator("priority"));
    potentials = removeTrailingNotCombinableFulfillmentGroupOffers(potentials);
    boolean fgOfferApplied = false;
    for (FulfillmentGroupOfferPotential potential : potentials) {
        Offer offer = potential.getOffer();
        boolean alreadyContainsTotalitarianOffer = order.isTotalitarianOfferApplied();
        List<PromotableCandidateFulfillmentGroupOffer> candidates = offerMap.get(potential);
        for (PromotableCandidateFulfillmentGroupOffer candidate : candidates) {
            applyFulfillmentGroupOffer(candidate.getFulfillmentGroup(), candidate);
            fgOfferApplied = true;
        }
        for (PromotableFulfillmentGroup fg : order.getFulfillmentGroups()) {
            fg.chooseSaleOrRetailAdjustments();
        }
        if ((offer.isTotalitarianOffer() != null && offer.isTotalitarianOffer()) || alreadyContainsTotalitarianOffer) {
            fgOfferApplied = compareAndAdjustFulfillmentGroupOffers(order, fgOfferApplied);
            if (fgOfferApplied) {
                break;
            }
        } else if (!offer.isCombinableWithOtherOffers()) {
            break;
        }
    }
    return fgOfferApplied;
}
Also used : HashMap(java.util.HashMap) NullComparator(org.apache.commons.collections.comparators.NullComparator) FulfillmentGroupOfferPotential(org.broadleafcommerce.core.offer.service.discount.FulfillmentGroupOfferPotential) ArrayList(java.util.ArrayList) ReverseComparator(org.apache.commons.collections.comparators.ReverseComparator) BeanComparator(org.apache.commons.beanutils.BeanComparator) Money(org.broadleafcommerce.common.money.Money) BroadleafCurrency(org.broadleafcommerce.common.currency.domain.BroadleafCurrency) PromotableCandidateFulfillmentGroupOffer(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateFulfillmentGroupOffer) Offer(org.broadleafcommerce.core.offer.domain.Offer) PromotableFulfillmentGroup(org.broadleafcommerce.core.offer.service.discount.domain.PromotableFulfillmentGroup) ArrayList(java.util.ArrayList) List(java.util.List) PromotableCandidateFulfillmentGroupOffer(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateFulfillmentGroupOffer)

Aggregations

BeanComparator (org.apache.commons.beanutils.BeanComparator)6 NullComparator (org.apache.commons.collections.comparators.NullComparator)6 ComparatorChain (org.apache.commons.collections.comparators.ComparatorChain)3 ReverseComparator (org.apache.commons.collections.comparators.ReverseComparator)3 ArrayList (java.util.ArrayList)2 Comparator (java.util.Comparator)2 SortOption (com.infiniteautomation.mango.db.query.SortOption)1 HashMap (java.util.HashMap)1 List (java.util.List)1 BroadleafCurrency (org.broadleafcommerce.common.currency.domain.BroadleafCurrency)1 Money (org.broadleafcommerce.common.money.Money)1 Offer (org.broadleafcommerce.core.offer.domain.Offer)1 FulfillmentGroupOfferPotential (org.broadleafcommerce.core.offer.service.discount.FulfillmentGroupOfferPotential)1 PromotableCandidateFulfillmentGroupOffer (org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateFulfillmentGroupOffer)1 PromotableFulfillmentGroup (org.broadleafcommerce.core.offer.service.discount.domain.PromotableFulfillmentGroup)1 Order (org.motechproject.mds.util.Order)1 CompoundComparator (org.springframework.util.comparator.CompoundComparator)1