Exemplo n.º 1
0
        public void Test_CalculateDiscount_Scenario1()
        {
            List <int> currentBasketProducts = new List <int>()
            {
                butter.Id, butter.Id, butter.Id,
                milk.Id,
                bread.Id, bread.Id
            };

            decimal expected    = 0.5M;
            var     discountDTO = _discountService.CalculateDiscount(currentBasketProducts);

            Assert.AreEqual(expected, discountDTO.TotalDiscount);
        }
        public BasketDTO AddProduct(ProductInsertDTO productInsertDTO)
        {
            if (productInsertDTO.ProductId > 0)
            {
                var newProductDTO = GetProductById(productInsertDTO.ProductId);

                for (int i = 0; i < productInsertDTO.Amount; i++)
                {
                    productInsertDTO.CurrentBasketProducts.Add(newProductDTO);
                }
            }

            decimal totalSum      = productInsertDTO.CurrentBasketProducts.Select(x => x.Price).Sum();
            var     productIdList = productInsertDTO.CurrentBasketProducts.Select(x => x.Id).ToList();
            var     discountDTO   = _discountService.CalculateDiscount(productIdList);

            BasketDTO basketDTO = new BasketDTO()
            {
                CurrentBasketProducts = productInsertDTO.CurrentBasketProducts,
                DiscountDTO           = discountDTO,
                TotalCost             = totalSum - discountDTO.TotalDiscount
            };

            _logService.LogBasketDetails(basketDTO);

            return(basketDTO);
        }
Exemplo n.º 3
0
        public float CalculateTotalCost(BasketModel basketModel)
        {
            var totalCost = basketModel.Items
                            .Where(t => t.Amount > 0)
                            .Sum(t => t.Amount * t.Product.Price);
            var totalDisc = _discountService.CalculateDiscount(basketModel);

            return(totalCost - totalDisc);
        }
Exemplo n.º 4
0
        public ProductDto ApplyDiscount(Entity entity, int amount)
        {
            var productDto = _mapper.Map <ProductDto>(entity);

            if (productDto.Price.HasValue && productDto.Quantity >= amount)
            {
                productDto.Quantity = amount;
                productDto.Price    = productDto.Price - _discountService.CalculateDiscount(productDto.Price.Value, amount);
            }
            return(productDto);
        }
Exemplo n.º 5
0
        public DraftId Choose(string offerIdString, string tenantIdString, DateTime from, DateTime to)
        {
            var offerId  = OfferId.From(offerIdString);
            var offer    = _offerRepository.Get(offerId);
            var tenantId = TenantId.From(tenantIdString);
            var period   = Period.From(from, to);

            var discount     = _discountService.CalculateDiscount(tenantIdString, offerIdString, from, to);
            var draftFactory = new DraftFactory(discount, new DefaultDraftNumberGenerator());
            var draft        = offer.Choose(tenantId, period, draftFactory);

            _draftRepository.Save(draft);

            return(draft.Id);
        }
Exemplo n.º 6
0
        public async Task CreateOrderAsync(string userName)
        {
            var user = await _users.GetUserByUserName(userName);

            var allItems = await _items.Find(x => x.ShopCartId == user.ShopCartId);

            if (allItems.Count() == 0)
            {
                throw new ArgumentNullException("ShopCart does not contain purchases");
            }

            Order newOrder = new Order()
            {
                User = user
            };

            foreach (var it in allItems)
            {
                newOrder.Details.Add(new OrderDetail()
                {
                    ProductId = it.ProductId,
                    TotalCost = it.Product.Price * it.Quantity,
                    Quantity  = it.Quantity
                });
            }

            newOrder.TotalCost = allItems.Sum(x => x.Quantity * x.Product.Price);

            var userRoles = await _users.GetUserRoles(user);

            newOrder.TotalCost *= _discount.CalculateDiscount(userRoles);

            await _orders.AddOrderAsync(newOrder);

            await _items.DeleteRangeAsync(allItems);
        }
Exemplo n.º 7
0
 public double CalculateOrderPriceWithDiscount() => this.Value - _discountService.CalculateDiscount(this.Value);