public PromotionResult EvaluatePromotion(IEvaluationContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var promoContext = context as PromotionEvaluationContext;

            if (promoContext == null)
            {
                throw new ArgumentException($"{nameof(context)} type {context.GetType()} must be derived from PromotionEvaluationContext");
            }

            var promotions = _promotionSearchService.SearchPromotions(new PromotionSearchCriteria {
                OnlyActive = true, StoreIds = new[] { promoContext.StoreId }, Take = int.MaxValue
            }).Results;

            var result = new PromotionResult();

            Func <PromotionEvaluationContext, IEnumerable <PromotionReward> > evalFunc = (evalContext) => promotions.SelectMany(x => x.EvaluatePromotion(evalContext))
                                                                                         .OrderByDescending(x => x.Promotion.IsExclusive)
                                                                                         .ThenByDescending(x => x.Promotion.Priority)
                                                                                         .Where(x => x.IsValid)
                                                                                         .ToList();

            EvalAndCombineRewardsRecursively(promoContext, evalFunc, result.Rewards, new List <PromotionReward>());
            return(result);
        }
예제 #2
0
        public GenericSearchResult <Promotion> SearchPromotions(PromotionSearchCriteria criteria)
        {
            var cacheKey = GetCacheKey("IPromotionSearchService.SearchPromotions", criteria.GetCacheKey());
            var retVal   = _cacheManager.Get(cacheKey, RegionName, () => _promoSearchService.SearchPromotions(criteria));

            return(retVal);
        }
        public IHttpActionResult PromotionsSearch(PromotionSearchCriteria criteria)
        {
            var retVal = new GenericSearchResult <webModel.Promotion>();

            //Scope bound ACL filtration
            criteria = FilterPromotionSearchCriteria(User.Identity.Name, criteria);

            var promoSearchResult = _promoSearchService.SearchPromotions(criteria);

            retVal.TotalCount = promoSearchResult.TotalCount;
            retVal.Results    = promoSearchResult.Results.Select(x => x.ToWebModel()).ToList();
            return(Ok(retVal));
        }
