public static decimal TotalsExcludeAPF(MyHLShoppingCart cart, string countryCode)
        {
            if (APFDueProvider.IsAPFSkuPresent(cart.CartItems))
            {
                var calcTheseItems = new List <ShoppingCartItem_V01>();
                calcTheseItems.AddRange(from i in cart.CartItems
                                        where !APFDueProvider.IsAPFSku(i.SKU)
                                        select
                                        new ShoppingCartItem_V01(i.ID, i.SKU, i.Quantity, i.Updated,
                                                                 i.MinQuantity));

                // remove A and L type
                var allItems =
                    CatalogProvider.GetCatalogItems(
                        (from s in calcTheseItems where s.SKU != null select s.SKU).ToList(), countryCode);
                if (null != allItems && allItems.Count > 0)
                {
                    var skuExcluded = (from c in allItems.Values
                                       where (c as CatalogItem_V01).ProductType != ServiceProvider.CatalogSvc.ProductType.Product
                                       select c.SKU);
                    calcTheseItems.RemoveAll(s => skuExcluded.Contains(s.SKU));
                }

                var totals = cart.Calculate(calcTheseItems);
                return(totals != null ? (totals as OrderTotals_V01).AmountDue : decimal.Zero);
            }
            return(cart.Totals != null ? (cart.Totals as OrderTotals_V01).AmountDue : decimal.Zero);
        }
Example #2
0
        private ShoppingCartRuleResult PerformRules(ShoppingCart_V02 cart,
                                                    ShoppingCartRuleReason reason,
                                                    ShoppingCartRuleResult Result)
        {
            if (reason == ShoppingCartRuleReason.CartItemsBeingAdded)
            {
                var SKUIds = new List <string[]>();
                SKUIds.Add(new[] { HLConfigManager.Configurations.DOConfiguration.TodayMagazineSku, "3" });
                SKUIds.Add(new[] { "4150", "10" });

                if (cart.CurrentItems[0].SKU == HLConfigManager.Configurations.DOConfiguration.TodayMagazineSku)
                {
                    var catItems = CatalogProvider.GetCatalogItems((from c in cart.CartItems
                                                                    select c.SKU.Trim()).ToList <string>(),
                                                                   Country);
                    if (catItems != null)
                    {
                        if (!catItems.Any(c => c.Value.ProductType == ProductType.Product))
                        {
                            Result.Result = RulesResult.Failure;
                            Result.AddMessage(
                                string.Format(
                                    HttpContext.GetGlobalResourceObject(
                                        string.Format("{0}_Rules", HLConfigManager.Platform), "AddProductFirst")
                                    .ToString(), cart.CurrentItems[0].SKU));
                            cart.RuleResults.Add(Result);
                            return(Result);
                        }
                    }
                }

                foreach (string[] sku in SKUIds)
                {
                    int quantity = 0;
                    if (cart.CurrentItems[0].SKU == sku[0])
                    {
                        quantity += cart.CurrentItems[0].Quantity;
                        if (cart.CartItems.Exists(item => item.SKU == sku[0]))
                        {
                            quantity += cart.CartItems.Where(item => item.SKU == sku[0]).First().Quantity;
                        }
                        if (quantity > Convert.ToInt32(sku[1]))
                        {
                            Result.Result = RulesResult.Failure;
                            Result.AddMessage(
                                string.Format(
                                    HttpContext.GetGlobalResourceObject(
                                        string.Format("{0}_Rules", HLConfigManager.Platform), "SKUQuantityExceeds")
                                    .ToString(), sku[1], sku[0]));
                            cart.RuleResults.Add(Result);
                        }
                    }
                }
            }
            return(Result);
        }
Example #3
0
        private bool ProductInCart(ShoppingCart_V02 cart)
        {
            bool hasProduct = false;
            var  catItems   =
                CatalogProvider.GetCatalogItems((from c in cart.CartItems select c.SKU.Trim()).ToList <string>(), Country);

            if (catItems != null)
            {
                if (catItems.Any(c => c.Value.ProductType == ProductType.Product))
                {
                    hasProduct = true;
                }
            }

            return(hasProduct);
        }
        private bool ProductInCart(ShoppingCart_V02 cart)
        {
            bool hasProduct = false;
            var  catItems   =
                CatalogProvider.GetCatalogItems((from c in cart.CartItems select c.SKU.Trim()).ToList <string>(), Country);

            if (catItems != null)
            {
                if (catItems.Any(c => c.Value.ProductType == ProductType.Product))
                {
                    hasProduct = true;
                }
            }
            //if we don't get items of type product,check whether items are in allowed SKUs
            if (AllowedSKUToAddPromoSKU.Any(s => s == cart.CartItems[0].SKU))
            {
                hasProduct = true;
            }

            return(hasProduct);
        }
