Search in sources :

Example 6 with BroadleafCurrency

use of org.broadleafcommerce.common.currency.domain.BroadleafCurrency in project BroadleafCommerce by BroadleafCommerce.

the class PromotableOfferUtility method computeAdjustmentValue.

public static Money computeAdjustmentValue(Money currentPriceDetailValue, BigDecimal offerUnitValue, OfferHolder offerHolder, PromotionRounding rounding) {
    Offer offer = offerHolder.getOffer();
    BroadleafCurrency currency = offerHolder.getCurrency();
    OfferDiscountType discountType = offer.getDiscountType();
    Money adjustmentValue;
    if (currency != null) {
        adjustmentValue = new Money(currency);
    } else {
        adjustmentValue = new Money();
    }
    if (OfferDiscountType.AMOUNT_OFF.equals(discountType)) {
        adjustmentValue = new Money(offerUnitValue, currency);
    }
    if (OfferDiscountType.FIX_PRICE.equals(discountType)) {
        adjustmentValue = currentPriceDetailValue.subtract(new Money(offerUnitValue, currency));
    }
    if (OfferDiscountType.PERCENT_OFF.equals(discountType)) {
        BigDecimal offerValue = currentPriceDetailValue.getAmount().multiply(offerUnitValue.divide(new BigDecimal("100"), 5, RoundingMode.HALF_EVEN));
        if (rounding.isRoundOfferValues()) {
            offerValue = offerValue.setScale(rounding.getRoundingScale(), rounding.getRoundingMode());
        }
        adjustmentValue = new Money(offerValue, currency);
    }
    if (currentPriceDetailValue.lessThan(adjustmentValue)) {
        adjustmentValue = currentPriceDetailValue;
    }
    return adjustmentValue;
}
Also used : Money(org.broadleafcommerce.common.money.Money) BroadleafCurrency(org.broadleafcommerce.common.currency.domain.BroadleafCurrency) AdvancedOffer(org.broadleafcommerce.core.offer.domain.AdvancedOffer) Offer(org.broadleafcommerce.core.offer.domain.Offer) OfferDiscountType(org.broadleafcommerce.core.offer.service.type.OfferDiscountType) BigDecimal(java.math.BigDecimal)

Example 7 with BroadleafCurrency

use of org.broadleafcommerce.common.currency.domain.BroadleafCurrency in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafCurrencyResolverImpl method resolveCurrency.

@Override
public BroadleafRequestedCurrencyDto resolveCurrency(WebRequest request) {
    BroadleafCurrency desiredCurrency = null;
    // 1) Check request for currency
    desiredCurrency = (BroadleafCurrency) request.getAttribute(CURRENCY_VAR, WebRequest.SCOPE_REQUEST);
    // 2) Check for a request parameter
    if (desiredCurrency == null && BLCRequestUtils.getURLorHeaderParameter(request, CURRENCY_CODE_PARAM) != null) {
        String currencyCode = BLCRequestUtils.getURLorHeaderParameter(request, CURRENCY_CODE_PARAM);
        desiredCurrency = broadleafCurrencyService.findCurrencyByCode(currencyCode);
        if (LOG.isTraceEnabled()) {
            LOG.trace("Attempt to find currency by param " + currencyCode + " resulted in " + desiredCurrency);
        }
    }
    // 3) Check session for currency
    if (desiredCurrency == null && BLCRequestUtils.isOKtoUseSession(request)) {
        desiredCurrency = (BroadleafCurrency) request.getAttribute(CURRENCY_VAR, WebRequest.SCOPE_GLOBAL_SESSION);
    }
    // 4) Check locale for currency
    if (desiredCurrency == null) {
        Locale locale = (Locale) request.getAttribute(BroadleafLocaleResolverImpl.LOCALE_VAR, WebRequest.SCOPE_REQUEST);
        if (locale != null) {
            desiredCurrency = locale.getDefaultCurrency();
        }
    }
    // 5) Lookup default currency from DB
    BroadleafCurrency defaultCurrency = broadleafCurrencyService.findDefaultBroadleafCurrency();
    if (desiredCurrency == null) {
        desiredCurrency = defaultCurrency;
    }
    // For an out-of-box installation, only one currency is supported, so even though we have a
    // desired currency, we may not have any prices that support it.
    BroadleafCurrency currencyToUse = defaultCurrency;
    if (BLCRequestUtils.isOKtoUseSession(request)) {
        request.setAttribute(CURRENCY_VAR, currencyToUse, WebRequest.SCOPE_GLOBAL_SESSION);
    }
    BroadleafRequestedCurrencyDto dto = new BroadleafRequestedCurrencyDto(currencyToUse, desiredCurrency);
    return dto;
}
Also used : Locale(org.broadleafcommerce.common.locale.domain.Locale) BroadleafCurrency(org.broadleafcommerce.common.currency.domain.BroadleafCurrency) BroadleafRequestedCurrencyDto(org.broadleafcommerce.common.currency.domain.BroadleafRequestedCurrencyDto)

