public CheckPromotionConditionResult CheckConditions(Promotion promotion, PricingContext context)
        {
            var result = new CheckPromotionConditionResult();

            if (!promotion.Conditions.Any())
            {
                result.Success = true;
                foreach (var item in context.Items)
                {
                    result.MatchedItems.Add(item);
                }

                return result;
            }

            var operators = _ruleEngine.ComparisonOperatorManager.Operators.Select(o => o.Name).ToList();

            foreach (var item in context.Items)
            {
                var contextModel = new PromotionConditionContextModel
                {
                    Item = item,
                    Customer = context.Customer
                };

                if (_ruleEngine.CheckConditions(promotion.Conditions, contextModel))
                {
                    result.Success = true;
                    result.MatchedItems.Add(item);
                }
            }

            return result;
        }
Exemplo n.º 2
0
        public void Create(Promotion promotion)
        {
            if (promotion.RequireCouponCode && IsCouponAlreadyTaken(promotion.CouponCode, promotion.Id))
            {
                throw new InvalidOperationException("Coupon code has been taken.");
            }

            _repository.Insert(promotion);
        }
Exemplo n.º 3
0
        public PromotionContext(
            Promotion promotion, object policyConfig, IEnumerable<PriceCalculationItem> conditionMatchedItems, PriceCalculationContext pricingContext)
        {
            Require.NotNull(promotion, "promotion");
            Require.NotNull(pricingContext, "pricingContext");

            Promotion = promotion;
            PolicyConfig = policyConfig;
            ConditionMatchedItems = (conditionMatchedItems ?? Enumerable.Empty<PriceCalculationItem>()).ToList();
            PricingContext = pricingContext;
        }
Exemplo n.º 4
0
        public PromotionContext(
            Promotion promotion,
            IEnumerable<PricingItem> conditionMatchedItems,
            PricingContext pricingContext)
        {
            Require.NotNull(promotion, "promotion");
            Require.NotNull(pricingContext, "pricingContext");

            Promotion = promotion;
            ConditionMatchedItems = (conditionMatchedItems ?? Enumerable.Empty<PricingItem>()).ToList();
            PricingContext = pricingContext;
        }
Exemplo n.º 5
0
        public bool Enable(Promotion promotion)
        {
            if (promotion.IsEnabled)
            {
                return false;
            }

            promotion.IsEnabled = true;

            _repository.Database.SaveChanges();

            Event.Raise(new PromotionEnabled(promotion), _instance);

            return true;
        }
Exemplo n.º 6
0
        public PromotionRowModel(Promotion promotion)
        {
            Id = promotion.Id;
            Name = promotion.Name;
            IsEnabled = promotion.IsEnabled;
            Priority = promotion.Priority;

            if (promotion.RequireCouponCode)
            {
                CouponCode = promotion.CouponCode;
            }
            else
            {
                CouponCode = "-";
            }
        }
Exemplo n.º 7
0
        public void Delete(Promotion promotion)
        {
            if (promotion.IsEnabled)
            {
                Disable(promotion);
            }

            promotion.OverlappablePromotions.Clear();

            var referencingPromotions = Query().Where(p => p.OverlappablePromotions.Any(x => x.Id == promotion.Id)).ToList();

            foreach (var each in referencingPromotions)
            {
                each.RemoveOverlappablePromotion(promotion.Id);
            }

            _repository.Delete(promotion);
        }
Exemplo n.º 8
0
        private PromotionMatch TryMatchPromotion(Promotion promotion, PriceCalculationContext context)
        {
            if (promotion.RequireCouponCode && context.CouponCode != promotion.CouponCode)
            {
                return null;
            }

            var isMatch = false;
            var conditionMatchedItems = new List<PriceCalculationItem>();

            if (!promotion.Conditions.Any())
            {
                isMatch = true;
            }
            else
            {
                var conditionChecker = new PromotionConditionChecker(_ruleEngine);
                var result = conditionChecker.CheckConditions(promotion, context);
                if (result.Success)
                {
                    isMatch = true;
                    foreach (var item in result.MatchedItems)
                    {
                        if (!conditionMatchedItems.Contains(item))
                        {
                            conditionMatchedItems.Add(item);
                        }
                    }
                }
            }

            if (isMatch)
            {
                return new PromotionMatch(promotion, conditionMatchedItems);
            }

            return null;
        }
Exemplo n.º 9
0
 public PromotionEnabled(Promotion promotion)
 {
     PromotionId = promotion.Id;
 }
Exemplo n.º 10
0
 public PromotionCreated(Promotion promotion)
 {
     PromotionId = promotion.Id;
 }
Exemplo n.º 11
0
        public virtual bool CanBeOverlappedUsedWith(Promotion other)
        {
            if (Priority < other.Priority)
            {
                return other.CanBeOverlappedUsedWith(this);
            }

            if (OverlappingUsage == PromotionOverlappingUsage.NotAllowed)
            {
                return false;
            }
            else if (OverlappingUsage == PromotionOverlappingUsage.AllowedWithAnyPromotion)
            {
                return true;
            }
            else if (OverlappingUsage == PromotionOverlappingUsage.AllowedWithSpecifiedPromotions)
            {
                return OverlappablePromotions.Any(x => x.Id == other.Id);
            }
            else
            {
                throw new NotSupportedException(OverlappingUsage + " is not supported.");
            }
        }
Exemplo n.º 12
0
 public void UpdateSimplePropertiesTo(Promotion promotion)
 {
     promotion.Name = Name.Trim();
     promotion.StartTimeUtc = StartTime == null ? null : (DateTime?)StartTime.Value.ToUniversalTime();
     promotion.EndTimeUtc = EndTime == null ? null : (DateTime?)EndTime.Value.ToUniversalTime();
     promotion.RequireCouponCode = RequireCouponCode;
     promotion.CouponCode = CouponCode.TrimOrNull();
     promotion.Priority = Priority;
     promotion.PromotionPolicyName = PromotionPolicy;
     promotion.OverlappingUsage = OverlappingUsage;
 }
Exemplo n.º 13
0
 public void UpdateFrom(Promotion promotion)
 {
     Id = promotion.Id;
     Name = promotion.Name;
     StartTime = promotion.StartTimeUtc == null ? null : (DateTime?)promotion.StartTimeUtc.Value.ToLocalTime();
     EndTime = promotion.EndTimeUtc == null ? null : (DateTime?)promotion.EndTimeUtc.Value.ToLocalTime();
     RequireCouponCode = promotion.RequireCouponCode;
     CouponCode = promotion.CouponCode;
     Priority = promotion.Priority;
     PromotionPolicy = promotion.PromotionPolicyName;
     OverlappingUsage = promotion.OverlappingUsage;
 }
Exemplo n.º 14
0
 public PromotionMatch(Promotion promotion, IEnumerable<PricingItem> conditionMatchedItems)
 {
     Promotion = promotion;
     ConditionMatchedItems = new List<PricingItem>(conditionMatchedItems);
 }
Exemplo n.º 15
0
 public PromotionDeleted(Promotion promotion)
 {
     PromotionId = promotion.Id;
     PromotionName = promotion.Name;
 }
Exemplo n.º 16
0
 public PromotionUpdated(Promotion promotion)
 {
     PromotionId = promotion.Id;
 }