Example #5
0
        public static decimal TotalsExcludeAPF(MyHLShoppingCart cart, string countryCode)
        {
            var calcTheseItems = new List <ShoppingCartItem_V01>();

            calcTheseItems.AddRange(from i in cart.CartItems
                                    where !APFDueProvider.IsAPFSku(i.SKU)
                                    select
                                    new ShoppingCartItem_V01(i.ID, i.SKU, i.Quantity, i.Updated,
                                                             i.MinQuantity));

            // remove A and L type
            var allItems =
                CatalogProvider.GetCatalogItems(
                    (from s in calcTheseItems where s.SKU != null select s.SKU).ToList(), countryCode);

            if (null != allItems && allItems.Count > 0)
            {
                var totals = cart.Calculate(calcTheseItems);
                return(totals != null ? (totals as OrderTotals_V01).AmountDue : decimal.Zero);
            }

            return(decimal.Zero);
        }
        /// <summary>
        ///     The IShoppingCart Rule Interface implementation
        /// </summary>
        /// <param name="cart">The current Shopping Cart</param>
        /// <param name="reason">The Rule invoke Reason</param>
        /// <param name="Result">The Rule Results collection</param>
        /// <returns>The cumulative rule results - including the results of this iteration</returns>
        protected virtual ShoppingCartRuleResult PerformRules(ShoppingCart_V02 cart,
                                                              ShoppingCartRuleReason reason,
                                                              ShoppingCartRuleResult Result)
        {
            if (reason == ShoppingCartRuleReason.CartItemsBeingAdded)
            {
                var     purchasingLimitManager               = PurchasingLimitManager(cart.DistributorID);
                decimal DistributorRemainingVolumePoints     = 0;
                decimal DistributorRemainingDiscountedRetail = 0;
                //decimal CartVolumePoints = 0;
                decimal NewVolumePoints = 0;

                decimal DistributorRemainingEarnings = 0;
                //decimal CartEarnings = 0;
                decimal NewEarnings = 0;

                decimal Discount = 0;

                var myhlCart = cart as MyHLShoppingCart;

                if (null == myhlCart)
                {
                    LoggerHelper.Error(
                        string.Format("{0} myhlCart is null {1}", Locale, cart.DistributorID));
                    Result.Result = RulesResult.Failure;
                    return(Result);
                }

                PurchasingLimits_V01 PurchasingLimits =
                    PurchasingLimitProvider.GetCurrentPurchasingLimits(cart.DistributorID);

                purchasingLimitManager.SetPurchasingLimits(PurchasingLimits);


                if (null == PurchasingLimits)
                {
                    LoggerHelper.Error(
                        string.Format("{0} PurchasingLimits could not be retrieved for distributor {1}", Locale,
                                      cart.DistributorID));
                    Result.Result = RulesResult.Failure;
                    return(Result);
                }

                DistributorRemainingVolumePoints         =
                    DistributorRemainingDiscountedRetail = PurchasingLimits.RemainingVolume;
                DistributorRemainingEarnings             = PurchasingLimits.RemainingEarnings;

                var distributorOrderingProfile = DistributorOrderingProfileProvider.GetProfile(cart.DistributorID,
                                                                                               Country);
                Discount = Convert.ToDecimal(distributorOrderingProfile.StaticDiscount);


                if (myhlCart.Totals != null && (myhlCart.Totals as OrderTotals_V01).DiscountPercentage != 0.0M)
                {
                    Discount = (myhlCart.Totals as OrderTotals_V01).DiscountPercentage;
                }

                myhlCart.SetDiscountForLimits(Discount);

                var currentItem = CatalogProvider.GetCatalogItem(cart.CurrentItems[0].SKU, Country);
                if (currentItem != null)
                {
                    if (currentItem.ProductType == ServiceProvider.CatalogSvc.ProductType.EventTicket)
                    {
                        PurchasingLimitProvider.GetPurchasingLimits(cart.DistributorID, "ETO");
                    }
                    else
                    {
                        NewVolumePoints = currentItem.VolumePoints * cart.CurrentItems[0].Quantity;
                    }
                }

                if (NewVolumePoints > 0 || PurchasingLimits.PurchaseLimitType == PurchaseLimitType.Earnings ||
                    purchasingLimitManager.PurchasingLimitsRestriction == PurchasingLimitRestrictionType.MarketingPlan)
                {
                    if (PurchasingLimits.PurchaseLimitType == PurchaseLimitType.Volume)
                    {
                        if (PurchasingLimits.maxVolumeLimit == -1)
                        {
                            return(Result);
                        }
                        decimal cartVolume = (cart as MyHLShoppingCart).VolumeInCart;

                        if (DistributorRemainingVolumePoints - (cartVolume + NewVolumePoints) < 0)
                        {
                            Result.Result = RulesResult.Failure;
                            if (purchasingLimitManager.PurchasingLimitsRestriction ==
                                PurchasingLimitRestrictionType.MarketingPlan)
                            //MPE Thresholds
                            {
                                if (cart.CartItems.Exists(item => item.SKU == cart.CurrentItems[0].SKU))
                                {
                                    Result.AddMessage(
                                        string.Format(
                                            HttpContext.GetGlobalResourceObject(
                                                string.Format("{0}_Rules", HLConfigManager.Platform),
                                                "VolumePointExceedsThresholdByIncreasingQuantity").ToString(),
                                            cart.CurrentItems[0].SKU, cart.CurrentItems[0].Quantity));
                                }
                                else
                                {
                                    Result.AddMessage(
                                        string.Format(
                                            HttpContext.GetGlobalResourceObject(
                                                string.Format("{0}_Rules", HLConfigManager.Platform),
                                                "VolumePointExceedsThreshold").ToString(), cart.CurrentItems[0].SKU));
                                }
                            }
                            else
                            {
                                if (cart.CartItems.Exists(item => item.SKU == cart.CurrentItems[0].SKU))
                                {
                                    Result.AddMessage(
                                        string.Format(
                                            HttpContext.GetGlobalResourceObject(
                                                string.Format("{0}_Rules", HLConfigManager.Platform),
                                                "VolumePointExceedsByIncreasingQuantity").ToString(),
                                            cart.CurrentItems[0].SKU, cart.CurrentItems[0].Quantity));
                                }
                                else
                                {
                                    Result.AddMessage(
                                        string.Format(
                                            HttpContext.GetGlobalResourceObject(
                                                string.Format("{0}_Rules", HLConfigManager.Platform),
                                                "VolumePointExceeds").ToString(), cart.CurrentItems[0].SKU));
                                }
                            }
                            cart.RuleResults.Add(Result);
                        }
                    }
                    else if (PurchasingLimits.PurchaseLimitType == PurchaseLimitType.DiscountedRetail)
                    {
                        var myHlCart       = cart as MyHLShoppingCart;
                        var calcTheseItems = new List <ShoppingCartItem_V01>(cart.CartItems);
                        var existingItem   = cart.CartItems.Find(ci => ci.SKU == cart.CurrentItems[0].SKU);
                        if (null != existingItem)
                        {
                            existingItem.Quantity += cart.CurrentItems[0].Quantity;
                        }
                        else
                        {
                            calcTheseItems.Add(new ShoppingCartItem_V01(0, cart.CurrentItems[0].SKU,
                                                                        cart.CurrentItems[0].Quantity, DateTime.Now));
                        }
                        var totals = myhlCart.Calculate(calcTheseItems) as OrderTotals_V01;
                        if (null != existingItem)
                        {
                            existingItem.Quantity -= cart.CurrentItems[0].Quantity;
                        }
                        if (null == totals)
                        {
                            var message =
                                string.Format(
                                    "Purchasing Limits DiscountedRetail calculation failed due to Order Totals returning a null for Distributor {0}",
                                    cart.DistributorID);
                            LoggerHelper.Error(message);
                            throw new ApplicationException(message);
                        }
                        decimal newDiscountedRetail = 0.0M;

                        decimal dsicount = totals.DiscountPercentage;

                        var skus     = (from s in calcTheseItems where s.SKU != null select s.SKU).ToList();
                        var allItems = CatalogProvider.GetCatalogItems(skus, Country);
                        if (null != allItems && allItems.Count > 0)
                        {
                            newDiscountedRetail = (from t in totals.ItemTotalsList
                                                   from c in allItems.Values
                                                   where
                                                   (c as CatalogItem_V01).ProductType == ServiceProvider.CatalogSvc.ProductType.Product &&
                                                   c.SKU == (t as ItemTotal_V01).SKU
                                                   select(t as ItemTotal_V01).DiscountedPrice).Sum();
                        }

                        myhlCart.SetDiscountForLimits(totals.DiscountPercentage);

                        if (DistributorRemainingDiscountedRetail - newDiscountedRetail < 0)
                        {
                            Result.Result = RulesResult.Failure;
                            if (cart.CartItems.Exists(item => item.SKU == cart.CurrentItems[0].SKU))
                            {
                                Result.AddMessage(
                                    string.Format(
                                        HttpContext.GetGlobalResourceObject(
                                            string.Format("{0}_Rules", HLConfigManager.Platform),
                                            "DiscountedRetailExceedsByIncreasingQuantity").ToString(),
                                        cart.CurrentItems[0].SKU, cart.CurrentItems[0].Quantity));
                            }
                            else
                            {
                                Result.AddMessage(
                                    string.Format(
                                        HttpContext.GetGlobalResourceObject(
                                            string.Format("{0}_Rules", HLConfigManager.Platform),
                                            "DiscountedRetailExceeds").ToString(), cart.CurrentItems[0].SKU));
                            }
                            cart.RuleResults.Add(Result);
                        }
                    }
                    else if (PurchasingLimits.PurchaseLimitType == PurchaseLimitType.Earnings &&
                             cart.OrderSubType == "B1")
                    {
                        NewEarnings += (currentItem.EarnBase) * cart.CurrentItems[0].Quantity * Discount / 100;

                        if (DistributorRemainingEarnings -
                            ((cart as MyHLShoppingCart).ProductEarningsInCart + NewEarnings) < 0)
                        {
                            Result.Result = RulesResult.Failure;
                            if (cart.CartItems.Exists(item => item.SKU == cart.CurrentItems[0].SKU))
                            {
                                Result.AddMessage(
                                    string.Format(
                                        HttpContext.GetGlobalResourceObject(
                                            string.Format("{0}_Rules", HLConfigManager.Platform),
                                            "EarningExceedsByIncreasingQuantity").ToString(),
                                        cart.CurrentItems[0].SKU, cart.CurrentItems[0].Quantity));
                            }
                            else
                            {
                                Result.AddMessage(
                                    string.Format(
                                        HttpContext.GetGlobalResourceObject(
                                            string.Format("{0}_Rules", HLConfigManager.Platform), "EarningExceeds")
                                        .ToString(), cart.CurrentItems[0].SKU));
                            }
                            cart.RuleResults.Add(Result);
                        }
                    }
                    else if (PurchasingLimits.PurchaseLimitType == PurchaseLimitType.ProductCategory)
                    {
                        if (currentItem.ProductType == ServiceProvider.CatalogSvc.ProductType.Product)
                        {
                            Result.Result = RulesResult.Failure;
                            Result.AddMessage(
                                string.Format(
                                    HttpContext.GetGlobalResourceObject(
                                        string.Format("{0}_Rules", HLConfigManager.Platform),
                                        "PurchaseLimitTypeProductCategory").ToString(), cart.CurrentItems[0].SKU));
                            cart.RuleResults.Add(Result);
                        }
                        else
                        {
                            if (DistributorRemainingVolumePoints -
                                ((cart as MyHLShoppingCart).VolumeInCart + NewVolumePoints) < 0)
                            {
                                Result.Result = RulesResult.Failure;
                                if (cart.CartItems.Exists(item => item.SKU == cart.CurrentItems[0].SKU))
                                {
                                    Result.AddMessage(
                                        string.Format(
                                            HttpContext.GetGlobalResourceObject(
                                                string.Format("{0}_Rules", HLConfigManager.Platform),
                                                "VolumePointExceedsThresholdByIncreasingQuantity").ToString(),
                                            cart.CurrentItems[0].SKU, cart.CurrentItems[0].Quantity));
                                }
                                else
                                {
                                    Result.AddMessage(
                                        string.Format(
                                            HttpContext.GetGlobalResourceObject(
                                                string.Format("{0}_Rules", HLConfigManager.Platform),
                                                "VolumePointExceedsThreshold").ToString(), cart.CurrentItems[0].SKU));
                                }
                            }
                        }
                    }
                    else if (PurchasingLimits.PurchaseLimitType == PurchaseLimitType.TotalPaid)
                    {
                        Result.Result = RulesResult.Success;

                        var myHlCart       = cart as MyHLShoppingCart;
                        var calcTheseItems = new List <ShoppingCartItem_V01>();
                        calcTheseItems.AddRange(from i in cart.CartItems
                                                where !APFDueProvider.IsAPFSku(i.SKU)
                                                select
                                                new ShoppingCartItem_V01(i.ID, i.SKU, i.Quantity, i.Updated,
                                                                         i.MinQuantity));

                        var existingItem =
                            calcTheseItems.Find(ci => ci.SKU == cart.CurrentItems[0].SKU);
                        if (null != existingItem)
                        {
                            existingItem.Quantity += cart.CurrentItems[0].Quantity;
                        }
                        else
                        {
                            calcTheseItems.Add(new ShoppingCartItem_V01(0, cart.CurrentItems[0].SKU,
                                                                        cart.CurrentItems[0].Quantity, DateTime.Now));
                        }

                        // remove A and L type
                        var allItems =
                            CatalogProvider.GetCatalogItems(
                                (from s in calcTheseItems where s.SKU != null select s.SKU).ToList(), Country);
                        if (null != allItems && allItems.Count > 0)
                        {
                            var skuExcluded = (from c in allItems.Values
                                               where (c as CatalogItem_V01).ProductType != ServiceProvider.CatalogSvc.ProductType.Product
                                               select c.SKU);
                            calcTheseItems.RemoveAll(s => skuExcluded.Contains(s.SKU));
                        }

                        var totals = myhlCart.Calculate(calcTheseItems);
                        if (null == totals)
                        {
                            var message =
                                string.Format(
                                    "Purchasing Limits DiscountedRetail calculation failed due to Order Totals returning a null for Distributor {0}",
                                    cart.DistributorID);
                            LoggerHelper.Error(message);
                            throw new ApplicationException(message);
                        }
                        if (DistributorRemainingVolumePoints - (totals as OrderTotals_V01).AmountDue < 0)
                        {
                            Result.Result = RulesResult.Failure;
                            if (cart.CartItems.Exists(item => item.SKU == cart.CurrentItems[0].SKU))
                            {
                                Result.AddMessage(
                                    string.Format(
                                        HttpContext.GetGlobalResourceObject(
                                            string.Format("{0}_Rules", HLConfigManager.Platform),
                                            "DiscountedRetailExceedsByIncreasingQuantity").ToString(),
                                        cart.CurrentItems[0].SKU, cart.CurrentItems[0].Quantity));
                            }
                            else
                            {
                                Result.AddMessage(
                                    string.Format(
                                        HttpContext.GetGlobalResourceObject(
                                            string.Format("{0}_Rules", HLConfigManager.Platform),
                                            "DiscountedRetailExceeds").ToString(), cart.CurrentItems[0].SKU));
                            }
                            cart.RuleResults.Add(Result);
                        }
                    }
                    else
                    {
                        Result.Result = RulesResult.Success;
                    }
                }
            }

            return(Result);
        }
        private void DoApfDue(string distributorID,
                              ShoppingCart_V01 result,
                              string cacheKey,
                              string locale,
                              bool cartHasItems,
                              ShoppingCartRuleResult ruleResult,
                              bool justEntered)
        {
            var cart = result as MyHLShoppingCart;

            if (cart == null)
            {
                return;
            }

            try
            {
                string level;
                if (DistributorProfileModel != null)
                {
                    level = DistributorProfileModel.TypeCode.ToUpper();
                }
                else
                {
                    level = GetMemberLevelFromDistributorProfile(cart.DistributorID);
                }

                var distributorOrderingProfile = DistributorOrderingProfileProvider.GetProfile(cart.DistributorID,
                                                                                               cart.CountryCode);
                if ((distributorOrderingProfile.HardCashOnly &&
                     !HLConfigManager.Configurations.PaymentsConfiguration.AllowWireForHardCash))
                {
                    return;
                }
                var apfItems = new List <ShoppingCartItem_V01>();
                if (cart.CartItems != null && cart.CartItems.Count > 0)
                {
                    //Stash off all non-APF items - to be re-added if appropriate
                    var nonApfItems =
                        (from c in cart.CartItems where APFDueProvider.IsAPFSku(c.SKU.Trim()) == false select c)
                        .ToList <ShoppingCartItem_V01>();
                    apfItems =
                        (from c in cart.CartItems where APFDueProvider.IsAPFSku(c.SKU.Trim()) select c)
                        .ToList <ShoppingCartItem_V01>();
                    if (nonApfItems.Count > 0 ||
                        HLConfigManager.Configurations.APFConfiguration.StandaloneAPFOnlyAllowed)
                    {
                        // Clear the cart
                        cart.DeleteItemsFromCart(null, true);
                        //if (APFDueProvider.CanEditAPFOrder(distributorID, locale, level))
                        //{
                        //Global rule - they can always edit the cart ie add remove products at least
                        var list =
                            CatalogProvider.GetCatalogItems((from p in nonApfItems select p.SKU).ToList(), Country);
                        var products =
                            (from c in list where c.Value.ProductType == ProductType.Product select c.Value.SKU).ToList();
                        var nonproducts =
                            (from c in list where c.Value.ProductType != ProductType.Product select c.Value.SKU).ToList();
                        if (!HLConfigManager.Configurations.APFConfiguration.AllowNonProductItemsWithStandaloneAPF)
                        //We don't allow non product items alone on an apf order
                        {
                            if (products.Count == 0)
                            {
                                if (nonproducts.Count > 0)
                                {
                                    ruleResult.Result = RulesResult.Success;
                                    ruleResult.AddMessage(
                                        HttpContext.GetGlobalResourceObject(
                                            string.Format("{0}_ErrorMessage", HLConfigManager.Platform),
                                            "NonProductItemsRemovedForStandaloneAPF") as string);
                                    cart.RuleResults.Add(ruleResult);
                                }
                            }
                            else
                            {
                                cart.AddItemsToCart(nonApfItems, true);
                            }
                        }
                        else
                        {
                            if (!HLConfigManager.Configurations.APFConfiguration.StandaloneAPFOnlyAllowed)
                            {
                                cart.AddItemsToCart(nonApfItems, true);
                            }
                        }
                    }
                }
                else if (null != cart && null != cart.RuleResults)
                {
                    var rules =
                        (from rule in cart.RuleResults
                         where rule.RuleName == RuleName && rule.Result == RulesResult.Failure
                         select rule);
                    if (null != rules && rules.Count() > 0)
                    {
                        cart.RuleResults.Remove(rules.First());
                    }
                }

                //Add the APF in
                var apfSku = new List <ShoppingCartItem_V01>();
                var sku    = APFDueProvider.GetAPFSku();
                apfSku.Add(new ShoppingCartItem_V01(0, sku, 1, DateTime.Now));
                if (!cart.APFEdited)
                {
                    apfSku[0].Quantity = 1; //CalcQuantity(distributorOrderingProfile.ApfDueDate);
                    if (cart.CartItems.Exists(c => c.SKU == apfSku[0].SKU))
                    {
                        var apf =
                            (from a in cart.CartItems where a.SKU == apfSku[0].SKU select a).First();
                        cart.DeleteItemsFromCart(
                            (from a in cart.CartItems where a.SKU == apfSku[0].SKU select a.SKU).ToList(), true);
                    }
                    if (cart.CartItems.Count == 0) //This is now a Standalone APF
                    {
                        SetAPFDeliveryOption(cart);
                    }
                    cart.AddItemsToCart(apfSku, true);

                    if (justEntered)
                    {
                        ruleResult.AddMessage(
                            HttpContext.GetGlobalResourceObject(
                                string.Format("{0}_ErrorMessage", HLConfigManager.Platform), "APFDueAdded") as string);
                        ruleResult.Result = RulesResult.Success;

                        SetApfRuleResponse(ruleResult, ApfAction.None, sku, "ApfRule", TypeOfApf.CantDSRemoveAPF,
                                           "APFDueAdded");

                        cart.RuleResults.Add(ruleResult);
                    }
                    else
                    {
                        foreach (ShoppingCartRuleResult r in cart.RuleResults)
                        {
                            if (r.RuleName == RuleName)
                            {
                                r.Messages.Clear();
                                r.AddMessage(string.Empty);
                            }
                        }
                    }
                }
                //else
                //{
                //    if (APFDueProvider.CanRemoveAPF(distributorID, locale, level))
                //    {
                //        cart.AddItemsToCart(apfSku, true);
                //    }
                //}
            }
            catch (Exception ex)
            {
                LoggerHelper.Error(string.Format("doAPFDue DS:{0} ERR:{1}", distributorID, ex));
            }
        }
        protected override ShoppingCartRuleResult PerformRules(MyHLShoppingCart cart,
                                                               ShoppingCartRuleReason reason,
                                                               ShoppingCartRuleResult Result)
        {
            Result.Result = RulesResult.Success;

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

            base.PerformRules(cart, reason, Result);

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

            if (currentlimits.PurchaseLimitType == PurchaseLimitType.None || currentlimits.LimitsRestrictionType != LimitsRestrictionType.PurchasingLimits)
            {
                return(Result);
            }

            if (cart.ItemsBeingAdded != null && cart.ItemsBeingAdded.Count > 0)
            {
                var calcTheseItems = new List <ShoppingCartItem_V01>();
                calcTheseItems.AddRange(from i in cart.CartItems
                                        where !APFDueProvider.IsAPFSku(i.SKU)
                                        select
                                        new ShoppingCartItem_V01(i.ID, i.SKU, i.Quantity, i.Updated,
                                                                 i.MinQuantity));
                bool          bExceed  = false;
                List <string> skuToAdd = new List <string>();

                foreach (var item in cart.ItemsBeingAdded)
                {
                    if (bExceed == true)
                    {
                        Result = reportError(cart, item, Result);
                        continue;
                    }
                    var existingItem =
                        calcTheseItems.Find(ci => ci.SKU == item.SKU);
                    if (null != existingItem)
                    {
                        existingItem.Quantity += item.Quantity;
                    }
                    else
                    {
                        calcTheseItems.Add(new ShoppingCartItem_V01(0, item.SKU, item.Quantity, DateTime.Now));
                    }

                    // remove A and L type
                    var allItems =
                        CatalogProvider.GetCatalogItems(
                            (from s in calcTheseItems where s.SKU != null select s.SKU).ToList(), Country);
                    if (null != allItems && allItems.Count > 0)
                    {
                        var skuExcluded = (from c in allItems.Values
                                           where (c as CatalogItem_V01).ProductType != ServiceProvider.CatalogSvc.ProductType.Product
                                           select c.SKU);
                        calcTheseItems.RemoveAll(s => skuExcluded.Contains(s.SKU));
                    }

                    var totals = cart.Calculate(calcTheseItems, false);
                    if (null == totals)
                    {
                        var message =
                            string.Format(
                                "Purchasing Limits DiscountedRetail calculation failed due to Order Totals returning a null for Distributor {0}",
                                cart.DistributorID);
                        LoggerHelper.Error(message);
                        throw new ApplicationException(message);
                    }

                    if (currentlimits.RemainingVolume - (totals as OrderTotals_V01).AmountDue < 0)
                    {
                        bExceed = true;
                        Result  = reportError(cart, item, Result);
                    }
                    else
                    {
                        skuToAdd.Add(item.SKU);
                    }
                }

                cart.ItemsBeingAdded.RemoveAll(s => !skuToAdd.Contains(s.SKU));
            }
            return(Result);
        }
        /// <summary>
        ///     Performs the rules.
        /// </summary>
        /// <param name="cart">The cart.</param>
        /// <param name="reason">The reason.</param>
        /// <param name="Result">The result.</param>
        /// <returns></returns>
        private ShoppingCartRuleResult PerformRules(ShoppingCart_V02 cart,
                                                    ShoppingCartRuleReason reason,
                                                    ShoppingCartRuleResult Result)
        {
            if (!IsRuleTime())
            {
                return(Result);
            }

            if (reason == ShoppingCartRuleReason.CartItemsBeingAdded)
            {
                if (cart != null && cart.CurrentItems[0].SKU == promoSku)
                {
                    Result.Result = RulesResult.Failure;
                    Result.AddMessage(string.Empty);
                    cart.RuleResults.Add(Result);
                    return(Result);
                }
            }
            else if (reason == ShoppingCartRuleReason.CartItemsAdded)
            {
                var hlCart       = cart as MyHLShoppingCart;
                int quantity0020 = 0;

                if (cart == null || hlCart == null)
                {
                    return(Result);
                }
                if (cart != null && cart.CurrentItems[0].SKU == promoSku)
                {
                    Result.Result = RulesResult.Failure;
                    Result.AddMessage(string.Empty);
                    cart.RuleResults.Add(Result);
                    return(Result);
                }
                if (cart.CartItems.Any(i => i.SKU == "0020"))
                {
                    quantity0020 = cart.CartItems.Find(m => m.SKU == "0020").Quantity;
                }
                if (quantity0020 == 0)
                {
                    //check whether cart contains promo SKU,if yes delete it.
                    if (cart.CartItems.Any(i => i.SKU == promoSku))
                    {
                        hlCart.DeleteItemsFromCart(new List <string>(new[] { promoSku }), true);
                        Result.Result = RulesResult.Success;
                        cart.RuleResults.Add(Result);
                        return(Result);
                    }
                }
                else if (quantity0020 > 0)
                {
                    //check, cart contain the promo sku?
                    if (cart.CartItems.Any(i => i.SKU == promoSku))
                    {
                        return(Result); //SKU already present - exit.
                    }
                    return(AddToCart(hlCart, Result));
                }
            }
            else if (reason == ShoppingCartRuleReason.CartItemsRemoved)
            {
                var hlCart = cart as MyHLShoppingCart;

                //If there are no sku to get the promo remaining in the cart, remove the promo sku if it's already there
                if (cart.CartItems.Any(i => i.SKU == promoSku))
                {
                    var catItems =
                        CatalogProvider.GetCatalogItems(
                            (from c in cart.CartItems select c.SKU.Trim()).ToList <string>(), Country);
                    if (catItems != null)
                    {
                        bool has0020added = cart.CartItems.Any(i => i.SKU == "0020");
                        if (!has0020added)
                        {
                            hlCart.DeleteItemsFromCart(new List <string>(new[] { promoSku }), true);
                            Result.Result = RulesResult.Success;
                            cart.RuleResults.Add(Result);
                            return(Result);
                        }
                    }
                    //if it's the last sku in the cart, remove it.
                    if (cart.CartItems.Count == 1)
                    {
                        hlCart.DeleteItemsFromCart(new List <string>(new[] { promoSku }), true);
                        Result.Result = RulesResult.Success;
                        cart.RuleResults.Add(Result);
                        return(Result);
                    }
                }
            }
            return(Result);
        }
