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