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