public SystemOperationResult CheckForStockOnItems(Order o)
        {

            // Build a list of product quantities to check
            Dictionary<string, ProductIdCombo> products = new Dictionary<string, ProductIdCombo>();
            foreach (Orders.LineItem item in o.Items)
            {
                ProductIdCombo combo = new ProductIdCombo() { Bvin = item.ProductId, VariantId = item.VariantId, Quantity = item.Quantity };

                if (products.ContainsKey(combo.Key()))
                {
                    products[combo.Key()].Quantity += combo.Quantity;
                }
                else
                {
                    products.Add(combo.Key(), combo);
                }
            }

            // Now check each quantity for the order
            foreach (string key in products.Keys)
            {
                Catalog.Product prod = null;
                Orders.LineItem lineItem = null;
                foreach (LineItem item in o.Items)
                {
                    if (item.ProductId + item.VariantId == key)
                    {
                        //we just need this to get the product 
                        //name if the product is not found
                        lineItem = item;
                        prod = item.GetAssociatedProduct(this);
                        break;
                    }
                }

                if (prod != null)
                {
                    if (prod.Status == Catalog.ProductStatus.Disabled)
                    {
                        return new SystemOperationResult(false, Content.SiteTerms.ReplaceTermVariable(Content.SiteTerms.GetTerm(Content.SiteTermIds.ProductNotAvailable), "productName", prod.ProductName));
                    }

                    ProductIdCombo checkcombo = products[key];

                    Catalog.InventoryCheckData data = CatalogServices.InventoryCheck(prod, checkcombo.VariantId);

                    if (data.IsAvailableForSale)
                    {
                        if (data.IsInStock)
                        {
                            if (data.Qty < checkcombo.Quantity)
                            {
                                string message = Content.SiteTerms.GetTerm(Content.SiteTermIds.CartNotEnoughQuantity);
                                message = Content.SiteTerms.ReplaceTermVariable(message, "ProductName", prod.ProductName);
                                message = Content.SiteTerms.ReplaceTermVariable(message, "Quantity", data.Qty.ToString());
                                return new SystemOperationResult(false, message);
                            }
                        }
                    }
                    else
                    {
                        return new SystemOperationResult(false, Content.SiteTerms.ReplaceTermVariable(Content.SiteTerms.GetTerm(Content.SiteTermIds.CartOutOfStock), "productName", prod.ProductName));
                    }
                }
                else
                {
                    return new SystemOperationResult(false, Content.SiteTerms.ReplaceTermVariable("{productname} is not availble at the moment.", "productName", lineItem.ProductName));
                }
            }

            return new SystemOperationResult(true, string.Empty);
        }
Esempio n. 2
0
        public SystemOperationResult CheckForStockOnItems(LineItem item)
        {
            // Build a list of product quantities to check
            var products = new Dictionary <string, ProductIdCombo>();

            var product = item.GetAssociatedProduct(this);

            if (product == null || product.Status == ProductStatus.Disabled)
            {
                var productNotAvailable = GlobalLocalization.GetFormattedString("ProductNotAvailable",
                                                                                product.ProductName);
                return(new SystemOperationResult(false, productNotAvailable));
            }

            if (!product.IsBundle)
            {
                var combo = new ProductIdCombo
                {
                    ProductId   = item.ProductId,
                    VariantId   = item.VariantId,
                    ProductName = item.ProductName,
                    Quantity    = item.Quantity,
                    Product     = product
                };

                if (products.ContainsKey(combo.Key()))
                {
                    products[combo.Key()].Quantity += combo.Quantity;
                }
                else
                {
                    products.Add(combo.Key(), combo);
                }
            }
            else
            {
                foreach (var bundledProductAdv in product.BundledProducts)
                {
                    var bundledProduct = bundledProductAdv.BundledProduct;
                    if (bundledProduct == null)
                    {
                        continue;
                    }

                    var optionSelection = item.SelectionData.GetSelections(bundledProductAdv.Id);
                    var variant         = bundledProduct.Variants.FindBySelectionData(optionSelection, bundledProduct.Options);
                    var variantId       = variant != null ? variant.Bvin : string.Empty;

                    var combo = new ProductIdCombo
                    {
                        ProductId   = bundledProduct.Bvin,
                        VariantId   = variantId,
                        ProductName = bundledProduct.ProductName,
                        Quantity    = bundledProductAdv.Quantity,
                        Product     = bundledProduct
                    };

                    if (products.ContainsKey(combo.Key()))
                    {
                        products[combo.Key()].Quantity += combo.Quantity;
                    }
                    else
                    {
                        products.Add(combo.Key(), combo);
                    }
                }
            }


            // Now check each quantity for the order
            foreach (var key in products.Keys)
            {
                var checkcombo = products[key];
                var prod       = checkcombo.Product;

                var data = CatalogServices.SimpleProductInventoryCheck(prod, checkcombo.VariantId, checkcombo.Quantity);

                if (!data.IsAvailableForSale)
                {
                    if (data.Qty == 0)
                    {
                        var cartOutOfStock = GlobalLocalization.GetFormattedString("CartOutOfStock", prod.ProductName);
                        return(new SystemOperationResult(false, cartOutOfStock));
                    }
                    var message = GlobalLocalization.GetFormattedString("CartNotEnoughQuantity", prod.ProductName,
                                                                        data.Qty);
                    return(new SystemOperationResult(false, message));
                }
            }

            return(new SystemOperationResult(true, string.Empty));
        }