Search in sources :

Example 51 with Money

use of org.broadleafcommerce.common.money.Money in project BroadleafCommerce by BroadleafCommerce.

the class FixedPriceFulfillmentPricingProvider method estimateCostForFulfillmentGroup.

@Override
public FulfillmentEstimationResponse estimateCostForFulfillmentGroup(FulfillmentGroup fulfillmentGroup, Set<FulfillmentOption> options) throws FulfillmentPriceException {
    FulfillmentEstimationResponse response = new FulfillmentEstimationResponse();
    HashMap<FulfillmentOption, Money> shippingPrices = new HashMap<FulfillmentOption, Money>();
    response.setFulfillmentOptionPrices(shippingPrices);
    for (FulfillmentOption option : options) {
        if (canCalculateCostForFulfillmentGroup(fulfillmentGroup, option)) {
            Money price = ((FixedPriceFulfillmentOption) option).getPrice();
            shippingPrices.put(option, price);
        }
    }
    return response;
}
Also used : Money(org.broadleafcommerce.common.money.Money) HashMap(java.util.HashMap) FixedPriceFulfillmentOption(org.broadleafcommerce.core.order.fulfillment.domain.FixedPriceFulfillmentOption) FixedPriceFulfillmentOption(org.broadleafcommerce.core.order.fulfillment.domain.FixedPriceFulfillmentOption) FulfillmentOption(org.broadleafcommerce.core.order.domain.FulfillmentOption)

Example 52 with Money

use of org.broadleafcommerce.common.money.Money in project BroadleafCommerce by BroadleafCommerce.

the class FixedPriceFulfillmentPricingProvider method calculateCostForFulfillmentGroup.

@Override
public FulfillmentGroup calculateCostForFulfillmentGroup(FulfillmentGroup fulfillmentGroup) throws FulfillmentPriceException {
    if (canCalculateCostForFulfillmentGroup(fulfillmentGroup, fulfillmentGroup.getFulfillmentOption())) {
        Money price = ((FixedPriceFulfillmentOption) fulfillmentGroup.getFulfillmentOption()).getPrice();
        fulfillmentGroup.setRetailShippingPrice(price);
        fulfillmentGroup.setSaleShippingPrice(price);
        fulfillmentGroup.setShippingPrice(price);
        return fulfillmentGroup;
    }
    throw new IllegalArgumentException("Cannot estimate shipping cost for the fulfillment option: " + fulfillmentGroup.getFulfillmentOption().getClass().getName());
}
Also used : Money(org.broadleafcommerce.common.money.Money) FixedPriceFulfillmentOption(org.broadleafcommerce.core.order.fulfillment.domain.FixedPriceFulfillmentOption)

Example 53 with Money

use of org.broadleafcommerce.common.money.Money in project BroadleafCommerce by BroadleafCommerce.

the class StructuredContentServiceImpl method buildFieldValues.

/**
 * Parses the given {@link StructuredContent} into its {@link StructuredContentDTO} representation. This will also
 * format the values from {@link StructuredContentDTO#getValues()} into their actual data types. For instance, if the
 * given {@link StructuredContent} has a DATE field, then this method will ensure that the resulting object in the values
 * map of the DTO is a {@link Date} rather than just a String representing a date.
 * <p/>
 * Current support of parsing field types is:
 * DATE - {@link Date}
 * BOOLEAN - {@link Boolean}
 * DECIMAL - {@link BigDecimal}
 * INTEGER - {@link Integer}
 * MONEY - {@link Money}
 * <p/>
 * All other fields are treated as strings. This will also fix URL strings that have the CMS prefix (like images) by
 * prepending the standard CMS prefix with the particular environment prefix
 *
 * @param sc
 * @param scDTO
 * @param secure
 * @see {@link StaticAssetService#getStaticAssetEnvironmentUrlPrefix()}
 */
