private void RemoveItemsAdded(ref ShoppingCart_V02 cart)
        {
            var hlCart = cart as MyHLShoppingCart;
            ShoppingCartItemList cartItems = new ShoppingCartItemList();

            cartItems.AddRange(hlCart.CartItems);

            foreach (ShoppingCartItem_V01 item in hlCart.CurrentItems)
            {
                if (cartItems.Exists(i => i.SKU == item.SKU && i.Quantity >= item.Quantity))
                {
                    cartItems.FirstOrDefault(i => i.SKU == item.SKU).Quantity -= item.Quantity;
                    if (cartItems.FirstOrDefault(i => i.SKU == item.SKU).Quantity < 1)
                    {
                        cartItems.Remove(cartItems.FirstOrDefault(i => i.SKU == item.SKU));
                    }
                }
            }

            // Clear Cart and Re Add initial items
            hlCart.DeleteItemsFromCart((from i in hlCart.CartItems select i.SKU).ToList <string>());
            hlCart.AddItemsToCart(cartItems, true);

            cart = hlCart;
        }
Exemple #2
0
        public static ShoppingCartItemList GetDistributorShoppingCartItemList(List <string> skus)
        {
            Random random = new Random();
            var    items  = new ShoppingCartItemList();

            items.AddRange(from s in skus
                           select new ShoppingCartItem_V01 {
                SKU = s, Quantity = random.Next(1, 10)
            });
            return(items);
        }
        private ShoppingCartRuleResult CartItemBeingDeletedRuleHandler(ShoppingCart_V01 cart,
                                                                       ShoppingCartRuleResult result,
                                                                       string level)
        {
            if (APFDueProvider.IsAPFDueAndNotPaid(cart.DistributorID, Locale) &&
                HLConfigManager.Configurations.APFConfiguration.StandaloneAPFOnlyAllowed)
            {
                result.Result = RulesResult.Failure;
                result.AddMessage(
                    HttpContext.GetGlobalResourceObject(string.Format("{0}_ErrorMessage", HLConfigManager.Platform),
                                                        "CompleteAPFPurchase") as string);
                var sku = APFDueProvider.GetAPFSku();
                SetApfRuleResponse(result, ApfAction.None, sku, "ApfRule", TypeOfApf.StandaloneAPFOnlyAllowed,
                                   "CompleteAPFPurchase");

                cart.RuleResults.Add(result);
                return(result);
            }

            //Deleting is a little different than Inserting, because delete handles multiple skus while insert only does one
            //so we can use the cart.CurrentItems to control this. Anything the rule says to not delete, we remove from the CurrentItems
            //Delete post-rule will only delete what is left in there
            bool isStandaloneAPF = APFDueProvider.hasOnlyAPFSku(cart.CartItems, Locale);
            var  toRemove        = new List <ShoppingCartItem_V01>();
            var  hlCart          = cart as MyHLShoppingCart;

            if (null != hlCart)
            {
                foreach (ShoppingCartItem_V01 item in cart.CurrentItems)
                {
                    if (APFDueProvider.IsAPFSku(item.SKU))
                    {
                        if (
                            !APFDueProvider.CanRemoveAPF(cart.DistributorID, Thread.CurrentThread.CurrentCulture.Name,
                                                         level))
                        {
                            result.Result = RulesResult.Failure;
                            result.AddMessage(
                                HttpContext.GetGlobalResourceObject(
                                    string.Format("{0}_ErrorMessage", HLConfigManager.Platform), "CannotRemoveAPFSku")
                                as string);
                            var sku = APFDueProvider.GetAPFSku();
                            SetApfRuleResponse(result, ApfAction.None, sku, "ApfRule", TypeOfApf.StandaloneAPFOnlyAllowed,
                                               "CannotRemoveAPFSku");

                            cart.RuleResults.Add(result);
                        }
                        else
                        {
                            if (APFDueProvider.IsAPFDueAndNotPaid(cart.DistributorID, Locale))
                            {
                                var originalItem =
                                    (from c in cart.CartItems where c.SKU == cart.CurrentItems[0].SKU select c).ToList();
                                int requiredQuantity = APFDueProvider.APFQuantityDue(cart.DistributorID, Locale);
                                if (level == "SP")
                                {
                                    item.Quantity = requiredQuantity;
                                    result.Result = RulesResult.Recalc;
                                    result.AddMessage(
                                        HttpContext.GetGlobalResourceObject(
                                            string.Format("{0}_ErrorMessage", HLConfigManager.Platform),
                                            "RequiredAPFLeftInCart") as string);
                                    var sku = APFDueProvider.GetAPFSku();
                                    SetApfRuleResponse(result, ApfAction.None, sku, "ApfRule", TypeOfApf.StandaloneAPFOnlyAllowed,
                                                       "RequiredAPFLeftInCart");
                                    cart.RuleResults.Add(result);
                                    //toRemove.Add(item);
                                    cart.APFEdited = false;
                                    return(result);
                                }

                                //Add for ChinaDO
                                if (HLConfigManager.Configurations.APFConfiguration.CantDSRemoveAPF)
                                {
                                    result.Result = RulesResult.Recalc;
                                    result.AddMessage(
                                        HttpContext.GetGlobalResourceObject(
                                            string.Format("{0}_ErrorMessage", HLConfigManager.Platform),
                                            "RequiredAPFLeftInCart") as string);
                                    var sku = APFDueProvider.GetAPFSku();
                                    SetApfRuleResponse(result, ApfAction.None, sku, "ApfRule", TypeOfApf.StandaloneAPFOnlyAllowed,
                                                       "RequiredAPFLeftInCart");
                                    cart.RuleResults.Add(result);
                                    //toRemove.Add(item);
                                    cart.APFEdited = false;
                                    return(result);
                                }
                            }
                            cart.APFEdited = true;
                            toRemove.Add(item); // added
                        }
                    }
                    else
                    {
                        toRemove.Add(item); // added
                    }
                }
                //if (toRemove.Count > 0)
                //{
                //    cart.CurrentItems.Remove(toRemove[0]);
                //}
                var remainingItems = (from c in cart.CartItems
                                      where !toRemove.Any(x => x.SKU == c.SKU)
                                      select c).ToList();

                //List<ShoppingCartItem_V01> remainingItems = (from c in cart.CartItems where c.SKU != cart.CurrentItems[0].SKU select c).ToList();
                var mags = CartHasOnlyTodaysMagazine(remainingItems);
                if (mags.Count > 0)
                {
                    foreach (ShoppingCartItem_V01 item in mags)
                    {
                        (cart as MyHLShoppingCart).DeleteTodayMagazine(item.SKU);
                        remainingItems.Remove(item);
                        if (!cart.CurrentItems.Exists(c => c.SKU == item.SKU))
                        {
                            cart.CurrentItems.Add(item);
                        }
                    }
                    result.Result = RulesResult.Failure;
                    result.AddMessage(
                        HttpContext.GetGlobalResourceObject(
                            string.Format("{0}_ErrorMessage", HLConfigManager.Platform),
                            "NoTodayMagazineWithStandalonAPF") as string);
                    var sku = APFDueProvider.GetAPFSku();
                    SetApfRuleResponse(result, ApfAction.None, sku, "ApfRule", TypeOfApf.StandaloneAPFOnlyAllowed,
                                       "NoTodayMagazineWithStandalonAPF");
                    cart.RuleResults.Add(result);
                }

                // added
                if (remainingItems.Count > 0)
                {
                    var list = new ShoppingCartItemList();
                    list.AddRange(remainingItems);
                    isStandaloneAPF = APFDueProvider.hasOnlyAPFSku(list, Locale);
                    if (isStandaloneAPF)
                    {
                        SetAPFDeliveryOption(hlCart);
                    }
                    else
                    {
                        if (APFDueProvider.IsAPFSkuPresent(cart.CurrentItems))
                        {
                            SetNonStandAloneAPFDeliveryOption(hlCart);
                        }
                    }
                }
                else
                {
                    SetNonStandAloneAPFDeliveryOption(hlCart);
                }
            }
            //if((from c in cart.CurrentItems where APFDueProvider.IsAPFSku(c.SKU) select c).Count() >0)
            //{
            //    if (isStandaloneAPF)
            //    {
            //        SetAPFDeliveryOption(hlCart);
            //    }
            //}

            return(result);
        }