Example 8 with BroadleafCurrency

use of org.broadleafcommerce.common.currency.domain.BroadleafCurrency in project BroadleafCommerce by BroadleafCommerce.

the class SkuImpl method getPriceData.

@Override
public DynamicSkuPrices getPriceData() {
    if (SkuPricingConsiderationContext.hasDynamicPricing()) {
        DynamicSkuPrices dynamicPrices = SkuPricingConsiderationContext.getDynamicSkuPrices(this);
        return dynamicPrices;
    } else {
        DynamicSkuPrices dsp = new DynamicSkuPrices();
        BroadleafCurrency tmpCurrency;
        if (currency != null) {
            tmpCurrency = currency;
        } else {
            tmpCurrency = BroadleafRequestContext.getCurrency();
        }
        if (retailPrice != null) {
            dsp.setRetailPrice(new Money(retailPrice, tmpCurrency));
        }
        if (salePrice != null) {
            dsp.setSalePrice(new Money(salePrice, tmpCurrency));
        }
        return dsp;
    }
}
Also used : Money(org.broadleafcommerce.common.money.Money) BroadleafCurrency(org.broadleafcommerce.common.currency.domain.BroadleafCurrency) DynamicSkuPrices(org.broadleafcommerce.core.catalog.service.dynamic.DynamicSkuPrices) ClonePolicyCollectionOverride(org.broadleafcommerce.common.extensibility.jpa.clone.ClonePolicyCollectionOverride)

Example 9 with BroadleafCurrency

use of org.broadleafcommerce.common.currency.domain.BroadleafCurrency 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)

Example 10 with BroadleafCurrency

use of org.broadleafcommerce.common.currency.domain.BroadleafCurrency in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafCurrencyProvider method provideUSCurrency.

@DataProvider(name = "USCurrency")
public static Object[][] provideUSCurrency() {
    BroadleafCurrency currency = new BroadleafCurrencyImpl();
    currency.setCurrencyCode("USD");
    currency.setDefaultFlag(true);
    currency.setFriendlyName("US Dollar");
    return new Object[][] { { currency } };
}
Also used : BroadleafCurrency(org.broadleafcommerce.common.currency.domain.BroadleafCurrency) BroadleafCurrencyImpl(org.broadleafcommerce.common.currency.domain.BroadleafCurrencyImpl) DataProvider(org.testng.annotations.DataProvider)

Aggregations

BroadleafCurrency (org.broadleafcommerce.common.currency.domain.BroadleafCurrency)15 Money (org.broadleafcommerce.common.money.Money)5 BigDecimal (java.math.BigDecimal)3 ArrayList (java.util.ArrayList)2 Currency (java.util.Currency)2 HashMap (java.util.HashMap)2 Query (javax.persistence.Query)2 BroadleafCurrencyImpl (org.broadleafcommerce.common.currency.domain.BroadleafCurrencyImpl)2 Locale (org.broadleafcommerce.common.locale.domain.Locale)2 Offer (org.broadleafcommerce.core.offer.domain.Offer)2 DataProvider (org.testng.annotations.DataProvider)2 NumberFormat (java.text.NumberFormat)1 List (java.util.List)1 BeanComparator (org.apache.commons.beanutils.BeanComparator)1 NullComparator (org.apache.commons.collections.comparators.NullComparator)1 ReverseComparator (org.apache.commons.collections.comparators.ReverseComparator)1 BroadleafRequestedCurrencyDto (org.broadleafcommerce.common.currency.domain.BroadleafRequestedCurrencyDto)1 ClonePolicyCollectionOverride (org.broadleafcommerce.common.extensibility.jpa.clone.ClonePolicyCollectionOverride)1 CurrencyConversionService (org.broadleafcommerce.common.money.CurrencyConversionService)1 PaymentRequestDTO (org.broadleafcommerce.common.payment.dto.PaymentRequestDTO)1