PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/WebStore.UI/WebStore/AdminDiscountEdit.aspx.cs

http://mojoportal.codeplex.com
C# | 334 lines | 250 code | 69 blank | 15 comment | 28 complexity | 764d311930bc0ccdc6d5d452462d2150 MD5 | raw file
Possible License(s): CPL-1.0, CC-BY-SA-3.0, GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause, Apache-2.0
  1. // Author: Joe Audette
  2. // Created: 2009-03-03
  3. // Last Modified: 2012-10-02
  4. //
  5. // The use and distribution terms for this software are covered by the
  6. // Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
  7. // which can be found in the file CPL.TXT at the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by
  9. // the terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. using System;
  13. using System.Data.Common;
  14. using System.Globalization;
  15. using System.Web.UI;
  16. using mojoPortal.Web;
  17. using mojoPortal.Web.Framework;
  18. using mojoPortal.Web.UI;
  19. using mojoPortal.Business;
  20. using Resources;
  21. using WebStore.Business;
  22. using WebStore.Helpers;
  23. namespace WebStore.UI
  24. {
  25. public partial class AdminDiscountEditPage : NonCmsBasePage
  26. {
  27. private int pageId = -1;
  28. private int moduleId = -1;
  29. private Store store = null;
  30. private Guid discountGuid = Guid.Empty;
  31. protected Double timeOffset = 0;
  32. private TimeZoneInfo timeZone = null;
  33. protected CultureInfo currencyCulture = CultureInfo.CurrentCulture;
  34. protected SiteUser currentUser = null;
  35. protected void Page_Load(object sender, EventArgs e)
  36. {
  37. LoadParams();
  38. if (!UserCanEditModule(moduleId, Store.FeatureGuid))
  39. {
  40. SiteUtils.RedirectToAccessDeniedPage(this);
  41. return;
  42. }
  43. LoadSettings();
  44. if (store == null)
  45. {
  46. SiteUtils.RedirectToAccessDeniedPage(this);
  47. return;
  48. }
  49. PopulateLabels();
  50. if (!Page.IsPostBack)
  51. {
  52. PopulateControls();
  53. }
  54. AnalyticsSection = ConfigHelper.GetStringProperty("AnalyticsWebStoreSection", "store");
  55. }
  56. private void PopulateControls()
  57. {
  58. if (discountGuid == Guid.Empty) { return; }
  59. Discount discount = new Discount(discountGuid);
  60. txtDiscountCode.Text = discount.DiscountCode;
  61. txtDescription.Text = discount.Description;
  62. if (timeZone != null)
  63. {
  64. dpBeginDate.Text = DateTimeHelper.LocalizeToCalendar(discount.ValidityStartDate.ToLocalTime(timeZone).ToString("g"));
  65. if (discount.ValidityEndDate < DateTime.MaxValue)
  66. {
  67. dpEndDate.Text = DateTimeHelper.LocalizeToCalendar(discount.ValidityEndDate.ToLocalTime(timeZone).ToString("g"));
  68. }
  69. }
  70. else
  71. {
  72. dpBeginDate.Text = DateTimeHelper.LocalizeToCalendar(discount.ValidityStartDate.AddHours(timeOffset).ToString("g"));
  73. if (discount.ValidityEndDate < DateTime.MaxValue)
  74. {
  75. dpEndDate.Text = DateTimeHelper.LocalizeToCalendar(discount.ValidityEndDate.AddHours(timeOffset).ToString("g"));
  76. }
  77. }
  78. lblCountOfUse.Text = discount.UseCount.ToInvariantString();
  79. txtMaxCount.Text = discount.MaxCount.ToInvariantString();
  80. txtMinOrderAmount.Text = discount.MinOrderAmount.ToString("c", currencyCulture);
  81. txtAbsoluteDiscount.Text = discount.AbsoluteDiscount.ToString("c", currencyCulture);
  82. txtPercentageDiscount.Text = string.Format(currencyCulture, "{0:0%}", discount.PercentageDiscount);
  83. ckAllowOtherDiscounts.Checked = discount.AllowOtherDiscounts;
  84. }
  85. void btnSave_Click(object sender, EventArgs e)
  86. {
  87. Page.Validate("Discount");
  88. if ((!Page.IsValid) || (!ValidateBusinessRules())) { return; }
  89. Discount discount = new Discount(discountGuid);
  90. discount.DiscountCode = txtDiscountCode.Text;
  91. discount.Description = txtDescription.Text;
  92. discount.SiteGuid = siteSettings.SiteGuid;
  93. discount.ModuleGuid = store.ModuleGuid;
  94. discount.StoreGuid = store.Guid;
  95. //TODO: add support for offer specific discounts
  96. discount.OfferGuid = Guid.Empty;
  97. if (timeZone != null)
  98. {
  99. discount.ValidityStartDate = DateTime.Parse(dpBeginDate.Text).ToUtc(timeZone);
  100. if (dpEndDate.Text.Length > 0)
  101. {
  102. discount.ValidityEndDate = DateTime.Parse(dpEndDate.Text).ToUtc(timeZone);
  103. }
  104. else
  105. {
  106. discount.ValidityEndDate = DateTime.MaxValue;
  107. }
  108. }
  109. else
  110. {
  111. discount.ValidityStartDate = DateTime.Parse(dpBeginDate.Text).AddHours(-timeOffset);
  112. if (dpEndDate.Text.Length > 0)
  113. {
  114. discount.ValidityEndDate = DateTime.Parse(dpEndDate.Text).AddHours(-timeOffset);
  115. }
  116. else
  117. {
  118. discount.ValidityEndDate = DateTime.MaxValue;
  119. }
  120. }
  121. try
  122. {
  123. discount.AbsoluteDiscount = decimal.Parse(txtAbsoluteDiscount.Text, NumberStyles.Currency, currencyCulture);
  124. }
  125. catch (FormatException)
  126. {
  127. lblError.Text = WebStoreResources.DiscountInvalidAmountWarning;
  128. return;
  129. }
  130. decimal percentDiscount = 0;
  131. try
  132. {
  133. percentDiscount = decimal.Parse(txtPercentageDiscount.Text.Replace("%", string.Empty), NumberStyles.Number, currencyCulture);
  134. percentDiscount = percentDiscount / 100;
  135. }
  136. catch (FormatException)
  137. {
  138. lblError.Text = WebStoreResources.DiscountPercentInvalidWarning;
  139. return;
  140. }
  141. discount.PercentageDiscount = percentDiscount;
  142. int maxCount = 0;
  143. int.TryParse(txtMaxCount.Text, NumberStyles.Integer, CultureInfo.InvariantCulture, out maxCount);
  144. discount.MaxCount = maxCount;
  145. decimal minOrderAmount = 0;
  146. decimal.TryParse(txtMinOrderAmount.Text, NumberStyles.Currency, currencyCulture, out minOrderAmount);
  147. discount.MinOrderAmount = minOrderAmount;
  148. discount.AllowOtherDiscounts = ckAllowOtherDiscounts.Checked;
  149. discount.CreatedBy = currentUser.UserGuid;
  150. discount.LastModBy = currentUser.UserGuid;
  151. try
  152. {
  153. discount.Save();
  154. string redirectUrl = SiteRoot + "/WebStore/AdminDiscountEdit.aspx?pageid="
  155. + pageId.ToInvariantString()
  156. + "&mid=" + moduleId.ToInvariantString()
  157. + "&d=" + discount.DiscountGuid.ToString();
  158. WebUtils.SetupRedirect(this, redirectUrl);
  159. }
  160. catch (DbException)
  161. {
  162. // most likely a duplicate contstraint
  163. lblError.Text = WebStoreResources.DuplicateCodeException;
  164. }
  165. }
  166. private bool ValidateBusinessRules()
  167. {
  168. // check if the discount code is already used
  169. Discount d = new Discount(store.ModuleGuid, txtDiscountCode.Text);
  170. if ((d.DiscountGuid != Guid.Empty) && (d.DiscountGuid != discountGuid))
  171. {
  172. lblError.Text = WebStoreResources.DuplicateCodeException;
  173. return false;
  174. }
  175. return true;
  176. }
  177. void btnDelete_Click(object sender, EventArgs e)
  178. {
  179. if (discountGuid == Guid.Empty) { return; }
  180. // TODO: should we allow deleting discounts that have been used?
  181. Discount.Delete(discountGuid);
  182. string redirectUrl = SiteRoot + "/WebStore/AdminDiscounts.aspx?pageid="
  183. + pageId.ToInvariantString() + "&mid=" + moduleId.ToInvariantString();
  184. WebUtils.SetupRedirect(this, redirectUrl);
  185. }
  186. private void PopulateLabels()
  187. {
  188. Control c = Master.FindControl("Breadcrumbs");
  189. if (c != null)
  190. {
  191. BreadcrumbsControl crumbs = (BreadcrumbsControl)c;
  192. crumbs.ForceShowBreadcrumbs = true;
  193. crumbs.AddedCrumbs = crumbs.ItemWrapperTop + "<a href='"
  194. + SiteRoot + "/WebStore/AdminDashboard.aspx?pageid=" + pageId.ToInvariantString() + "&amp;mid=" + moduleId.ToInvariantString()
  195. + "'>" + WebStoreResources.StoreManagerLink
  196. + "</a>" + crumbs.ItemWrapperBottom
  197. + crumbs.Separator
  198. + crumbs.ItemWrapperTop + "<a href='"
  199. + SiteRoot + "/WebStore/AdminDiscounts.aspx?pageid=" + pageId.ToInvariantString() + "&amp;mid=" + moduleId.ToInvariantString()
  200. + "'>" + WebStoreResources.DiscountAdministration
  201. + "</a>" + crumbs.ItemWrapperTop;
  202. }
  203. Title = SiteUtils.FormatPageTitle(siteSettings, WebStoreResources.DiscountAdministration);
  204. btnSave.Text = WebStoreResources.DiscountSaveButton;
  205. btnDelete.Text = WebStoreResources.DiscountDeleteButton;
  206. reqDiscountCode.ErrorMessage = WebStoreResources.DiscountCodeRequiredWarning;
  207. reqDescription.ErrorMessage = WebStoreResources.DiscountDescriptionRequiredWarning;
  208. reqBeginDate.ErrorMessage = WebStoreResources.DiscountBeginDateRequiredWarning;
  209. reqMaxUseCount.ErrorMessage = WebStoreResources.DiscountMaxUsesRequiredWarning;
  210. reqMinOrderAmount.ErrorMessage = WebStoreResources.DiscountMinOrderRequiredFieldWarning;
  211. reqDiscountAmount.ErrorMessage = WebStoreResources.DiscountAmountRequiredFieldWarning;
  212. reqPercentDiscount.ErrorMessage = WebStoreResources.DiscountPercentageRequiredFieldWarning;
  213. lblLeaveBlankForNoEndDate.Text = WebStoreResources.DiscountBlankEndDateHelp;
  214. lblZeroIsUnlimitedUse.Text = WebStoreResources.DiscountMaxUseHelp;
  215. lblZeroMeansNoMinimum.Text = WebStoreResources.DiscountMinOrderHelp;
  216. if (!Page.IsPostBack)
  217. {
  218. if (timeZone != null)
  219. {
  220. dpBeginDate.Text = DateTimeHelper.LocalizeToCalendar(DateTime.UtcNow.ToLocalTime(timeZone).ToString("g"));
  221. }
  222. else
  223. {
  224. dpBeginDate.Text = DateTimeHelper.LocalizeToCalendar(DateTime.UtcNow.AddHours(timeOffset).ToString("g"));
  225. }
  226. decimal zero = 0;
  227. txtMinOrderAmount.Text = zero.ToString("c", currencyCulture);
  228. txtAbsoluteDiscount.Text = zero.ToString("c", currencyCulture);
  229. txtPercentageDiscount.Text = string.Format(currencyCulture, "{0:0%}", zero);
  230. }
  231. }
  232. private void LoadSettings()
  233. {
  234. timeOffset = SiteUtils.GetUserTimeOffset();
  235. timeZone = SiteUtils.GetUserTimeZone();
  236. currencyCulture = ResourceHelper.GetCurrencyCulture(siteSettings.GetCurrency().Code);
  237. currentUser = SiteUtils.GetCurrentSiteUser();
  238. store = StoreHelper.GetStore();
  239. btnDelete.Visible = (discountGuid != Guid.Empty);
  240. AddClassToBody("webstore webstorediscountedit");
  241. }
  242. private void LoadParams()
  243. {
  244. pageId = WebUtils.ParseInt32FromQueryString("pageid", pageId);
  245. moduleId = WebUtils.ParseInt32FromQueryString("mid", moduleId);
  246. discountGuid = WebUtils.ParseGuidFromQueryString("d", discountGuid);
  247. }
  248. #region OnInit
  249. protected override void OnPreInit(EventArgs e)
  250. {
  251. AllowSkinOverride = true;
  252. base.OnPreInit(e);
  253. }
  254. override protected void OnInit(EventArgs e)
  255. {
  256. base.OnInit(e);
  257. this.Load += new EventHandler(this.Page_Load);
  258. btnSave.Click += new EventHandler(btnSave_Click);
  259. btnDelete.Click += new EventHandler(btnDelete_Click);
  260. SuppressMenuSelection();
  261. SuppressPageMenu();
  262. }
  263. #endregion
  264. }
  265. }