Search in sources :

Example 31 with BundleOrderItem

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

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

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

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

the class AddWorkflowPriceOrderIfNecessaryActivity method execute.

@Override
public ProcessContext<CartOperationRequest> execute(ProcessContext<CartOperationRequest> context) throws Exception {
    CartOperationRequest request = context.getSeedData();
    Order order = request.getOrder();
    // go ahead and carry out that delete here.
    if (CollectionUtils.isNotEmpty(request.getMultishipOptionsToDelete())) {
        for (Long[] pack : request.getMultishipOptionsToDelete()) {
            if (pack[1] == null) {
                orderMultishipOptionService.deleteOrderItemOrderMultishipOptions(pack[0]);
            } else {
                orderMultishipOptionService.deleteOrderItemOrderMultishipOptions(pack[0], pack[1].intValue());
            }
        }
    }
    // ones that should be deleted. Delete them here.
    if (CollectionUtils.isNotEmpty(request.getFgisToDelete())) {
        for (FulfillmentGroupItem fgi : request.getFgisToDelete()) {
            for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
                ListIterator<FulfillmentGroupItem> fgItemIter = fg.getFulfillmentGroupItems().listIterator();
                while (fgItemIter.hasNext()) {
                    FulfillmentGroupItem fgi2 = fgItemIter.next();
                    if (fgi2 == fgi) {
                        fgItemIter.remove();
                        fgItemDao.delete(fgi2);
                    }
                }
            }
        }
    }
    // We now need to delete any OrderItems that were marked as such, including their children, if any
    for (OrderItem oi : request.getOisToDelete()) {
        order.getOrderItems().remove(oi);
        orderItemService.delete(oi);
        if (oi.getParentOrderItem() != null) {
            OrderItem parentItem = oi.getParentOrderItem();
            parentItem.getChildOrderItems().remove(oi);
        }
    }
    // We need to build up a map of OrderItem to which FulfillmentGroupItems reference that particular OrderItem.
    // We'll also save the order item and build up a map of the unsaved items to their saved counterparts.
    Map<OrderItem, List<FulfillmentGroupItem>> oiFgiMap = new HashMap<>();
    Map<OrderItem, OrderItem> savedOrderItems = new HashMap<>();
    for (OrderItem oi : order.getOrderItems()) {
        if (oi instanceof BundleOrderItem) {
            // We first need to save the discrete order items that are part of this bundle. Once they're saved, we'll
            // mark them and remove them from this bundle.
            List<DiscreteOrderItem> doisToAdd = new ArrayList<>();
            ListIterator<DiscreteOrderItem> li = ((BundleOrderItem) oi).getDiscreteOrderItems().listIterator();
            while (li.hasNext()) {
                DiscreteOrderItem doi = li.next();
                getOiFgiMap(order, oiFgiMap, doi);
                DiscreteOrderItem savedDoi = (DiscreteOrderItem) orderItemService.saveOrderItem(doi);
                savedOrderItems.put(doi, savedDoi);
                li.remove();
                doisToAdd.add(savedDoi);
            }
            // After the discrete order items are saved, we can re-add the saved versions to our bundle and then
            // save the bundle as well.
            ((BundleOrderItem) oi).getDiscreteOrderItems().addAll(doisToAdd);
            BundleOrderItem savedBoi = (BundleOrderItem) orderItemService.saveOrderItem(oi);
            savedOrderItems.put(oi, savedBoi);
            // to to the saved version of the bundle.
            for (DiscreteOrderItem doi : savedBoi.getDiscreteOrderItems()) {
                doi.setBundleOrderItem(savedBoi);
            }
        } else {
            getOiFgiMap(order, oiFgiMap, oi);
            savedOrderItems.put(oi, orderItemService.saveOrderItem(oi));
        }
    }
    // Now, we'll update the orderitems in the order to their saved counterparts
    ListIterator<OrderItem> li = order.getOrderItems().listIterator();
    List<OrderItem> oisToAdd = new ArrayList<>();
    while (li.hasNext()) {
        OrderItem oi = li.next();
        OrderItem savedOi = savedOrderItems.get(oi);
        oisToAdd.add(savedOi);
        li.remove();
    }
    order.getOrderItems().addAll(oisToAdd);
    for (Entry<OrderItem, List<FulfillmentGroupItem>> entry : oiFgiMap.entrySet()) {
        // Update any FulfillmentGroupItems that reference order items
        for (FulfillmentGroupItem fgi : entry.getValue()) {
            fgi.setOrderItem(savedOrderItems.get(entry.getKey()));
        }
        // We also need to update the orderItem on the request in case it's used by the caller of this workflow
        if (entry.getKey() == request.getOrderItem()) {
            request.setOrderItem(savedOrderItems.get(entry.getKey()));
        }
    }
    // We need to add the new item to the parent's child order items as well.
    updateChildOrderItem(request, order);
    // If a custom implementation needs to handle additional saves before the parent Order is saved, this method
    // can be overridden to provide that functionality.
    preSaveOperation(request);
    // Now that our collection items in our Order have been saved and the state of our Order is in a place where we
    // won't get a transient save exception, we are able to go ahead and save the order with optional pricing.
    order = orderService.save(order, request.isPriceOrder());
    request.setOrder(order);
    return context;
}
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) FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup) ArrayList(java.util.ArrayList) List(java.util.List)

