Search in sources :

Example 21 with DiscreteOrderItem

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

the class AutoBundleActivity method bundleItems.

/**
 * Builds a BundleOrderItem based on the passed in productBundle.    Creates new DiscreteOrderItems.
 * Removes the existing matching DiscreteOrderItems or modifies the quantity if needed.
 *
 * @param order
 * @param productBundle
 * @param numApplications
 * @throws PricingException
 * @throws ItemNotFoundException
 */
private Order bundleItems(Order order, ProductBundle productBundle, Integer numApplications, List<DiscreteOrderItem> unbundledItems) throws PricingException, RemoveFromCartException {
    BundleOrderItem bundleOrderItem = (BundleOrderItem) orderItemDao.create(OrderItemType.BUNDLE);
    bundleOrderItem.setQuantity(numApplications);
    bundleOrderItem.setCategory(productBundle.getDefaultCategory());
    bundleOrderItem.setSku(productBundle.getDefaultSku());
    bundleOrderItem.setName(productBundle.getName());
    bundleOrderItem.setProductBundle(productBundle);
    bundleOrderItem.setOrder(order);
    // make a copy of the fulfillment group items since they will be deleted when the order items are removed
    Map<Long, FulfillmentGroupItem> skuIdFulfillmentGroupMap = new HashMap<Long, FulfillmentGroupItem>();
    for (FulfillmentGroup fulfillmentGroup : order.getFulfillmentGroups()) {
        for (FulfillmentGroupItem fulfillmentGroupItem : fulfillmentGroup.getFulfillmentGroupItems()) {
            if (fulfillmentGroupItem.getOrderItem() instanceof DiscreteOrderItem) {
                DiscreteOrderItem discreteOrderItem = (DiscreteOrderItem) fulfillmentGroupItem.getOrderItem();
                skuIdFulfillmentGroupMap.put(discreteOrderItem.getSku().getId(), fulfillmentGroupItem);
            }
        }
    }
    for (SkuBundleItem skuBundleItem : productBundle.getSkuBundleItems()) {
        List<DiscreteOrderItem> itemMatches = new ArrayList<DiscreteOrderItem>();
        int skuMatches = populateItemMatchesForSku(itemMatches, order, unbundledItems, skuBundleItem.getSku().getId());
        int skusRequired = skuBundleItem.getQuantity() * numApplications;
        if (skuMatches < skusRequired) {
            throw new IllegalArgumentException("Something went wrong creating automatic bundles.  Not enough skus to fulfill bundle requirements for sku id: " + skuBundleItem.getSku().getId());
        }
        // this call also deletes any fulfillment group items that are associated with that order item
        for (DiscreteOrderItem item : itemMatches) {
            order = orderService.removeItem(order.getId(), item.getId(), false);
        }
        DiscreteOrderItem baseItem = null;
        if (itemMatches.size() > 0) {
            baseItem = itemMatches.get(0);
        } else {
            for (DiscreteOrderItem discreteOrderItem : unbundledItems) {
                if (discreteOrderItem.getSku().getId().equals(skuBundleItem.getSku().getId())) {
                    baseItem = discreteOrderItem;
                }
            }
        }
        // Add item to the skuBundle
        DiscreteOrderItem newSkuBundleItem = (DiscreteOrderItem) baseItem.clone();
        newSkuBundleItem.setSkuBundleItem(skuBundleItem);
        newSkuBundleItem.setBundleOrderItem(bundleOrderItem);
        newSkuBundleItem.setQuantity(skuBundleItem.getQuantity());
        newSkuBundleItem.setOrder(null);
        bundleOrderItem.getDiscreteOrderItems().add(newSkuBundleItem);
        if (skuMatches > skusRequired) {
            // Add a non-bundled item to the order with the remaining sku count.
            DiscreteOrderItem newOrderItem = (DiscreteOrderItem) baseItem.clone();
            newOrderItem.setBundleOrderItem(null);
            newOrderItem.setSkuBundleItem(null);
            newOrderItem.setQuantity(skuMatches - skusRequired);
            newOrderItem = (DiscreteOrderItem) orderItemDao.save(newOrderItem);
            newOrderItem.setOrder(order);
            newOrderItem.updateSaleAndRetailPrices();
            // Re-associate fulfillment group item to newOrderItem
            FulfillmentGroupItem fulfillmentGroupItem = skuIdFulfillmentGroupMap.get(newSkuBundleItem.getSku().getId());
            if (fulfillmentGroupItem != null) {
                FulfillmentGroupItem newFulfillmentGroupItem = fulfillmentGroupItem.clone();
                newFulfillmentGroupItem.setOrderItem(newOrderItem);
                newFulfillmentGroupItem.setQuantity(newOrderItem.getQuantity());
                newFulfillmentGroupItem = fulfillmentGroupItemDao.save(newFulfillmentGroupItem);
                // these associations may have not been committed yet. This order is used in other activities and will not be reloaded if in a transaction.
                for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
                    if (newFulfillmentGroupItem.getFulfillmentGroup() != null && fg.getId().equals(newFulfillmentGroupItem.getFulfillmentGroup().getId())) {
                        fg.addFulfillmentGroupItem(newFulfillmentGroupItem);
                    }
                }
            }
            order.getOrderItems().add(newOrderItem);
        }
    }
    bundleOrderItem.updateSaleAndRetailPrices();
    order.getOrderItems().add(bundleOrderItem);
    order = orderService.save(order, false);
    for (OrderItem orderItem : order.getOrderItems()) {
        if (orderItem instanceof BundleOrderItem) {
            BundleOrderItem bundleItem = (BundleOrderItem) orderItem;
            for (DiscreteOrderItem discreteOrderItem : bundleItem.getDiscreteOrderItems()) {
                // Re-associate fulfillment group item to newly created skuBundles
                FulfillmentGroupItem fulfillmentGroupItem = skuIdFulfillmentGroupMap.get(discreteOrderItem.getSku().getId());
                if (fulfillmentGroupItem != null) {
                    FulfillmentGroupItem newFulfillmentGroupItem = fulfillmentGroupItem.clone();
                    newFulfillmentGroupItem.setOrderItem(discreteOrderItem);
                    newFulfillmentGroupItem.setQuantity(discreteOrderItem.getQuantity());
                    // these associations may have not been committed yet. This order is used in other activities and will not be reloaded if in a transaction.
                    for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
                        if (newFulfillmentGroupItem.getFulfillmentGroup() != null && fg.getId().equals(newFulfillmentGroupItem.getFulfillmentGroup().getId())) {
                            fg.addFulfillmentGroupItem(newFulfillmentGroupItem);
                        }
                    }
                }
            }
        }
    }
    // reload order with new fulfillment group items
    order = orderService.save(order, false);
    return order;
}
Also used : DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) SkuBundleItem(org.broadleafcommerce.core.catalog.domain.SkuBundleItem) 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)

