public void VerifyBulkItemPromotionWithTwoItem()
        {
            ActivePromotion activePromotion = new ActivePromotion();

            BulkItemPromotion bulkItemPromotionA = new BulkItemPromotion("A", 3, 130);
            BulkItemPromotion bulkItemPromotionB = new BulkItemPromotion("B", 2, 45);


            activePromotion.AddPromotion(bulkItemPromotionA);
            activePromotion.AddPromotion(bulkItemPromotionB);

            CartItem cartItemA = new CartItem(50, "A", 5);
            CartItem cartItemB = new CartItem(30, "B", 3);


            Cart cart = new Cart();

            cart.AddItem(cartItemA);
            cart.AddItem(cartItemB);

            PromotionEngine promotionEngine = new PromotionEngine(activePromotion);

            int totalCost = promotionEngine.checkout(cart);

            Assert.AreEqual(305, totalCost);
        }
        public void VerifyMultiplePromotion_Scenerio_1()
        {
            ActivePromotion activePromotion = new ActivePromotion();

            BulkItemPromotion    bulkItemPromotionA    = new BulkItemPromotion("A", 3, 130);
            BulkItemPromotion    bulkItemPromotionB    = new BulkItemPromotion("B", 2, 45);
            CombineItemPromotion combineItemPromotionC = new CombineItemPromotion("C", "D", 30);


            activePromotion.AddPromotion(bulkItemPromotionA);
            activePromotion.AddPromotion(bulkItemPromotionB);
            activePromotion.AddPromotion(combineItemPromotionC);


            CartItem cartItemA = new CartItem(50, "A", 3);
            CartItem cartItemB = new CartItem(30, "B", 5);
            CartItem cartItemC = new CartItem(20, "C", 1);
            CartItem cartItemD = new CartItem(15, "D", 1);



            Cart cart = new Cart();

            cart.AddItem(cartItemA);
            cart.AddItem(cartItemB);
            cart.AddItem(cartItemC);
            cart.AddItem(cartItemD);

            PromotionEngine promotionEngine = new PromotionEngine(activePromotion);

            int totalCost = promotionEngine.checkout(cart);

            Assert.AreEqual(280, totalCost);
        }
        public ActivePromotion GetActivePromotion(int activePromotionId)
        {
            ActivePromotion activePromotion =
                (from l in Global.activePromotions
                 where l.PromotionId == activePromotionId
                 select new ActivePromotion
            {
                Promotions = l.Promotions,
                ActivePromotions = l.ActivePromotions,
                unit = l.unit,
            }).FirstOrDefault();

            return(activePromotion);
        }
Exemplo n.º 4
0
        /// <summary>
        /// To calcuate order with promotion applied on it
        /// </summary>
        /// <param name="order"></param>
        /// <param name="defaultValue"></param>
        /// <param name="promo"></param>
        /// <returns></returns>
        public static int Calculate(this SKUOrder order, int defaultValue, ActivePromotion promo)
        {
            int applyPromotionQuantity = 0;

            if (order.Quantity >= promo.Quantity)
            {
                applyPromotionQuantity = order.Quantity / promo.Quantity;
            }

            if (applyPromotionQuantity != 0)
            {
                return((applyPromotionQuantity * promo.Value) + (order.Quantity - (promo.Quantity * applyPromotionQuantity)) * defaultValue);
            }

            return(order.Quantity * defaultValue);
        }