Exemple #4
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);
        }
        /// <summary>
        /// Gets the basic shopping cart.
        /// </summary>
        /// <param name="distributorId">The distributor id.</param>
        /// <param name="locale">The locale.</param>
        /// <param name="freightCode">The freight code.</param>
        /// <param name="wareHouseCode">The ware house code.</param>
        /// <param name="calculate">if set to <c>true</c> [calculate].</param>
        /// <param name="totals">The totals.</param>
        /// <param name="shoppingCartItem">The shopping cart item.</param>
        /// <param name="orderCType">Type of the order C.</param>
        /// <returns></returns>
        internal static MyHLShoppingCart GetBasicShoppingCart(string distributorId, string locale, string freightCode, string wareHouseCode, bool calculate, OrderTotals_V01 totals, List <DistributorShoppingCartItem> shoppingCartItem, OrderCategoryType orderCType)
        {
            if (string.IsNullOrEmpty(locale))
            {
                locale = "en-US";
            }

            if (string.IsNullOrEmpty(freightCode))
            {
                freightCode = HLConfigManager.CurrentPlatformConfigs[locale].ShoppingCartConfiguration.DefaultFreightCode;
            }

            if (string.IsNullOrEmpty(wareHouseCode))
            {
                wareHouseCode = HLConfigManager.CurrentPlatformConfigs[locale].ShoppingCartConfiguration.DefaultWarehouse;
            }

            var cartItemList = new ShoppingCartItemList();

            cartItemList.AddRange(shoppingCartItem.Select(i => new ShoppingCartItem_V01
            {
                ID                 = i.ID,
                MinQuantity        = i.MinQuantity,
                PartialBackordered = i.PartialBackordered,
                Quantity           = i.Quantity,
                SKU                = i.SKU,
                Updated            = i.Updated
            }));

            var shoppingCart = new MyHLShoppingCart
            {
                Locale            = locale,
                ShoppingCartItems = shoppingCartItem,
                CartItems         = cartItemList,
                DistributorID     = string.IsNullOrEmpty(distributorId) ? "webtest1" : distributorId,
                FreightCode       = freightCode,
                CurrentItems      = cartItemList,
                DeliveryInfo      = new ShippingInfo
                {
                    FreightCode   = freightCode,
                    WarehouseCode = wareHouseCode,
                    Option        = DeliveryOptionType.Shipping,
                    Address       = new ShippingAddress_V02
                    {
                        FirstName = string.Empty,
                        LastName  = string.Empty,
                        Recipient = string.Empty
                    }
                },
                CountryCode   = locale.Substring(3),
                OrderCategory = orderCType
            };

            // Calculate totals.
            if (calculate)
            {
                shoppingCart.Totals = shoppingCart.Calculate();
            }
            else
            {
                // Use dummy totals
                shoppingCart.Totals = totals ?? new OrderTotals_V01
                {
                    AmountDue          = 1000M,
                    BalanceAmount      = 900M,
                    DiscountPercentage = 50M,
                    VolumePoints       = 1000
                };
            }

            // Rules results
            shoppingCart.RuleResults = new List <ShoppingCartRuleResult>();

            return(shoppingCart);
        }