Search in sources :

Example 61 with OrderItem

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

the class AutoBundleActivity method unBundleItems.

/**
 * Removes all automatic bundles from the order and replaces with DiscreteOrderItems.
 *
 * @param order
 */
private List<DiscreteOrderItem> unBundleItems(Order order) throws PricingException {
    List<DiscreteOrderItem> unbundledItems = null;
    for (OrderItem orderItem : order.getOrderItems()) {
        if (orderItem instanceof BundleOrderItem) {
            BundleOrderItem bundleOrderItem = (BundleOrderItem) orderItem;
            if (bundleOrderItem.getProductBundle() != null && bundleOrderItem.getProductBundle().getAutoBundle()) {
                if (unbundledItems == null) {
                    unbundledItems = new ArrayList<DiscreteOrderItem>();
                }
                for (DiscreteOrderItem item : bundleOrderItem.getDiscreteOrderItems()) {
                    DiscreteOrderItem newOrderItem = (DiscreteOrderItem) item.clone();
                    newOrderItem.setQuantity(item.getQuantity() * bundleOrderItem.getQuantity());
                    newOrderItem.setSkuBundleItem(null);
                    newOrderItem.setBundleOrderItem(null);
                    newOrderItem.updateSaleAndRetailPrices();
                    newOrderItem.setOrder(order);
                    unbundledItems.add(newOrderItem);
                }
            }
        }
    }
    return unbundledItems;
}
Also used : DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem)

Example 62 with OrderItem

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

the class AutoBundleActivity method countMaximumApplications.

protected int countMaximumApplications(Order order, SkuBundleItem skuBundleItem, Set<Long> processedBundles) {
    int skuMatches = 0;
    Long skuId = skuBundleItem.getSku().getId();
    for (OrderItem orderItem : order.getOrderItems()) {
        if (orderItem instanceof DiscreteOrderItem) {
            DiscreteOrderItem item = (DiscreteOrderItem) orderItem;
            if (skuId.equals(item.getSku().getId())) {
                skuMatches = skuMatches + item.getQuantity();
            }
        } else if (orderItem instanceof BundleOrderItem) {
            BundleOrderItem bundleItem = (BundleOrderItem) orderItem;
            if (bundleItem.getProductBundle() != null && bundleItem.getProductBundle().getAutoBundle()) {
                if (!processedBundles.contains(bundleItem.getId())) {
                    for (DiscreteOrderItem discreteItem : bundleItem.getDiscreteOrderItems()) {
                        if (skuId.equals(discreteItem.getSku().getId())) {
                            skuMatches = skuMatches + (discreteItem.getQuantity() * bundleItem.getQuantity());
                        }
                    }
                }
            }
        }
    }
    return skuMatches / skuBundleItem.getQuantity();
}
Also used : DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem)

Example 63 with OrderItem

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

the class FulfillmentGroupItemStrategyImpl method verify.

@Override
public CartOperationRequest verify(CartOperationRequest request) throws PricingException {
    Order order = request.getOrder();
    if (isRemoveEmptyFulfillmentGroups() && order.getFulfillmentGroups() != null) {
        ListIterator<FulfillmentGroup> fgIter = order.getFulfillmentGroups().listIterator();
        while (fgIter.hasNext()) {
            FulfillmentGroup fg = fgIter.next();
            if (fg.getFulfillmentGroupItems() == null || fg.getFulfillmentGroupItems().size() == 0) {
                fgIter.remove();
                fulfillmentGroupService.delete(fg);
            }
        }
    }
    Map<Long, Integer> oiQuantityMap = new HashMap<Long, Integer>();
    List<OrderItem> expandedOrderItems = new ArrayList<OrderItem>();
    for (OrderItem oi : order.getOrderItems()) {
        if (oi instanceof BundleOrderItem) {
            for (DiscreteOrderItem doi : ((BundleOrderItem) oi).getDiscreteOrderItems()) {
                expandedOrderItems.add(doi);
            }
        } else if (oi instanceof DiscreteOrderItem) {
            expandedOrderItems.add(oi);
        } else {
            expandedOrderItems.add(oi);
        }
    }
    for (OrderItem oi : expandedOrderItems) {
        Integer oiQuantity = oiQuantityMap.get(oi.getId());
        if (oiQuantity == null) {
            oiQuantity = 0;
        }
        if (oi instanceof DiscreteOrderItem && ((DiscreteOrderItem) oi).getBundleOrderItem() != null) {
            oiQuantity += ((DiscreteOrderItem) oi).getBundleOrderItem().getQuantity() * oi.getQuantity();
        } else {
            oiQuantity += oi.getQuantity();
        }
        oiQuantityMap.put(oi.getId(), oiQuantity);
    }
    for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
        for (FulfillmentGroupItem fgi : fg.getFulfillmentGroupItems()) {
            Long oiId = fgi.getOrderItem().getId();
            Integer oiQuantity = oiQuantityMap.get(oiId);
            if (oiQuantity == null) {
                throw new IllegalStateException("Fulfillment group items and discrete order items are not in sync. DiscreteOrderItem id: " + oiId);
            }
            oiQuantity -= fgi.getQuantity();
            oiQuantityMap.put(oiId, oiQuantity);
        }
    }
    for (Entry<Long, Integer> entry : oiQuantityMap.entrySet()) {
        if (!entry.getValue().equals(0)) {
            throw new IllegalStateException("Not enough fulfillment group items found for DiscreteOrderItem id: " + entry.getKey());
        }
    }
    return request;
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup)