Exemplo n.º 5
0
        public int CalculateFinalResult(int skuId, int promptioid, int orderUnit, string filePath)
        {
            int calculateValue = 0;

            try
            {
                ActivePromotion activePromotion = blActivePromotion.GetActivePromotion(promptioid);

                blSkuIdPrice blSkuIdPrice   = new blSkuIdPrice();
                SkuIdPrice   skuidPrice     = blSkuIdPrice.GetSkuIdPrice(skuId);
                int          finalUnitPrice = 0;
                if (activePromotion.SubPromotionId != 0)
                {
                    if (Global.resultSkuIdPrice != null)
                    {
                        if (Global.resultSkuIdPrice.Exists(x => x.skuid == activePromotion.SubPromotionId))
                        {
                            finalUnitPrice       = activePromotion.Promotions;
                            skuidPrice.unitprice = finalUnitPrice;
                            UpdateJson(activePromotion.SubPromotionId, 0, filePath);
                            AddDataToJson(skuidPrice, filePath);
                            return(finalUnitPrice);
                        }
                    }
                    finalUnitPrice = skuidPrice.unitprice;
                }
                else
                {
                    finalUnitPrice = activePromotion.unit;
                }

                int remainingUnit = orderUnit % finalUnitPrice * skuidPrice.unitprice;
                int groupedUnit   = orderUnit / finalUnitPrice * activePromotion.Promotions;
                calculateValue       = remainingUnit + groupedUnit;
                skuidPrice.unitprice = calculateValue;
                AddDataToJson(skuidPrice, filePath);
            }
            catch (System.Exception)
            {
                throw;
            }

            return(calculateValue);
        }
Exemplo n.º 6
0
        public object Get(ActivePromotions request)
        {
            var accountId = new Guid(this.GetSession().UserAuthId);

            return(_promotionDao.GetUnlockedPromotionsForUser(accountId)
                   .Select(promotionDetail =>
            {
                var activePromotion = new ActivePromotion
                {
                    Name = promotionDetail.Name,
                    Description = promotionDetail.Description,
                    Code = promotionDetail.Code,
                    ExpirationDate = promotionDetail.GetEndDateTime()
                };

                AddProgressToPromotion(accountId, promotionDetail, activePromotion);

                return activePromotion;
            }).ToArray());
        }
        public void VerifyBulkItemPromotionWithOneItem()
        {
            ActivePromotion activePromotion = new ActivePromotion();

            BulkItemPromotion bulkItemPromotion = new BulkItemPromotion("A", 3, 130);


            activePromotion.AddPromotion(bulkItemPromotion);

            CartItem cartItem = new CartItem(50, "A", 5);

            Cart cart = new Cart();

            cart.AddItem(cartItem);

            PromotionEngine promotionEngine = new PromotionEngine(activePromotion);

            int totalCost = promotionEngine.checkout(cart);

            Assert.AreEqual(230, totalCost);
        }
Exemplo n.º 8
0
        public int CalculateFinalResult(int skuId, int promptioid, int orderUnit)
        {
            int calculateValue = 0;

            try
            {
                blActivePromotion blActivePromotion = new blActivePromotion();
                ActivePromotion   activePromotion   = blActivePromotion.GetActivePromotion(promptioid);

                blSkuIdPrice blSkuIdPrice = new blSkuIdPrice();
                SkuIdPrice   skuidPrice   = blSkuIdPrice.GetSkuIdPrice(skuId);
                //int orderUnit = Convert.ToInt32(txtUnit.Text);
                int remainingUnit = orderUnit % activePromotion.unit * skuidPrice.unitprice;
                int groupedUnit   = orderUnit / activePromotion.unit * activePromotion.Promotions;

                calculateValue = remainingUnit + groupedUnit;
            }
            catch (System.Exception)
            {
                throw;
            }
            return(calculateValue);
        }
 /// <summary>
 /// Calucate Order value with applying promotion
 /// </summary>
 /// <param name="skuOrder"></param>
 /// <param name="defaultPriceValue"></param>
 /// <param name="promotion"></param>
 /// <returns></returns>
 private int ApplyPromotion(SkuOrder skuOrder, int defaultPriceValue, ActivePromotion promotion)
 {
     return(skuOrder.Calculate(defaultPriceValue, promotion));
 }
