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