use of org.broadleafcommerce.core.catalog.domain.ProductBundle in project BroadleafCommerce by BroadleafCommerce.
the class LegacyOrderServiceImpl method addItemToOrder.
@Override
public Order addItemToOrder(Long orderId, OrderItemRequestDTO orderItemRequestDTO, boolean priceOrder) throws PricingException {
if (orderItemRequestDTO.getQuantity() == null || orderItemRequestDTO.getQuantity() == 0) {
LOG.debug("Not adding item to order because quantity is zero.");
return null;
}
if (orderItemRequestDTO.getQuantity() < 0) {
throw new IllegalArgumentException("Quantity cannot be negative");
}
Order order = validateOrder(orderId);
Product product = validateProduct(orderItemRequestDTO.getProductId());
Sku sku = determineSku(product, orderItemRequestDTO.getSkuId(), orderItemRequestDTO.getItemAttributes());
if (sku == null) {
return null;
}
Category category = determineCategory(product, orderItemRequestDTO.getCategoryId());
if (product == null || !(product instanceof ProductBundle)) {
DiscreteOrderItem item = orderItemService.createDiscreteOrderItem(createDiscreteOrderItemRequest(order, null, sku, product, category, orderItemRequestDTO.getQuantity(), orderItemRequestDTO.getItemAttributes()));
item.setOrder(order);
List<OrderItem> orderItems = order.getOrderItems();
orderItems.add(item);
return updateOrder(order, priceOrder);
} else {
ProductBundle bundle = (ProductBundle) product;
BundleOrderItem bundleOrderItem = (BundleOrderItem) orderItemDao.create(OrderItemType.BUNDLE);
bundleOrderItem.setQuantity(orderItemRequestDTO.getQuantity());
bundleOrderItem.setCategory(category);
bundleOrderItem.setSku(sku);
bundleOrderItem.setName(product.getName());
bundleOrderItem.setProductBundle(bundle);
bundleOrderItem.setOrder(order);
for (SkuBundleItem skuBundleItem : bundle.getSkuBundleItems()) {
Product bundleProduct = skuBundleItem.getBundle();
Sku bundleSku = skuBundleItem.getSku();
Category bundleCategory = determineCategory(bundleProduct, orderItemRequestDTO.getCategoryId());
DiscreteOrderItem bundleDiscreteItem = orderItemService.createDiscreteOrderItem(createDiscreteOrderItemRequest(null, bundleOrderItem, bundleSku, bundleProduct, bundleCategory, skuBundleItem.getQuantity(), orderItemRequestDTO.getItemAttributes()));
bundleDiscreteItem.setBundleOrderItem(bundleOrderItem);
bundleDiscreteItem.setSkuBundleItem(skuBundleItem);
bundleOrderItem.getDiscreteOrderItems().add(bundleDiscreteItem);
}
List<OrderItem> orderItems = order.getOrderItems();
orderItems.add(bundleOrderItem);
return updateOrder(order, priceOrder);
}
}
use of org.broadleafcommerce.core.catalog.domain.ProductBundle in project BroadleafCommerce by BroadleafCommerce.
the class CommonSetupBaseTest method addProductBundle.
public ProductBundle addProductBundle() {
// Create the product
Product p = addTestProduct("bundleproduct1", "bundlecat1");
// Create the sku for the ProductBundle object
Sku bundleSku = catalogService.createSku();
bundleSku.setName(p.getName());
bundleSku.setRetailPrice(new Money(44.99));
bundleSku.setActiveStartDate(p.getActiveStartDate());
bundleSku.setActiveEndDate(p.getActiveEndDate());
bundleSku.setDiscountable(true);
// Create the ProductBundle and associate the sku
ProductBundle bundle = (ProductBundle) catalogService.createProduct(ProductType.BUNDLE);
bundle.setDefaultCategory(p.getDefaultCategory());
bundle.setDefaultSku(bundleSku);
bundle = (ProductBundle) catalogService.saveProduct(bundle);
// Reverse-associate the ProductBundle to the sku (Must be done this way because it's a
// bidirectional OneToOne relationship
// bundleSku.setDefaultProduct(bundle);
// catalogService.saveSku(bundleSku);
// Wrap the product/sku that is part of the bundle in a SkuBundleItem
SkuBundleItem skuBundleItem = new SkuBundleItemImpl();
skuBundleItem.setBundle(bundle);
skuBundleItem.setQuantity(1);
skuBundleItem.setSku(p.getDefaultSku());
// Add the SkuBundleItem to the ProductBundle
bundle.getSkuBundleItems().add(skuBundleItem);
bundle = (ProductBundle) catalogService.saveProduct(bundle);
return bundle;
}
use of org.broadleafcommerce.core.catalog.domain.ProductBundle in project BroadleafCommerce by BroadleafCommerce.
the class OrderTest method addBundleToOrder.
@Test(groups = { "addBundleToOrder" }, dependsOnGroups = { "addAnotherItemToOrder" })
@Rollback(false)
@Transactional
public void addBundleToOrder() throws AddToCartException {
numOrderItems++;
Sku sku = skuDao.readFirstSku();
Order order = orderService.findOrderById(orderId);
assert order != null;
assert sku.getId() != null;
ProductBundle bundleItem = addProductBundle();
OrderItemRequestDTO orderItemRequestDTO = new OrderItemRequestDTO();
orderItemRequestDTO.setProductId(bundleItem.getId());
orderItemRequestDTO.setSkuId(bundleItem.getDefaultSku().getId());
orderItemRequestDTO.setQuantity(1);
order = orderService.addItem(order.getId(), orderItemRequestDTO, true);
BundleOrderItem item = (BundleOrderItem) orderService.findLastMatchingItem(order, bundleItem.getDefaultSku().getId(), null);
bundleOrderItemId = item.getId();
assert item != null;
assert item.getQuantity() == 1;
assert item.getDiscreteOrderItems().size() == 1;
}
use of org.broadleafcommerce.core.catalog.domain.ProductBundle 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.catalog.domain.ProductBundle 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