예제 #4
0
        public IHttpActionResult PromotionsSearch(PromotionSearchCriteria criteria)
        {
            var retVal = new GenericSearchResult <Promotion>();

            //Scope bound ACL filtration
            criteria = FilterPromotionSearchCriteria(User.Identity.Name, criteria);

            var promoSearchResult = _promoSearchService.SearchPromotions(criteria);

            foreach (var promotion in promoSearchResult.Results.OfType <DynamicPromotion>())
            {
                promotion.PredicateVisualTreeSerialized = null;
                promotion.PredicateSerialized           = null;
                promotion.RewardsSerialized             = null;
            }

            retVal.TotalCount = promoSearchResult.TotalCount;
            retVal.Results    = promoSearchResult.Results.ToList();
            return(Ok(retVal));
        }
        public PromotionResult EvaluatePromotion(IEvaluationContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var promoContext = context as PromotionEvaluationContext;

            if (promoContext == null)
            {
                throw new ArgumentException($"{nameof(context)} type {context.GetType()} must be derived from PromotionEvaluationContext");
            }

            var promotions = _promotionSearchService.SearchPromotions(new PromotionSearchCriteria {
                OnlyActive = true, Store = promoContext.StoreId, Take = int.MaxValue
            }).Results;

            var result = new PromotionResult();

            var rewards = promotions.SelectMany(x => x.EvaluatePromotion(context)).Where(x => x.IsValid).ToArray();

            var firstOrderExlusiveReward = rewards.FirstOrDefault(x => x.Promotion.IsExclusive);

            if (firstOrderExlusiveReward != null)
            {
                //Add only rewards from exclusive promotion
                result.Rewards.AddRange(rewards.Where(x => x.Promotion == firstOrderExlusiveReward.Promotion));
            }
            else
            {
                //best shipment promotion
                var curShipmentAmount              = promoContext.ShipmentMethodCode != null ? promoContext.ShipmentMethodPrice : 0m;
                var allShipmentRewards             = rewards.OfType <ShipmentReward>().ToArray();
                var groupedByShippingMethodRewards = allShipmentRewards.GroupBy(x => x.ShippingMethod).Where(x => x.Key != null);
                foreach (var shipmentRewards in groupedByShippingMethodRewards)
                {
                    var bestShipmentReward = GetBestAmountReward(curShipmentAmount, shipmentRewards);
                    if (bestShipmentReward != null)
                    {
                        result.Rewards.Add(bestShipmentReward);
                    }
                }

                //best catalog item promotion
                var allItemsRewards = rewards.OfType <CatalogItemAmountReward>().ToArray();
                var groupRewards    = allItemsRewards.GroupBy(x => x.ProductId).Where(x => x.Key != null);
                foreach (var groupReward in groupRewards)
                {
                    var item = promoContext.PromoEntries.FirstOrDefault(x => x.ProductId == groupReward.Key);
                    if (item != null)
                    {
                        var bestItemReward = GetBestAmountReward(item.Price, groupReward);
                        if (bestItemReward != null)
                        {
                            result.Rewards.Add(bestItemReward);
                        }
                    }
                }

                //best order promotion
                var cartSubtotalRewards = rewards.OfType <CartSubtotalReward>().Where(x => x.IsValid).OrderByDescending(x => x.Amount);
                var cartSubtotalReward  = cartSubtotalRewards.FirstOrDefault(x => !string.IsNullOrEmpty(x.Coupon)) ?? cartSubtotalRewards.FirstOrDefault();
                if (cartSubtotalReward != null)
                {
                    result.Rewards.Add(cartSubtotalReward);
                }
                //Gifts
                rewards.OfType <GiftReward>().ToList().ForEach(x => result.Rewards.Add(x));

                //Special offer
                rewards.OfType <SpecialOfferReward>().ToList().ForEach(x => result.Rewards.Add(x));
            }

            return(result);
        }
        private BackupObject GetBackupObject(Action <ExportImportProgressInfo> progressCallback)
        {
            var result       = new BackupObject();
            var progressInfo = new ExportImportProgressInfo {
                Description = "Search promotions..."
            };

            progressCallback(progressInfo);
            var allPromotions = _promotionSearchService.SearchPromotions(new Domain.Marketing.Model.Promotions.Search.PromotionSearchCriteria
            {
                Take = int.MaxValue
            }).Results;

            progressInfo.Description = String.Format("{0} promotions loading...", allPromotions.Count());
            progressCallback(progressInfo);
            result.Promotions = _promotionService.GetPromotionsByIds(allPromotions.Select(x => x.Id).ToArray());

            progressInfo.Description = "Search dynamic content objects...";
            progressCallback(progressInfo);

            progressInfo.Description = String.Format("Loading folders...");
            progressCallback(progressInfo);
            result.ContentFolders = LoadFoldersRecursive(null);

            progressInfo.Description = String.Format("Loading places...");
            progressCallback(progressInfo);
            result.ContentPlaces = _dynamicContentSearchService.SearchContentPlaces(new DynamicContentPlaceSearchCriteria {
                Take = int.MaxValue
            }).Results.ToList();

            progressInfo.Description = String.Format("Loading contents...");
            progressCallback(progressInfo);
            result.ContentItems = _dynamicContentSearchService.SearchContentItems(new DynamicContentItemSearchCriteria {
                Take = int.MaxValue
            }).Results.ToList();

            progressInfo.Description = String.Format("Loading publications...");
            progressCallback(progressInfo);
            result.ContentPublications = _dynamicContentSearchService.SearchContentPublications(new DynamicContentPublicationSearchCriteria {
                Take = int.MaxValue
            }).Results.ToList();

            progressInfo.Description = String.Format("Loading coupons...");
            progressCallback(progressInfo);
            var couponsTotal = _couponService.SearchCoupons(new CouponSearchCriteria {
                Take = 0
            }).TotalCount;
            var pageSize = 500;

            Paginate(couponsTotal, pageSize, (x) =>
            {
                progressInfo.Description = String.Format($"Loading coupons: {Math.Min(x * pageSize, couponsTotal)} of {couponsTotal} loaded");
                progressCallback(progressInfo);
                result.Coupons.AddRange(_couponService.SearchCoupons(new CouponSearchCriteria {
                    Skip = (x - 1) * pageSize, Take = pageSize
                }).Results);
            });

            progressInfo.Description = String.Format("Loading usages...");
            progressCallback(progressInfo);
            var usagesTotal = _usageService.SearchUsages(new PromotionUsageSearchCriteria {
                Take = 0
            }).TotalCount;

            Paginate(usagesTotal, pageSize, (x) =>
            {
                progressInfo.Description = String.Format($"Loading usages: {Math.Min(x * pageSize, usagesTotal)} of {usagesTotal} loaded");
                progressCallback(progressInfo);
                result.Usages.AddRange(_usageService.SearchUsages(new PromotionUsageSearchCriteria {
                    Skip = (x - 1) * pageSize, Take = pageSize
                }).Results);
            });

            return(result);
        }
 protected virtual GenericSearchResult <Promotion> SearchPromotions(PromotionSearchCriteria criteria)
 {
     return(_promotionSearchService.SearchPromotions(criteria));
 }