Search in sources :

Example 86 with OrderItem

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

the class OrderItemServiceImpl method buildOrderItemRequestDTOFromOrderItem.

@Override
public OrderItemRequestDTO buildOrderItemRequestDTOFromOrderItem(OrderItem item) {
    OrderItemRequestDTO orderItemRequest;
    if (item instanceof DiscreteOrderItem) {
        DiscreteOrderItem doi = (DiscreteOrderItem) item;
        orderItemRequest = new OrderItemRequestDTO();
        if (doi.getCategory() != null) {
            orderItemRequest.setCategoryId(doi.getCategory().getId());
        }
        if (doi.getProduct() != null) {
            orderItemRequest.setProductId(doi.getProduct().getId());
        }
        if (doi.getSku() != null) {
            orderItemRequest.setSkuId(doi.getSku().getId());
        }
        if (doi.getAdditionalAttributes() != null) {
            for (Entry<String, String> entry : doi.getAdditionalAttributes().entrySet()) {
                orderItemRequest.getAdditionalAttributes().put(entry.getKey(), entry.getValue());
            }
        }
    } else {
        orderItemRequest = new NonDiscreteOrderItemRequestDTO();
        NonDiscreteOrderItemRequestDTO ndr = (NonDiscreteOrderItemRequestDTO) orderItemRequest;
        ndr.setItemName(item.getName());
        ndr.setOverrideRetailPrice(item.getRetailPrice());
        ndr.setOverrideSalePrice(item.getSalePrice());
    }
    orderItemRequest.setQuantity(item.getQuantity());
    if (item.getOrderItemAttributes() != null) {
        for (Entry<String, OrderItemAttribute> entry : item.getOrderItemAttributes().entrySet()) {
            orderItemRequest.getItemAttributes().put(entry.getKey(), entry.getValue().getValue());
        }
    }
    if (CollectionUtils.isNotEmpty(item.getChildOrderItems())) {
        for (OrderItem childItem : item.getChildOrderItems()) {
            orderItemRequest.getChildOrderItems().add(buildOrderItemRequestDTOFromOrderItem(childItem));
        }
    }
    return orderItemRequest;
}
Also used : DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItemRequestDTO(org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO) NonDiscreteOrderItemRequestDTO(org.broadleafcommerce.core.order.service.call.NonDiscreteOrderItemRequestDTO) NonDiscreteOrderItemRequestDTO(org.broadleafcommerce.core.order.service.call.NonDiscreteOrderItemRequestDTO) OrderItemAttribute(org.broadleafcommerce.core.order.domain.OrderItemAttribute) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) GiftWrapOrderItem(org.broadleafcommerce.core.order.domain.GiftWrapOrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem)

Example 87 with OrderItem

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

the class LegacyCartServiceImpl method addAllItemsToCartFromNamedOrder.

public Order addAllItemsToCartFromNamedOrder(Order namedOrder, boolean priceOrder) throws PricingException {
    Order cartOrder = orderDao.readCartForCustomer(namedOrder.getCustomer());
    if (cartOrder == null) {
        cartOrder = createNewCartForCustomer(namedOrder.getCustomer());
    }
    List<OrderItem> items = new ArrayList<OrderItem>(namedOrder.getOrderItems());
    for (int i = 0; i < items.size(); i++) {
        OrderItem orderItem = items.get(i);
        // only run pricing routines on the last item.
        boolean shouldPriceOrder = (priceOrder && (i == items.size() - 1));
        if (moveNamedOrderItems) {
            moveItemToOrder(namedOrder, cartOrder, orderItem, shouldPriceOrder);
        } else {
            addOrderItemToOrder(cartOrder, orderItem, shouldPriceOrder);
        }
    }
    return cartOrder;
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) ArrayList(java.util.ArrayList)

Example 88 with OrderItem

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

the class LegacyMergeCartServiceImpl method mergeGiftWrapOrderItems.