Example 22 with DiscreteOrderItem

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

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

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

the class ConsolidateFulfillmentFeesActivity method execute.

@Override
public ProcessContext<Order> execute(ProcessContext<Order> context) throws Exception {
    Order order = context.getSeedData();
    for (FulfillmentGroup fulfillmentGroup : order.getFulfillmentGroups()) {
        // create and associate all the Fulfillment Fees
        for (FulfillmentGroupItem item : fulfillmentGroup.getFulfillmentGroupItems()) {
            List<SkuFee> fees = null;
            if (item.getOrderItem() instanceof BundleOrderItem) {
                fees = ((BundleOrderItem) item.getOrderItem()).getSku().getFees();
            } else if (item.getOrderItem() instanceof DiscreteOrderItem) {
                fees = ((DiscreteOrderItem) item.getOrderItem()).getSku().getFees();
            }
            if (fees != null) {
                for (SkuFee fee : fees) {
                    if (SkuFeeType.FULFILLMENT.equals(fee.getFeeType())) {
                        if (shouldApplyFeeToFulfillmentGroup(fee, fulfillmentGroup)) {
                            FulfillmentGroupFee fulfillmentFee = fulfillmentGroupService.createFulfillmentGroupFee();
                            fulfillmentFee.setName(fee.getName());
                            fulfillmentFee.setTaxable(fee.getTaxable());
                            fulfillmentFee.setAmount(fee.getAmount());
                            fulfillmentFee.setFulfillmentGroup(fulfillmentGroup);
                            fulfillmentGroup.addFulfillmentGroupFee(fulfillmentFee);
                        }
                    }
                }
            }
        }
        if (fulfillmentGroup.getFulfillmentGroupFees().size() > 0) {
            fulfillmentGroup = fulfillmentGroupService.save(fulfillmentGroup);
        }
    }
    context.setSeedData(order);
    return context;
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) SkuFee(org.broadleafcommerce.core.catalog.domain.SkuFee) FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup) FulfillmentGroupFee(org.broadleafcommerce.core.order.domain.FulfillmentGroupFee)

Example 25 with DiscreteOrderItem

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

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