Ejemplo n.º 1
0
        public virtual AdjustInventoryResult AdjustInventory(Product product, bool decrease, int quantity, string attributesXml)
        {
            Guard.NotNull(product, nameof(product));

            var result = new AdjustInventoryResult();

            switch (product.ManageInventoryMethod)
            {
            case ManageInventoryMethod.DontManageStock:
            {
                //do nothing
            }
            break;

            case ManageInventoryMethod.ManageStock:
            {
                result.StockQuantityOld = product.StockQuantity;
                if (decrease)
                {
                    result.StockQuantityNew = product.StockQuantity - quantity;
                }
                else
                {
                    result.StockQuantityNew = product.StockQuantity + quantity;
                }

                bool newPublished             = product.Published;
                bool newDisableBuyButton      = product.DisableBuyButton;
                bool newDisableWishlistButton = product.DisableWishlistButton;

                //check if minimum quantity is reached
                switch (product.LowStockActivity)
                {
                case LowStockActivity.DisableBuyButton:
                    newDisableBuyButton      = product.MinStockQuantity >= result.StockQuantityNew;
                    newDisableWishlistButton = product.MinStockQuantity >= result.StockQuantityNew;
                    break;

                case LowStockActivity.Unpublish:
                    newPublished = product.MinStockQuantity <= result.StockQuantityNew;
                    break;
                }

                product.StockQuantity         = result.StockQuantityNew;
                product.DisableBuyButton      = newDisableBuyButton;
                product.DisableWishlistButton = newDisableWishlistButton;
                product.Published             = newPublished;

                UpdateProduct(product);

                //send email notification
                if (decrease && product.NotifyAdminForQuantityBelow > result.StockQuantityNew)
                {
                    _services.MessageFactory.SendQuantityBelowStoreOwnerNotification(product, _localizationSettings.DefaultAdminLanguageId);
                }
            }
            break;

            case ManageInventoryMethod.ManageStockByAttributes:
            {
                var combination = _productAttributeParser.FindProductVariantAttributeCombination(product.Id, attributesXml);
                if (combination != null)
                {
                    result.StockQuantityOld = combination.StockQuantity;
                    if (decrease)
                    {
                        result.StockQuantityNew = combination.StockQuantity - quantity;
                    }
                    else
                    {
                        result.StockQuantityNew = combination.StockQuantity + quantity;
                    }

                    combination.StockQuantity = result.StockQuantityNew;
                    _productAttributeService.UpdateProductVariantAttributeCombination(combination);
                }
            }
            break;

            default:
                break;
            }

            var attributeValues = _productAttributeParser.ParseProductVariantAttributeValues(attributesXml);

            attributeValues
            .Where(x => x.ValueType == ProductVariantAttributeValueType.ProductLinkage)
            .ToList()
            .Each(x =>
            {
                var linkedProduct = GetProductById(x.LinkedProductId);
                if (linkedProduct != null)
                {
                    AdjustInventory(linkedProduct, decrease, quantity * x.Quantity, "");
                }
            });

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adjusts inventory
        /// </summary>
        /// <param name="product">Product</param>
        /// <param name="decrease">A value indicating whether to increase or descrease product stock quantity</param>
        /// <param name="quantity">Quantity</param>
        /// <param name="attributesXml">Attributes in XML format</param>
        /// <returns>Adjust inventory result</returns>
        public virtual AdjustInventoryResult AdjustInventory(Product product, bool decrease, int quantity, string attributesXml)
        {
            if (product == null)
                throw new ArgumentNullException("product");

            var result = new AdjustInventoryResult();

            switch (product.ManageInventoryMethod)
            {
                case ManageInventoryMethod.DontManageStock:
                    {
                        //do nothing
                    }
                    break;
                case ManageInventoryMethod.ManageStock:
                    {
                        result.StockQuantityOld = product.StockQuantity;
                        if (decrease)
                            result.StockQuantityNew = product.StockQuantity - quantity;
                        else
                            result.StockQuantityNew = product.StockQuantity + quantity;

                        bool newPublished = product.Published;
                        bool newDisableBuyButton = product.DisableBuyButton;
                        bool newDisableWishlistButton = product.DisableWishlistButton;

                        //check if minimum quantity is reached
                        switch (product.LowStockActivity)
                        {
                            case LowStockActivity.DisableBuyButton:
                                newDisableBuyButton = product.MinStockQuantity >= result.StockQuantityNew;
                                newDisableWishlistButton = product.MinStockQuantity >= result.StockQuantityNew;
                                break;
                            case LowStockActivity.Unpublish:
                                newPublished = product.MinStockQuantity <= result.StockQuantityNew;
                                break;
                        }

                        product.StockQuantity = result.StockQuantityNew;
                        product.DisableBuyButton = newDisableBuyButton;
                        product.DisableWishlistButton = newDisableWishlistButton;
                        product.Published = newPublished;

                        UpdateProduct(product);

                        //send email notification
                        if (decrease && product.NotifyAdminForQuantityBelow > result.StockQuantityNew)
                            _workflowMessageService.SendQuantityBelowStoreOwnerNotification(product, _localizationSettings.DefaultAdminLanguageId);
                    }
                    break;
                case ManageInventoryMethod.ManageStockByAttributes:
                    {
                        var combination = _productAttributeParser.FindProductVariantAttributeCombination(product, attributesXml);
                        if (combination != null)
                        {
                            result.StockQuantityOld = combination.StockQuantity;
                            if (decrease)
                                result.StockQuantityNew = combination.StockQuantity - quantity;
                            else
                                result.StockQuantityNew = combination.StockQuantity + quantity;

                            combination.StockQuantity = result.StockQuantityNew;
                            _productAttributeService.UpdateProductVariantAttributeCombination(combination);
                        }
                    }
                    break;
                default:
                    break;
            }

            var attributeValues = _productAttributeParser.ParseProductVariantAttributeValues(attributesXml);

            attributeValues
                .Where(x => x.ValueType == ProductVariantAttributeValueType.ProductLinkage)
                .ToList()
                .Each(x =>
            {
                var linkedProduct = GetProductById(x.LinkedProductId);
                if (linkedProduct != null)
                    AdjustInventory(linkedProduct, decrease, quantity * x.Quantity, "");
            });

            return result;
        }