protected Order mergeGiftWrapOrderItems(MergeCartResponse mergeCartResponse, Order customerCart, Map<OrderItem, OrderItem> oldNewItemMap) throws PricingException {
    // update any remaining gift wrap items with their cloned wrapped item values, instead of the originals
    Iterator<OrderItem> addedItems = mergeCartResponse.getAddedItems().iterator();
    while (addedItems.hasNext()) {
        OrderItem addedItem = addedItems.next();
        if (addedItem instanceof GiftWrapOrderItem) {
            GiftWrapOrderItem giftItem = (GiftWrapOrderItem) addedItem;
            List<OrderItem> itemsToAdd = new ArrayList<OrderItem>();
            Iterator<OrderItem> wrappedItems = giftItem.getWrappedItems().iterator();
            while (wrappedItems.hasNext()) {
                OrderItem wrappedItem = wrappedItems.next();
                if (oldNewItemMap.containsKey(wrappedItem)) {
                    OrderItem newItem = oldNewItemMap.get(wrappedItem);
                    newItem.setGiftWrapOrderItem(giftItem);
                    itemsToAdd.add(newItem);
                    wrappedItem.setGiftWrapOrderItem(null);
                    wrappedItems.remove();
                }
            }
            giftItem.getWrappedItems().addAll(itemsToAdd);
        } else if (addedItem instanceof BundleOrderItem) {
            // a GiftWrapOrderItem inside a BundleOrderItem can only wrap other members of that bundle
            // or root members of the order - not members of an entirely different bundle
            boolean isValidBundle = true;
            Map<String, DiscreteOrderItem> newItemsMap = new HashMap<String, DiscreteOrderItem>();
            for (DiscreteOrderItem newItem : ((BundleOrderItem) addedItem).getDiscreteOrderItems()) {
                newItemsMap.put(newItem.getSku().getId() + "_" + newItem.getPrice(), newItem);
            }
            checkBundle: {
                for (DiscreteOrderItem itemFromBundle : ((BundleOrderItem) addedItem).getDiscreteOrderItems()) {
                    if (itemFromBundle instanceof GiftWrapOrderItem) {
                        GiftWrapOrderItem giftItem = (GiftWrapOrderItem) itemFromBundle;
                        List<OrderItem> itemsToAdd = new ArrayList<OrderItem>();
                        Iterator<OrderItem> wrappedItems = giftItem.getWrappedItems().iterator();
                        while (wrappedItems.hasNext()) {
                            OrderItem wrappedItem = wrappedItems.next();
                            if (oldNewItemMap.containsKey(wrappedItem)) {
                                OrderItem newItem = oldNewItemMap.get(wrappedItem);
                                newItem.setGiftWrapOrderItem(giftItem);
                                itemsToAdd.add(newItem);
                                wrappedItem.setGiftWrapOrderItem(null);
                                wrappedItems.remove();
                            } else if (wrappedItem instanceof DiscreteOrderItem) {
                                DiscreteOrderItem discreteWrappedItem = (DiscreteOrderItem) wrappedItem;
                                String itemKey = discreteWrappedItem.getSku().getId() + "_" + discreteWrappedItem.getPrice();
                                if (newItemsMap.containsKey(itemKey)) {
                                    OrderItem newItem = newItemsMap.get(itemKey);
                                    newItem.setGiftWrapOrderItem(giftItem);
                                    itemsToAdd.add(newItem);
                                    discreteWrappedItem.setGiftWrapOrderItem(null);
                                    wrappedItems.remove();
                                } else {
                                    isValidBundle = false;
                                    break checkBundle;
                                }
                            } else {
                                isValidBundle = false;
                                break checkBundle;
                            }
                        }
                        giftItem.getWrappedItems().addAll(itemsToAdd);
                    }
                }
            }
            if (!isValidBundle) {
                customerCart = removeItemFromOrder(customerCart, addedItem, false);
                addedItems.remove();
                mergeCartResponse.getRemovedItems().add(addedItem);
            }
        }
    }
    // If found, remove, because this is invalid. A GiftWrapOrderItem cannot wrap OrderItems located in an entirely different bundle.
    for (OrderItem addedItem : mergeCartResponse.getAddedItems()) {
        if (addedItem instanceof BundleOrderItem) {
            boolean containsGiftWrap = false;
            for (DiscreteOrderItem discreteOrderItem : ((BundleOrderItem) addedItem).getDiscreteOrderItems()) {
                if (discreteOrderItem instanceof GiftWrapOrderItem) {
                    containsGiftWrap = true;
                    break;
                }
            }
            if (!containsGiftWrap) {
                for (DiscreteOrderItem discreteOrderItem : ((BundleOrderItem) addedItem).getDiscreteOrderItems()) {
                    discreteOrderItem.setGiftWrapOrderItem(null);
                }
            }
        }
    }
    return customerCart;
}
Also used : DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) 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) GiftWrapOrderItem(org.broadleafcommerce.core.order.domain.GiftWrapOrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) Iterator(java.util.Iterator) GiftWrapOrderItem(org.broadleafcommerce.core.order.domain.GiftWrapOrderItem) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 89 with OrderItem

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

