public void ScenarioB()
        {
            PromotionController promotionController = new PromotionController();

            PromotionRequestModel  request  = new PromotionRequestModel();
            PromotionResponseModel response = new PromotionResponseModel();

            request.PromotionId = 1;

            request.CartItems.Add(
                new CartItemModel
            {
                ItemName = "A",
                ItemQty  = 5
            });

            request.CartItems.Add(new CartItemModel
            {
                ItemName = "B",
                ItemQty  = 5
            });

            request.CartItems.Add(new CartItemModel
            {
                ItemName = "C",
                ItemQty  = 1
            });

            response = promotionController.ApplyPromotion(request);

            Assert.AreEqual(370, response.TotalCost);
        }
Beispiel #2
0
        public static IDiscountResult GetPromotionDiscount(IRuleContext ruleContext, out IList <IDiscountResult> discountResults)
        {
            PromotionController promotionController = CreatePromotionController();

            discountResults = new List <IDiscountResult>();

            IQueryable <PromotionsData.PromotionUsage>       promotionUsages       = GetPromotionUsagesByCustomer(ruleContext.CustomerId).Where(p => p.Complete == false);
            Dictionary <IPromotionUsage, IPromotionDiscount> AllPromotionDiscounts = new Dictionary <IPromotionUsage, IPromotionDiscount>();

            //Need to loop all promos and all discount types in those promos so we can build up a list of all the discounts on this order
            foreach (PromotionsData.PromotionUsage promotionUsage in promotionUsages)
            {
                foreach (IPromotionDiscount promoDiscount in promotionUsage.Promotion.PromotionDiscounts)
                {
                    //We need to add only one item per promo usage but we want to order by the non-shipping option when it has shipping plus another discount type on one promo
                    //this only works becuase we restrict promos to shipping plus one other type of discount.
                    if (promotionUsage.Promotion.PromotionDiscounts.Count == 1 || promoDiscount.SequenceNumber != (int)PromotionDiscountBase.PromotionSequence.Shipping)
                    {
                        AllPromotionDiscounts.Add(promotionUsage, promoDiscount);
                    }
                }
            }

            //Sort the discounts, this is incase we need to deal with line item -vs- order level coupon priority
            var sortedPromotionDiscounts = AllPromotionDiscounts.ToArray().OrderBy(apd => apd.Key.Id).OrderBy(apd => apd.Value.SequenceNumber);
            var discountContext          = CreateDiscountContext(ruleContext);

            foreach (KeyValuePair <IPromotionUsage, IPromotionDiscount> discountPair in sortedPromotionDiscounts)
            {
                var promotionRuleContext = CreateRuleContext(ruleContext, discountPair.Key.PromotionId);

                List <DiscountableItem> discountableItems = GetDiscountableItems(promotionRuleContext, discountPair.Key.PromotionId);

                var promotionDiscountContext = CreateDiscountContext(discountContext, discountableItems);

                IDiscountResult discountResult = promotionController.ApplyPromotion(discountPair.Key, promotionRuleContext, promotionDiscountContext, () => new SimpleDiscountResult(), CreatePromotionController(), AppLogic.CustomerLevelAllowsCoupons(ruleContext.CustomerLevel));

                if (discountResult != null)
                {
                    discountResults.Add(discountResult);
                }
            }

            return(promotionController.CombineDiscounts(discountResults, delegate() { return new SimpleDiscountResult(); }));
        }