Example 35 with BundleOrderItem

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

the class UpdateCartServiceImpl method copyCartToCurrentContext.

@Override
public UpdateCartResponse copyCartToCurrentContext(Order currentCart) {
    if (currentCart.getOrderItems() == null) {
        return null;
    }
    BroadleafCurrency currency = findActiveCurrency();
    if (currency == null) {
        return null;
    }
    // Reprice order logic
    List<OrderItemRequestDTO> itemsToReprice = new ArrayList<>();
    List<OrderItem> itemsToRemove = new ArrayList<>();
    List<OrderItem> itemsToReset = new ArrayList<>();
    boolean repriceOrder = true;
    for (OrderItem orderItem : currentCart.getOrderItems()) {
        // Lookup price in price list, if null, then add to itemsToRemove
        if (orderItem instanceof DiscreteOrderItem) {
            DiscreteOrderItem doi = (DiscreteOrderItem) orderItem;
            if (checkAvailabilityInLocale(doi, currency)) {
                OrderItemRequestDTO itemRequest = new OrderItemRequestDTO();
                itemRequest.setProductId(doi.getProduct().getId());
                itemRequest.setQuantity(doi.getQuantity());
                itemsToReprice.add(itemRequest);
                itemsToReset.add(orderItem);
            } else {
                itemsToRemove.add(orderItem);
            }
        } else if (orderItem instanceof BundleOrderItem) {
            BundleOrderItem boi = (BundleOrderItem) orderItem;
            for (DiscreteOrderItem doi : boi.getDiscreteOrderItems()) {
                if (checkAvailabilityInLocale(doi, currency)) {
                    OrderItemRequestDTO itemRequest = new OrderItemRequestDTO();
                    itemRequest.setProductId(doi.getProduct().getId());
                    itemRequest.setQuantity(doi.getQuantity());
                    itemsToReprice.add(itemRequest);
                    itemsToReset.add(orderItem);
                } else {
                    itemsToRemove.add(orderItem);
                }
            }
        }
    }
    for (OrderItem orderItem : itemsToReset) {
        try {
            currentCart = orderService.removeItem(currentCart.getId(), orderItem.getId(), false);
        } catch (RemoveFromCartException e) {
            LOG.error("Could not remove from cart.", e);
        }
    }
    for (OrderItemRequestDTO itemRequest : itemsToReprice) {
        try {
            currentCart = orderService.addItem(currentCart.getId(), itemRequest, false);
        } catch (AddToCartException e) {
            LOG.error("Could not add to cart.", e);
        }
    }
    // Reprice and save the cart
    try {
        currentCart = orderService.save(currentCart, repriceOrder);
    } catch (PricingException e) {
        LOG.error("Could not save cart.", e);
    }
    setSavedCurrency(currency);
    UpdateCartResponse updateCartResponse = new UpdateCartResponse();
    updateCartResponse.setRemovedItems(itemsToRemove);
    updateCartResponse.setOrder(currentCart);
    return updateCartResponse;
}
Also used : DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) ArrayList(java.util.ArrayList) OrderItemRequestDTO(org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO) BroadleafCurrency(org.broadleafcommerce.common.currency.domain.BroadleafCurrency) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) PricingException(org.broadleafcommerce.core.pricing.service.exception.PricingException) UpdateCartResponse(org.broadleafcommerce.core.order.service.call.UpdateCartResponse) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) AddToCartException(org.broadleafcommerce.core.order.service.exception.AddToCartException) RemoveFromCartException(org.broadleafcommerce.core.order.service.exception.RemoveFromCartException)

Aggregations

BundleOrderItem (org.broadleafcommerce.core.order.domain.BundleOrderItem)35 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)30 OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)23 Order (org.broadleafcommerce.core.order.domain.Order)16 ArrayList (java.util.ArrayList)13 FulfillmentGroupItem (org.broadleafcommerce.core.order.domain.FulfillmentGroupItem)10 FulfillmentGroup (org.broadleafcommerce.core.order.domain.FulfillmentGroup)9 Sku (org.broadleafcommerce.core.catalog.domain.Sku)8 HashMap (java.util.HashMap)7 ProductBundle (org.broadleafcommerce.core.catalog.domain.ProductBundle)5 GiftWrapOrderItem (org.broadleafcommerce.core.order.domain.GiftWrapOrderItem)5 Transactional (org.springframework.transaction.annotation.Transactional)5 Product (org.broadleafcommerce.core.catalog.domain.Product)4 OrderItemRequestDTO (org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO)4 Test (org.testng.annotations.Test)4 List (java.util.List)3 Money (org.broadleafcommerce.common.money.Money)3 Category (org.broadleafcommerce.core.catalog.domain.Category)3 SkuBundleItem (org.broadleafcommerce.core.catalog.domain.SkuBundleItem)3 BigDecimal (java.math.BigDecimal)2