Search in sources :

Example 31 with Sku

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

the class UncacheableDataProcessor method addCartData.

protected void addCartData(Map<String, Object> attrMap) {
    Order cart = CartState.getCart();
    int cartQty = 0;
    List<Long> cartItemIdsWithOptions = new ArrayList<>();
    List<Long> cartItemIdsWithoutOptions = new ArrayList<>();
    if (cart != null && cart.getOrderItems() != null) {
        cartQty = cart.getItemCount();
        for (OrderItem item : cart.getOrderItems()) {
            if (item instanceof SkuAccessor) {
                Sku sku = ((SkuAccessor) item).getSku();
                if (sku != null && sku.getProduct() != null && item.getParentOrderItem() == null) {
                    if (useSku) {
                        cartItemIdsWithoutOptions.add(sku.getId());
                    } else {
                        Product product = sku.getProduct();
                        List<ProductOptionXref> optionXrefs = product.getProductOptionXrefs();
                        if (optionXrefs == null || optionXrefs.isEmpty()) {
                            cartItemIdsWithoutOptions.add(product.getId());
                        } else {
                            cartItemIdsWithOptions.add(product.getId());
                        }
                    }
                }
            }
        }
    }
    attrMap.put("cartItemCount", cartQty);
    attrMap.put("cartItemIdsWithOptions", cartItemIdsWithOptions);
    attrMap.put("cartItemIdsWithoutOptions", cartItemIdsWithoutOptions);
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) ProductOptionXref(org.broadleafcommerce.core.catalog.domain.ProductOptionXref) ArrayList(java.util.ArrayList) Product(org.broadleafcommerce.core.catalog.domain.Product) Sku(org.broadleafcommerce.core.catalog.domain.Sku) SkuAccessor(org.broadleafcommerce.core.order.domain.SkuAccessor)

Example 32 with Sku

use of org.broadleafcommerce.core.catalog.domain.Sku 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);
        }
    }
}
Also used : DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) Sku(org.broadleafcommerce.core.catalog.domain.Sku)

Example 33 with Sku

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

the class DecrementInventoryRollbackHandler method rollbackState.

@Override
public void rollbackState(Activity<ProcessContext<CheckoutSeed>> activity, ProcessContext<CheckoutSeed> processContext, Map<String, Object> stateConfiguration) throws RollbackFailureException {
    if (shouldExecute(activity, processContext, stateConfiguration)) {
        String orderId = "(Not Known)";
        if (stateConfiguration.get(ROLLBACK_BLC_ORDER_ID) != null) {
            orderId = String.valueOf(stateConfiguration.get(ROLLBACK_BLC_ORDER_ID));
        }
        @SuppressWarnings("unchecked") Map<Sku, Integer> inventoryToIncrement = (Map<Sku, Integer>) stateConfiguration.get(ROLLBACK_BLC_INVENTORY_DECREMENTED);
        @SuppressWarnings("unchecked") Map<Sku, Integer> inventoryToDecrement = (Map<Sku, Integer>) stateConfiguration.get(ROLLBACK_BLC_INVENTORY_INCREMENTED);
        Map<String, Object> contextualInformation = new HashMap<>();
        contextualInformation.put(ContextualInventoryService.ROLLBACK_STATE_KEY, stateConfiguration.get(EXTENDED_ROLLBACK_STATE));
        contextualInformation.put(ContextualInventoryService.ORDER_KEY, processContext.getSeedData().getOrder());
        if (inventoryToIncrement != null && !inventoryToIncrement.isEmpty()) {
            try {
                inventoryService.incrementInventory(inventoryToIncrement, contextualInformation);
            } catch (Exception ex) {
                RollbackFailureException rfe = new RollbackFailureException("An unexpected error occured in the error handler of the checkout workflow trying to compensate for inventory. This happend for order ID: " + orderId + ". This should be corrected manually!", ex);
                rfe.setActivity(activity);
                rfe.setProcessContext(processContext);
                rfe.setStateItems(stateConfiguration);
                throw rfe;
            }
        }
        if (inventoryToDecrement != null && !inventoryToDecrement.isEmpty()) {
            try {
                inventoryService.decrementInventory(inventoryToDecrement, contextualInformation);
            } catch (InventoryUnavailableException e) {
                // This is an awkward, unlikely state.  I just added some inventory, but something happened, and I want to remove it, but it's already gone!
                RollbackFailureException rfe = new RollbackFailureException("While trying roll back (decrement) inventory, we found that there was none left decrement.", e);
                rfe.setActivity(activity);
                rfe.setProcessContext(processContext);
                rfe.setStateItems(stateConfiguration);
                throw rfe;
            } catch (RuntimeException ex) {
                LOG.error("An unexpected error occured in the error handler of the checkout workflow trying to compensate for inventory. This happend for order ID: " + StringUtil.sanitize(orderId) + ". This should be corrected manually!", ex);
                RollbackFailureException rfe = new RollbackFailureException("An unexpected error occured in the error handler of the checkout workflow " + "trying to compensate for inventory. This happend for order ID: " + orderId + ". This should be corrected manually!", ex);
                rfe.setActivity(activity);
                rfe.setProcessContext(processContext);
                rfe.setStateItems(stateConfiguration);
                throw rfe;
            }
        }
    }
}
Also used : RollbackFailureException(org.broadleafcommerce.core.workflow.state.RollbackFailureException) HashMap(java.util.HashMap) InventoryUnavailableException(org.broadleafcommerce.core.inventory.service.InventoryUnavailableException) InventoryUnavailableException(org.broadleafcommerce.core.inventory.service.InventoryUnavailableException) RollbackFailureException(org.broadleafcommerce.core.workflow.state.RollbackFailureException) Sku(org.broadleafcommerce.core.catalog.domain.Sku) HashMap(java.util.HashMap) Map(java.util.Map)

