Search in sources :

Example 1 with SkuBundleItem

use of org.broadleafcommerce.core.catalog.domain.SkuBundleItem 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 SkuBundleItem

use of org.broadleafcommerce.core.catalog.domain.SkuBundleItem 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 SkuBundleItem

use of org.broadleafcommerce.core.catalog.domain.SkuBundleItem 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)

Example 4 with SkuBundleItem

use of org.broadleafcommerce.core.catalog.domain.SkuBundleItem in project BroadleafCommerce by BroadleafCommerce.

the class SkuBundleItemCustomPersistenceHandler method fetch.

@Override
public DynamicResultSet fetch(PersistencePackage persistencePackage, CriteriaTransferObject cto, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
    try {
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        ForeignKey foreignKey = (ForeignKey) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.FOREIGNKEY);
        // sort it
        if (foreignKey != null && foreignKey.getSortField() != null) {
            FilterAndSortCriteria sortCriteria = cto.get(foreignKey.getSortField());
            sortCriteria.setSortAscending(foreignKey.getSortAscending());
        }
        // get the default properties from Inventory and its subclasses
        Map<String, FieldMetadata> originalProps = helper.getSimpleMergedProperties(SkuBundleItem.class.getName(), persistencePerspective);
        // Pull back the Inventory based on the criteria from the client
        List<FilterMapping> filterMappings = helper.getFilterMappings(persistencePerspective, cto, ceilingEntityFullyQualifiedClassname, originalProps);
        // attach the product option criteria
        skuPersistenceHandler.applyProductOptionValueCriteria(filterMappings, cto, persistencePackage, "sku");
        List<Serializable> records = helper.getPersistentRecords(persistencePackage.getCeilingEntityFullyQualifiedClassname(), filterMappings, cto.getFirstResult(), cto.getMaxResults());
        // Convert Skus into the client-side Entity representation
        Entity[] payload = helper.getRecords(originalProps, records);
        int totalRecords = helper.getTotalRecords(persistencePackage.getCeilingEntityFullyQualifiedClassname(), filterMappings);
        for (int i = 0; i < payload.length; i++) {
            Entity entity = payload[i];
            SkuBundleItem bundleItem = (SkuBundleItem) records.get(i);
            Sku bundleSku = bundleItem.getSku();
            entity.findProperty("sku").setDisplayValue(bundleSku.getName());
            List<ProductOptionValue> productOptionValues = new ArrayList<>();
            for (SkuProductOptionValueXref skuProductOptionValueXref : bundleSku.getProductOptionValueXrefs()) {
                productOptionValues.add(skuProductOptionValueXref.getProductOptionValue());
            }
            Property optionsProperty = skuPersistenceHandler.getConsolidatedOptionProperty(productOptionValues);
            entity.addProperty(optionsProperty);
        }
        return new DynamicResultSet(null, payload, totalRecords);
    } catch (Exception e) {
        throw new ServiceException("There was a problem fetching inventory. See server logs for more details", e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) Serializable(java.io.Serializable) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) ArrayList(java.util.ArrayList) FilterMapping(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping) ForeignKey(org.broadleafcommerce.openadmin.dto.ForeignKey) ServiceException(org.broadleafcommerce.common.exception.ServiceException) ProductOptionValue(org.broadleafcommerce.core.catalog.domain.ProductOptionValue) SkuProductOptionValueXref(org.broadleafcommerce.core.catalog.domain.SkuProductOptionValueXref) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) SkuBundleItem(org.broadleafcommerce.core.catalog.domain.SkuBundleItem) FilterAndSortCriteria(org.broadleafcommerce.openadmin.dto.FilterAndSortCriteria) Sku(org.broadleafcommerce.core.catalog.domain.Sku) Property(org.broadleafcommerce.openadmin.dto.Property) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet)

Example 5 with SkuBundleItem

use of org.broadleafcommerce.core.catalog.domain.SkuBundleItem 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;
}
Also used : DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) SkuBundleItem(org.broadleafcommerce.core.catalog.domain.SkuBundleItem) FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup)

Aggregations

SkuBundleItem (org.broadleafcommerce.core.catalog.domain.SkuBundleItem)6 ProductBundle (org.broadleafcommerce.core.catalog.domain.ProductBundle)4 Sku (org.broadleafcommerce.core.catalog.domain.Sku)4 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)4 Product (org.broadleafcommerce.core.catalog.domain.Product)3 BundleOrderItem (org.broadleafcommerce.core.order.domain.BundleOrderItem)3 ArrayList (java.util.ArrayList)2 Category (org.broadleafcommerce.core.catalog.domain.Category)2 OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)2 Serializable (java.io.Serializable)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 ServiceException (org.broadleafcommerce.common.exception.ServiceException)1 Money (org.broadleafcommerce.common.money.Money)1 ProductOptionValue (org.broadleafcommerce.core.catalog.domain.ProductOptionValue)1 SkuBundleItemImpl (org.broadleafcommerce.core.catalog.domain.SkuBundleItemImpl)1 SkuProductOptionValueXref (org.broadleafcommerce.core.catalog.domain.SkuProductOptionValueXref)1 FulfillmentGroup (org.broadleafcommerce.core.order.domain.FulfillmentGroup)1 FulfillmentGroupItem (org.broadleafcommerce.core.order.domain.FulfillmentGroupItem)1 Order (org.broadleafcommerce.core.order.domain.Order)1