protected override ShoppingCartRuleResult PerformRules(MyHLShoppingCart cart, ShoppingCartRuleReason reason, ShoppingCartRuleResult Result)
        {
            //if (!GetPurchaseRestrictionManager(cart.DistributorID).CanPurchase)
            //{
            //    cart.ItemsBeingAdded.Clear();
            //    Result.AddMessage(
            //        string.Format(
            //            HttpContext.GetGlobalResourceObject(
            //                string.Format("{0}_ErrorMessage", HLConfigManager.Platform), "CantBuy").ToString()));
            //    Result.Result = RulesResult.Failure;
            //    return Result;
            //}

            var currentlimits = GetCurrentPurchasingLimits(cart.DistributorID, GetCurrentOrderMonth());

            if (cart == null || currentlimits == null)
            {
                return(Result);
            }

            if (cart.ItemsBeingAdded == null || cart.ItemsBeingAdded.Count == 0)
            {
                return(Result);
            }

            string processingCountryCode = DistributorProfileModel.ProcessingCountryCode;

            bool          bCanPurchase      = CanPurchase(cart.DistributorID);
            bool          bCanPurchasePType = CanPurchasePType(cart.DistributorID);
            var           errors            = new List <string>();
            decimal       NewVolumePoints   = decimal.Zero;
            decimal       cartVolume        = cart.VolumeInCart;
            bool          bLimitExceeded    = false;
            List <string> skuToAdd          = new List <string>();

            foreach (var item in cart.ItemsBeingAdded)
            {
                var currentItem = CatalogProvider.GetCatalogItem(item.SKU, Country);
                if (currentItem == null)
                {
                    continue;
                }

                if (APFDueProvider.IsAPFSku(item.SKU) || cart.OrderCategory == ServiceProvider.CatalogSvc.OrderCategoryType.ETO)
                {
                    skuToAdd.Add(item.SKU);
                }
                else
                {
                    if (bCanPurchase)
                    {
                        if (!bCanPurchasePType)
                        {
                            if (currentItem.ProductType == ServiceProvider.CatalogSvc.ProductType.Product)
                            {
                                Result.Result = RulesResult.Failure;
                                errors.Add(
                                    string.Format(
                                        HttpContext.GetGlobalResourceObject(
                                            string.Format("{0}_Rules", HLConfigManager.Platform),
                                            "PurchaseLimitTypeProductCategory").ToString(), item.SKU));
                                continue;
                            }
                        }
                    }
                    else
                    {
                        Result.Result = RulesResult.Failure;
                        errors.Add(
                            string.Format(
                                HttpContext.GetGlobalResourceObject(
                                    string.Format("{0}_ErrorMessage", HLConfigManager.Platform),
                                    "CantBuy").ToString()));
                        continue;
                    }

                    if (currentlimits.PurchaseLimitType == PurchaseLimitType.Volume || currentlimits.RestrictionPeriod == PurchasingLimitRestrictionPeriod.PerOrder)
                    {
                        if (currentlimits.maxVolumeLimit == -1)
                        {
                            skuToAdd.Add(item.SKU);
                            continue;
                        }
                        NewVolumePoints += currentItem.VolumePoints * item.Quantity;

                        //verifying VP limits didn't exceed
                        if (currentlimits.RemainingVolume - (cartVolume + NewVolumePoints) < 0)
                        {
                            if (currentlimits.LimitsRestrictionType == LimitsRestrictionType.FOP || currentlimits.LimitsRestrictionType == LimitsRestrictionType.OrderThreshold)
                            //MPE FOP
                            {
                                Result.Result = RulesResult.Failure;
                                ///Order exceeds the allowable volume for First Order Program. The Volume on the order needs to be reduced by {0:F2} VPs. The following SKU(s) have not been added to the cart.
                                if (!bLimitExceeded) //  to add this message only once
                                {
                                    errors.Add(
                                        string.Format(
                                            HttpContext.GetGlobalResourceObject(
                                                string.Format("{0}_Rules", HLConfigManager.Platform),
                                                "FOPVolumePointExceeds").ToString(), 1100,
                                            PurchaseRestrictionProvider.GetVolumeLimitsAfterFirstOrderFOP(
                                                processingCountryCode),
                                            PurchaseRestrictionProvider.GetThresholdPeriod(processingCountryCode), -999));
                                    // -999 should be replaced with caluclated value.
                                    bLimitExceeded = true;
                                }
                                /// Item SKU:{0}.
                                errors.Add(
                                    string.Format(
                                        HttpContext.GetGlobalResourceObject(
                                            string.Format("{0}_Rules", HLConfigManager.Platform),
                                            "VolumePointExceedsThreshold").ToString(), item.SKU));
                            }
                            else  //it exceeded VP but it's not FOP
                            {
                                var itemExists = cart.CartItems.Find(i => i.SKU == item.SKU);
                                if (itemExists != null)  //item already exists in cart
                                {
                                    Result.Result = RulesResult.Failure;
                                    errors.Add(
                                        string.Format(
                                            HttpContext.GetGlobalResourceObject(
                                                string.Format("{0}_Rules", HLConfigManager.Platform),
                                                "VolumePointExceedsByIncreasingQuantity").ToString(), item.SKU, item.Quantity));
                                }
                                else
                                {
                                    Result.Result = RulesResult.Failure;
                                    errors.Add(
                                        string.Format(
                                            HttpContext.GetGlobalResourceObject(
                                                string.Format("{0}_Rules", HLConfigManager.Platform),
                                                "VolumePointExceeds").ToString(), item.SKU));
                                }
                            }
                        }
                        else // // VP limits didn't exceeded, add item
                        {
                            skuToAdd.Add(item.SKU);
                        }
                    }
                    else  //purchasing limits are not per VP nor per order: add item
                    {
                        skuToAdd.Add(item.SKU);
                    }
                }
            }
            if (Result.Result == RulesResult.Failure && errors.Count > 0)
            {
                if (cart.OnCheckout && (currentlimits.LimitsRestrictionType == LimitsRestrictionType.FOP || currentlimits.LimitsRestrictionType == LimitsRestrictionType.OrderThreshold))
                {
                    Result.AddMessage(string.Format(HttpContext.GetGlobalResourceObject(
                                                        string.Format("{0}_Rules", HLConfigManager.Platform),
                                                        "FOPVolumePointExceedsOnCheckout").ToString(), 1100, PurchaseRestrictionProvider.GetVolumeLimitsAfterFirstOrderFOP(processingCountryCode), PurchaseRestrictionProvider.GetThresholdPeriod(processingCountryCode), (cartVolume + NewVolumePoints) - currentlimits.RemainingVolume));
                }
                else
                {
                    errors = errors.Select(x => x.Replace("-999", ((cartVolume + NewVolumePoints) - currentlimits.RemainingVolume).ToString())).ToList <string>();
                    Array.ForEach(errors.ToArray(), a => Result.AddMessage(a));
                }
            }
            cart.ItemsBeingAdded.RemoveAll(s => !skuToAdd.Contains(s.SKU));

            return(Result);
        }
        private ShoppingCartRuleResult performRules(MyHLShoppingCart cart, ShoppingCartRuleResult Result, PurchasingLimits_V01 currentlimits)
        {
            if (!GetPurchaseRestrictionManager(cart.DistributorID).CanPurchase)
            {
                cart.ItemsBeingAdded.Clear();
                Result.AddMessage(
                    string.Format(
                        HttpContext.GetGlobalResourceObject(
                            string.Format("{0}_ErrorMessage", HLConfigManager.Platform), "CantBuy").ToString()));
                Result.Result = RulesResult.Failure;
                return(Result);
            }

            if (cart.ItemsBeingAdded == null || cart.ItemsBeingAdded.Count == 0)
            {
                return(Result);
            }

            bool          bCanPurchasePType = CanPurchasePType(cart.DistributorID);
            var           errors            = new List <string>();
            decimal       NewVolumePoints   = decimal.Zero;
            decimal       cartVolume        = cart.VolumeInCart;
            bool          bLimitExceeded    = false;
            List <string> skuToAdd          = new List <string>();

            foreach (var item in cart.ItemsBeingAdded)
            {
                var currentItem = CatalogProvider.GetCatalogItem(item.SKU, Country);
                if (currentItem == null)
                {
                    continue;
                }
                if (!bCanPurchasePType)
                {
                    if (currentItem.ProductType == ServiceProvider.CatalogSvc.ProductType.Product)
                    {
                        Result.Result = RulesResult.Failure;
                        errors.Add(
                            string.Format(
                                HttpContext.GetGlobalResourceObject(
                                    string.Format("{0}_Rules", HLConfigManager.Platform),
                                    "PurchaseLimitTypeProductCategory").ToString(), item.SKU));
                        continue;
                    }
                }
                if (currentlimits.PurchaseLimitType == PurchaseLimitType.Volume)
                {
                    if (currentlimits.maxVolumeLimit == -1)
                    {
                        skuToAdd.Add(item.SKU);
                        continue;
                    }
                    NewVolumePoints += currentItem.VolumePoints * item.Quantity;

                    if (currentlimits.RemainingVolume - (cartVolume + NewVolumePoints) < 0)
                    {
                        Result.Result = RulesResult.Failure;
                        if (currentlimits.LimitsRestrictionType == LimitsRestrictionType.FOP)
                        //MPE FOP
                        {
                            string processingCountryCode = DistributorProfileModel.ProcessingCountryCode;
                            ///Order exceeds the allowable volume for First Order Program. The Volume on the order needs to be reduced by {0:F2} VPs. The following SKU(s) have not been added to the cart.
                            if (!bLimitExceeded) //  to add this message only once
                            {
                                if (currentlimits.PurchaseType == OrderPurchaseType.Consignment)
                                {
                                    errors.Add(
                                        string.Format(
                                            HttpContext.GetGlobalResourceObject(
                                                string.Format("{0}_Rules", HLConfigManager.Platform),
                                                "FOPConsignmentVolumePointExceeds").ToString(), 1100, PurchaseRestrictionProvider.GetVolumeLimitsAfterFirstOrderFOP(processingCountryCode), PurchaseRestrictionProvider.GetThresholdPeriod(processingCountryCode)));
                                }
                                else if (currentlimits.PurchaseType == OrderPurchaseType.PersonalConsumption)
                                {
                                    errors.Add(
                                        string.Format(
                                            HttpContext.GetGlobalResourceObject(
                                                string.Format("{0}_Rules", HLConfigManager.Platform),
                                                "FOPPersonalConsumptionVolumePointExceeds").ToString(), 1100, PurchaseRestrictionProvider.GetThresholdPeriod(processingCountryCode)));
                                }
                                else
                                {
                                    errors.Add(
                                        string.Format(
                                            HttpContext.GetGlobalResourceObject(
                                                string.Format("{0}_Rules", HLConfigManager.Platform),
                                                "FOPVolumePointExceeds").ToString(), 1100,
                                            PurchaseRestrictionProvider.GetVolumeLimitsAfterFirstOrderFOP(
                                                processingCountryCode),
                                            PurchaseRestrictionProvider.GetThresholdPeriod(processingCountryCode), -999));
                                    // -999 should be replaced with caluclated value.
                                }
                                bLimitExceeded = true;
                            }
                            /// Item SKU:{0}.
                            //errors.Add(
                            //    string.Format(
                            //        HttpContext.GetGlobalResourceObject(
                            //            string.Format("{0}_Rules", HLConfigManager.Platform),
                            //            "VolumePointExceedsThreshold").ToString(), item.SKU));
                        }
                        else
                        {
                            if (cart.CartItems.Exists(i => i.SKU == item.SKU))
                            {
                                ///The quantity of the item SKU:{0} can not be increased by {1} because it exceeds your volume points limit.
                                errors.Add(
                                    string.Format(
                                        HttpContext.GetGlobalResourceObject(
                                            string.Format("{0}_Rules", HLConfigManager.Platform),
                                            "VolumePointExceedsByIncreasingQuantity").ToString(),
                                        item.SKU, item.Quantity));
                            }
                            else
                            {
                                ///Item SKU:{0} has not been added to the cart since by adding that into the cart, you exceeded your volume points  limit.
                                errors.Add(
                                    string.Format(
                                        HttpContext.GetGlobalResourceObject(
                                            string.Format("{0}_Rules", HLConfigManager.Platform),
                                            "VolumePointExceeds").ToString(), item.SKU));
                            }
                        }
                    }
                    else
                    {
                        skuToAdd.Add(item.SKU);
                    }
                }
                else
                {
                    skuToAdd.Add(item.SKU);
                }
            }
            if (Result.Result == RulesResult.Failure && errors.Count > 0)
            {
                errors = errors.Select(x => x.Replace("-999", ((cartVolume + NewVolumePoints) - currentlimits.RemainingVolume).ToString())).ToList <string>();
                Array.ForEach(errors.ToArray(), a => Result.AddMessage(a));
            }
            cart.ItemsBeingAdded.RemoveAll(s => !skuToAdd.Contains(s.SKU));

            return(Result);
        }