Example 34 with Sku

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

the class InventoryServiceImpl method retrieveQuantitiesAvailable.

@Override
public Map<Sku, Integer> retrieveQuantitiesAvailable(Collection<Sku> skus, Map<String, Object> context) {
    ExtensionResultHolder<Map<Sku, Integer>> holder = new ExtensionResultHolder<Map<Sku, Integer>>();
    ExtensionResultStatusType res = extensionManager.getProxy().retrieveQuantitiesAvailable(skus, context, holder);
    if (ExtensionResultStatusType.NOT_HANDLED.equals(res)) {
        Map<Sku, Integer> inventories = new HashMap<>();
        for (Sku sku : skus) {
            Integer quantityAvailable = 0;
            if (checkBasicAvailablility(sku)) {
                InventoryType skuInventoryType = sku.getInventoryType();
                if (InventoryType.CHECK_QUANTITY.equals(skuInventoryType)) {
                    if (sku.getQuantityAvailable() != null) {
                        quantityAvailable = sku.getQuantityAvailable();
                    }
                } else if (sku.getInventoryType() == null || InventoryType.ALWAYS_AVAILABLE.equals(skuInventoryType)) {
                    quantityAvailable = null;
                }
            }
            inventories.put(sku, quantityAvailable);
        }
        return inventories;
    } else {
        return holder.getResult();
    }
}
Also used : HashMap(java.util.HashMap) InventoryType(org.broadleafcommerce.core.inventory.service.type.InventoryType) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) Sku(org.broadleafcommerce.core.catalog.domain.Sku) HashMap(java.util.HashMap) Map(java.util.Map) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Example 35 with Sku

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

the class InventoryServiceImpl method incrementSku.

protected void incrementSku(Map<Sku, Integer> skuQuantities, Map<String, Object> context) {
    for (Entry<Sku, Integer> entry : skuQuantities.entrySet()) {
        Sku sku = entry.getKey();
        Integer quantity = entry.getValue();
        if (quantity == null || quantity < 1) {
            throw new IllegalArgumentException("Quantity " + quantity + " is not valid. Must be greater than zero and not null.");
        }
        if (InventoryType.CHECK_QUANTITY.equals(sku.getInventoryType())) {
            Integer currentInventoryAvailable = retrieveQuantityAvailable(sku, context);
            if (currentInventoryAvailable == null) {
                throw new IllegalArgumentException("The current inventory for this Sku is null");
            }
            int newInventory = currentInventoryAvailable + quantity;
            sku.setQuantityAvailable(newInventory);
            catalogService.saveSku(sku);
        } else {
            LOG.info("Not incrementing inventory as the Sku has been marked as always available");
        }
    }
}
Also used : Sku(org.broadleafcommerce.core.catalog.domain.Sku)

Aggregations

Sku (org.broadleafcommerce.core.catalog.domain.Sku)80 Product (org.broadleafcommerce.core.catalog.domain.Product)34 ArrayList (java.util.ArrayList)27 SkuImpl (org.broadleafcommerce.core.catalog.domain.SkuImpl)22 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)18 Money (org.broadleafcommerce.common.money.Money)16 ProductImpl (org.broadleafcommerce.core.catalog.domain.ProductImpl)16 Order (org.broadleafcommerce.core.order.domain.Order)15 Category (org.broadleafcommerce.core.catalog.domain.Category)13 FulfillmentGroupItem (org.broadleafcommerce.core.order.domain.FulfillmentGroupItem)12 Test (org.testng.annotations.Test)12 DiscreteOrderItemImpl (org.broadleafcommerce.core.order.domain.DiscreteOrderItemImpl)10 OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)10 Transactional (org.springframework.transaction.annotation.Transactional)10 ProductBundle (org.broadleafcommerce.core.catalog.domain.ProductBundle)9 BundleOrderItem (org.broadleafcommerce.core.order.domain.BundleOrderItem)9 HashMap (java.util.HashMap)8 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)8 FulfillmentGroup (org.broadleafcommerce.core.order.domain.FulfillmentGroup)8 Entity (org.broadleafcommerce.openadmin.dto.Entity)8