public void Can_calculate_discount_amount_percentage() { var discount = new Discount() { UsePercentage = true, DiscountPercentage= 30 }; discount.GetDiscountAmount(100).ShouldEqual(30); discount.DiscountPercentage = 60; discount.GetDiscountAmount(200).ShouldEqual(120); }
public void Can_calculate_discount_amount_fixed() { var discount = new Discount() { UsePercentage = false, DiscountAmount = 10 }; discount.GetDiscountAmount(100).ShouldEqual(10); discount.DiscountAmount = 20; discount.GetDiscountAmount(200).ShouldEqual(20); }
public void Maximum_discount_amount_is_used() { var discount = new Discount { UsePercentage = true, DiscountPercentage = 30, MaximumDiscountAmount = 3.4M }; discount.GetDiscountAmount(100).ShouldEqual(3.4M); discount.DiscountPercentage = 60; discount.GetDiscountAmount(200).ShouldEqual(3.4M); discount.GetDiscountAmount(100).ShouldEqual(3.4M); discount.DiscountPercentage = 1; discount.GetDiscountAmount(200).ShouldEqual(2); }
/// <summary> /// Gets discount amount /// </summary> /// <param name="productVariant">Product variant</param> /// <param name="customer">The customer</param> /// <param name="additionalCharge">Additional charge</param> /// <param name="quantity">Product quantity</param> /// <param name="appliedDiscount">Applied discount</param> /// <returns>Discount amount</returns> public virtual decimal GetDiscountAmount(ProductVariant productVariant, Customer customer, decimal additionalCharge, int quantity, out Discount appliedDiscount) { appliedDiscount = null; decimal appliedDiscountAmount = decimal.Zero; //we don't apply discounts to products with price entered by a customer if (productVariant.CustomerEntersPrice) return appliedDiscountAmount; appliedDiscount = GetPreferredDiscount(productVariant, customer, additionalCharge, quantity); if (appliedDiscount != null) { decimal finalPriceWithoutDiscount = GetFinalPrice(productVariant, customer, additionalCharge, false, quantity); appliedDiscountAmount = appliedDiscount.GetDiscountAmount(finalPriceWithoutDiscount); } return appliedDiscountAmount; }
/// <summary> /// Gets an order discount (applied to order total) /// </summary> /// <param name="customer">Customer</param> /// <param name="orderTotal">Order total</param> /// <param name="appliedDiscount">Applied discount</param> /// <returns>Order discount</returns> protected virtual decimal GetOrderTotalDiscount(Customer customer, decimal orderTotal, out Discount appliedDiscount) { appliedDiscount = null; decimal discountAmount = decimal.Zero; if (_catalogSettings.IgnoreDiscounts) return discountAmount; var allDiscounts = _discountService.GetAllDiscounts(DiscountType.AssignedToOrderTotal); var allowedDiscounts = new List<Discount>(); if (allDiscounts != null) foreach (var discount in allDiscounts) if (_discountService.IsDiscountValid(discount, customer) && discount.DiscountType == DiscountType.AssignedToOrderTotal && !allowedDiscounts.ContainsDiscount(discount)) allowedDiscounts.Add(discount); appliedDiscount = allowedDiscounts.GetPreferredDiscount(orderTotal); if (appliedDiscount != null) discountAmount = appliedDiscount.GetDiscountAmount(orderTotal); if (discountAmount < decimal.Zero) discountAmount = decimal.Zero; if (_shoppingCartSettings.RoundPricesDuringCalculation) discountAmount = Math.Round(discountAmount, 2); return discountAmount; }
/// <summary> /// Gets discount amount /// </summary> /// <param name="product">Product</param> /// <param name="customer">The customer</param> /// <param name="productPriceWithoutDiscount">Already calculated product price without discount</param> /// <param name="appliedDiscount">Applied discount</param> /// <returns>Discount amount</returns> protected virtual decimal GetDiscountAmount(Product product, Customer customer, decimal productPriceWithoutDiscount, out Discount appliedDiscount) { if (product == null) throw new ArgumentNullException("product"); appliedDiscount = null; decimal appliedDiscountAmount = decimal.Zero; //we don't apply discounts to products with price entered by a customer if (product.CustomerEntersPrice) return appliedDiscountAmount; //discounts are disabled if (_catalogSettings.IgnoreDiscounts) return appliedDiscountAmount; var allowedDiscounts = GetAllowedDiscounts(product, customer); //no discounts if (allowedDiscounts.Count == 0) return appliedDiscountAmount; appliedDiscount = allowedDiscounts.GetPreferredDiscount(productPriceWithoutDiscount); if (appliedDiscount != null) appliedDiscountAmount = appliedDiscount.GetDiscountAmount(productPriceWithoutDiscount); return appliedDiscountAmount; }
/// <summary> /// Gets discount amount /// </summary> /// <param name="product">Product</param> /// <param name="customer">The customer</param> /// <param name="additionalCharge">Additional charge</param> /// <param name="quantity">Product quantity</param> /// <param name="appliedDiscount">Applied discount</param> /// <returns>Discount amount</returns> public virtual decimal GetDiscountAmount(Product product, Customer customer, decimal additionalCharge, int quantity, out Discount appliedDiscount) { if (product == null) throw new ArgumentNullException("product"); appliedDiscount = null; decimal appliedDiscountAmount = decimal.Zero; //we don't apply discounts to products with price entered by a customer if (product.CustomerEntersPrice) return appliedDiscountAmount; //discount are disabled if (_catalogSettings.IgnoreDiscounts) return appliedDiscountAmount; var allowedDiscounts = GetAllowedDiscounts(product, customer); //no discounts if (allowedDiscounts.Count == 0) return appliedDiscountAmount; decimal finalPriceWithoutDiscount = GetFinalPrice(product, customer, additionalCharge, false, quantity); appliedDiscount = allowedDiscounts.GetPreferredDiscount(finalPriceWithoutDiscount); if (appliedDiscount != null) appliedDiscountAmount = appliedDiscount.GetDiscountAmount(finalPriceWithoutDiscount); return appliedDiscountAmount; }
/// <summary> /// Gets a shipping discount /// </summary> /// <param name="customer">Customer</param> /// <param name="shippingTotal">Shipping total</param> /// <param name="appliedDiscount">Applied discount</param> /// <returns>Shipping discount</returns> protected virtual decimal GetShippingDiscount(Customer customer, decimal shippingTotal, out Discount appliedDiscount) { appliedDiscount = null; decimal shippingDiscountAmount = decimal.Zero; if (_catalogSettings.IgnoreDiscounts) return shippingDiscountAmount; var allDiscounts = _discountService.GetAllDiscounts(DiscountType.AssignedToShipping); var allowedDiscounts = new List<Discount>(); if (allDiscounts != null) foreach (var discount in allDiscounts) if (_discountService.ValidateDiscount(discount, customer).IsValid && discount.DiscountType == DiscountType.AssignedToShipping && !allowedDiscounts.ContainsDiscount(discount)) allowedDiscounts.Add(discount); appliedDiscount = allowedDiscounts.GetPreferredDiscount(shippingTotal); if (appliedDiscount != null) { shippingDiscountAmount = appliedDiscount.GetDiscountAmount(shippingTotal); } if (shippingDiscountAmount < decimal.Zero) shippingDiscountAmount = decimal.Zero; if (_shoppingCartSettings.RoundPricesDuringCalculation) shippingDiscountAmount = RoundingHelper.RoundPrice(shippingDiscountAmount); return shippingDiscountAmount; }
/// <summary> /// Gets an order discount (applied to order subtotal) /// </summary> /// <param name="customer">Customer</param> /// <param name="orderSubTotal">Order subtotal</param> /// <param name="appliedDiscount">Applied discount</param> /// <returns>Order discount</returns> protected virtual decimal GetOrderSubtotalDiscount(Customer customer, decimal orderSubTotal, out Discount appliedDiscount) { appliedDiscount = null; decimal discountAmount = decimal.Zero; if (_catalogSettings.IgnoreDiscounts) return discountAmount; var allDiscounts = _discountService.GetAllDiscounts(DiscountType.AssignedToOrderSubTotal); var allowedDiscounts = new List<Discount>(); if (allDiscounts != null) foreach (var discount in allDiscounts) if (_discountService.ValidateDiscount(discount, customer).IsValid && discount.DiscountType == DiscountType.AssignedToOrderSubTotal && !allowedDiscounts.ContainsDiscount(discount)) allowedDiscounts.Add(discount); appliedDiscount = allowedDiscounts.GetPreferredDiscount(orderSubTotal); if (appliedDiscount != null) discountAmount = appliedDiscount.GetDiscountAmount(orderSubTotal); if (discountAmount < decimal.Zero) discountAmount = decimal.Zero; return discountAmount; }