Search in sources :

Example 11 with DiscreteOrderItem

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

the class ValidateUpdateRequestActivity method execute.

@Override
public ProcessContext<CartOperationRequest> execute(ProcessContext<CartOperationRequest> context) throws Exception {
    CartOperationRequest request = context.getSeedData();
    OrderItemRequestDTO orderItemRequestDTO = request.getItemRequest();
    // Throw an exception if the user did not specify an orderItemId
    if (orderItemRequestDTO.getOrderItemId() == null) {
        throw new IllegalArgumentException("OrderItemId must be specified when removing from order");
    }
    // Throw an exception if the user tried to update an item to a negative quantity
    if (orderItemRequestDTO.getQuantity() < 0) {
        throw new IllegalArgumentException("Quantity cannot be negative");
    }
    // Throw an exception if the user did not specify an order to add the item to
    if (request.getOrder() == null) {
        throw new IllegalArgumentException("Order is required when updating item quantities");
    }
    // Throw an exception if the user is trying to update an order item that is part of a bundle
    OrderItem orderItem = orderItemService.readOrderItemById(orderItemRequestDTO.getOrderItemId());
    if (orderItem != null && orderItem instanceof DiscreteOrderItem) {
        DiscreteOrderItem doi = (DiscreteOrderItem) orderItem;
        if (doi.getBundleOrderItem() != null) {
            throw new IllegalArgumentException("Cannot update an item that is part of a bundle");
        }
    }
    return context;
}
Also used : DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) CartOperationRequest(org.broadleafcommerce.core.order.service.workflow.CartOperationRequest) OrderItemRequestDTO(org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem)

Example 12 with DiscreteOrderItem

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

the class AddWorkflowPriceOrderIfNecessaryActivity method updateChildOrderItem.

/**
 * Traverses the current OrderItem for a match to the parentOrderItemId.
 * If found, populates and returns true.
 *
 * @param request
 * @param orderItem
 * @return
 */
protected void updateChildOrderItem(CartOperationRequest request, Order order) {
    boolean updated = false;
    for (OrderItem oi : order.getOrderItems()) {
        if (oi instanceof BundleOrderItem) {
            BundleOrderItem boi = (BundleOrderItem) oi;
            // check the bundle children
            updated = checkAndUpdateChildren(request, boi);
            if (!updated) {
                for (OrderItem discreteOrderItem : boi.getDiscreteOrderItems()) {
                    // check the bundle discrete items
                    if (checkAndUpdateChildren(request, discreteOrderItem)) {
                        updated = true;
                        // break out of the discrete items loop
                        break;
                    }
                }
            }
        } else {
            updated = checkAndUpdateChildren(request, oi);
        }
        if (updated) {
            break;
        }
    }
}
Also used : 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 13 with DiscreteOrderItem

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

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

the class AutoBundleActivity method populateItemMatchesForSku.

protected int populateItemMatchesForSku(List<DiscreteOrderItem> matchingItems, Order order, List<DiscreteOrderItem> unbundledItems, Long skuId) {
    int skuMatches = 0;
    for (OrderItem orderItem : order.getOrderItems()) {
        if (orderItem instanceof DiscreteOrderItem) {
            DiscreteOrderItem item = (DiscreteOrderItem) orderItem;
            if (skuId.equals(item.getSku().getId())) {
                matchingItems.add(item);
                skuMatches = skuMatches + item.getQuantity();
            }
        }
    }
    if (unbundledItems != null) {
        for (DiscreteOrderItem discreteOrderItem : unbundledItems) {
            if (skuId.equals(discreteOrderItem.getSku().getId())) {
                skuMatches = skuMatches + discreteOrderItem.getQuantity();
            }
        }
    }
    return skuMatches;
}
Also used : DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem)

Example 15 with DiscreteOrderItem

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

the class AutoBundleActivity method handleAutomaticBundling.

public Order handleAutomaticBundling(Order order) throws PricingException, RemoveFromCartException {
    boolean itemsHaveBeenUnbundled = false;
    List<DiscreteOrderItem> unbundledItems = null;
    List<ProductBundle> productBundles = catalogService.findAutomaticProductBundles();
    Set<Long> processedBundleIds = new HashSet<Long>();
    for (ProductBundle productBundle : productBundles) {
        int existingUses = countExistingUsesOfBundle(order, productBundle);
        Integer maxApplications = null;
        for (SkuBundleItem skuBundleItem : productBundle.getSkuBundleItems()) {
            int maxSkuApplications = countMaximumApplications(order, skuBundleItem, processedBundleIds);
            if (maxApplications == null || maxApplications > maxSkuApplications) {
                maxApplications = maxSkuApplications;
            }
        }
        processedBundleIds.add(productBundle.getId());
        if (maxApplications != existingUses) {
            if (!itemsHaveBeenUnbundled) {
                // Store the discrete items that were part of automatic bundles
                unbundledItems = unBundleItems(order);
                order = removeAutomaticBundles(order);
                itemsHaveBeenUnbundled = true;
            }
            // Create a new bundle with maxApplication occurrences
            order = bundleItems(order, productBundle, maxApplications, unbundledItems);
        }
    }
    return order;
}
Also used : DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) SkuBundleItem(org.broadleafcommerce.core.catalog.domain.SkuBundleItem) ProductBundle(org.broadleafcommerce.core.catalog.domain.ProductBundle) HashSet(java.util.HashSet)

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