/// <summary> /// Validates the product inventory for items currently added in basket /// </summary> /// <param name="fixQuantity">Indicate if the quantity need to be fixed as per available stock. If no then it will only record warning messages</param> /// <returns>returns true if no issues found</returns> protected bool ValidateInventory(bool fixQuantity = false) { bool recalculateRequried = false; // VALIDATE INVENTORY List <string> warningMessages = new List <string>(); if (_StoreInventoryEnabled) { Dictionary <string, InventoryInfo> inventories = new Dictionary <string, InventoryInfo>(); string tempMessage; InventoryInfo info; foreach (BasketItem item in _Basket.Items) { if (item.OrderItemType != OrderItemType.Product || item.IsChildItem) { continue; } info = GetInventoryInfo(inventories, item); bool enforceInv = info.InventoryStatus.InventoryMode != InventoryMode.None && info.InventoryStatus.AllowBackorder == false; if (enforceInv) { // inventory needs to be enforced if (item.Quantity > info.NowAvailable) { if (fixQuantity) { if (info.NowAvailable < 1) { item.Quantity = 0; } else { item.Quantity = (short)info.NowAvailable; } item.Save(); recalculateRequried = true; } else { tempMessage = GetInventoryStockMessage(item, info); warningMessages.Add(tempMessage); } } // if this was a kit product and some quanity of it getting included // update the inventories for all its component products if (item.Product.KitStatus == KitStatus.Master && item.Quantity > 0) { InventoryInfo info1; foreach (InventoryManagerData invd1 in info.InventoryStatus.ChildItemInventoryData) { info1 = inventories[invd1.ProductId + "_" + invd1.OptionList + "_"]; if (invd1.InventoryMode != InventoryMode.None && invd1.AllowBackorder == false) { // ensure that none of the component product exceeds inventory int effectiveQuantity = item.Quantity * invd1.Multiplier; if (effectiveQuantity > info1.NowAvailable) { if (fixQuantity) { if (info1.NowAvailable < 1) { item.Quantity = 0; } else { item.Quantity = (short)(info1.NowAvailable / invd1.Multiplier); } } else { tempMessage = GetInventoryStockMessage(item, info1); warningMessages.Add(tempMessage); } } info1.NowAvailable -= item.Quantity * invd1.Multiplier; inventories[invd1.ProductId + "_" + invd1.OptionList + "_"] = info1; } } } info.NowAvailable -= item.Quantity; } int curQty = item.Quantity; tempMessage = ValidateMinMaxLimits(item, info); if (!string.IsNullOrEmpty(tempMessage)) { warningMessages.Add(tempMessage); if (enforceInv) { info.NowAvailable += curQty - item.Quantity; } } } // CHECK IF WE NEED TO DISPLAY THE WARNINGS if (!fixQuantity && warningMessages.Count > 0) { InventoryMessages.DataSource = warningMessages; InventoryMessages.DataBind(); InventoryPopup.Show(); } else if (warningMessages.Count > 0) { InventoryPopup.Hide(); // save the basket _Basket.Save(); } } if (recalculateRequried) { IBasketService service = AbleContext.Resolve <IBasketService>(); service.Combine(_Basket); service.Recalculate(_Basket); } return(warningMessages.Count == 0); }