Exemplo n.º 10
0
        private void AddProgressToPromotion(Guid accoundId, PromotionDetail promotionDetail, ActivePromotion activePromotion)
        {
            var progressDetail = _promotionDao.GetProgress(accoundId, promotionDetail.Id);

            activePromotion.Progress   = GetProgress(promotionDetail, progressDetail);
            activePromotion.UnlockGoal = GetUnlockGoal(promotionDetail);
        }
Exemplo n.º 11
0
        public long CalculateDiscountPercentage(string skuIds)
        {
            long totalPrice = 0;

            skuIds = new String(skuIds.Where(c => char.IsLetter(c)).ToArray());
            var skuIdCounts = skuIds.ToUpper().GroupBy(c => c)
                              .OrderBy(c => c.Key)
                              .ToDictionary(grp => grp.Key, grp => grp.Count());
            var activePromotion       = new ActivePromotion();
            var listOfActivePromotion = activePromotion.GetActivePromotion();

            foreach (var item in skuIdCounts)
            {
                var skuId = item.Key.ToString();
                var discountOnUnitCount = item.Value;
                var selectedSKUId       = listOfActivePromotion
                                          .Where(x => x.SKUId.Equals(skuId, StringComparison.InvariantCultureIgnoreCase))
                                          .FirstOrDefault();

                if (selectedSKUId.SKUId.Equals(skuId, StringComparison.InvariantCultureIgnoreCase))
                {
                    if (selectedSKUId.IsCombinationDiscount == false)
                    {
                        if (selectedSKUId.DiscountOnUnitCount == discountOnUnitCount)
                        {
                            totalPrice = totalPrice + selectedSKUId.DiscountUnitPrice;
                        }
                        else
                        {
                            var skuIdItem = this.GetSelectedSKUId(skuId);

                            if (discountOnUnitCount > selectedSKUId.DiscountOnUnitCount)
                            {
                                int v = discountOnUnitCount / selectedSKUId.DiscountOnUnitCount;
                                totalPrice = totalPrice + (v * selectedSKUId.DiscountUnitPrice);

                                if (discountOnUnitCount % selectedSKUId.DiscountOnUnitCount != 0)
                                {
                                    totalPrice = totalPrice + (skuIdItem.AmountPerUnit * (discountOnUnitCount % selectedSKUId.DiscountOnUnitCount));
                                }
                            }
                            else
                            {
                                totalPrice = totalPrice + (skuIdItem.AmountPerUnit * discountOnUnitCount);
                            }
                        }
                    }
                    else
                    {
                        var combinationSKUIds = selectedSKUId.CombinationSKUIds.Split(',');

                        var pre = skuIdCounts.Where(x => x.Key.ToString()
                                                    .Equals(combinationSKUIds[combinationSKUIds.Count() - 1], StringComparison.InvariantCultureIgnoreCase))
                                  .FirstOrDefault();

                        if (pre.Value > 0)
                        {
                            totalPrice = totalPrice + selectedSKUId.DiscountUnitPrice;

                            var c1 = skuIdCounts.FirstOrDefault(x => x.Key.ToString() == skuId.ToString()).Value;

                            if (c1 > 1)
                            {
                                var skuIdItem = this.GetSelectedSKUId(skuId.ToString());
                                totalPrice = totalPrice + ((c1 - 1) * skuIdItem.AmountPerUnit);
                            }

                            var c2 = skuIdCounts.FirstOrDefault(x => x.Key.ToString() == pre.Key.ToString()).Value;

                            if (c2 > 1)
                            {
                                var skuIdItem = this.GetSelectedSKUId(pre.Key.ToString());
                                totalPrice = totalPrice + ((c2 - 1) * skuIdItem.AmountPerUnit);
                            }

                            break;
                        }
                        else
                        {
                            var skuIdItem = this.GetSelectedSKUId(skuId);
                            totalPrice = totalPrice + skuIdItem.AmountPerUnit;
                        }
                    }
                }
                else
                {
                }
            }

            return(totalPrice);
        }