private async Task UpdateDiscountMappingsAsync(Product product, List <int> passedDiscountIds) { if (passedDiscountIds == null) { return; } var allDiscounts = await DiscountService.GetAllDiscountsAsync(DiscountType.AssignedToSkus, showHidden : true); var appliedProductDiscount = await DiscountService.GetAppliedDiscountsAsync(product); foreach (var discount in allDiscounts) { if (passedDiscountIds.Contains(discount.Id)) { //new discount if (appliedProductDiscount.Count(d => d.Id == discount.Id) == 0) { appliedProductDiscount.Add(discount); } } else { //remove discount if (appliedProductDiscount.Count(d => d.Id == discount.Id) > 0) { appliedProductDiscount.Remove(discount); } } } await _productService.UpdateProductAsync(product); await _productService.UpdateHasDiscountsAppliedAsync(product); }
public void ShouldThrownArgumentNullExceptionForNullDiscount( DiscountService sut) { Action a = () => sut.AddProductDiscount(null !); a.Should().Throw <ArgumentNullException>(); }
public void ShouldBe0WhenNoProductsToDiscount( DiscountService sut) { Price result = sut.ApplyDiscounts(Enumerable.Empty <IProduct>()); result.Should().Be(0.0m); }
public ActionResult SaveNewRule(int ID, string systemName, string prefix) { var rules = DiscountService.GetRules(); if (string.IsNullOrEmpty(systemName) || !rules.ContainsKey(systemName)) { return(Content(RP.T("Admin.Discount.RuleNotFound").ToString())); } var rule = rules[systemName]; var saved = rule.GetConfigItem(); MapModel(saved, prefix); System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer(); var discount = _db.Discounts.FirstOrDefault(x => x.ID == ID); if (discount != null) { discount.RuleList.Add(new JsonKeyValue() { Name = systemName, Value = js.Serialize(saved) }); } //js.Serialize(); discount.RulesData = js.Serialize(discount.RuleList); _db.SaveChanges(); return(Json(new { result = "ok" })); }
private void UpdateDiscountMappings(Product product, List <int> passedDiscountIds) { if (passedDiscountIds == null) { return; } var allDiscounts = DiscountService.GetAllDiscounts(DiscountType.AssignedToSkus, showHidden: true); foreach (var discount in allDiscounts) { if (passedDiscountIds.Contains(discount.Id)) { //new discount if (product.AppliedDiscounts.Count(d => d.Id == discount.Id) == 0) { product.AppliedDiscounts.Add(discount); } } else { //remove discount if (product.AppliedDiscounts.Count(d => d.Id == discount.Id) > 0) { product.AppliedDiscounts.Remove(discount); } } } _productService.UpdateProduct(product); _productService.UpdateHasDiscountsApplied(product); }
public async Task FirstControllerTest() { var discountService = new DiscountService(); //var mockItemRepository = new ItemRepository(); var mockItemRepository = new Mock <IItemRepository>(); mockItemRepository.Setup(m => m.GetAll()).Returns(new List <Item>() { new Item() { Name = "Test" }, new Item() { Name = "Test2" } }); var controller = new ItemController(mockItemRepository.Object, discountService); var result = await controller.GetAll(); result.Value.Should().NotBeEmpty(); result.Value.Should().HaveCount(2); }
public void ComputeTaxForItemWiseTaxCalulator() { IDiscountService discountService = new DiscountService(); ITaxService taxService = new TaxCalculatorService("IO"); IShoppingCartService scs = new ShoppingCartService(discountService); var cs = new CustomerService(scs); var custId = cs.CreateCustomer("Paniraj N"); var shoppingCart = cs.CreateShoppingCart(custId); var ps = new ProductService(); var pA = ps.CreateProduct("A", "product A", 50, ProductCategory.Basic); var pB = ps.CreateProduct("B", "product B", 30, ProductCategory.Luxury); var pC = ps.CreateProduct("C", "product C", 20, ProductCategory.Luxury); var discountA = discountService.CreateDiscount("Discount on A", "Discount if Purchase 3 As"); var pAs = new KeyValuePair <Product, int>(pA, 3); var discountCombinationAs = new List <KeyValuePair <Product, int> >(); discountCombinationAs.Add(pAs); var dci = new DiscountCombinationItems(discountCombinationAs, 20, 0); discountService.CreateDiscountCombinationItems(discountA.Id, dci); scs.AddItem(shoppingCart, pA, 1); scs.AddItem(shoppingCart, pB, 1); scs.AddItem(shoppingCart, pC, 1); shoppingCart = scs.ComputeBill(shoppingCart); taxService.CalculateTax(shoppingCart); //There is no discount applicable for this card, cart value is 100, Basic Tax is 10. Assert.AreEqual(15, shoppingCart.TaxAmount); }
public ShoppingCartModel(ShoppingCartService shoppingService, IOfferRepository offerRepo, DiscountService discountService) { ItemsInCart = shoppingService; OItems = new List <OrderItem>(); OfferRepository = offerRepo; discount = discountService; }
public async Task Should_Not_Return_Discount_For__Invalid_Shurikens() { //Arrange var expected = GetCompiledRuleDictionary(); Product product = new Product { Id = "1", Name = "Product1", Description = "Test Desc1", Price = 2, ProductType = ProductType.Shurikens }; var lineItem = new LineItem { Product = product, Quantity = 99 }; var lineItems = new List <LineItem> { lineItem }; var mockDiscountFactory = new Mock <IDiscountFactory>(); var mockRuleService = new Mock <IRuleService>(); mockRuleService.Setup(x => x.GetCompiledRuleDictionary()).Returns(expected); mockDiscountFactory.Setup(x => x.GetDiscount(It.IsAny <ProductType>())).Returns( new BuyXGetYDiscountOffWhole( "Buy 100 or more Shurikens and get 30% off whole basket.", new List <ProductType> { ProductType.Shurikens }, 0.3m, 100)); var discountService = new DiscountService(mockRuleService.Object, mockDiscountFactory.Object); // Act var results = await discountService.GetDiscounts(lineItems); //Assert Assert.AreEqual(results.Count(), 0); }
public void Setup() { _discountRule1 = new Mock <IDiscountRule>(); _discountRule2 = new Mock <IDiscountRule>(); _discountService = new DiscountService(new[] { _discountRule1.Object, _discountRule2.Object }); }
public DiscountServiceTest( StripeMockFixture stripeMockFixture, MockHttpClientFixture mockHttpClientFixture) : base(stripeMockFixture, mockHttpClientFixture) { this.service = new DiscountService(this.StripeClient); }
static void Main(string[] args) { Console.WriteLine("Enter Customer Id"); int customerId = Int32.Parse(Console.ReadLine()); while (customerId.ToString() == null) { Console.WriteLine("Enter Customer Id"); customerId = Int32.Parse(Console.ReadLine()); } var stockService = new StockService(new StockRepository()); var discountService = new DiscountService(new DiscountRepository()); var paymentGateway = new PaymentRepository(new PaymentGatewayClient(new System.Net.Http.HttpClient())); List <string> items = new List <string>() { "Breads", "Milk", "Biscuits", "DryFruits" }; ShoppingService shopping = new ShoppingService(customerId, stockService, discountService, paymentGateway, new Logger()); var count = shopping.GetItemsToBuy(items); var resutl = count > 0 ? $"Shopping done for {count} products" : $"Shopping done for {count} products"; Console.WriteLine(resutl); Console.ReadLine(); }
public CheckoutController(ILogger <CheckoutController> log, PaymentService pagamentoService, DiscountService discountService, VoucherService voucherService) { this.log = log; this.pagamentoService = pagamentoService; this.discountService = discountService; this.voucherService = voucherService; }
public IEnumerable <Order> GetDiscount() { DiscountService b = new DiscountService(10); IEnumerable <Order> g = GetAll(); return(b.GetDiscount(g, 1)); }
public void ReturnNoDiscountValueIfDiscountIsZero() { var discount = new Discount() { Id = 1, Code = "A", Price = new Price() { Amount = 0, Currency = "GBP", Symbol = "£" }, Description = "Three Quantity of Product A Cost £1.50", IsActive = true, Quantity = 3, IsMultiBuy = true }; var _mockShoppingBasketWithZeroDiscount = _mockShoppingBasket; _mockShoppingBasketWithZeroDiscount.SetupGet(x => x.Products).Returns(new List <Product>() { GetSingleProduct(0.0m) }); var discountService = new DiscountService(_mockShoppingBasketWithZeroDiscount.Object); Assert.Equal(Discount.Validator.NoDiscount, discountService.ApplyDiscount()); }
public async Task Should_Not_Return_Discount_For_Invalid_LargeBowl() { //Arrange var expected = GetCompiledRuleDictionary(); Product product = new Product { Id = "1", Name = "Product1", Description = "Test Desc1", Price = 2, ProductType = ProductType.Bags }; var lineItem = new LineItem { Product = product, Quantity = 0 }; var lineItems = new List <LineItem> { lineItem }; var mockDiscountFactory = new Mock <IDiscountFactory>(); var mockRuleService = new Mock <IRuleService>(); mockRuleService.Setup(x => x.GetCompiledRuleDictionary()).Returns(expected); mockDiscountFactory.Setup(x => x.GetDiscount(It.IsAny <ProductType>())).Returns( new BuyXGetYForFree( "Buy a Large bowl of Trifle and get a free Paper Mask.", new List <ProductType> { ProductType.LargeBowl }, 1, ProductType.PaperMask, 1)); var discountService = new DiscountService(mockRuleService.Object, mockDiscountFactory.Object); // Act var results = await discountService.GetDiscounts(lineItems); //Assert Assert.AreEqual(results.Count(), 0); }
private async Task UpdateDiscountsAsync(Category category, List <int> passedDiscountIds) { if (passedDiscountIds == null) { return; } var allDiscounts = await DiscountService.GetAllDiscountsAsync(DiscountType.AssignedToCategories, showHidden : true); var appliedCategoryDiscount = await DiscountService.GetAppliedDiscountsAsync(category); foreach (var discount in allDiscounts) { if (passedDiscountIds.Contains(discount.Id)) { //new discount if (appliedCategoryDiscount.Count(d => d.Id == discount.Id) == 0) { appliedCategoryDiscount.Add(discount); } } else { //remove discount if (appliedCategoryDiscount.Count(d => d.Id == discount.Id) > 0) { appliedCategoryDiscount.Remove(discount); } } } await _categoryService.UpdateCategoryAsync(category); }
public async Task Should_Not_Return_Discount_For_Invalid_Bags() { //Arrange var expected = GetCompiledRuleDictionary(); Product product = new Product { Id = "1", Name = "Product1", Description = "Test Desc1", Price = 2, ProductType = ProductType.Bags }; var lineItem = new LineItem { Product = product, Quantity = 1 }; var lineItems = new List <LineItem> { lineItem }; var mockDiscountFactory = new Mock <IDiscountFactory>(); var mockRuleService = new Mock <IRuleService>(); mockRuleService.Setup(x => x.GetCompiledRuleDictionary()).Returns(expected); mockDiscountFactory.Setup(x => x.GetDiscount(It.IsAny <ProductType>())).Returns( new BuyXGetYDiscountOffEachExceptFirst( "Buy 2 or more Bags of Pogs and get 50% off each bag (excluding the first one).", new List <ProductType> { ProductType.Bags }, 0.5m, 2)); var discountService = new DiscountService(mockRuleService.Object, mockDiscountFactory.Object); // Act var results = await discountService.GetDiscounts(lineItems); //Assert Assert.AreEqual(results.Count(), 0); }
public void ComputeBillScenarioATest() { IDiscountService discountService = new DiscountService(); IShoppingCartService scs = new ShoppingCartService(discountService); var cs = new CustomerService(scs); var custId = cs.CreateCustomer("Paniraj N"); var shoppingCart = cs.CreateShoppingCart(custId); var ps = new ProductService(); var pA = ps.CreateProduct("A", "product A", 50, ProductCategory.Basic); var pB = ps.CreateProduct("B", "product B", 30, ProductCategory.Luxury); var pC = ps.CreateProduct("C", "product C", 20, ProductCategory.Luxury); var discountA = discountService.CreateDiscount("Discount on A", "Discount if Purchase 3 As"); var pAs = new KeyValuePair <Product, int>(pA, 3); var discountCombinationAs = new List <KeyValuePair <Product, int> >(); discountCombinationAs.Add(pAs); var dci = new DiscountCombinationItems(discountCombinationAs, 20, 0); discountService.CreateDiscountCombinationItems(discountA.Id, dci); scs.AddItem(shoppingCart, pA, 1); scs.AddItem(shoppingCart, pB, 1); scs.AddItem(shoppingCart, pC, 1); shoppingCart = scs.ComputeBill(shoppingCart); Assert.AreEqual(100, shoppingCart.TotalBillAmount - shoppingCart.TotalDiscountAmount); }
public async Task GetAll_VegetableControllerTesting() { //Arrange var mockMapper = new MapperConfiguration(cfg => { cfg.AddProfile(new MappingProfile()); }); IMapper mapper = mockMapper.CreateMapper(); var discountService = new DiscountService(); var priceCalculationService = new PriceCalculationService(discountService, mapper); var repository = new Mock <IGenericRepository <Vegetable> >(); repository.Setup(r => r.GetAll()).ReturnsAsync(new List <Vegetable>() { new Vegetable() { Price = 4M } }); var serviceBuyItem = new BuyItemService <Vegetable>(mapper, repository.Object, priceCalculationService); var vegetablesController = new VegetablesController(mapper, repository.Object, serviceBuyItem); //Act var result = vegetablesController.GetAll(); //Assert result.Should().NotBeNull(); }
public ActionResult RemoveRule(int ID, int Num, string systemName, string prefix) { var rules = DiscountService.GetRules(); if (string.IsNullOrEmpty(systemName) || !rules.ContainsKey(systemName)) { return(Content(RP.T("Admin.Discount.RuleNotFound").ToString())); } var rule = rules[systemName]; var saved = rule.GetConfigItem(); MapModel(saved); System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer(); var discount = _db.Discounts.FirstOrDefault(x => x.ID == ID); if (discount != null) { for (var i = 1; i <= discount.RuleList.Count; i++) { if (i == Num) { discount.RuleList.RemoveAt(i - 1); break; } } } //js.Serialize(); discount.RulesData = js.Serialize(discount.RuleList); _db.SaveChanges(); return(Json(new { result = "ok" })); }
private void UpdateDiscounts(Manufacturer manufacturer, List <int> passedDiscountIds) { if (passedDiscountIds == null) { return; } var allDiscounts = DiscountService.GetAllDiscounts(DiscountType.AssignedToManufacturers, showHidden: true); foreach (var discount in allDiscounts) { if (passedDiscountIds.Contains(discount.Id)) { //new discount if (manufacturer.AppliedDiscounts.Count(d => d.Id == discount.Id) == 0) { manufacturer.AppliedDiscounts.Add(discount); } } else { //remove discount if (manufacturer.AppliedDiscounts.Count(d => d.Id == discount.Id) > 0) { manufacturer.AppliedDiscounts.Remove(discount); } } } _manufacturerService.UpdateManufacturer(manufacturer); }
public ActionResult DiscountRuleList(int ID) { var discount = _db.Discounts.FirstOrDefault(x => x.ID == ID); if (discount != null) { var list = new List <JsonKeyObject>(); var rules = DiscountService.GetRules(); System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer(); foreach (var r in discount.RuleList) { if (rules.ContainsKey(r.Name)) { var ruleprovider = rules[r.Name]; var confitem = ruleprovider.GetConfigItem(); var o = js.Deserialize(r.Value, confitem.GetType()); list.Add(new JsonKeyObject() { Name = r.Name, Object = o }); } } return(PartialView(list)); } return(Content(RP.T("Admin.Discount.NotFound").ToString())); }
public void Add_IsNull_ExceptionThrown() { var sut = new DiscountService(); // Assert Assert.Throws <NullReferenceException>(() => sut.CalculateBasketDiscounts(null, null)); }
internal static void IntegrationTest() { UnitTest(); //CatalogUrlResolvingService.Use(new CatalogUrlResolvingService()); //not yet (repo) DiscountCalculationService.Actual(); DiscountService.Actual(); ProductDiscountService.Actual(); ProductDiscountRepository.SetupNewMock().Setup(m => m.GetAll(It.IsAny <ILocalization>())).Returns(new List <DiscountProduct>()); OrderUpdatingService.Actual(); OrderService.Actual(); UrlRewritingService.Actual(); VATCheckService.Use(new ViesVatCheckService()); //StoreService.Actual(); // todo! //CountriesRepository.Use(new UwebshopApplicationCachedCountriesRepository()); // werken nog niet ivm HtppContext.Current //VATCountriesRepository.Use(new UwebshopApplicationCachedVATCountriesRepository()); //ProductService.Actual(); // maybe move Localize somewhere else :'( StoreFromUrlDeterminationService.Actual(); OrderDiscountService.Actual(); UrlFormatService.Actual(); ProductUrlService.Actual(); CategoryCatalogUrlService.Actual(); UrlLocalizationService.Actual(); UrlService.Actual(); InitializeServiceLocators(); }
public void RandomProductFor_PercentageDiscount_CalculateAppliedDiscount_With10PercentDiscount(List <Product> products, PercentageDiscount percentageDiscount) { var discountRate = 0.10m; // Arrange percentageDiscount._percentage = discountRate; var productNames = products.Select(x => x.Name).ToList(); productNames.Add(percentageDiscount._discountedProduct.Name); var disconts = new List <IDiscount>(); disconts.Add(percentageDiscount); _productService.Setup(x => x.GetProductByName(percentageDiscount._discountedProduct.Name)).Returns(percentageDiscount._discountedProduct); foreach (var item in products) { _productService.Setup(x => x.GetProductByName(item.Name)).Returns(item); } var sut = new ShoppingBasketService(_productService.Object); var basket = sut.AddProducts(productNames.ToArray()); var sutD = new DiscountService(); // Act var result = sutD.CalculateBasketDiscounts(basket, disconts); var subTotalPrice = basket.Sum(item => item.Product.Price * item.Quantity); var totalPrice = subTotalPrice - result.Sum(item => item.Amount); var discontMessage = string.Join(" --- ", result.Select(x => x.Description)); // Assert Assert.Equal(subTotalPrice - totalPrice, percentageDiscount._discountedProduct.Price * percentageDiscount._percentage); }
public void ShouldThrownArguementNullExceptionForNullProducts( DiscountService sut) { Action a = () => sut.ApplyDiscounts(null !); a.Should().Throw <ArgumentNullException>(); }
public void PercentageDiscount_CalculateAppliedDiscount_HalfPriceDiscount() { var basket = new List <ProductQuantity>(); var discounts = new List <IDiscount>(); var product = new Product() { Id = 1, Name = "Apples", Price = 100m }; var productQuantity = new ProductQuantity() { Quantity = 1, Product = product }; basket.Add(productQuantity); var percentageDiscount = new HalfPriceDiscount(product, productQuantity); discounts.Add(percentageDiscount); var sut = new DiscountService(); // Act var result = sut.CalculateBasketDiscounts(basket, discounts); // Assert Assert.Equal(result.Any(), true); Assert.Equal(result[0].Type, DiscountType.HalfPrice); Assert.Equal(result[0].Amount, 50.00m); Assert.Equal(result[0].Description, "Apples 50% OFF: - 50p"); }
private void UpdateDiscounts(Category category, List <int> passedDiscountIds) { if (passedDiscountIds == null) { return; } var allDiscounts = DiscountService.GetAllDiscounts(DiscountType.AssignedToCategories, showHidden: true); foreach (var discount in allDiscounts) { if (passedDiscountIds.Contains(discount.Id)) { //new discount if (category.AppliedDiscounts.Count(d => d.Id == discount.Id) == 0) { category.AppliedDiscounts.Add(discount); } } else { //remove discount if (category.AppliedDiscounts.Count(d => d.Id == discount.Id) > 0) { category.AppliedDiscounts.Remove(discount); } } } _categoryService.UpdateCategory(category); }
public ActionResult DiscountList(DiscountListVM model) { /* if(model.PriceType_TC.IsNullOrEmpty()) * { * ModelState.AddModelError(model.For(x => x.PriceType_TC), "�ב�עוכ�םמו ןמכו"); * model.Discounts = new List<Discount>(); * return View(model); * * }*/ model.Student = StudentService.GetAll().ByPrimeryKey(model.StudentID); // model.Group = GroupService.GetAll().ByPrimeryKey(model.GroupID); var orderDetails = new OrderDetail { Course_TC = model.Group != null ? model.Group.Course_TC : null, Group = model.Group, PriceType_TC = "��", }; orderDetails.Order = new Order(); model.Discounts = DiscountService.GetDiscountsFor(orderDetails, model.Student); return(View(model)); }