public bool Process(ShoppingCartOverviewModel model, Discount curdiscount, object DiscountConfigItem, User user, int lim) { var rule = (PercentFixedDicountConfig)DiscountConfigItem; var items = model.Items.Where(x => rule.ProductShopIDs.Contains("," + x.ProductShopID + ",")).ToList(); if (items.Count > 0) { foreach (var item in items) { if (!DiscountService.LimitCheck(lim, curdiscount.Limit, curdiscount.LimitType)) { break; } if (curdiscount.IsPercent) { item.Price = item.Price - (item.Price * curdiscount.Percent / 100); } else { item.Price = item.Price - curdiscount.Amount; } if (item.Price < 0) { item.Price = 0; } lim++; } return(true); } return(false); }
// public string Title = "Percent or fixed amount discount"; //public DiscountType Type //{ // get // { // return DiscountType.ForCartTotal; // } //} public bool Process(ShoppingCartOverviewModel model, Discount curdiscount, object DiscountConfigItem, User user, int lim) { var rule = (BuyOneGetOtherFreeDicountConfig)DiscountConfigItem; var productsList = curdiscount.GetProductsList(); var totalQuantity = model.Items.Where(x => productsList.Contains(x.ProductShopID.ToString())).Select(x => x.Quantity) .DefaultIfEmpty(0).Sum(); if (totalQuantity >= rule.QuantityFrom) { var items = model.Items.Where(x => rule.FreeProductShopID == x.ProductShopID) .ToList(); if (items.Count == 0) { if (rule.AutoAddToCart) { var cartItem = new ShoppingCartItem() { ProductShopID = rule.FreeProductShopID, Quantity = 1, ShopID = model.ShopID }; ShoppingCartService.AddToCart(user.ID, cartItem); var cartItemModel = ShoppingCartService.GetShoppingCartItemByID(cartItem.ID); model.Items.Add(cartItemModel); items.Add(cartItemModel); } } if (items.Count > 0) { foreach (var item in items) { if (!DiscountService.LimitCheck(lim, curdiscount.Limit, curdiscount.LimitType)) { break; } if (item.Quantity <= rule.MaxFreeQuantity) { item.Price = 0;// show that price is zero } else { item.TotalDiscountAmount += item.Price * rule.MaxFreeQuantity;// can be only one free, so we change unit price } lim++; } return true; } } return false; }
// public string Title = "Percent or fixed amount discount"; //public DiscountType Type { // get{ //return DiscountType.ForCartItem; //} //} public bool Process(ShoppingCartOverviewModel model, Discount curdiscount, object DiscountConfigItem, User user, int lim) { var rule = (BuyOneOfThisGetOtherFreeDiscountConfig)DiscountConfigItem; var productsList = curdiscount.GetProductsList(); var totalQuantity = model.Items.Where(x => productsList.Contains(x.ProductShopID.ToString())).Select(x => x.Quantity) .DefaultIfEmpty(0).Sum(); if (totalQuantity >= rule.MinTotalOfRequiredQuantity) { var items = model.Items.Where(x => rule.ProductShopForFreeIDs.Contains(x.ProductShopID.ToString())) .ToList(); decimal restQuantity = rule.MaxFreeQuantity; if (items.Count > 0) { foreach (var item in items) { if (!DiscountService.LimitCheck(lim, curdiscount.Limit, curdiscount.LimitType)) { break; } if (item.Quantity <= restQuantity) { item.Price = 0;// show that price is zero } else { item.TotalDiscountAmount += item.Price * restQuantity;// can be only one free, so we change unit price } restQuantity -= item.Quantity; lim++; } return(true); } } return(false); }
public void ProcessItems(ShoppingCartOverviewModel model, User user) { //discounts if (model.Items.Count > 0) { var userdata = ShoppingCartService.GetCheckoutData(); System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer(); var discounts = LS.Get <Discount>(model.ShopID.ToString() + "_" + DiscountType.ForCartItem.ToString()); // search discounts for shop and type bool success = false; foreach (var curdiscount in discounts) { #region process discount for cart items bool breakmain = false; if (curdiscount.IsCodeRequired) { if (string.IsNullOrEmpty(userdata.CouponCode) || userdata.CouponCode != curdiscount.DiscountCode) { continue; // code doesn`t match } } if (!DiscountService.ExpiriedCheck(curdiscount)) { continue; } int curUsesNumber = GetCurrentLimitPosition(curdiscount, user, _UseCache, userdata.FullName, userdata.Phone, userdata.Address, user.Email); if (LimitCheck(curUsesNumber, curdiscount.Limit, curdiscount.LimitType)) { success = true; foreach (var d in GetRules().Where(x => // x.Value.Type == DiscountType.ForCartItem && curdiscount.RuleList.Select(y => y.Name).Contains(x.Key) )) { var confitem = d.Value.GetConfigItem(); int i = 0; foreach (var r in curdiscount.RuleList.Where(x => x.Name == d.Key)) { i++; //from cache :) var o = curdiscount.GetRuleConfigObject(i.ToString() + r.Name, () => { return(js.Deserialize(r.Value, confitem.GetType())); }); success = success && d.Value.Process(model, curdiscount, o, user, curUsesNumber); } } if (success) { var productsList = curdiscount.GetProductsList(); if (curdiscount.IsCodeRequired && curdiscount.DiscountCode == userdata.CouponCode) { model.DiscountByCouponeCodeText = curdiscount.Name; } foreach (var item in model.Items.Where(x => productsList.Contains(x.ProductShopID.ToString()))) { if (!DiscountService.LimitCheck(curUsesNumber, curdiscount.Limit, curdiscount.LimitType)) { break; } if (item.Price > 0 && item.TotalDiscountAmount < item.Price * item.Quantity)//only if actual { if (curdiscount.DiscountCartItemType == DiscountCartItemType.ForItemPrice) { if (curdiscount.IsPercent) { item.Price = item.Price - (item.Price * curdiscount.Percent / 100); } else { item.Price = item.Price - curdiscount.Amount; } if (item.Price < 0) { item.Price = 0; } } else { if (curdiscount.IsPercent) { item.TotalDiscountAmount = item.Price * curdiscount.Percent * item.Quantity / 100; } else { item.TotalDiscountAmount = curdiscount.Amount; } } if (!string.IsNullOrEmpty(item.DiscountDescription)) { item.DiscountDescription += ", " + curdiscount.Name; } else { item.DiscountDescription = curdiscount.Name; } item.DiscountIDs.Add(curdiscount.ID); if (curdiscount.LessShopFee) { model.IsLessMemberFee = true; } curUsesNumber++; } } if (curdiscount.PreventNext) { breakmain = true; } } if (breakmain) { break; } } #endregion } } }