Search in sources :

Example 11 with FulfillmentGroupItem

use of org.broadleafcommerce.core.order.domain.FulfillmentGroupItem in project BroadleafCommerce by BroadleafCommerce.

the class FulfillmentItemPricingActivity method distributeOrderSavingsToItems.

/**
 * Distributes the order adjustments (if any) to the individual fulfillment group items.
 * @param order
 * @param totalAllItems
 * @return
 */
protected Money distributeOrderSavingsToItems(Order order, BigDecimal totalAllItems) {
    Money returnAmount = new Money(order.getCurrency());
    BigDecimal orderAdjAmt = order.getOrderAdjustmentsValue().getAmount();
    for (FulfillmentGroup fulfillmentGroup : order.getFulfillmentGroups()) {
        for (FulfillmentGroupItem fgItem : fulfillmentGroup.getFulfillmentGroupItems()) {
            BigDecimal fgItemAmount = fgItem.getTotalItemAmount().getAmount();
            BigDecimal proratedAdjAmt = totalAllItems.compareTo(BigDecimal.ZERO) == 0 ? totalAllItems : orderAdjAmt.multiply(fgItemAmount).divide(totalAllItems, RoundingMode.FLOOR);
            fgItem.setProratedOrderAdjustmentAmount(new Money(proratedAdjAmt, order.getCurrency()));
            returnAmount = returnAmount.add(fgItem.getProratedOrderAdjustmentAmount());
        }
    }
    return returnAmount;
}
Also used : Money(org.broadleafcommerce.common.money.Money) FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup) BigDecimal(java.math.BigDecimal)

Example 12 with FulfillmentGroupItem

use of org.broadleafcommerce.core.order.domain.FulfillmentGroupItem 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;
                }
            }
        }
    }
}
Also used : Money(org.broadleafcommerce.common.money.Money) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem)

Example 13 with FulfillmentGroupItem

use of org.broadleafcommerce.core.order.domain.FulfillmentGroupItem 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);
            }
        }
    }
}
Also used : Money(org.broadleafcommerce.common.money.Money) FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup)

Example 14 with FulfillmentGroupItem

use of org.broadleafcommerce.core.order.domain.FulfillmentGroupItem 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);
}
Also used : Money(org.broadleafcommerce.common.money.Money) FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup) FulfillmentGroupFee(org.broadleafcommerce.core.order.domain.FulfillmentGroupFee) TaxDetail(org.broadleafcommerce.core.order.domain.TaxDetail)

Example 15 with FulfillmentGroupItem

use of org.broadleafcommerce.core.order.domain.FulfillmentGroupItem in project BroadleafCommerce by BroadleafCommerce.

the class PromotableFulfillmentGroupImpl method getDiscountableOrderItems.

@Override
public List<PromotableOrderItem> getDiscountableOrderItems() {
    if (discountableOrderItems != null) {
        return discountableOrderItems;
    }
    discountableOrderItems = new ArrayList<PromotableOrderItem>();
    List<Long> discountableOrderItemIds = new ArrayList<Long>();
    for (FulfillmentGroupItem fgItem : fulfillmentGroup.getFulfillmentGroupItems()) {
        OrderItem orderItem = fgItem.getOrderItem();
        if (orderItem.isDiscountingAllowed()) {
            discountableOrderItemIds.add(fgItem.getOrderItem().getId());
        } else {
            if (orderItem instanceof OrderItemContainer) {
                OrderItemContainer orderItemContainer = (OrderItemContainer) orderItem;
                if (orderItemContainer.getAllowDiscountsOnChildItems()) {
                    for (OrderItem containedOrderItem : orderItemContainer.getOrderItems()) {
                        if (!containedOrderItem.isDiscountingAllowed()) {
                            discountableOrderItemIds.add(containedOrderItem.getId());
                        }
                    }
                }
            }
        }
    }
    for (PromotableOrderItem item : promotableOrder.getDiscountableOrderItems()) {
        if (discountableOrderItemIds.contains(item.getOrderItemId())) {
            discountableOrderItems.add(item);
        }
    }
    return discountableOrderItems;
}
Also used : FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) OrderItemContainer(org.broadleafcommerce.core.order.domain.OrderItemContainer) ArrayList(java.util.ArrayList)

Aggregations

FulfillmentGroupItem (org.broadleafcommerce.core.order.domain.FulfillmentGroupItem)48 FulfillmentGroup (org.broadleafcommerce.core.order.domain.FulfillmentGroup)39 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)25 Order (org.broadleafcommerce.core.order.domain.Order)21 OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)20 Money (org.broadleafcommerce.common.money.Money)18 ArrayList (java.util.ArrayList)16 BundleOrderItem (org.broadleafcommerce.core.order.domain.BundleOrderItem)15 Sku (org.broadleafcommerce.core.catalog.domain.Sku)12 Transactional (org.springframework.transaction.annotation.Transactional)11 FulfillmentGroupItemImpl (org.broadleafcommerce.core.order.domain.FulfillmentGroupItemImpl)9 Address (org.broadleafcommerce.profile.core.domain.Address)9 BigDecimal (java.math.BigDecimal)8 Test (org.testng.annotations.Test)8 SkuImpl (org.broadleafcommerce.core.catalog.domain.SkuImpl)7 DiscreteOrderItemImpl (org.broadleafcommerce.core.order.domain.DiscreteOrderItemImpl)7 FulfillmentGroupImpl (org.broadleafcommerce.core.order.domain.FulfillmentGroupImpl)7 HashMap (java.util.HashMap)6 AddressImpl (org.broadleafcommerce.profile.core.domain.AddressImpl)6 ISOCountry (org.broadleafcommerce.common.i18n.domain.ISOCountry)5