protected void buildFieldValues(StructuredContent sc, StructuredContentDTO scDTO, boolean secure) {
    String cmsPrefix = staticAssetPathService.getStaticAssetUrlPrefix();
    Map<String, StructuredContentFieldXref> scFieldXrefs = MapUtils.emptyIfNull(sc.getStructuredContentFieldXrefs());
    scDTO.getValues().put("id", sc.getId());
    for (String fieldKey : scFieldXrefs.keySet()) {
        StructuredContentField scf = scFieldXrefs.get(fieldKey).getStructuredContentField();
        String originalValue = scf.getValue();
        if (hasCmsPrefix(originalValue, cmsPrefix)) {
            buildFieldValueWithCmsPrefix(originalValue, scDTO, secure, fieldKey);
        } else {
            FieldDefinition definition = null;
            StructuredContentType scType = sc.getStructuredContentType();
            StructuredContentFieldTemplate scFieldTemplate = scType.getStructuredContentFieldTemplate();
            List<FieldGroup> scFieldGroups = ListUtils.emptyIfNull(scFieldTemplate.getFieldGroups());
            Iterator<FieldGroup> groupIterator = scFieldGroups.iterator();
            while (groupIterator.hasNext() && definition == null) {
                FieldGroup group = groupIterator.next();
                for (FieldDefinition def : group.getFieldDefinitions()) {
                    if (def.getName().equals(fieldKey)) {
                        definition = def;
                        break;
                    }
                }
            }
            if (definition != null) {
                Object value = null;
                if (originalValue != null) {
                    switch(definition.getFieldType()) {
                        case DATE:
                            try {
                                value = FormatUtil.getDateFormat().parse(originalValue);
                            } catch (Exception e) {
                                value = originalValue;
                            }
                            break;
                        case BOOLEAN:
                            value = new Boolean(originalValue);
                            break;
                        case DECIMAL:
                            value = new BigDecimal(originalValue);
                            break;
                        case INTEGER:
                            value = Integer.parseInt(originalValue);
                            break;
                        case MONEY:
                            value = new Money(originalValue);
                            break;
                        case ADDITIONAL_FOREIGN_KEY:
                            value = FOREIGN_LOOKUP + '|' + definition.getAdditionalForeignKeyClass() + '|' + originalValue;
                            break;
                        default:
                            value = originalValue;
                            break;
                    }
                }
                scDTO.getValues().put(fieldKey, value);
            } else {
                scDTO.getValues().put(fieldKey, scFieldXrefs.get(fieldKey).getStructuredContentField().getValue());
            }
        }
    }
    // allow modules to contribute to the fields of the DTO
    extensionManager.getProxy().populateAdditionalStructuredContentFields(sc, scDTO, secure);
}
Also used : FieldGroup(org.broadleafcommerce.cms.field.domain.FieldGroup) FieldDefinition(org.broadleafcommerce.cms.field.domain.FieldDefinition) BigDecimal(java.math.BigDecimal) StructuredContentFieldTemplate(org.broadleafcommerce.cms.structure.domain.StructuredContentFieldTemplate) Money(org.broadleafcommerce.common.money.Money) StructuredContentField(org.broadleafcommerce.cms.structure.domain.StructuredContentField) StructuredContentType(org.broadleafcommerce.cms.structure.domain.StructuredContentType) StructuredContentFieldXref(org.broadleafcommerce.cms.structure.domain.StructuredContentFieldXref)

Example 54 with Money

use of org.broadleafcommerce.common.money.Money in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafCurrencyUtils method getUnitAmount.

/**
 * Returns the unit amount (e.g. .01 for US and all other 2 decimal currencies)
 *
 * @param difference
 * @return
 */
public static Money getUnitAmount(Money difference) {
    Currency currency = BroadleafCurrencyUtils.getCurrency(difference);
    BigDecimal divisor = new BigDecimal(Math.pow(10, currency.getDefaultFractionDigits()));
    BigDecimal unitAmount = new BigDecimal("1").divide(divisor);
    if (difference.lessThan(BigDecimal.ZERO)) {
        unitAmount = unitAmount.negate();
    }
    return new Money(unitAmount, currency);
}
Also used : Money(org.broadleafcommerce.common.money.Money) BroadleafCurrency(org.broadleafcommerce.common.currency.domain.BroadleafCurrency) Currency(java.util.Currency) BigDecimal(java.math.BigDecimal)

Example 55 with Money

use of org.broadleafcommerce.common.money.Money in project BroadleafCommerce by BroadleafCommerce.

the class AbstractBaseProcessor method meetsItemQualifierSubtotal.

