using System; using System.Data; using System.Collections.Generic; using System.Collections; using System.Text; public class SingleDiscount { public int DiscountID; private bool _IsOneTimeDiscount = false; private bool _DiscountApplied = false; private bool _GrantsFreeShipping = false; private bool _AppliesToAllItems = false; private decimal _RequiredSubtotal; private decimal _DiscountDollarAmount; private decimal _DiscountAmount = 0; private int _DiscountPercentage; private string _DiscountCode; private string _DiscountTitle; #region accessors public string DiscountCode { get { return _DiscountCode; } set { _DiscountCode = value; } } public bool IsOneTimeDiscount { get { return _IsOneTimeDiscount; } set { _IsOneTimeDiscount = value; } } public bool DiscountApplied { get { return _DiscountApplied; } set { _DiscountApplied = value; } } public bool GrantsFreeShipping { get { return _GrantsFreeShipping; } set { _GrantsFreeShipping = value; } } public bool AppliesToAllItems { get { return _AppliesToAllItems; } set { _AppliesToAllItems = value; } } public decimal RequiredSubtotal { get { return _RequiredSubtotal; } set { _RequiredSubtotal = value; } } public decimal DiscountDollarAmount { get { return _DiscountDollarAmount; } set { _DiscountDollarAmount = value; } } public decimal DiscountAmount { get { return _DiscountAmount; } set { _DiscountAmount = value; } } public int DiscountPercentage { get { return _DiscountPercentage; } set { _DiscountPercentage = value; } } public string DiscountTitle { get { return _DiscountTitle + (!DiscountApplied ? " (NOT APPLIED)" : ""); } set { _DiscountTitle = value; } } #endregion public void RunDiscount() { DatabaseClass Database = DatabaseClass.Current; ShoppingCart ShoppingCart = ShoppingCart.Current; // reset the discount amount DiscountAmount = 0; if (ShoppingCart.SubTotal > this.RequiredSubtotal) { if (!AppliesToAllItems) { DataTable dt = Database.DiscountItemMap_GetItemIDsDiscountID(DiscountID); int TotalItemsMatched = 0; decimal TotalCombinedPrice = 0; foreach (DataRow dr in dt.Rows) { int ItemID = (int)dr["ItemID"]; foreach (ShoppingCartItem sci in ShoppingCart) { if (sci.ItemID == ItemID) { TotalItemsMatched++; TotalCombinedPrice += sci.Price; } } } if (TotalItemsMatched == 0) { DiscountApplied = false; } else { DiscountApplied = true; DiscountAmount += ((DiscountPercentage / 100M) * TotalCombinedPrice); // apply percentage DiscountAmount += DiscountDollarAmount; // apply dollar amount // apply free shipping if (GrantsFreeShipping) ShoppingCart.IsFreeShipping = true; } } else { // this discount applies to all items, so we dont need to evaluate DiscountApplied = true; DiscountAmount += ((DiscountPercentage / 100M) * ShoppingCart.SubTotal); //apply percentage DiscountAmount += DiscountDollarAmount; // apply dollar amount // apply free shipping if (GrantsFreeShipping) ShoppingCart.IsFreeShipping = true; } } else DiscountApplied = false; } } public class DiscountList : List { public decimal TotalDiscountAmount; public void RemoveByDiscountCode(string DiscountCode) { SingleDiscount sd = FindByDiscountCode(DiscountCode); if (sd != null) this.Remove(sd); } public SingleDiscount FindByDiscountCode(string DiscountCode) { foreach (SingleDiscount sd in this) if (sd.DiscountCode == DiscountCode) return sd; return null; } public void RunDiscounts() { TotalDiscountAmount = 0; foreach (SingleDiscount sd in this) { sd.RunDiscount(); TotalDiscountAmount += sd.DiscountAmount; } } }