Beispiel #1
0
        private void PopulateDynamicExpression(DynamicPromotion dynamicPromotion, JObject jObj)
        {
            var dynamicExpressionToken = jObj["dynamicExpression"];
            var dynamicExpression      = dynamicExpressionToken?.ToObject <PromoDynamicExpressionTree>();

            if (dynamicExpression?.Children != null)
            {
                var conditionExpression = dynamicExpression.GetConditionExpression();
                dynamicPromotion.PredicateSerialized = _expressionSerializer.SerializeExpression(conditionExpression);

                var rewards = dynamicExpression.GetRewards();
                dynamicPromotion.RewardsSerialized = JsonConvert.SerializeObject(rewards, new JsonSerializerSettings {
                    TypeNameHandling = TypeNameHandling.All
                });

                // Clear availableElements in expression to decrease size
                dynamicExpression.AvailableChildren = null;
                var allBlocks = ((DynamicExpression)dynamicExpression).Traverse(x => x.Children);
                foreach (var block in allBlocks)
                {
                    block.AvailableChildren = null;
                }

                dynamicPromotion.PredicateVisualTreeSerialized = JsonConvert.SerializeObject(dynamicExpression);
            }
        }
        public void EvaluatePromotion_GetBestPaymentReward()
        {
            //Agganre
            var blockReward = new BlockReward().WithChildrens(new RewardPaymentGetOfAbs()
            {
                Amount = 10m, PaymentMethod = "PayTest"
            });
            var dynamicPromotion = new DynamicPromotion
            {
                DynamicExpression = AbstractTypeFactory <PromotionConditionAndRewardTree> .TryCreateInstance()
            };

            dynamicPromotion.DynamicExpression.WithChildrens(blockReward);

            var evalPolicy = GetPromotionEvaluationPolicy(new[] { dynamicPromotion });
            var productA   = new ProductPromoEntry {
                ProductId = "ProductA", Price = 100, Quantity = 1
            };
            var context = new PromotionEvaluationContext
            {
                PaymentMethodCode  = "PayTest",
                PaymentMethodPrice = 5m,
                PromoEntries       = new[] { productA }
            };

            //Act
            var rewards = evalPolicy.EvaluatePromotionAsync(context).GetAwaiter().GetResult().Rewards.OfType <PaymentReward>().ToList();

            //Assert
            Assert.Equal(10m, rewards.First().Amount);
            Assert.True(rewards.First().IsValid);
        }
        public virtual PromotionEntity FromModel(DynamicPromotion promotion, PrimaryKeyResolvingMap pkMap)
        {
            if (promotion == null)
            {
                throw new NullReferenceException(nameof(promotion));
            }

            pkMap.AddPair(promotion, this);

            this.Id                            = promotion.Id;
            this.CreatedBy                     = promotion.CreatedBy;
            this.CreatedDate                   = promotion.CreatedDate;
            this.ModifiedBy                    = promotion.ModifiedBy;
            this.ModifiedDate                  = promotion.ModifiedDate;
            this.StartDate                     = promotion.StartDate ?? DateTime.UtcNow;
            this.EndDate                       = promotion.EndDate;
            this.StoreId                       = promotion.Store;
            this.Name                          = promotion.Name;
            this.Description                   = promotion.Description;
            this.IsActive                      = promotion.IsActive;
            this.EndDate                       = promotion.EndDate;
            this.Priority                      = promotion.Priority;
            this.IsExclusive                   = promotion.IsExclusive;
            this.IsAllowCombiningWithSelf      = promotion.IsAllowCombiningWithSelf;
            this.PredicateVisualTreeSerialized = promotion.PredicateVisualTreeSerialized;
            this.PredicateSerialized           = promotion.PredicateSerialized;
            this.RewardsSerialized             = promotion.RewardsSerialized;
            this.PerCustomerLimit              = promotion.MaxPersonalUsageCount;
            this.TotalLimit                    = promotion.MaxUsageCount;
            this.PerCustomerLimit              = promotion.MaxPersonalUsageCount;
            return(this);
        }
        public static coreModel.Promotion ToCoreModel(this webModel.Promotion promotion)
        {
            var retVal = new DynamicPromotion();

            retVal.InjectFrom(promotion);
            retVal.Coupons = promotion.Coupons;

            if (promotion.DynamicExpression != null && promotion.DynamicExpression.Children != null)
            {
                var conditionExpression = promotion.DynamicExpression.GetConditionExpression();
                retVal.PredicateSerialized = SerializationUtil.SerializeExpression(conditionExpression);
                var rewards = promotion.DynamicExpression.GetRewards();
                retVal.RewardsSerialized = JsonConvert.SerializeObject(rewards, new JsonSerializerSettings {
                    TypeNameHandling = TypeNameHandling.All
                });

                //Clear availableElements in expression (for decrease size)
                promotion.DynamicExpression.AvailableChildren = null;
                var allBlocks = ((DynamicExpression)promotion.DynamicExpression).Traverse(x => x.Children);
                foreach (var block in allBlocks)
                {
                    block.AvailableChildren = null;
                }
                retVal.PredicateVisualTreeSerialized = JsonConvert.SerializeObject(promotion.DynamicExpression);
            }
            return(retVal);
        }
        private DynamicPromotion GetDynamicPromotion(IConditionExpression[] conditions, IRewardExpression[] rewards)
        {
            var dynamicPromotion = new DynamicPromotion(expressionSerializer, couponService, promotionUsageService);

            dynamicPromotion.PredicateSerialized = GetPredicateSerialized(conditions);
            dynamicPromotion.RewardsSerialized   = GetRewardsSerialized(rewards);

            return(dynamicPromotion);
        }
