Search in sources :

Example 1 with ProductBundle

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);
    }
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) Category(org.broadleafcommerce.core.catalog.domain.Category) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) SkuBundleItem(org.broadleafcommerce.core.catalog.domain.SkuBundleItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) ProductBundle(org.broadleafcommerce.core.catalog.domain.ProductBundle) Product(org.broadleafcommerce.core.catalog.domain.Product) Sku(org.broadleafcommerce.core.catalog.domain.Sku)

Example 2 with ProductBundle

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;
}
Also used : Money(org.broadleafcommerce.common.money.Money) SkuBundleItem(org.broadleafcommerce.core.catalog.domain.SkuBundleItem) ProductBundle(org.broadleafcommerce.core.catalog.domain.ProductBundle) Product(org.broadleafcommerce.core.catalog.domain.Product) Sku(org.broadleafcommerce.core.catalog.domain.Sku) SkuBundleItemImpl(org.broadleafcommerce.core.catalog.domain.SkuBundleItemImpl)

Example 3 with ProductBundle

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;
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) OrderItemRequestDTO(org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) ProductBundle(org.broadleafcommerce.core.catalog.domain.ProductBundle) Sku(org.broadleafcommerce.core.catalog.domain.Sku) Test(org.testng.annotations.Test) Rollback(org.springframework.test.annotation.Rollback) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with ProductBundle

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;
}
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 5 with ProductBundle

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

ProductBundle (org.broadleafcommerce.core.catalog.domain.ProductBundle)14 Product (org.broadleafcommerce.core.catalog.domain.Product)9 Sku (org.broadleafcommerce.core.catalog.domain.Sku)9 BundleOrderItem (org.broadleafcommerce.core.order.domain.BundleOrderItem)6 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)6 Category (org.broadleafcommerce.core.catalog.domain.Category)4 SkuBundleItem (org.broadleafcommerce.core.catalog.domain.SkuBundleItem)4 Order (org.broadleafcommerce.core.order.domain.Order)4 OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)3 ServiceException (org.broadleafcommerce.common.exception.ServiceException)2 ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)2 Money (org.broadleafcommerce.common.money.Money)2 CategoryProductXref (org.broadleafcommerce.core.catalog.domain.CategoryProductXref)2 DiscreteOrderItemRequest (org.broadleafcommerce.core.order.service.call.DiscreteOrderItemRequest)2 OrderItemRequestDTO (org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO)2 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)2 Entity (org.broadleafcommerce.openadmin.dto.Entity)2 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)2 PersistencePerspective (org.broadleafcommerce.openadmin.dto.PersistencePerspective)2 Transactional (org.springframework.transaction.annotation.Transactional)2