Beispiel #1
0
        public static void ClearPromotionUsages(int customerId, string promotionCode, bool removeAutoAssigned)
        {
            var db = PromotionsData.DataContextProvider.Current;

            var promotionUsages =
                from pu in db.PromotionUsages
                join p in db.Promotions on pu.PromotionId equals p.Id
                join pli in db.PromotionLineItems on pu.Id equals pli.PromotionUsageId into pui
                from pli in pui.DefaultIfEmpty()
                join sc in db.ShoppingCarts on pli.shoppingCartRecordId equals sc.ShoppingCartRecID into sci
                from sc in sci.DefaultIfEmpty()
                where pu.CustomerId == customerId && !pu.Complete

                select new
            {
                promoUsage    = pu,
                promo         = p,
                promoLineItem = pli,
                scItem        = sc
            };

            if (!string.IsNullOrEmpty(promotionCode))
            {
                promotionUsages = promotionUsages
                                  .Where(puli => puli.promo.Code == promotionCode);
            }

            var giftCarts = promotionUsages
                            .Where(puli => puli.promoLineItem.isAGift).Select(puli => puli.scItem).Where(sc => sc != null);

            db.ShoppingCarts
            .DeleteAllOnSubmit(giftCarts);

            db.PromotionUsages
            .DeleteAllOnSubmit(promotionUsages.Select(puli => puli.promoUsage));

            //Only add auto assigned promos to the removed list if they are intentionally removed by user
            if (removeAutoAssigned)
            {
                foreach (var promoUsage in promotionUsages.Where(puli => puli.promo.AutoAssigned))
                {
                    PromotionManager.IgnorePromotion(promoUsage.promoUsage.CustomerId, promoUsage.promo.Id);
                }
            }

            db.SubmitChanges();
        }
Beispiel #2
0
        public static void PrioritizePromotions(IRuleContext ruleContext)
        {
            if (!IsValidCartType(ruleContext))
            {
                return;
            }

            RemoveDuplicatePromotionUsages(ruleContext.CustomerId);

            var assignedPromotions = GetAssignedPromotions(ruleContext.CustomerId)
                                     .ToList(); // get all assigned promotions

            if (assignedPromotions.Any())
            {
                var priority = 0m;

                if (assignedPromotions.Where(w => w.Priority != 0).Any())
                {
                    priority = assignedPromotions.Where(w => w.Priority != 0).Min(m => m.Priority);                     // get lowest priority
                }
                foreach (var p in assignedPromotions.Where(w => w.Priority == 0 || w.Priority == priority))
                {
                    if (PromotionManager.ValidatePromotion(p.Code, ruleContext).All(vr => vr.IsValid))
                    {
                        PromotionManager.AssignPromotion(ruleContext.CustomerId, p.Code);
                    }
                    else
                    {
                        PromotionManager.ClearPromotionUsages(ruleContext.CustomerId, p.Code, false);
                    }
                }

                foreach (var p in assignedPromotions.Where(w => w.Priority > priority))
                {
                    PromotionManager.ClearPromotionUsages(ruleContext.CustomerId, p.Code, false);
                }
            }
        }
Beispiel #3
0
        public static IList <PromotionsData.Promotion> AutoAssignPromotions(int customerId, IRuleContext ruleContext)
        {
            IList <PromotionsData.Promotion> autoAssignPromotions = new List <PromotionsData.Promotion>();

            if (ruleContext.CartType != null && ruleContext.CartType != (Int32)CartTypeEnum.ShoppingCart)
            {
                return(autoAssignPromotions);
            }

            PromotionController promotionController = CreatePromotionController();

            if (!AppLogic.AppConfigExists("AspDotNetStorefront.Promotions.excludestates"))
            {
                AppLogic.AddAppConfig("AspDotNetStorefront.Promotions.excludestates", "states to be excluded from shipping promotions", String.Empty, "", true);
            }
            string excludeConfig = AppLogic.AppConfig("AspDotNetStorefront.Promotions.excludestates");

            if (excludeConfig.Length > 0)
            {
                ruleContext.ExcludeStates = excludeConfig.Split(',');
            }

            var promosToAssign = PromotionsData.DataContextProvider.Current.Promotions.Where(p => p.Active && p.AutoAssigned && !PromotionManager.GetRemovedAutoAssignPromotionIdList(customerId).Contains(p.Id));

            foreach (PromotionsData.Promotion promotion in promosToAssign)
            {
                ruleContext.PromotionId = promotion.Id;
                if (promotionController.ValidatePromotion(promotion, ruleContext, AppLogic.CustomerLevelAllowsCoupons(ruleContext.CustomerLevel)).All(vr => vr.IsValid))
                {
                    AssignPromotion(customerId, promotion.Id);
                    autoAssignPromotions.Add(promotion);
                }
            }

            return(autoAssignPromotions);
        }