Example 64 with OrderItem

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

the class CheckUpdateAvailabilityActivity method execute.

@Override
public ProcessContext<CartOperationRequest> execute(ProcessContext<CartOperationRequest> context) throws Exception {
    CartOperationRequest request = context.getSeedData();
    OrderItemRequestDTO orderItemRequestDTO = request.getItemRequest();
    if (orderItemRequestDTO instanceof NonDiscreteOrderItemRequestDTO) {
        return context;
    }
    Sku sku;
    Long orderItemId = request.getItemRequest().getOrderItemId();
    OrderItem orderItem = orderItemService.readOrderItemById(orderItemId);
    if (orderItem instanceof DiscreteOrderItem) {
        sku = ((DiscreteOrderItem) orderItem).getSku();
    } else if (orderItem instanceof BundleOrderItem) {
        sku = ((BundleOrderItem) orderItem).getSku();
    } else {
        LOG.warn("Could not check availability; did not recognize passed-in item " + orderItem.getClass().getName());
        return context;
    }
    Order order = context.getSeedData().getOrder();
    Integer requestedQuantity = request.getItemRequest().getQuantity();
    checkSkuAvailability(order, sku, requestedQuantity);
    Integer previousQty = orderItem.getQuantity();
    for (OrderItem child : orderItem.getChildOrderItems()) {
        Sku childSku = ((DiscreteOrderItem) child).getSku();
        Integer childQuantity = child.getQuantity();
        childQuantity = childQuantity / previousQty;
        checkSkuAvailability(order, childSku, childQuantity * requestedQuantity);
    }
    return context;
}
Also used : DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) Order(org.broadleafcommerce.core.order.domain.Order) OrderItemRequestDTO(org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO) NonDiscreteOrderItemRequestDTO(org.broadleafcommerce.core.order.service.call.NonDiscreteOrderItemRequestDTO) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) NonDiscreteOrderItemRequestDTO(org.broadleafcommerce.core.order.service.call.NonDiscreteOrderItemRequestDTO) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) Sku(org.broadleafcommerce.core.catalog.domain.Sku)

Example 65 with OrderItem

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

the class UpdateOrderItemActivity method execute.

@Override
public ProcessContext<CartOperationRequest> execute(ProcessContext<CartOperationRequest> context) throws Exception {
    CartOperationRequest request = context.getSeedData();
    OrderItemRequestDTO orderItemRequestDTO = request.getItemRequest();
    Order order = request.getOrder();
    OrderItem orderItem = null;
    for (OrderItem oi : order.getOrderItems()) {
        if (oi.getId().equals(orderItemRequestDTO.getOrderItemId())) {
            orderItem = oi;
        }
    }
    if (orderItem == null || !order.getOrderItems().contains(orderItem)) {
        throw new ItemNotFoundException("Order Item (" + orderItemRequestDTO.getOrderItemId() + ") not found in Order (" + order.getId() + ")");
    }
    OrderItem itemFromOrder = order.getOrderItems().get(order.getOrderItems().indexOf(orderItem));
    if (orderItemRequestDTO.getQuantity() >= 0) {
        int previousQty = itemFromOrder.getQuantity();
        request.setOrderItemQuantityDelta(orderItemRequestDTO.getQuantity() - itemFromOrder.getQuantity());
        itemFromOrder.setQuantity(orderItemRequestDTO.getQuantity());
        for (OrderItem child : itemFromOrder.getChildOrderItems()) {
            int childQuantity = child.getQuantity();
            childQuantity = childQuantity / previousQty;
            child.setQuantity(childQuantity * orderItemRequestDTO.getQuantity());
        }
        // Update any additional attributes of the passed in request
        if (itemFromOrder instanceof DiscreteOrderItem) {
            DiscreteOrderItem discreteOrderItem = (DiscreteOrderItem) itemFromOrder;
            discreteOrderItem.getAdditionalAttributes().putAll(orderItemRequestDTO.getAdditionalAttributes());
        }
        request.setOrderItem(itemFromOrder);
    }
    return context;
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) CartOperationRequest(org.broadleafcommerce.core.order.service.workflow.CartOperationRequest) OrderItemRequestDTO(org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) ItemNotFoundException(org.broadleafcommerce.core.order.service.exception.ItemNotFoundException)

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