Search in sources :

Example 41 with OrderItem

use of org.broadleafcommerce.core.order.domain.OrderItem 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 42 with OrderItem

use of org.broadleafcommerce.core.order.domain.OrderItem 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 43 with OrderItem

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

the class AutoBundleActivity method populateItemMatchesForSku.

protected int populateItemMatchesForSku(List<DiscreteOrderItem> matchingItems, Order order, List<DiscreteOrderItem> unbundledItems, Long skuId) {
    int skuMatches = 0;
    for (OrderItem orderItem : order.getOrderItems()) {
        if (orderItem instanceof DiscreteOrderItem) {
            DiscreteOrderItem item = (DiscreteOrderItem) orderItem;
            if (skuId.equals(item.getSku().getId())) {
                matchingItems.add(item);
                skuMatches = skuMatches + item.getQuantity();
            }
        }
    }
    if (unbundledItems != null) {
        for (DiscreteOrderItem discreteOrderItem : unbundledItems) {
            if (skuId.equals(discreteOrderItem.getSku().getId())) {
                skuMatches = skuMatches + discreteOrderItem.getQuantity();
            }
        }
    }
    return skuMatches;
}
Also used : DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem)

Example 44 with OrderItem

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

the class OrderOfferProcessorImpl method synchronizeOrderItems.

protected void synchronizeOrderItems(PromotableOrder promotableOrder) {
    Order order = promotableOrder.getOrder();
    Map<OrderItem, PromotableOrderItem> promotableItemMap = offerServiceUtilities.buildPromotableItemMap(promotableOrder);
    List<OrderItem> orderItemList = offerServiceUtilities.buildOrderItemList(order);
    for (OrderItem orderItem : orderItemList) {
        PromotableOrderItem promotableItem = promotableItemMap.get(orderItem);
        if (promotableItem == null) {
            continue;
        }
        synchronizeItemPriceDetails(orderItem, promotableItem);
        synchronizeItemQualifiers(orderItem, promotableItem);
    }
}
Also used : PromotableOrder(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrder) Order(org.broadleafcommerce.core.order.domain.Order) PromotableOrderItem(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) PromotableOrderItem(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem)

Example 45 with OrderItem

use of org.broadleafcommerce.core.order.domain.OrderItem 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

OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)100 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)69 BundleOrderItem (org.broadleafcommerce.core.order.domain.BundleOrderItem)54 Order (org.broadleafcommerce.core.order.domain.Order)46 ArrayList (java.util.ArrayList)32 GiftWrapOrderItem (org.broadleafcommerce.core.order.domain.GiftWrapOrderItem)23 FulfillmentGroupItem (org.broadleafcommerce.core.order.domain.FulfillmentGroupItem)20 FulfillmentGroup (org.broadleafcommerce.core.order.domain.FulfillmentGroup)19 Transactional (org.springframework.transaction.annotation.Transactional)19 Test (org.testng.annotations.Test)16 Money (org.broadleafcommerce.common.money.Money)13 OrderItemRequestDTO (org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO)13 HashMap (java.util.HashMap)11 Sku (org.broadleafcommerce.core.catalog.domain.Sku)10 CartOperationRequest (org.broadleafcommerce.core.order.service.workflow.CartOperationRequest)9 PromotableOrderItem (org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem)7 BigDecimal (java.math.BigDecimal)5 ItemNotFoundException (org.broadleafcommerce.core.order.service.exception.ItemNotFoundException)5 List (java.util.List)4 Product (org.broadleafcommerce.core.catalog.domain.Product)4