示例#1
0
        public void When_constructed()
        {
            var sut = new PromotionalDiscount(5m, "discount note");

            Assert.That(sut.DiscountedSubTotal, Is.EqualTo(5m));
            Assert.That(sut.DiscountNote, Is.EqualTo("discount note"));
        }
        public void When_promotion_applies()
        {
            var rule = new Mock<IPromotionalDiscountRule>();
            rule.Setup(p => p.PromotionApplies(It.IsAny<LineItem>())).Returns(true);
            var promotionalDiscount = new PromotionalDiscount(7m, "discount note");
            rule.Setup(p => p.Calculate(It.IsAny<LineItem>())).Returns(promotionalDiscount);
            var sut = new Promotions(new List<IPromotionalDiscountRule>() { rule.Object });
            var productCatalog = new ProductCatalog();
            productCatalog.AddProduct("apple", 0.75m);
            var lineItem = new LineItem("apple", 7, productCatalog, sut);

            var actual = sut.CalculatePromotionalCost(lineItem);
            Assert.That(actual.DiscountedSubTotal, Is.EqualTo(7m));
            Assert.That(actual.DiscountNote, Is.EqualTo("discount note"));
        }
        public PromotionalDiscount Calculate(LineItem lineItem)
        {
            var discount = GetCurrentDiscount(lineItem);
            var hasDiscount = PromotionApplies(lineItem);
            var discountedSubTotal = lineItem.SubTotal;
            var discountNote = "";
            if (hasDiscount)
            {
                var applicableQuantity = lineItem.Quantity / discount.DiscountQuantity;
                discountedSubTotal = applicableQuantity * discount.DiscountPrice + (lineItem.Quantity - applicableQuantity * discount.DiscountQuantity) * lineItem.PricePerUnit;
                discountNote = $"***Discount on {lineItem.Barcode}: Buy {discount.DiscountQuantity} {lineItem.Barcode} for {discount.DiscountPrice:C2}, New Price {discountedSubTotal:C2}, Savings {(lineItem.SubTotal - discountedSubTotal):C2}";
            }

            var promotionalDiscount = new PromotionalDiscount(discountedSubTotal, discountNote);
            return promotionalDiscount;
        }
        public PromotionalDiscount Calculate(LineItem lineItem)
        {
            var discount = GetCurrentDiscount(lineItem);
            var hasDiscount = discount != null;
            var discountedSubTotal = lineItem.DiscountedSubTotal;
            var note = "";
            if (hasDiscount)
            {
                var discountPriceCount = lineItem.Quantity / (discount.QuantityFullPrice + discount.QuantityDiscounted) * discount.QuantityDiscounted;
                discountedSubTotal = lineItem.Quantity * lineItem.PricePerUnit - discountPriceCount * (decimal)discount.DiscountPercentage / 100m * lineItem.PricePerUnit;
                note = $"***Discount on apple: Buy {discount.QuantityFullPrice} {lineItem.Barcode} get {discount.QuantityDiscounted} at {(lineItem.PricePerUnit * (100m - (decimal)discount.DiscountPercentage) / 100m):C2}, New Price {discountedSubTotal:C2}, Savings {(lineItem.SubTotal - discountedSubTotal):C2}";
            }

            var promotionalDiscount = new PromotionalDiscount(discountedSubTotal, note);
            return promotionalDiscount;
        }
示例#5
0
        public IHttpActionResult CreateList(List <PromotionalDiscountModel> list)
        {
            foreach (var item in list)
            {
                PromotionalDiscount model = new PromotionalDiscount();
                model.DiscountBranches = item.BranchList.Select(x => new DiscountBranch
                {
                    BranchID = Convert.ToInt16(x),
                    IsActive = true
                }).ToList();

                db.PromotionalDiscounts.Add(model);
                db.SaveChanges();
            }
            return(Ok(true));
        }
        public PromotionalDiscount Calculate(LineItem lineItem)
        {
            var discount           = GetCurrentDiscount(lineItem);
            var hasDiscount        = discount != null;
            var discountedSubTotal = lineItem.DiscountedSubTotal;
            var note = "";

            if (hasDiscount)
            {
                var discountPriceCount = lineItem.Quantity / (discount.QuantityFullPrice + discount.QuantityDiscounted) * discount.QuantityDiscounted;
                discountedSubTotal = lineItem.Quantity * lineItem.PricePerUnit - discountPriceCount * (decimal)discount.DiscountPercentage / 100m * lineItem.PricePerUnit;
                note = $"***Discount on apple: Buy {discount.QuantityFullPrice} {lineItem.Barcode} get {discount.QuantityDiscounted} at {(lineItem.PricePerUnit * (100m - (decimal)discount.DiscountPercentage) / 100m):C2}, New Price {discountedSubTotal:C2}, Savings {(lineItem.SubTotal - discountedSubTotal):C2}";
            }

            var promotionalDiscount = new PromotionalDiscount(discountedSubTotal, note);

            return(promotionalDiscount);
        }
        public PromotionalDiscount Calculate(LineItem lineItem)
        {
            var discount           = GetCurrentDiscount(lineItem);
            var hasDiscount        = PromotionApplies(lineItem);
            var discountedSubTotal = lineItem.SubTotal;
            var discountNote       = "";

            if (hasDiscount)
            {
                var applicableQuantity = lineItem.Quantity / discount.DiscountQuantity;
                discountedSubTotal = applicableQuantity * discount.DiscountPrice + (lineItem.Quantity - applicableQuantity * discount.DiscountQuantity) * lineItem.PricePerUnit;
                discountNote       = $"***Discount on {lineItem.Barcode}: Buy {discount.DiscountQuantity} {lineItem.Barcode} for {discount.DiscountPrice:C2}, New Price {discountedSubTotal:C2}, Savings {(lineItem.SubTotal - discountedSubTotal):C2}";
            }

            var promotionalDiscount = new PromotionalDiscount(discountedSubTotal, discountNote);

            return(promotionalDiscount);
        }
示例#8
0
        public void When_promotion_applies()
        {
            var rule = new Mock <IPromotionalDiscountRule>();

            rule.Setup(p => p.PromotionApplies(It.IsAny <LineItem>())).Returns(true);
            var promotionalDiscount = new PromotionalDiscount(7m, "discount note");

            rule.Setup(p => p.Calculate(It.IsAny <LineItem>())).Returns(promotionalDiscount);
            var sut = new Promotions(new List <IPromotionalDiscountRule>()
            {
                rule.Object
            });
            var productCatalog = new ProductCatalog();

            productCatalog.AddProduct("apple", 0.75m);
            var lineItem = new LineItem("apple", 7, productCatalog, sut);

            var actual = sut.CalculatePromotionalCost(lineItem);

            Assert.That(actual.DiscountedSubTotal, Is.EqualTo(7m));
            Assert.That(actual.DiscountNote, Is.EqualTo("discount note"));
        }
 public void When_constructed()
 {
     var sut = new PromotionalDiscount(5m, "discount note");
     Assert.That(sut.DiscountedSubTotal, Is.EqualTo(5m));
     Assert.That(sut.DiscountNote, Is.EqualTo("discount note"));
 }