예제 #1
0
        bool IsVoucherThresholdMet(OfferVoucher voucher)
        {
            bool          isValid           = false;
            const decimal thresholdBoundary = 0.01m;
            decimal       adjustedPrice     = _originalPrice;

            List <Product> invalidThresholdItems = BasketItems.Where(w => w.Category == "Gift Vouchers").ToList();

            if (invalidThresholdItems.Count > 0)
            {
                decimal totalInvalidCost = 0;
                foreach (Product product in invalidThresholdItems)
                {
                    totalInvalidCost = totalInvalidCost + product.Price;
                }
                adjustedPrice = (adjustedPrice - totalInvalidCost);
            }

            decimal remainingCostToReachThreshold = (voucher.Threshold - adjustedPrice) + thresholdBoundary;

            if ((invalidThresholdItems.Count() == 0 && _originalPrice > voucher.Threshold) || remainingCostToReachThreshold <= 0)
            {
                isValid = true;
            }
            else
            {
                _errorMessage = $"You have not reached the spend threshold for the voucher {voucher.VoucherCode}. Spend another £{remainingCostToReachThreshold} to receive the £{voucher.Discount} discount from your basket total.";
            }

            return(isValid);
        }
예제 #2
0
        bool IsCategoryOfferVoucherValid(CategoryOfferVoucher voucher)
        {
            bool isValid = false;

            if (IsVoucherThresholdMet(voucher))
            {
                List <Product> qualifyingProducts = BasketItems.Where(w => w.Category == voucher.ProductCategory).ToList();

                if (qualifyingProducts.Count > 0)
                {
                    decimal qualifyingProductsTotalPrice = 0;
                    decimal adjustedProductDiscount      = 0;

                    foreach (Product product in qualifyingProducts)
                    {
                        qualifyingProductsTotalPrice = qualifyingProductsTotalPrice + product.Price;
                    }
                    if (voucher.Discount > qualifyingProductsTotalPrice)
                    {
                        adjustedProductDiscount = adjustedProductDiscount + qualifyingProductsTotalPrice;
                    }

                    isValid        = true;
                    _discountTotal = _discountTotal + adjustedProductDiscount;
                }
                else
                {
                    _errorMessage = $"There are no products in your basket applicable to voucher {voucher.VoucherCode}.";
                }
            }
            return(isValid);
        }