protected boolean meetsItemQualifierSubtotal(Offer offer, CandidatePromotionItems candidateItem) {
    Money qualifyingSubtotal = offer.getQualifyingItemSubTotal();
    if (!hasPositiveValue(qualifyingSubtotal)) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("Offer " + offer.getName() + " does not have an item subtotal requirement.");
        }
        return true;
    }
    if (isEmpty(offer.getQualifyingItemCriteriaXref())) {
        if (OfferType.ORDER_ITEM.equals(offer.getType())) {
            if (LOG.isWarnEnabled()) {
                LOG.warn("Offer " + offer.getName() + " has a subtotal item requirement but no item qualification criteria.");
            }
            return false;
        } else {
            // Checking if targets meet subtotal for item offer with no item criteria.
            Money accumulatedTotal = null;
            for (PromotableOrderItem orderItem : candidateItem.getAllCandidateTargets()) {
                Money itemPrice = orderItem.getCurrentBasePrice().multiply(orderItem.getQuantity());
                accumulatedTotal = accumulatedTotal == null ? itemPrice : accumulatedTotal.add(itemPrice);
                if (accumulatedTotal.greaterThan(qualifyingSubtotal)) {
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("Offer " + offer.getName() + " meets qualifying item subtotal.");
                    }
                    return true;
                }
            }
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Offer " + offer.getName() + " does not meet qualifying item subtotal.");
        }
    } else {
        if (candidateItem.getCandidateQualifiersMap() != null) {
            Money accumulatedTotal = null;
            Set<PromotableOrderItem> usedItems = new HashSet<PromotableOrderItem>();
            for (OfferItemCriteria criteria : candidateItem.getCandidateQualifiersMap().keySet()) {
                List<PromotableOrderItem> promotableItems = candidateItem.getCandidateQualifiersMap().get(criteria);
                if (promotableItems != null) {
                    for (PromotableOrderItem item : promotableItems) {
                        if (!usedItems.contains(item)) {
                            usedItems.add(item);
                            Money itemPrice = item.getCurrentBasePrice().multiply(item.getQuantity());
                            accumulatedTotal = accumulatedTotal == null ? itemPrice : accumulatedTotal.add(itemPrice);
                            if (accumulatedTotal.greaterThan(qualifyingSubtotal)) {
                                if (LOG.isTraceEnabled()) {
                                    LOG.trace("Offer " + offer.getName() + " meets the item subtotal requirement.");
                                }
                                return true;
                            }
                        }
                    }
                }
            }
        }
    }
    if (LOG.isTraceEnabled()) {
        LOG.trace("Offer " + offer.getName() + " does not meet the item subtotal qualifications.");
    }
    return false;
}
Also used : Money(org.broadleafcommerce.common.money.Money) OfferItemCriteria(org.broadleafcommerce.core.offer.domain.OfferItemCriteria) PromotableOrderItem(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem) HashSet(java.util.HashSet)

Aggregations

Money (org.broadleafcommerce.common.money.Money)158 Order (org.broadleafcommerce.core.order.domain.Order)43 BigDecimal (java.math.BigDecimal)38 Offer (org.broadleafcommerce.core.offer.domain.Offer)29 Test (org.testng.annotations.Test)29 Transactional (org.springframework.transaction.annotation.Transactional)24 FulfillmentGroup (org.broadleafcommerce.core.order.domain.FulfillmentGroup)22 ArrayList (java.util.ArrayList)21 CommonSetupBaseTest (org.broadleafcommerce.test.CommonSetupBaseTest)21 FulfillmentGroupItem (org.broadleafcommerce.core.order.domain.FulfillmentGroupItem)19 CustomerOffer (org.broadleafcommerce.core.offer.domain.CustomerOffer)18 Sku (org.broadleafcommerce.core.catalog.domain.Sku)16 DiscreteOrderItemImpl (org.broadleafcommerce.core.order.domain.DiscreteOrderItemImpl)14 OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)13 HashMap (java.util.HashMap)12 SkuImpl (org.broadleafcommerce.core.catalog.domain.SkuImpl)12 FixedPriceFulfillmentOption (org.broadleafcommerce.core.order.fulfillment.domain.FixedPriceFulfillmentOption)12 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)11 FulfillmentGroupImpl (org.broadleafcommerce.core.order.domain.FulfillmentGroupImpl)11 FixedPriceFulfillmentOptionImpl (org.broadleafcommerce.core.order.fulfillment.domain.FixedPriceFulfillmentOptionImpl)10