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 ProcessTotals(ShoppingCartOverviewModel model, User user) { if (model.Items.Count > 0) { var userdata = ShoppingCartService.GetCheckoutData(); System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer(); foreach (var typDisc in _TotalProcessKeys) { var discounts = LS.Get <Discount>(model.ShopID.ToString() + "_" + typDisc) ; // search discounts for shop and type discounts.AddRange(LS.Get <Discount>("0_" + typDisc)); bool success = false; if (typDisc == DiscountType.ForCartTotal.ToString()) { //fix total after previous discounts model.TotalWithoutShip = model.SubTotal + model.Fee; model.Total = model.SubTotal + model.ShippingCost + model.Fee; } if (discounts.Count > 0) { foreach (var curdiscount in discounts) { #region process discount for cart totals 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); 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) { bool actual = false; #region subtotal if (curdiscount.DiscountType == DiscountType.ForCartSubTotal) { if (model.SubTotal > 0) { decimal minus = 0; if (curdiscount.IsPercent) { minus = (model.SubTotal * curdiscount.Percent / 100); model.SubTotal = model.SubTotal - minus; } else { minus = curdiscount.Amount; model.SubTotal = model.SubTotal - curdiscount.Amount; } if (model.SubTotal < 0) { model.SubTotal = 0; } model.TotalDiscount += minus; actual = true; } } #endregion #region ship else if (curdiscount.DiscountType == DiscountType.ForCartShip) { if (model.SubTotal < model.FreeShipFrom) { model.ShippingCost = model.ShopShipCost; } if (model.ShippingCost > 0) { decimal minus = 0; if (curdiscount.IsPercent) { minus = (model.ShippingCost * curdiscount.Percent / 100); model.ShippingCost = model.ShippingCost - minus; } else { minus = curdiscount.Amount; model.ShippingCost = model.ShippingCost - curdiscount.Amount; } if (model.ShippingCost < 0) { model.ShippingCost = 0; } model.TotalDiscount += minus; actual = true; } } #endregion #region payment fee //else if (curdiscount.DiscountType == DiscountType.ForCartFee) //{ // if (model.Fee > 0) // { // decimal minus = 0; // if (curdiscount.IsPercent) // { // minus = (model.Fee * curdiscount.Percent / 100); // model.Fee = model.Fee - minus; // } // else // { // minus = curdiscount.Amount; // model.Fee = model.Fee - curdiscount.Amount; // } // if (model.Fee < 0) { model.Fee = 0; } // model.TotalDiscount += minus; // actual = true; // } //} #endregion #region total else if (curdiscount.DiscountType == DiscountType.ForCartTotal) { if (model.Total > 0) { decimal minus = 0; if (curdiscount.IsPercent) { minus = (model.Total * curdiscount.Percent / 100); model.Total = model.Total - minus; } else { minus = curdiscount.Amount; model.Total = model.Total - curdiscount.Amount; } if (model.Total < 0) { model.Total = 0; } model.TotalDiscount += minus; actual = true; } } #endregion if (actual)//if actual { if (curdiscount.IsCodeRequired && curdiscount.DiscountCode == userdata.CouponCode) { model.DiscountByCouponeCodeText = curdiscount.Name; } curUsesNumber++; if (!string.IsNullOrEmpty(model.DiscountDescription)) { model.DiscountDescription += ", " + curdiscount.Name; } else { model.DiscountDescription = curdiscount.Name; } if (curdiscount.LessShopFee) { model.IsLessMemberFee = true; } model.DiscountIDs.Add(curdiscount.ID);//for history save if (curdiscount.PreventNext) { breakmain = true; } } } } if (breakmain) { break; } #endregion } } } } }
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 } } }