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;
}
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;
}
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;
}
}
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;
}
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 } };
}
Aggregations