public static bool ApplyDiscount(Store store, Cart cart, string discountCode, out string errorMessage) { if (cart == null) { errorMessage = WebStoreResources.DiscountInvalidCartError; return false; } if (store == null) { errorMessage = WebStoreResources.DiscountInvalidCartError; return false; } if (string.IsNullOrEmpty(discountCode)) { errorMessage = WebStoreResources.DiscountCodeNotProvidedError; return false; } Discount discount = new Discount(store.ModuleGuid, discountCode); if (discount.DiscountGuid == Guid.Empty) { errorMessage = WebStoreResources.DiscountCodeNotFoundError; return false; } if (discount.ValidityEndDate < DateTime.UtcNow) { errorMessage = WebStoreResources.DiscountCodeExpiredError; return false; } if (discount.ValidityStartDate > DateTime.UtcNow) { errorMessage = WebStoreResources.DiscountCodeNotActiveError; return false; } if (!discount.AllowOtherDiscounts) { cart.DiscountCodesCsv = discount.DiscountCode; } if (cart.DiscountCodesCsv.Length == 0) { cart.DiscountCodesCsv = discount.DiscountCode; } else { if (!cart.DiscountCodesCsv.Contains(discount.DiscountCode)) { cart.DiscountCodesCsv += "," + discount.DiscountCode; } } return ValidateAndApplyDiscounts(store, cart, out errorMessage); }
private static List<Discount> LoadListFromReader(IDataReader reader) { List<Discount> discountList = new List<Discount>(); try { while (reader.Read()) { Discount discount = new Discount(); discount.discountGuid = new Guid(reader["DiscountGuid"].ToString()); discount.discountCode = reader["DiscountCode"].ToString(); discount.description = reader["Description"].ToString(); discount.siteGuid = new Guid(reader["SiteGuid"].ToString()); discount.moduleGuid = new Guid(reader["ModuleGuid"].ToString()); discount.storeGuid = new Guid(reader["StoreGuid"].ToString()); discount.offerGuid = new Guid(reader["OfferGuid"].ToString()); if (reader["ValidityEndDate"] != DBNull.Value) { discount.validityStartDate = Convert.ToDateTime(reader["ValidityStartDate"]); } discount.validityEndDate = Convert.ToDateTime(reader["ValidityEndDate"]); discount.useCount = Convert.ToInt32(reader["UseCount"]); discount.maxCount = Convert.ToInt32(reader["MaxCount"]); discount.minOrderAmount = Convert.ToDecimal(reader["MinOrderAmount"]); discount.absoluteDiscount = Convert.ToDecimal(reader["AbsoluteDiscount"]); discount.percentageDiscount = Convert.ToDecimal(reader["PercentageDiscount"]); discount.createdBy = new Guid(reader["CreatedBy"].ToString()); discount.createdUtc = Convert.ToDateTime(reader["CreatedUtc"]); discount.lastModBy = new Guid(reader["LastModBy"].ToString()); discount.lastModUtc = Convert.ToDateTime(reader["LastModUtc"]); discount.allowOtherDiscounts = Convert.ToBoolean(reader["AllowOtherDiscounts"]); discountList.Add(discount); } } finally { reader.Close(); } return discountList; }
public static List<Discount> GetValidDiscounts(Guid moduleGuid, Cart cart, string discountCodeCsv) { List<Discount> discountList = new List<Discount>(); if (moduleGuid == Guid.Empty) { return discountList; } if (string.IsNullOrEmpty(discountCodeCsv)) { return discountList; } string[] discountCodes = discountCodeCsv.Split(','); bool foundAllowOtherConflict = false; foreach (string code in discountCodes) { if (code.Length > 0) { Discount discount = new Discount(moduleGuid, code); // validity checks if (discount.DiscountGuid == Guid.Empty) { continue; } if (discount.ValidityStartDate > DateTime.UtcNow) { continue; } if (discount.ValidityEndDate < DateTime.UtcNow) { continue; } if((discount.AbsoluteDiscount == 0)&&(discount.PercentageDiscount == 0)){ continue;} if ((discount.MaxCount> 0)&&(discount.UseCount >= discount.MaxCount)) { continue; } if((discount.MinOrderAmount > 0)&&(discount.MinOrderAmount > cart.SubTotal)) { continue; } if ((discount.OfferGuid != Guid.Empty)&&(!cart.HasOffer(discount.offerGuid))) { continue; } if (!discount.AllowOtherDiscounts) { if (discountList.Count > 0) { discountList.Clear(); } foundAllowOtherConflict = true; } discountList.Add(discount); // only add the first valid discount if it doesn't allow others if (foundAllowOtherConflict) return discountList; } } return discountList; }
/// <summary> /// Compares 2 instances of Discount. /// </summary> public static int CompareByValidityStartDate(Discount discount1, Discount discount2) { return discount1.ValidityStartDate.CompareTo(discount2.ValidityStartDate); }
/// <summary> /// Compares 2 instances of Discount. /// </summary> public static int CompareByUseCount(Discount discount1, Discount discount2) { return discount1.UseCount.CompareTo(discount2.UseCount); }
/// <summary> /// Compares 2 instances of Discount. /// </summary> public static int CompareByMaxCount(Discount discount1, Discount discount2) { return discount1.MaxCount.CompareTo(discount2.MaxCount); }
/// <summary> /// Compares 2 instances of Discount. /// </summary> public static int CompareByDiscountCode(Discount discount1, Discount discount2) { return(discount1.DiscountCode.CompareTo(discount2.DiscountCode)); }
private bool ValidateBusinessRules() { // check if the discount code is already used Discount d = new Discount(store.ModuleGuid, txtDiscountCode.Text); if ((d.DiscountGuid != Guid.Empty) && (d.DiscountGuid != discountGuid)) { lblError.Text = WebStoreResources.DuplicateCodeException; return false; } return true; }
/// <summary> /// Compares 2 instances of Discount. /// </summary> public static int CompareByDescription(Discount discount1, Discount discount2) { return discount1.Description.CompareTo(discount2.Description); }
/// <summary> /// Compares 2 instances of Discount. /// </summary> public static int CompareByCreatedUtc(Discount discount1, Discount discount2) { return discount1.CreatedUtc.CompareTo(discount2.CreatedUtc); }
/// <summary> /// Compares 2 instances of Discount. /// </summary> public static int CompareByLastModUtc(Discount discount1, Discount discount2) { return(discount1.LastModUtc.CompareTo(discount2.LastModUtc)); }
/// <summary> /// Compares 2 instances of Discount. /// </summary> public static int CompareByCreatedUtc(Discount discount1, Discount discount2) { return(discount1.CreatedUtc.CompareTo(discount2.CreatedUtc)); }
/// <summary> /// Compares 2 instances of Discount. /// </summary> public static int CompareByMaxCount(Discount discount1, Discount discount2) { return(discount1.MaxCount.CompareTo(discount2.MaxCount)); }
/// <summary> /// Compares 2 instances of Discount. /// </summary> public static int CompareByValidityStartDate(Discount discount1, Discount discount2) { return(discount1.ValidityStartDate.CompareTo(discount2.ValidityStartDate)); }
/// <summary> /// Compares 2 instances of Discount. /// </summary> public static int CompareByDescription(Discount discount1, Discount discount2) { return(discount1.Description.CompareTo(discount2.Description)); }
void btnSave_Click(object sender, EventArgs e) { Page.Validate("Discount"); if ((!Page.IsValid) || (!ValidateBusinessRules())) { return; } Discount discount = new Discount(discountGuid); discount.DiscountCode = txtDiscountCode.Text; discount.Description = txtDescription.Text; discount.SiteGuid = siteSettings.SiteGuid; discount.ModuleGuid = store.ModuleGuid; discount.StoreGuid = store.Guid; //TODO: add support for offer specific discounts discount.OfferGuid = Guid.Empty; if (timeZone != null) { discount.ValidityStartDate = DateTime.Parse(dpBeginDate.Text).ToUtc(timeZone); if (dpEndDate.Text.Length > 0) { discount.ValidityEndDate = DateTime.Parse(dpEndDate.Text).ToUtc(timeZone); } else { discount.ValidityEndDate = DateTime.MaxValue; } } else { discount.ValidityStartDate = DateTime.Parse(dpBeginDate.Text).AddHours(-timeOffset); if (dpEndDate.Text.Length > 0) { discount.ValidityEndDate = DateTime.Parse(dpEndDate.Text).AddHours(-timeOffset); } else { discount.ValidityEndDate = DateTime.MaxValue; } } try { discount.AbsoluteDiscount = decimal.Parse(txtAbsoluteDiscount.Text, NumberStyles.Currency, currencyCulture); } catch (FormatException) { lblError.Text = WebStoreResources.DiscountInvalidAmountWarning; return; } decimal percentDiscount = 0; try { percentDiscount = decimal.Parse(txtPercentageDiscount.Text.Replace("%", string.Empty), NumberStyles.Number, currencyCulture); percentDiscount = percentDiscount / 100; } catch (FormatException) { lblError.Text = WebStoreResources.DiscountPercentInvalidWarning; return; } discount.PercentageDiscount = percentDiscount; int maxCount = 0; int.TryParse(txtMaxCount.Text, NumberStyles.Integer, CultureInfo.InvariantCulture, out maxCount); discount.MaxCount = maxCount; decimal minOrderAmount = 0; decimal.TryParse(txtMinOrderAmount.Text, NumberStyles.Currency, currencyCulture, out minOrderAmount); discount.MinOrderAmount = minOrderAmount; discount.AllowOtherDiscounts = ckAllowOtherDiscounts.Checked; discount.CreatedBy = currentUser.UserGuid; discount.LastModBy = currentUser.UserGuid; try { discount.Save(); string redirectUrl = SiteRoot + "/WebStore/AdminDiscountEdit.aspx?pageid=" + pageId.ToInvariantString() + "&mid=" + moduleId.ToInvariantString() + "&d=" + discount.DiscountGuid.ToString(); WebUtils.SetupRedirect(this, redirectUrl); } catch (DbException) { // most likely a duplicate contstraint lblError.Text = WebStoreResources.DuplicateCodeException; } }
private void PopulateControls() { if (discountGuid == Guid.Empty) { return; } Discount discount = new Discount(discountGuid); txtDiscountCode.Text = discount.DiscountCode; txtDescription.Text = discount.Description; if (timeZone != null) { dpBeginDate.Text = DateTimeHelper.LocalizeToCalendar(discount.ValidityStartDate.ToLocalTime(timeZone).ToString("g")); if (discount.ValidityEndDate < DateTime.MaxValue) { dpEndDate.Text = DateTimeHelper.LocalizeToCalendar(discount.ValidityEndDate.ToLocalTime(timeZone).ToString("g")); } } else { dpBeginDate.Text = DateTimeHelper.LocalizeToCalendar(discount.ValidityStartDate.AddHours(timeOffset).ToString("g")); if (discount.ValidityEndDate < DateTime.MaxValue) { dpEndDate.Text = DateTimeHelper.LocalizeToCalendar(discount.ValidityEndDate.AddHours(timeOffset).ToString("g")); } } lblCountOfUse.Text = discount.UseCount.ToInvariantString(); txtMaxCount.Text = discount.MaxCount.ToInvariantString(); txtMinOrderAmount.Text = discount.MinOrderAmount.ToString("c", currencyCulture); txtAbsoluteDiscount.Text = discount.AbsoluteDiscount.ToString("c", currencyCulture); txtPercentageDiscount.Text = string.Format(currencyCulture, "{0:0%}", discount.PercentageDiscount); ckAllowOtherDiscounts.Checked = discount.AllowOtherDiscounts; }
/// <summary> /// Compares 2 instances of Discount. /// </summary> public static int CompareByDiscountCode(Discount discount1, Discount discount2) { return discount1.DiscountCode.CompareTo(discount2.DiscountCode); }
/// <summary> /// Compares 2 instances of Discount. /// </summary> public static int CompareByLastModUtc(Discount discount1, Discount discount2) { return discount1.LastModUtc.CompareTo(discount2.LastModUtc); }
public static List <Discount> GetValidDiscounts(Guid moduleGuid, Cart cart, string discountCodeCsv) { List <Discount> discountList = new List <Discount>(); if (moduleGuid == Guid.Empty) { return(discountList); } if (string.IsNullOrEmpty(discountCodeCsv)) { return(discountList); } string[] discountCodes = discountCodeCsv.Split(','); bool foundAllowOtherConflict = false; foreach (string code in discountCodes) { if (code.Length > 0) { Discount discount = new Discount(moduleGuid, code); // validity checks if (discount.DiscountGuid == Guid.Empty) { continue; } if (discount.ValidityStartDate > DateTime.UtcNow) { continue; } if (discount.ValidityEndDate < DateTime.UtcNow) { continue; } if ((discount.AbsoluteDiscount == 0) && (discount.PercentageDiscount == 0)) { continue; } if ((discount.MaxCount > 0) && (discount.UseCount >= discount.MaxCount)) { continue; } if ((discount.MinOrderAmount > 0) && (discount.MinOrderAmount > cart.SubTotal)) { continue; } if ((discount.OfferGuid != Guid.Empty) && (!cart.HasOffer(discount.offerGuid))) { continue; } if (!discount.AllowOtherDiscounts) { if (discountList.Count > 0) { discountList.Clear(); } foundAllowOtherConflict = true; } discountList.Add(discount); // only add the first valid discount if it doesn't allow others if (foundAllowOtherConflict) { return(discountList); } } } return(discountList); }