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;
}
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;
}
}
}
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;
}
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;
}
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;
}
Aggregations