Esempio n. 1
0
        private void CalculateBuyProductGetDiscountOnSecondProductOffer(Product product, SpecialOffer offer, Basket basket, Guid transactionId)
        {
            _logger.LogInformation($"{transactionId} - Calculating BuyProductGetDiscountOnSecondProductOffer for {product.ProductName}");

            int primaryItemCount;
            int SecondaryItemCount;

            primaryItemCount = basket.BasketItems[product];

            var secondaryProduct = basket.BasketItems.Keys.Where(k => k.ProductName == offer.SecondaryProduct).SingleOrDefault();

            if (secondaryProduct != null)
            {
                SecondaryItemCount = basket.BasketItems[secondaryProduct];

                // keep calculating offers until we run out of products that match
                while (primaryItemCount >= offer.PrimaryProductCount && SecondaryItemCount > 0)
                {
                    var totalDiscount = secondaryProduct.UnitPrice * ((float)offer.PercentageDiscount / 100);

                    basket.AddOffer((int)totalDiscount
                                    , $"Buy {offer.PrimaryProductCount} {product.ProductName}, get {offer.PercentageDiscount}% off {secondaryProduct.ProductName}: -{totalDiscount}p");

                    primaryItemCount -= offer.PrimaryProductCount;
                    SecondaryItemCount--;
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Calculate Percentage discount offers - A multibuy offer is one in which one or more of a single product
        /// </summary>
        /// <param name="product"></param>
        /// <param name="offer"></param>
        /// <param name="basket"></param>
        /// <param name="transactionId"></param>
        private void CalculatePercentageDiscount(Product product, SpecialOffer offer, Basket basket, Guid transactionId)
        {
            _logger.LogInformation($"{transactionId} - Calculating Percentage discount offer for {product.ProductName}");

            if (DateTime.Now < offer.ExpiryDate)
            {
                var itemCount     = basket.BasketItems[product];
                var totalDiscount = itemCount * product.UnitPrice * ((float)offer.PercentageDiscount / 100);

                basket.AddOffer((int)totalDiscount, $"{product.ProductName} {offer.PercentageDiscount}% off: -{totalDiscount}p");
            }
        }