use of org.broadleafcommerce.common.money.Money in project BroadleafCommerce by BroadleafCommerce.
the class FulfillmentItemPricingActivity method fixItemTotalRoundingIssues.
/**
* Because an item may have multiple price details that don't round cleanly, we may have pennies
* left over that need to be distributed.
*
* This method may not be needed because the sum of the item amounts is derived from a double price (OrderItem's total)
* being multiplied and divided by whole numbers of which guarantees that each item amount is a clean multiple
* of the price of a single unit of that item. This behavior being enforced in populateItemTotalAmount. So we will
* never get a fraction of a cent that could cause totalItemAmount and totalFGItemAmount to be different values.
*
* @param order
* @param partialOrderItemMap
*/
protected void fixItemTotalRoundingIssues(Order order, Map<OrderItem, List<FulfillmentGroupItem>> partialOrderItemMap) {
for (OrderItem orderItem : partialOrderItemMap.keySet()) {
Money totalItemAmount = orderItem.getTotalPrice();
Money totalFGItemAmount = sumItemAmount(partialOrderItemMap.get(orderItem), order);
Money amountDiff = totalItemAmount.subtract(totalFGItemAmount);
if (!(amountDiff.getAmount().compareTo(BigDecimal.ZERO) == 0)) {
long numApplicationsNeeded = countNumberOfUnits(amountDiff);
Money unitAmount = getUnitAmount(amountDiff);
for (FulfillmentGroupItem fgItem : partialOrderItemMap.get(orderItem)) {
numApplicationsNeeded = numApplicationsNeeded - applyDifferenceToAmount(fgItem, numApplicationsNeeded, unitAmount);
if (numApplicationsNeeded == 0) {
break;
}
}
}
}
}
use of org.broadleafcommerce.common.money.Money in project BroadleafCommerce by BroadleafCommerce.
the class FulfillmentItemPricingActivity method populateItemTotalAmount.
/**
* Sets the fulfillment amount which includes the relative portion of the total price for
* the corresponding order item.
*
* @param order
* @param partialOrderItemMap
*/
protected void populateItemTotalAmount(Order order, Map<OrderItem, List<FulfillmentGroupItem>> partialOrderItemMap) {
for (FulfillmentGroup fulfillmentGroup : order.getFulfillmentGroups()) {
for (FulfillmentGroupItem fgItem : fulfillmentGroup.getFulfillmentGroupItems()) {
OrderItem orderItem = fgItem.getOrderItem();
int fgItemQty = fgItem.getQuantity();
int orderItemQty = orderItem.getQuantity();
Money totalItemAmount = orderItem.getTotalPrice();
if (fgItemQty != orderItemQty) {
// We need to keep track of all of these items in case we need to distribute a remainder
// to one or more of the items.
List<FulfillmentGroupItem> fgItemList = partialOrderItemMap.get(orderItem);
if (fgItemList == null) {
fgItemList = new ArrayList<>();
partialOrderItemMap.put(orderItem, fgItemList);
}
fgItemList.add(fgItem);
fgItem.setTotalItemAmount(totalItemAmount.multiply(fgItemQty).divide(orderItemQty));
} else {
fgItem.setTotalItemAmount(totalItemAmount);
}
}
}
}
use of org.broadleafcommerce.common.money.Money in project BroadleafCommerce by BroadleafCommerce.
the class ShippingActivity method execute.
@Override
public ProcessContext<Order> execute(ProcessContext<Order> context) throws Exception {
Order order = context.getSeedData();
/*
* 1. Get FGs from Order
* 2. take each FG and call shipping module with the shipping svc
* 3. add FG back to order
*/
Money totalShipping = BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, order.getCurrency());
for (FulfillmentGroup fulfillmentGroup : order.getFulfillmentGroups()) {
fulfillmentGroup = shippingService.calculateShippingForFulfillmentGroup(fulfillmentGroup);
totalShipping = totalShipping.add(fulfillmentGroup.getShippingPrice());
}
order.setTotalShipping(totalShipping);
context.setSeedData(order);
return context;
}
use of org.broadleafcommerce.common.money.Money in project BroadleafCommerce by BroadleafCommerce.
the class TotalActivity method setTaxSums.
protected void setTaxSums(Order order) {
if (order.getTaxOverride()) {
Money zeroMoney = BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, order.getCurrency());
for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
if (fg.getTaxes() != null) {
fg.getTaxes().clear();
}
fg.setTotalTax(zeroMoney);
for (FulfillmentGroupItem fgi : fg.getFulfillmentGroupItems()) {
if (fgi.getTaxes() != null) {
fgi.getTaxes().clear();
}
fgi.setTotalTax(zeroMoney);
}
for (FulfillmentGroupFee fee : fg.getFulfillmentGroupFees()) {
if (fee.getTaxes() != null) {
fee.getTaxes().clear();
}
fee.setTotalTax(zeroMoney);
}
fg.setTotalFulfillmentGroupTax(zeroMoney);
fg.setTotalItemTax(zeroMoney);
fg.setTotalFeeTax(zeroMoney);
}
order.setTotalTax(zeroMoney);
return;
}
Money orderTotalTax = BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, order.getCurrency());
for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
Money fgTotalFgTax = BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, order.getCurrency());
Money fgTotalItemTax = BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, order.getCurrency());
Money fgTotalFeeTax = BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, order.getCurrency());
// Add in all FG specific taxes (such as shipping tax)
if (fg.getTaxes() != null) {
for (TaxDetail tax : fg.getTaxes()) {
fgTotalFgTax = fgTotalFgTax.add(tax.getAmount());
}
}
for (FulfillmentGroupItem item : fg.getFulfillmentGroupItems()) {
Money itemTotalTax = BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, order.getCurrency());
// Add in all taxes for this item
if (item.getTaxes() != null) {
for (TaxDetail tax : item.getTaxes()) {
itemTotalTax = itemTotalTax.add(tax.getAmount());
}
}
item.setTotalTax(itemTotalTax);
fgTotalItemTax = fgTotalItemTax.add(itemTotalTax);
}
for (FulfillmentGroupFee fee : fg.getFulfillmentGroupFees()) {
Money feeTotalTax = BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, order.getCurrency());
// Add in all taxes for this fee
if (fee.getTaxes() != null) {
for (TaxDetail tax : fee.getTaxes()) {
feeTotalTax = feeTotalTax.add(tax.getAmount());
}
}
fee.setTotalTax(feeTotalTax);
fgTotalFeeTax = fgTotalFeeTax.add(feeTotalTax);
}
Money fgTotalTax = BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, order.getCurrency()).add(fgTotalFgTax).add(fgTotalItemTax).add(fgTotalFeeTax);
// Set the fulfillment group tax sums
fg.setTotalFulfillmentGroupTax(fgTotalFgTax);
fg.setTotalItemTax(fgTotalItemTax);
fg.setTotalFeeTax(fgTotalFeeTax);
fg.setTotalTax(fgTotalTax);
orderTotalTax = orderTotalTax.add(fgTotalTax);
}
order.setTotalTax(orderTotalTax);
}
use of org.broadleafcommerce.common.money.Money in project BroadleafCommerce by BroadleafCommerce.
the class FulfillmentPricingServiceImpl method estimateCostForFulfillmentGroup.
@Override
public FulfillmentEstimationResponse estimateCostForFulfillmentGroup(FulfillmentGroup fulfillmentGroup, Set<FulfillmentOption> options) throws FulfillmentPriceException {
FulfillmentEstimationResponse response = new FulfillmentEstimationResponse();
HashMap<FulfillmentOption, Money> prices = new HashMap<FulfillmentOption, Money>();
response.setFulfillmentOptionPrices(prices);
for (FulfillmentPricingProvider provider : providers) {
// to, then the response from the pricing provider should not include the options that it could not respond to.
try {
FulfillmentEstimationResponse processorResponse = provider.estimateCostForFulfillmentGroup(fulfillmentGroup, options);
if (processorResponse != null && processorResponse.getFulfillmentOptionPrices() != null && processorResponse.getFulfillmentOptionPrices().size() > 0) {
prices.putAll(processorResponse.getFulfillmentOptionPrices());
}
} catch (FulfillmentPriceException e) {
// Shouldn't completely fail the rest of the estimation on a pricing exception. Another provider might still
// be able to respond
String errorMessage = "FulfillmentPriceException thrown when trying to estimate fulfillment costs from ";
errorMessage += provider.getClass().getName();
errorMessage += ". Underlying message was: " + e.getMessage();
LOG.error(errorMessage);
}
}
return response;
}
Aggregations