Example #10
0
        private ShoppingCartRuleResult PerformRules(ShoppingCart_V02 cart,
                                                    ShoppingCartRuleReason reason,
                                                    ShoppingCartRuleResult Result)
        {
            if (cart == null)
            {
                return(Result);
            }

            if (reason == ShoppingCartRuleReason.CartItemsBeingAdded)
            {
                string sku     = cart.CurrentItems[0].SKU.Trim();
                var    allowed = new List <string>();
                if (_IBPAllowedSubTypes.TryGetValue(sku, out allowed))
                {
                    string orderSubType = cart.OrderSubType;
                    if (string.IsNullOrEmpty(cart.OrderSubType))
                    {
                        var purchasingLimits =
                            PurchasingLimitProvider.GetCurrentPurchasingLimits(cart.DistributorID);
                        if (null != purchasingLimits)
                        {
                            orderSubType = purchasingLimits.PurchaseSubType;
                        }
                    }
                    if (!allowed.Contains(orderSubType))
                    {
                        Result.Result = RulesResult.Failure;
                        var errorMessage =
                            HttpContext.GetGlobalResourceObject(string.Format("{0}_Rules", HLConfigManager.Platform),
                                                                "SKU" + sku + "Limitations") ??
                            string.Format("SKU {0} Limitations", sku);

                        Result.AddMessage(string.Format(errorMessage.ToString(), cart.CurrentItems[0].SKU));
                        cart.RuleResults.Add(Result);
                    }
                }

                if (cart.CurrentItems[0].SKU == HLConfigManager.Configurations.DOConfiguration.TodayMagazineSku)
                {
                    var catItems =
                        CatalogProvider.GetCatalogItems(
                            (from c in cart.CartItems select c.SKU.Trim()).ToList <string>(), Country);
                    if (catItems != null)
                    {
                        if (!catItems.Any(c => c.Value.ProductType == ProductType.Product))
                        {
                            Result.Result = RulesResult.Failure;
                            Result.AddMessage(
                                string.Format(
                                    HttpContext.GetGlobalResourceObject(
                                        string.Format("{0}_Rules", HLConfigManager.Platform), "AddProductFirst")
                                    .ToString(), cart.CurrentItems[0].SKU));
                            cart.RuleResults.Add(Result);
                        }
                    }
                }
            }
            else if (reason == ShoppingCartRuleReason.CartItemsAdded)
            {
                var tempShoppingCartList = new ShoppingCartItemList();
                tempShoppingCartList.AddRange(from c in cart.CartItems
                                              select new ShoppingCartItem_V01(c.ID, c.SKU, c.Quantity, c.Updated));
                var item = tempShoppingCartList.Where(i => i.SKU == cart.CurrentItems[0].SKU);
                if (null != item && item.Count() > 0)
                {
                    var itemFirst = item.First();
                    itemFirst.Quantity += cart.CurrentItems[0].Quantity;
                }
                else
                {
                    tempShoppingCartList.Add(cart.CurrentItems[0]);
                }
            }
            else if (reason == ShoppingCartRuleReason.CartCalculated)
            {
                string orderSubType = cart.OrderSubType;
                if (string.IsNullOrEmpty(cart.OrderSubType))
                {
                    var purchasingLimits =
                        PurchasingLimitProvider.GetCurrentPurchasingLimits(cart.DistributorID);
                    if (null != purchasingLimits)
                    {
                        orderSubType = purchasingLimits.PurchaseSubType;
                    }
                }

                var myhlCart = cart as MyHLShoppingCart;
                if (myhlCart != null)
                {
                    var found =
                        (from a in _IBPAllowedSubTypes.Keys.ToList()
                         from s in myhlCart.ShoppingCartItems
                         where a == s.SKU
                         select a);
                    if (found.Count() > 0)
                    {
                        var skuToDelete = new List <string>();
                        foreach (string sku in found)
                        {
                            var allowed = new List <string>();
                            if (_IBPAllowedSubTypes.TryGetValue(sku, out allowed))
                            {
                                if (!allowed.Contains(orderSubType))
                                {
                                    skuToDelete.Add(sku);
                                }
                            }
                        }
                        if (skuToDelete.Count() > 0)
                        {
                            myhlCart.DeleteItemsFromCart(skuToDelete);
                            foreach (string sku in skuToDelete)
                            {
                                Result.Result = RulesResult.Failure;
                                Result.AddMessage(
                                    string.Format(
                                        HttpContext.GetGlobalResourceObject(
                                            string.Format("{0}_Rules", HLConfigManager.Platform),
                                            "SKU" + sku + "Limitations").ToString(), sku));
                                cart.RuleResults.Add(Result);
                            }
                        }
                    }
                }
            }

            return(Result);
        }
        private ShoppingCartRuleResult PerformRules(ShoppingCart_V02 cart,
                                                    ShoppingCartRuleReason reason,
                                                    ShoppingCartRuleResult Result)
        {
            var hlCart = cart as MyHLShoppingCart;

            if (hlCart == null)
            {
                return(Result);
            }

            if (reason == ShoppingCartRuleReason.CartItemsBeingAdded)
            {
                if (cart.CartItems.Any(i => i.SKU == promoSku))
                {
                    return(Result); //SKU already present - exit.
                }

                //If the item being added is a Product, add the promo sku.
                var current = CatalogProvider.GetCatalogItem(cart.CurrentItems[0].SKU, Country);
                if (null != current && current.ProductType == ProductType.Product)
                {
                    return(AddToCart(hlCart, Result));
                }
                else
                {
                    //If there are already products in the cart, add the promo sku.
                    var catItems =
                        CatalogProvider.GetCatalogItems(
                            (from c in cart.CartItems select c.SKU.Trim()).ToList <string>(), Country);
                    if (catItems != null)
                    {
                        if (catItems.Any(c => c.Value.ProductType == ProductType.Product))
                        {
                            return(AddToCart(hlCart, Result));
                        }
                    }
                }
            }
            else if (reason == ShoppingCartRuleReason.CartItemsBeingRemoved)
            {
                //If they're trying to delete it, stop them.
                if (cart.CurrentItems[0].SKU == promoSku)
                {
                    cart.CurrentItems.Clear();
                }
            }
            else if (reason == ShoppingCartRuleReason.CartItemsRemoved)
            {
                //If there are no Product types remaining in the cart, remove the promo sku if it's already there
                if (cart.CartItems.Any(i => i.SKU == promoSku))
                {
                    var catItems =
                        CatalogProvider.GetCatalogItems(
                            (from c in cart.CartItems select c.SKU.Trim()).ToList <string>(), Country);
                    if (catItems != null)
                    {
                        if (!catItems.Any(c => c.Value.ProductType == ProductType.Product))
                        {
                            hlCart.DeleteItemsFromCart(new List <string>(new[] { promoSku }), true);
                            Result.Result = RulesResult.Success;
                            cart.RuleResults.Add(Result);
                            return(Result);
                        }
                    }
                    //if it's the last sku in the cart, remove it.
                    if (cart.CartItems.Count == 1)
                    {
                        hlCart.DeleteItemsFromCart(new List <string>(new[] { promoSku }), true);
                        Result.Result = RulesResult.Success;
                        cart.RuleResults.Add(Result);
                        return(Result);
                    }
                }
            }

            return(Result);
        }
        private void populateInvoiceOptions()
        {
            if (_shoppingCart != null && _shoppingCart.DeliveryInfo != null)
            {
                //divInvoiceOptions
                if (_invoiceOptionRBL != null)
                {
                    divInvoiceOptions.Controls.Remove(_invoiceOptionRBL);
                }
                _invoiceOptionRBL = new RadioButtonList();
                _invoiceOptionRBL.EnableViewState = true;
                ListItemCollection result = new ListItemCollection();

                CatalogItemList catItems = CatalogProvider.GetCatalogItems((from c in _shoppingCart.CartItems
                                                                            select c.SKU.Trim()).ToList <string>(),
                                                                           (this.Page as ProductsBase).CountryCode);
                var list = new List <CatalogItem_V01>();
                list.AddRange(catItems.Select(c => c.Value as CatalogItem_V01).ToList <CatalogItem_V01>());
                var invoiceOptions = new List <InvoiceHandlingType>();
                invoiceOptions = ShippingProvider.GetInvoiceOptions(_shoppingCart.DeliveryInfo.Address, list,
                                                                    this.ShoppingCart);
                //CR for Argies and MX
                if (!HLConfigManager.Configurations.CheckoutConfiguration.AlwaysDisplayInvoiceOption &&
                    (null == invoiceOptions || invoiceOptions.Count == 0))
                {
                    this.Visible      = false;
                    _invoiceOptionRBL = null;
                    return;
                }

                var entries = GlobalResourceHelper.GetGlobalEnumeratorElements("InvoiceOptions");

                foreach (var entry in entries)
                {
                    var key   = entry.Key;
                    var parts = key.Split('_');
                    if (parts.Length > 1)
                    {
                        key = parts[0];
                    }
                    var value = entry.Value;
                    if (result.FindByValue(key) == null)
                    {
                        if (invoiceOptions != null)
                        {
                            if (
                                invoiceOptions.Exists(
                                    c => c == (InvoiceHandlingType)Enum.Parse(typeof(InvoiceHandlingType), key)))
                            {
                                var newItem = new ListItem(value, key);
                                if (_invoiceOptionRBL.Items.FindByValue(key) == null)
                                {
                                    _invoiceOptionRBL.Items.Add(newItem);
                                }
                            }
                        }
                        else
                        {
                            _invoiceOptionRBL.Items.Add(new ListItem(value, key));
                        }
                    }
                }
                if (_invoiceOptionRBL.Items.Count > 0)
                {
                    if (!HLConfigManager.Configurations.CheckoutConfiguration.DisableDefaultInvoiceOption)
                    {
                        ListItem defaultOption = null;
                        if (!string.IsNullOrEmpty(HLConfigManager.Configurations.CheckoutConfiguration.DefaultInvoiceOption))
                        {
                            defaultOption =
                                _invoiceOptionRBL.Items.FindByValue(
                                    HLConfigManager.Configurations.CheckoutConfiguration.DefaultInvoiceOption);
                        }
                        if (defaultOption == null && _shoppingCart.Locale != "zh-TW")
                        {
                            // Selecting the first from Shipping rules.
                            defaultOption = _invoiceOptionRBL.Items.FindByValue(invoiceOptions[0].ToString());
                        }
                        if (defaultOption != null)
                        {
                            defaultOption.Selected = true;
                        }
                        else
                        {
                            if (_shoppingCart.Locale == "zh-TW" && _invoiceOptionRBL.Items.Count > 1)
                            {
                                _invoiceOptionRBL.Items[1].Selected = true;
                            }
                            else
                            {
                                _invoiceOptionRBL.Items[0].Selected = true;
                            }
                        }
                    }
                }

                if (_invoiceOptionRBL.Items.Count > 0)
                {
                    _invoiceOptionRBL.AutoPostBack          = true;
                    _invoiceOptionRBL.SelectedIndexChanged += new EventHandler(_invoiceOptionRBL_SelectedIndexChanged);
                }

                divInvoiceOptions.Controls.Add(_invoiceOptionRBL);
            }
        }