#pragma warning restore CS3016 // Arrays as attribute arguments is not CLS-compliant
        public void CheckPromotionValid(IConditionExpression[] conditions, IRewardExpression[] rewards, IEvaluationContext context, DynamicPromotionEvaluationResult evaluationResult)
        {
            DynamicPromotion dynamicPromotion = GetDynamicPromotion(conditions, rewards);

            var result = dynamicPromotion.EvaluatePromotion(context);

            Assert.Equal(evaluationResult.ValidCount, result.Count(r => r.IsValid));
            Assert.Equal(evaluationResult.InvalidCount, result.Count(r => !r.IsValid));
        }
Beispiel #7
0
        public virtual Promotion ToModel(DynamicPromotion promotion)
        {
            if (promotion == null)
            {
                throw new NullReferenceException(nameof(promotion));
            }

            promotion.Id                            = this.Id;
            promotion.CreatedBy                     = this.CreatedBy;
            promotion.CreatedDate                   = this.CreatedDate;
            promotion.ModifiedBy                    = this.ModifiedBy;
            promotion.ModifiedDate                  = this.ModifiedDate;
            promotion.StartDate                     = this.StartDate;
            promotion.EndDate                       = this.EndDate;
            promotion.Store                         = this.StoreId;
            promotion.Name                          = this.Name;
            promotion.Description                   = this.Description;
            promotion.IsActive                      = this.IsActive;
            promotion.EndDate                       = this.EndDate;
            promotion.Priority                      = this.Priority;
            promotion.IsExclusive                   = this.IsExclusive;
            promotion.IsAllowCombiningWithSelf      = this.IsAllowCombiningWithSelf;
            promotion.PredicateVisualTreeSerialized = this.PredicateVisualTreeSerialized;
            promotion.PredicateSerialized           = this.PredicateSerialized;
            promotion.RewardsSerialized             = this.RewardsSerialized;
            promotion.MaxPersonalUsageCount         = this.PerCustomerLimit;
            promotion.MaxUsageCount                 = this.TotalLimit;
            promotion.MaxPersonalUsageCount         = this.PerCustomerLimit;
            promotion.HasCoupons                    = this.HasCoupons;


            if (!string.IsNullOrEmpty(promotion.PredicateVisualTreeSerialized))
            {
                //Temporary back data compatibility fix for serialized expressions
                promotion.PredicateVisualTreeSerialized = promotion.PredicateVisualTreeSerialized.Replace("VirtoCommerce.DynamicExpressionModule.", "VirtoCommerce.DynamicExpressionsModule.");
            }
            if (!string.IsNullOrEmpty(promotion.PredicateSerialized))
            {
                //Temporary back data compatibility fix for serialized expressions
                promotion.PredicateSerialized = promotion.PredicateSerialized.Replace("VirtoCommerce.DynamicExpressionModule.", "VirtoCommerce.DynamicExpressionsModule.");
            }

            if (this.Stores != null)
            {
                promotion.StoreIds = this.Stores.Select(x => x.StoreId).ToList();
            }

            return(promotion);
        }
        /// <summary>
        /// Converting to model type
        /// </summary>
        /// <param name="catalogBase"></param>
        /// <returns></returns>
        public static coreModel.Promotion ToCoreModel(this dataModel.Promotion dbEntity)
        {
            if (dbEntity == null)
            {
                throw new ArgumentNullException("dbEntity");
            }

            var retVal = new DynamicPromotion();

            retVal.InjectFrom(dbEntity);
            retVal.Coupons               = dbEntity.Coupons.Select(x => x.Code).ToArray();
            retVal.Store                 = dbEntity.StoreId;
            retVal.MaxUsageCount         = dbEntity.TotalLimit;
            retVal.MaxPersonalUsageCount = dbEntity.PerCustomerLimit;
            return(retVal);
        }
        public virtual Promotion ToModel(DynamicPromotion promotion)
        {
            if (promotion == null)
            {
                throw new NullReferenceException(nameof(promotion));
            }

            promotion.Id                            = Id;
            promotion.CreatedBy                     = CreatedBy;
            promotion.CreatedDate                   = CreatedDate;
            promotion.ModifiedBy                    = ModifiedBy;
            promotion.ModifiedDate                  = ModifiedDate;
            promotion.StartDate                     = StartDate;
            promotion.EndDate                       = EndDate;
            promotion.Store                         = StoreId;
            promotion.Name                          = Name;
            promotion.Description                   = Description;
            promotion.IsActive                      = IsActive;
            promotion.EndDate                       = EndDate;
            promotion.Priority                      = Priority;
            promotion.IsExclusive                   = IsExclusive;
            promotion.IsAllowCombiningWithSelf      = IsAllowCombiningWithSelf;
            promotion.PredicateVisualTreeSerialized = PredicateVisualTreeSerialized;
            promotion.PredicateSerialized           = PredicateSerialized;
            promotion.RewardsSerialized             = RewardsSerialized;
            promotion.MaxPersonalUsageCount         = PerCustomerLimit;
            promotion.MaxUsageCount                 = TotalLimit;
            promotion.MaxPersonalUsageCount         = PerCustomerLimit;
            promotion.HasCoupons                    = HasCoupons;


            if (!string.IsNullOrEmpty(promotion.PredicateVisualTreeSerialized))
            {
                promotion.PredicateVisualTreeSerialized = promotion.PredicateVisualTreeSerialized;
            }
            if (!string.IsNullOrEmpty(promotion.PredicateSerialized))
            {
                promotion.PredicateSerialized = promotion.PredicateSerialized;
            }

            if (Stores != null)
            {
                promotion.StoreIds = Stores.Select(x => x.StoreId).ToList();
            }

            return(promotion);
        }
        public virtual PromotionEntity FromModel(DynamicPromotion promotion, PrimaryKeyResolvingMap pkMap)
        {
            if (promotion == null)
            {
                throw new NullReferenceException(nameof(promotion));
            }

            pkMap.AddPair(promotion, this);

            Id                            = promotion.Id;
            CreatedBy                     = promotion.CreatedBy;
            CreatedDate                   = promotion.CreatedDate;
            ModifiedBy                    = promotion.ModifiedBy;
            ModifiedDate                  = promotion.ModifiedDate;
            StartDate                     = promotion.StartDate ?? DateTime.UtcNow;
            EndDate                       = promotion.EndDate;
            StoreId                       = promotion.Store;
            Name                          = promotion.Name;
            Description                   = promotion.Description;
            IsActive                      = promotion.IsActive;
            EndDate                       = promotion.EndDate;
            Priority                      = promotion.Priority;
            IsExclusive                   = promotion.IsExclusive;
            IsAllowCombiningWithSelf      = promotion.IsAllowCombiningWithSelf;
            PredicateVisualTreeSerialized = promotion.PredicateVisualTreeSerialized;
            PredicateSerialized           = promotion.PredicateSerialized;
            RewardsSerialized             = promotion.RewardsSerialized;
            PerCustomerLimit              = promotion.MaxPersonalUsageCount;
            TotalLimit                    = promotion.MaxUsageCount;
            PerCustomerLimit              = promotion.MaxPersonalUsageCount;

            if (promotion.StoreIds != null)
            {
                Stores = new ObservableCollection <PromotionStoreEntity>(promotion.StoreIds.Select(x => new PromotionStoreEntity {
                    StoreId = x, PromotionId = promotion.Id
                }));
            }

            return(this);
        }
        private void PopulateDynamicExpression(DynamicPromotion dynamicPromotion, JObject jObj, JsonSerializer serializer)
        {
            var dynamicExpressionToken = jObj["dynamicExpression"];
            var dynamicExpression      = dynamicExpressionToken?.ToObject <PromotionConditionAndRewardTree>(serializer);

            if (dynamicExpression?.Children != null)
            {
                var conditionExpression = dynamicExpression.GetConditions();
                dynamicPromotion.PredicateSerialized = JsonConvert.SerializeObject(conditionExpression);

                var rewards = dynamicExpression.GetRewards();
                dynamicPromotion.RewardsSerialized = JsonConvert.SerializeObject(rewards);

                // Clear availableElements in expression to decrease size
                dynamicExpression.AvailableChildren = null;
                var allBlocks = ((IConditionTree)dynamicExpression).Traverse(x => x.Children);
                foreach (var block in allBlocks)
                {
                    block.AvailableChildren = null;
                }

                dynamicPromotion.PredicateVisualTreeSerialized = JsonConvert.SerializeObject(dynamicExpression);
            }
        }
