Search in sources :

Example 11 with BundleOrderItem

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

the class OrderTest method checkOrderItems.

@Test(groups = { "checkOrderItems" }, dependsOnGroups = { "removeItemFromOrder" })
@Transactional
public void checkOrderItems() throws PricingException {
    Order order = orderService.findOrderById(orderId);
    // However, the previous two transactions were rolled back and thus the items still exist.
    assert order.getOrderItems().size() == 1;
    // As mentioned, the bundleOrderItem however has gone away
    BundleOrderItem bundleOrderItem = (BundleOrderItem) orderItemService.readOrderItemById(bundleOrderItemId);
    assert bundleOrderItem == null;
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) Test(org.testng.annotations.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 12 with BundleOrderItem

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

the class OrderTest method testIllegalUpdateScenarios.

@Test(groups = { "testIllegalUpdateScenarios" }, dependsOnGroups = { "addItemToOrder" })
@Transactional
public void testIllegalUpdateScenarios() throws UpdateCartException, AddToCartException, RemoveFromCartException {
    Order order = orderService.findOrderById(orderId);
    assert order != null;
    Product activeProduct = addTestProduct("mug", "cups", true);
    Product inactiveProduct = addTestProduct("cup", "cups", false);
    // Inactive skus should not be added
    OrderItemRequestDTO itemRequest = new OrderItemRequestDTO().setQuantity(1).setSkuId(activeProduct.getDefaultSku().getId());
    boolean addSuccessful = true;
    try {
        order = orderService.addItem(orderId, itemRequest, true);
    } catch (AddToCartException e) {
        addSuccessful = false;
    }
    assert addSuccessful;
    // should not be able to update to negative quantity
    OrderItem item = orderService.findLastMatchingItem(order, activeProduct.getDefaultSku().getId(), activeProduct.getId());
    itemRequest = new OrderItemRequestDTO().setQuantity(-3).setOrderItemId(item.getId());
    boolean updateSuccessful = true;
    try {
        orderService.updateItemQuantity(orderId, itemRequest, true);
    } catch (UpdateCartException e) {
        updateSuccessful = false;
    }
    assert !updateSuccessful;
    // shouldn't be able to update the quantity of a DOI inside of a bundle
    ProductBundle bundle = addProductBundle();
    itemRequest = new OrderItemRequestDTO().setQuantity(1).setProductId(bundle.getId()).setSkuId(bundle.getDefaultSku().getId());
    addSuccessful = true;
    try {
        order = orderService.addItem(orderId, itemRequest, true);
    } catch (AddToCartException e) {
        addSuccessful = false;
    }
    assert addSuccessful;
    BundleOrderItem bundleItem = (BundleOrderItem) orderService.findLastMatchingItem(order, bundle.getDefaultSku().getId(), bundle.getId());
    // should just be a single DOI inside the bundle
    DiscreteOrderItem doi = bundleItem.getDiscreteOrderItems().get(0);
    itemRequest = new OrderItemRequestDTO().setQuantity(4).setOrderItemId(doi.getId());
    try {
        orderService.updateItemQuantity(orderId, itemRequest, true);
    } catch (UpdateCartException e) {
        updateSuccessful = false;
    }
    assert !updateSuccessful;
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItemRequestDTO(org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) AddToCartException(org.broadleafcommerce.core.order.service.exception.AddToCartException) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) ProductBundle(org.broadleafcommerce.core.catalog.domain.ProductBundle) Product(org.broadleafcommerce.core.catalog.domain.Product) UpdateCartException(org.broadleafcommerce.core.order.service.exception.UpdateCartException) Test(org.testng.annotations.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 13 with BundleOrderItem

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

the class OrderTest method removeBundleFromOrder.

@Test(groups = { "removeBundleFromOrder" }, dependsOnGroups = { "addBundleToOrder" })
@Rollback(false)
@Transactional
public void removeBundleFromOrder() throws RemoveFromCartException {
    Order order = orderService.findOrderById(orderId);
    List<OrderItem> orderItems = order.getOrderItems();
    assert orderItems.size() == numOrderItems;
    int startingSize = orderItems.size();
    BundleOrderItem bundleOrderItem = (BundleOrderItem) orderItemService.readOrderItemById(bundleOrderItemId);
    assert bundleOrderItem != null;
    assert bundleOrderItem.getDiscreteOrderItems() != null;
    assert bundleOrderItem.getDiscreteOrderItems().size() == 1;
    order = orderService.removeItem(order.getId(), bundleOrderItem.getId(), true);
    List<OrderItem> items = order.getOrderItems();
    assert items != null;
    assert items.size() == startingSize - 1;
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) 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) Test(org.testng.annotations.Test) Rollback(org.springframework.test.annotation.Rollback) Transactional(org.springframework.transaction.annotation.Transactional)

Example 14 with BundleOrderItem

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

the class i18nUpdateCartServiceExtensionHandler method fixTranslations.

protected void fixTranslations(Order cart) {
    for (DiscreteOrderItem orderItem : cart.getDiscreteOrderItems()) {
        Sku sku = orderItem.getSku();
        translateOrderItem(orderItem, sku);
    }
    for (OrderItem orderItem : cart.getOrderItems()) {
        if (orderItem instanceof BundleOrderItem) {
            BundleOrderItem bundleItem = (BundleOrderItem) orderItem;
            Sku sku = bundleItem.getSku();
            translateOrderItem(orderItem, sku);
        }
    }
}
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) Sku(org.broadleafcommerce.core.catalog.domain.Sku)

Example 15 with BundleOrderItem

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

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