the class LegacyMergeCartServiceImpl method mergeRegularOrderItems.

protected Order mergeRegularOrderItems(Order anonymousCart, MergeCartResponse mergeCartResponse, Order customerCart, Map<OrderItem, OrderItem> oldNewItemMap) throws PricingException {
    // currently we'll just add items
    for (OrderItem orderItem : anonymousCart.getOrderItems()) {
        if (orderItem instanceof DiscreteOrderItem) {
            orderItem.removeAllAdjustments();
            orderItem.removeAllCandidateItemOffers();
            DiscreteOrderItem discreteOrderItem = (DiscreteOrderItem) orderItem;
            if (discreteOrderItem.getSku().getActiveStartDate() != null) {
                if (discreteOrderItem.getSku().isActive(discreteOrderItem.getProduct(), orderItem.getCategory())) {
                    OrderItem newItem = addOrderItemToOrder(customerCart, discreteOrderItem.clone(), false);
                    mergeCartResponse.getAddedItems().add(newItem);
                    oldNewItemMap.put(orderItem, newItem);
                } else {
                    mergeCartResponse.getRemovedItems().add(orderItem);
                }
            } else {
                if (discreteOrderItem.getProduct().isActive() && orderItem.getCategory().isActive()) {
                    OrderItem newItem = addOrderItemToOrder(customerCart, discreteOrderItem.clone(), false);
                    mergeCartResponse.getAddedItems().add(newItem);
                    oldNewItemMap.put(orderItem, newItem);
                } else {
                    mergeCartResponse.getRemovedItems().add(orderItem);
                }
            }
        } else if (orderItem instanceof BundleOrderItem) {
            BundleOrderItem bundleOrderItem = (BundleOrderItem) orderItem;
            orderItem.removeAllAdjustments();
            orderItem.removeAllCandidateItemOffers();
            boolean removeBundle = false;
            for (DiscreteOrderItem discreteOrderItem : bundleOrderItem.getDiscreteOrderItems()) {
                discreteOrderItem.removeAllAdjustments();
                discreteOrderItem.removeAllCandidateItemOffers();
                if (discreteOrderItem.getSku().getActiveStartDate() != null) {
                    if (!discreteOrderItem.getSku().isActive(discreteOrderItem.getProduct(), orderItem.getCategory())) {
                        /*
                             * Bundle has an inactive item in it -- remove the whole bundle
                             */
                        removeBundle = true;
                    }
                } else {
                    if (!discreteOrderItem.getProduct().isActive() || !orderItem.getCategory().isActive()) {
                        removeBundle = true;
                    }
                }
            }
            if (!removeBundle) {
                OrderItem newItem = addOrderItemToOrder(customerCart, bundleOrderItem.clone(), false);
                mergeCartResponse.getAddedItems().add(newItem);
                oldNewItemMap.put(orderItem, newItem);
            } else {
                mergeCartResponse.getRemovedItems().add(orderItem);
            }
        }
    }
    return customerCart;
}
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) GiftWrapOrderItem(org.broadleafcommerce.core.order.domain.GiftWrapOrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem)

Example 90 with OrderItem

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

the class LegacyMergeCartServiceImpl method removeItemFromOrder.

protected Order removeItemFromOrder(Order order, OrderItem item, boolean priceOrder) throws PricingException {
    fulfillmentGroupService.removeOrderItemFromFullfillmentGroups(order, item);
    OrderItem itemFromOrder = order.getOrderItems().remove(order.getOrderItems().indexOf(item));
    itemFromOrder.setOrder(null);
    orderItemService.delete(itemFromOrder);
    order = orderService.save(order, priceOrder);
    return order;
}
Also used : DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) GiftWrapOrderItem(org.broadleafcommerce.core.order.domain.GiftWrapOrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem)

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