Search in sources :

Example 51 with DiscreteOrderItem

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

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

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

the class LegacyOrderServiceImpl method addDiscreteItemToOrder.

@Override
@Deprecated
public OrderItem addDiscreteItemToOrder(Order order, DiscreteOrderItemRequest itemRequest, boolean priceOrder) throws PricingException {
    itemRequest.setOrder(order);
    DiscreteOrderItem item = orderItemService.createDiscreteOrderItem(itemRequest);
    return addOrderItemToOrder(order, item, priceOrder);
}
Also used : DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem)

Example 54 with DiscreteOrderItem

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

the class LegacyOrderServiceImpl method removeOrderItemFromFullfillmentGroup.

protected void removeOrderItemFromFullfillmentGroup(Order order, OrderItem orderItem) {
    List<FulfillmentGroup> fulfillmentGroups = order.getFulfillmentGroups();
    for (FulfillmentGroup fulfillmentGroup : fulfillmentGroups) {
        Iterator<FulfillmentGroupItem> itr = fulfillmentGroup.getFulfillmentGroupItems().iterator();
        while (itr.hasNext()) {
            FulfillmentGroupItem fulfillmentGroupItem = itr.next();
            if (fulfillmentGroupItem.getOrderItem().equals(orderItem)) {
                itr.remove();
                fulfillmentGroupItemDao.delete(fulfillmentGroupItem);
            } else if (orderItem instanceof BundleOrderItem) {
                BundleOrderItem bundleOrderItem = (BundleOrderItem) orderItem;
                for (DiscreteOrderItem discreteOrderItem : bundleOrderItem.getDiscreteOrderItems()) {
                    if (fulfillmentGroupItem.getOrderItem().equals(discreteOrderItem)) {
                        itr.remove();
                        fulfillmentGroupItemDao.delete(fulfillmentGroupItem);
                        break;
                    }
                }
            }
        }
    }
}
Also used : DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup)

Example 55 with DiscreteOrderItem

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

the class LegacyOrderServiceImpl method removeItemFromBundle.

@Override
public Order removeItemFromBundle(Order order, BundleOrderItem bundle, OrderItem item, boolean priceOrder) throws PricingException {
    DiscreteOrderItem itemFromBundle = bundle.getDiscreteOrderItems().remove(bundle.getDiscreteOrderItems().indexOf(item));
    orderItemService.delete(itemFromBundle);
    itemFromBundle.setBundleOrderItem(null);
    order = updateOrder(order, priceOrder);
    return order;
}
Also used : DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem)

Aggregations

DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)57 BundleOrderItem (org.broadleafcommerce.core.order.domain.BundleOrderItem)30 OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)27 Order (org.broadleafcommerce.core.order.domain.Order)19 ArrayList (java.util.ArrayList)18 FulfillmentGroupItem (org.broadleafcommerce.core.order.domain.FulfillmentGroupItem)18 FulfillmentGroup (org.broadleafcommerce.core.order.domain.FulfillmentGroup)17 Sku (org.broadleafcommerce.core.catalog.domain.Sku)16 HashMap (java.util.HashMap)11 OrderItemRequestDTO (org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO)10 Money (org.broadleafcommerce.common.money.Money)9 Product (org.broadleafcommerce.core.catalog.domain.Product)9 DiscreteOrderItemImpl (org.broadleafcommerce.core.order.domain.DiscreteOrderItemImpl)8 Transactional (org.springframework.transaction.annotation.Transactional)8 SkuImpl (org.broadleafcommerce.core.catalog.domain.SkuImpl)7 FulfillmentGroupItemImpl (org.broadleafcommerce.core.order.domain.FulfillmentGroupItemImpl)7 GiftWrapOrderItem (org.broadleafcommerce.core.order.domain.GiftWrapOrderItem)7 FulfillmentGroupImpl (org.broadleafcommerce.core.order.domain.FulfillmentGroupImpl)6 List (java.util.List)5 ProductBundle (org.broadleafcommerce.core.catalog.domain.ProductBundle)5