Beispiel #12
0
        public void DynamicPromotion_Clone()
        {
            var blockCustomer = new BlockCustomerCondition()
                                .WithAvailConditions(
                new ConditionIsRegisteredUser(),
                new ConditionIsEveryone(),
                new ConditionIsFirstTimeBuyer(),
                new UserGroupsContainsCondition()
            {
                Group = "11"
            }
                );
            var blockCatalog = new BlockCatalogCondition()
                               .WithAvailConditions(
                new ConditionCategoryIs()
            {
                CategoryId = "11", CategoryName = "", ExcludingCategoryIds = new string[] { "1", "2" }, ExcludingProductIds = new string[] { "3", "4" }
            },
                new ConditionCodeContains()
            {
                Keyword = "keyword"
            },
                new ConditionCurrencyIs()
            {
                Currency = "USD"
            },
                new ConditionEntryIs()
            {
                ProductIds = new string[] { "1", "2" }, ProductNames = new string[] { "name1", "name2" }
            },
                new ConditionInStockQuantity()
            {
                CompareCondition = "CND", Quantity = 111, QuantitySecond = 222
            },
                new ConditionHasNoSalePrice()
                );
            var blockCart = new BlockCartCondition()
                            .WithAvailConditions(
                new ConditionAtNumItemsInCart()
            {
                CompareCondition = "CND", ExcludingCategoryIds = new string[] { "1", "2" }, ExcludingProductIds = new string[] { "3", "4" }, NumItem = 111, NumItemSecond = 222
            },
                new ConditionAtNumItemsInCategoryAreInCart()
            {
                CategoryId = "catid", CategoryName = "catname", CompareCondition = "CND", ExcludingCategoryIds = new string[] { "1", "2" }, ExcludingProductIds = new string[] { "3", "4" }, NumItem = 111, NumItemSecond = 222
            },
                new ConditionAtNumItemsOfEntryAreInCart()
            {
                CompareCondition = "CND", NumItem = 111, NumItemSecond = 222, ProductId = "Id", ProductName = "Name"
            },
                new ConditionCartSubtotalLeast()
            {
                CompareCondition = "CND", ExcludingCategoryIds = new string[] { "1", "2" }, ExcludingProductIds = new string[] { "3", "4" }, SubTotal = 111, SubTotalSecond = 222
            }
                );
            var blockReward = new BlockReward()
                              .WithAvailConditions(
                new RewardCartGetOfAbsSubtotal()
            {
                Amount = 444
            },
                new RewardCartGetOfRelSubtotal()
            {
                Amount = 444, MaxLimit = 555
            },
                new RewardItemGetFreeNumItemOfProduct()
            {
                NumItem = 55, ProductId = "Id", ProductName = "Name"
            },
                new RewardItemGetOfAbs()
            {
                Amount = 444, ProductId = "Id", ProductName = "Name"
            },
                new RewardItemGetOfAbsForNum()
            {
                Amount = 444, ProductId = "Id", ProductName = "Name", NumItem = 23
            },
                new RewardItemGetOfRel()
            {
                Amount = 444, ProductId = "Id", ProductName = "Name", MaxLimit = 23
            },
                new RewardItemGetOfRelForNum()
            {
                Amount = 444, ProductId = "Id", ProductName = "Name", MaxLimit = 23, NumItem = 32
            },
                new RewardItemGiftNumItem()
            {
                CategoryId = "catid", CategoryName = "catname", Description = "description", ProductId = "productId", ProductName = "ptoductName", Name = "Name", ImageUrl = "url:\\", MeasureUnit = "px", Quantity = 33
            },
                new RewardShippingGetOfAbsShippingMethod()
            {
                Amount = 444, ShippingMethod = "shipMethod"
            },
                new RewardShippingGetOfRelShippingMethod()
            {
                Amount = 444, ShippingMethod = "shipMethod", MaxLimit = 22
            },
                new RewardPaymentGetOfAbs()
            {
                Amount = 444, PaymentMethod = "payMethod"
            },
                new RewardPaymentGetOfRel()
            {
                Amount = 444, PaymentMethod = "payMethod", MaxLimit = 22
            },
                new RewardItemForEveryNumInGetOfRel()
            {
                Amount = 444, ForNthQuantity = 77, InEveryNthQuantity = 78, MaxLimit = 22, ItemLimit = 23, Product = new ProductContainer()
                {
                    ProductId = "prodID", ProductName = "prodName"
                }
            },
                new RewardItemForEveryNumOtherItemInGetOfRel()
            {
                Amount = 444, ForNthQuantity = 77, InEveryNthQuantity = 78, MaxLimit = 22, ItemLimit = 23, Product = new ProductContainer()
                {
                    ProductId = "prodID", ProductName = "prodName"
                }, ConditionalProduct = new ProductContainer()
                {
                    ProductId = "condProdID", ProductName = "condProdName"
                }
            }
                );

            var dynamicPromotion = new DynamicPromotion
            {
                DynamicExpression = AbstractTypeFactory <PromotionConditionAndRewardTree> .TryCreateInstance()
            };

            dynamicPromotion.DynamicExpression.WithChildrens(
                blockCustomer,
                blockCatalog,
                blockCart,
                blockReward
                );

            dynamicPromotion.AssertCloneIndependency();
        }