private Int32 UpdateForm(Int32 id) { if (!ValidateForm(id == 0)) { return(id); } AspDotNetStorefront.Promotions.Data.Promotion promotion = AspDotNetStorefront.Promotions.Data.DataContextProvider.Current.Promotions.FirstOrDefault(l => l.Id == id); if (promotion == null) { promotion = new AspDotNetStorefront.Promotions.Data.Promotion() { PromotionGuid = Guid.NewGuid(), PromotionDiscountData = XElement.Parse("<root />"), PromotionRuleData = XElement.Parse("<root />") }; AspDotNetStorefront.Promotions.Data.DataContextProvider.Current.Promotions.InsertOnSubmit(promotion); } promotion.Name = txtName.Text; promotion.Description = txtDescription.Text; promotion.UsageText = txtUsageText.Text; promotion.EmailText = string.Empty; promotion.Code = txtCode.Text; promotion.Priority = Decimal.Parse(txtPriority.Text); promotion.Active = rblActive.SelectedValue.ToBool(); promotion.AutoAssigned = chkAutoAssigned.Checked; promotion.CallToAction = CallToActionTextbox.Text; List <PromotionRuleBase> rules = new List <PromotionRuleBase>(); if (optRuleStartDate.Checked) { rules.Add(new StartDatePromotionRule { StartDate = txtRuleStartDate.SelectedDate ?? DateTime.Now }); } if (optRuleExpirationDate.Checked) { rules.Add(new ExpirationDatePromotionRule { ExpirationDate = txtRuleExpirationDate.SelectedDate ?? DateTime.Now }); } if (optRuleExpirationNumberOfUses.Checked) { if (chkRuleExpirationNumberOfUsesPerCustomer.Checked) { rules.Add(new ExpirationNumberOfUsesPerCustomerPromotionRule { NumberOfUsesAllowed = Int32.Parse(txtRuleExpirationNumberOfUses.Text) }); } else { rules.Add(new ExpirationNumberOfUsesPromotionRule { NumberOfUsesAllowed = Int32.Parse(txtRuleExpirationNumberOfUses.Text) }); } } if (chkRuleEmail.Checked) { rules.Add(new EmailAddressPromotionRule { EmailAddresses = txtRuleEmailAddresses.Text.Replace(" ", "").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries) }); } if (chkRuleCartAmount.Checked) { rules.Add(new MinimumCartAmountPromotionRule { CartAmount = Convert.ToDecimal(txtRuleCartAmount.Text) }); } if (chkRuleCustomerLevel.Checked) { rules.Add(new CustomerLevelPromotionRule { CustomerLevels = Array.ConvertAll <string, int>(txtRuleCustomerLevels.Text.Replace(" ", "").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries), i => Convert.ToInt32(i.ToString())) }); } if (chkRuleProductId.Checked) { Int32 quantity = 0; Int32.TryParse(txtRuleProductIdsRequireQuantity.Text, out quantity); rules.Add(new ProductIdPromotionRule { AndTogether = rblProductsAllOrAny.SelectedIndex == 0, Quantity = quantity, RequireQuantity = quantity > 0, ProductIds = Array.ConvertAll <string, int>(txtRuleProductIds.Text.Replace(" ", "").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries), i => Convert.ToInt32(i.ToString())) }); } if (chkRuleState.Checked) { rules.Add(new StatePromotionRule { States = txtRuleStates.Text.Replace(" ", "").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries) }); } if (chkRuleZipCode.Checked) { rules.Add(new ZipCodePromotionRule { ZipCodes = txtRuleZipCodes.Text.Replace(" ", "").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries) }); } if (chkRuleCountryCodes.Checked) { rules.Add(new CountryPromotionRule { CountryCodes = txtRuleCountryCodes.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(i => i.Trim()).ToArray() }); } if (chkRuleShippingDiscount.Checked && !String.IsNullOrEmpty(txtRuleShippingMethodID.Text)) { rules.Add(new ShippingMethodIdPromotionRule()); } if (chkRuleCategories.Checked && txtRuleCategories.Text.Length > 0) { String[] categoriesSplit = txtRuleCategories.Text.Replace(" ", "").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); int[] categories = Array.ConvertAll <string, int>(categoriesSplit, Convert.ToInt32); rules.Add(new CategoryPromotionRule { CategoryIds = categories }); } if (chkRuleSections.Checked && txtRuleSections.Text.Length > 0) { String[] sectionsSplit = txtRuleSections.Text.Replace(" ", "").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); int[] sections = Array.ConvertAll <string, int>(sectionsSplit, Convert.ToInt32); rules.Add(new SectionPromotionRule { SectionIds = sections }); } if (chkRuleManufacturers.Checked && txtRuleManufacturers.Text.Length > 0) { String[] manufacturersSplit = txtRuleManufacturers.Text.Replace(" ", "").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); int[] manufacturers = Array.ConvertAll <string, int>(manufacturersSplit, Convert.ToInt32); rules.Add(new ManufacturerPromotionRule { ManufacturerIds = manufacturers }); } if (chkRuleMinimumOrders.Checked) { rules.Add(new MinimumOrdersPromotionRule { MinimumOrdersAllowed = Int32.Parse(txtRuleMinimumOrders.Text), StartDateType = DateType.CustomDate, CustomStartDate = txtRuleMinimumOrdersCustomStartDate.SelectedDate ?? DateTime.Now, EndDateType = DateType.CustomDate, CustomEndDate = txtRuleMinimumOrdersCustomEndDate.SelectedDate ?? DateTime.Now, }); } if (chkRuleMinimumOrderAmount.Checked) { rules.Add(new MinimumOrderAmountPromotionRule { MinimumOrderAmountAllowed = Decimal.Parse(txtRuleMinimumOrderAmount.Text), StartDateType = DateType.CustomDate, CustomStartDate = txtRuleMinimumOrderAmountCustomStartDate.SelectedDate ?? DateTime.Now, EndDateType = DateType.CustomDate, CustomEndDate = txtRuleMinimumOrderAmountCustomEndDate.SelectedDate ?? DateTime.Now, }); } if (chkRuleMinimumProductsOrdered.Checked) { rules.Add(new MinimumProductsOrderedPromotionRule { MinimumProductsOrderedAllowed = Int32.Parse(txtRuleMinimumProductsOrdered.Text), StartDateType = DateType.CustomDate, CustomStartDate = txtRuleMinimumProductsOrderedCustomStartDate.SelectedDate ?? DateTime.Now, EndDateType = DateType.CustomDate, CustomEndDate = txtRuleMinimumProductsOrderedCustomEndDate.SelectedDate ?? DateTime.Now, ProductIds = Array.ConvertAll <string, int>(txtRuleMinimumProductsOrderedProductIds.Text.Replace(" ", "").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries), i => Convert.ToInt32(i.ToString())) }); } if (chkRuleMinimumProductsOrderedAmount.Checked) { rules.Add(new MinimumProductAmountOrderedPromotionRule { MinimumProductAmountOrderedAllowed = Decimal.Parse(txtRuleMinimumProductsOrderedAmount.Text), StartDateType = DateType.CustomDate, CustomStartDate = txtRuleMinimumProductsOrderedAmountCustomStartDate.SelectedDate ?? DateTime.Now, EndDateType = DateType.CustomDate, CustomEndDate = txtRuleMinimumProductsOrderedAmountCustomEndDate.SelectedDate ?? DateTime.Now, ProductIds = Array.ConvertAll <string, int>(txtRuleMinimumProductsOrderedAmountProductIds.Text.Replace(" ", "").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries), i => Convert.ToInt32(i.ToString())) }); } List <PromotionDiscountBase> discounts = new List <PromotionDiscountBase>(); if (chkRuleShippingDiscount.Checked && txtRuleShippingDiscountAmount.Text.Length > 0) { DiscountType discountType = (DiscountType)Int32.Parse(ddlRuleShippingDiscountType.SelectedValue); decimal discountAmount = decimal.Round(decimal.Parse(txtRuleShippingDiscountAmount.Text), 4); if (discountType == DiscountType.Percentage) { discountAmount /= 100; } String[] shipMethodsSplit = txtRuleShippingMethodID.Text .Replace(" ", "") .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); int[] shipMethods = Array.ConvertAll <string, int>(shipMethodsSplit, Convert.ToInt32); ShippingPromotionDiscount shippingPromotionDiscount = new ShippingPromotionDiscount(discountType, discountAmount, shipMethods); discounts.Add(shippingPromotionDiscount); rules.Add(new ShippingPromotionRule()); } if (chkRuleGiftWithPurchase.Checked) { decimal giftDiscountPercentage = decimal.Round(decimal.Parse(txtGiftWithPurchaseDiscountAmount.Text), 4); GiftProductPromotionDiscount giftWithPurchaseDiscount = new GiftProductPromotionDiscount { GiftProductIds = Array.ConvertAll <string, int>(txtRuleGiftWithPurchaseProductId.Text.Replace(" ", "").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries), i => Convert.ToInt32(i.ToString())), MatchQuantities = chkMatchQuantites.Checked, GiftDiscountPercentage = giftDiscountPercentage, PromotionId = promotion.Id }; discounts.Add(giftWithPurchaseDiscount); } if (chkRuleOrderDiscount.Checked) { OrderPromotionDiscount orderPromotionDiscount = new OrderPromotionDiscount { DiscountType = (DiscountType)Int32.Parse(ddlRuleOrderDiscountType.SelectedValue) }; if (orderPromotionDiscount.DiscountType == DiscountType.Fixed) { orderPromotionDiscount.DiscountAmount = decimal.Round(decimal.Parse(txtRuleOrderDiscountAmount.Text), 4); } else { orderPromotionDiscount.DiscountAmount = decimal.Round(decimal.Parse(txtRuleOrderDiscountAmount.Text) / 100, 4); } discounts.Add(orderPromotionDiscount); } if (chkRuleLineItemDiscount.Checked) { OrderItemPromotionDiscount orderItemPromotionDiscount = new OrderItemPromotionDiscount { DiscountType = (DiscountType)Int32.Parse(ddlRuleLineItemDiscountType.SelectedValue) }; if (orderItemPromotionDiscount.DiscountType == DiscountType.Fixed) { orderItemPromotionDiscount.DiscountAmount = decimal.Round(decimal.Parse(txtRuleLineItemDiscountAmount.Text), 4); } else { orderItemPromotionDiscount.DiscountAmount = decimal.Round(decimal.Parse(txtRuleLineItemDiscountAmount.Text) / 100, 4); } discounts.Add(orderItemPromotionDiscount); } // This needs to be below the discount and rule creation because it serializes it to xml, it doesn't just store a reference. promotion.PromotionRules = rules; promotion.PromotionDiscounts = discounts; AspDotNetStorefront.Promotions.Data.DataContextProvider.Current.SubmitChanges(); foreach (var promotionStore in promotion.PromotionStores.ToList()) { if (ssPromotion.SelectedStoreIDs.Contains(promotionStore.StoreID)) { continue; } AspDotNetStorefront.Promotions.Data.DataContextProvider.Current.PromotionStores.DeleteOnSubmit(promotionStore); promotion.PromotionStores.Remove(promotionStore); } foreach (var storeId in ssPromotion.SelectedStoreIDs) { if (promotion.PromotionStores.Any(ps => ps.StoreID == storeId)) { continue; } var newPromotionStore = new AspDotNetStorefront.Promotions.Data.PromotionStore { CreatedOn = DateTime.Now, PromotionID = promotion.Id, StoreID = storeId }; AspDotNetStorefront.Promotions.Data.DataContextProvider.Current.PromotionStores.InsertOnSubmit(newPromotionStore); promotion.PromotionStores.Add(newPromotionStore); } AspDotNetStorefront.Promotions.Data.DataContextProvider.Current.SubmitChanges(); BindData(); CancelForm(); return(promotion.Id); }
private void EditForm(Int32 id) { ClearForm(); AspDotNetStorefront.Promotions.Data.Promotion promotion = AspDotNetStorefront.Promotions.Data.DataContextProvider.Current.Promotions.FirstOrDefault(l => l.Id == id); if (promotion == null) { ToggleView(true, true); return; } else { ToggleView(true, false); } txtId.Value = promotion.Id.ToString(); lblTitle.Text = txtName.Text = promotion.Name; txtDescription.Text = promotion.Description; txtUsageText.Text = promotion.UsageText; txtCode.Text = promotion.Code; txtPriority.Text = promotion.Priority.ToString(); rblActive.SelectedValue = promotion.Active.ToString().ToLower(); chkAutoAssigned.Checked = promotion.AutoAssigned; CallToActionTextbox.Text = promotion.CallToAction; ssPromotion.SelectedStoreIDs = promotion.PromotionStores.Select(ps => ps.StoreID).ToArray(); foreach (PromotionRuleBase rule in promotion.PromotionRules) { StartDatePromotionRule startDateRule = rule as StartDatePromotionRule; if (startDateRule != null) { optRuleStartDate.Checked = true; txtRuleStartDate.SelectedDate = startDateRule.StartDate; } ExpirationDatePromotionRule expirationDateRule = rule as ExpirationDatePromotionRule; if (expirationDateRule != null) { optRuleExpirationDate.Checked = true; txtRuleExpirationDate.SelectedDate = expirationDateRule.ExpirationDate; } ExpirationNumberOfUsesPromotionRule expirationDateNumberOfUsesRule = rule as ExpirationNumberOfUsesPromotionRule; if (expirationDateNumberOfUsesRule != null) { optRuleExpirationNumberOfUses.Checked = true; txtRuleExpirationNumberOfUses.Text = expirationDateNumberOfUsesRule.NumberOfUsesAllowed.ToString(); chkRuleExpirationNumberOfUsesPerCustomer.Checked = false; } ExpirationNumberOfUsesPerCustomerPromotionRule expirationDateNumberOfUsesPerCustomerRule = rule as ExpirationNumberOfUsesPerCustomerPromotionRule; if (expirationDateNumberOfUsesPerCustomerRule != null) { optRuleExpirationNumberOfUses.Checked = true; txtRuleExpirationNumberOfUses.Text = expirationDateNumberOfUsesPerCustomerRule.NumberOfUsesAllowed.ToString(); chkRuleExpirationNumberOfUsesPerCustomer.Checked = true; } EmailAddressPromotionRule emailAddressRule = rule as EmailAddressPromotionRule; if (emailAddressRule != null) { chkRuleEmail.Checked = true; txtRuleEmailAddresses.Text = String.Join(",", emailAddressRule.EmailAddresses); continue; } MinimumCartAmountPromotionRule cartAmountRule = rule as MinimumCartAmountPromotionRule; if (cartAmountRule != null) { chkRuleCartAmount.Checked = true; txtRuleCartAmount.Text = cartAmountRule.CartAmount.ToString(); continue; } CustomerLevelPromotionRule customerLevelRule = rule as CustomerLevelPromotionRule; if (customerLevelRule != null) { chkRuleCustomerLevel.Checked = true; String[] customerLevels = Array.ConvertAll <int, string>(customerLevelRule.CustomerLevels, new Converter <int, string>(Convert.ToString)); txtRuleCustomerLevels.Text = String.Join(",", customerLevels); continue; } CategoryPromotionRule categoryPromotionRule = rule as CategoryPromotionRule; if (categoryPromotionRule != null) { chkRuleCategories.Checked = true; String[] categories = Array.ConvertAll <int, string>(categoryPromotionRule.CategoryIds, new Converter <int, string>(Convert.ToString)); txtRuleCategories.Text = String.Join(",", categories); } SectionPromotionRule sectionPromotionRule = rule as SectionPromotionRule; if (sectionPromotionRule != null) { chkRuleSections.Checked = true; String[] sections = Array.ConvertAll <int, string>(sectionPromotionRule.SectionIds, new Converter <int, string>(Convert.ToString)); txtRuleSections.Text = String.Join(",", sections); } ManufacturerPromotionRule manufacturerPromotionRule = rule as ManufacturerPromotionRule; if (manufacturerPromotionRule != null) { chkRuleManufacturers.Checked = true; String[] manufacturers = Array.ConvertAll <int, string>(manufacturerPromotionRule.ManufacturerIds, new Converter <int, string>(Convert.ToString)); txtRuleManufacturers.Text = String.Join(",", manufacturers); } ProductIdPromotionRule productIdRule = rule as ProductIdPromotionRule; if (productIdRule != null) { chkRuleProductId.Checked = true; String[] productIds = Array.ConvertAll <int, string>(productIdRule.ProductIds, new Converter <int, string>(Convert.ToString)); txtRuleProductIds.Text = String.Join(",", productIds); rblProductsAllOrAny.SelectedIndex = CommonLogic.IIF(productIdRule.AndTogether, 0, 1); txtRuleProductIdsRequireQuantity.Text = productIdRule.Quantity.ToString(); continue; } StatePromotionRule stateRule = rule as StatePromotionRule; if (stateRule != null) { chkRuleState.Checked = true; txtRuleStates.Text = String.Join(",", stateRule.States); continue; } ZipCodePromotionRule zipCodeRule = rule as ZipCodePromotionRule; if (zipCodeRule != null) { chkRuleZipCode.Checked = true; txtRuleZipCodes.Text = String.Join(",", zipCodeRule.ZipCodes); continue; } CountryPromotionRule countryRule = rule as CountryPromotionRule; if (countryRule != null) { chkRuleCountryCodes.Checked = true; txtRuleCountryCodes.Text = String.Join(",", countryRule.CountryCodes); continue; } MinimumOrdersPromotionRule minimumOrdersRule = rule as MinimumOrdersPromotionRule; if (minimumOrdersRule != null) { chkRuleMinimumOrders.Checked = true; txtRuleMinimumOrders.Text = minimumOrdersRule.MinimumOrdersAllowed.ToString(); txtRuleMinimumOrdersCustomStartDate.SelectedDate = minimumOrdersRule.CustomStartDate; txtRuleMinimumOrdersCustomEndDate.SelectedDate = minimumOrdersRule.CustomEndDate; } MinimumOrderAmountPromotionRule minimumOrderAmountRule = rule as MinimumOrderAmountPromotionRule; if (minimumOrderAmountRule != null) { chkRuleMinimumOrderAmount.Checked = true; txtRuleMinimumOrderAmount.Text = minimumOrderAmountRule.MinimumOrderAmountAllowed.ToString(); txtRuleMinimumOrderAmountCustomStartDate.SelectedDate = minimumOrderAmountRule.CustomStartDate; txtRuleMinimumOrderAmountCustomEndDate.SelectedDate = minimumOrderAmountRule.CustomEndDate; } MinimumProductsOrderedPromotionRule minimumProductsOrderedRule = rule as MinimumProductsOrderedPromotionRule; if (minimumProductsOrderedRule != null) { chkRuleMinimumProductsOrdered.Checked = true; txtRuleMinimumProductsOrdered.Text = minimumProductsOrderedRule.MinimumProductsOrderedAllowed.ToString(); txtRuleMinimumProductsOrderedCustomStartDate.SelectedDate = minimumProductsOrderedRule.CustomStartDate; txtRuleMinimumProductsOrderedCustomEndDate.SelectedDate = minimumProductsOrderedRule.CustomEndDate; txtRuleMinimumProductsOrderedProductIds.Text = String.Join(",", Array.ConvertAll <int, string>(minimumProductsOrderedRule.ProductIds, i => i.ToString())); } MinimumProductAmountOrderedPromotionRule minimumProductAmountOrderedRule = rule as MinimumProductAmountOrderedPromotionRule; if (minimumProductAmountOrderedRule != null) { chkRuleMinimumProductsOrderedAmount.Checked = true; txtRuleMinimumProductsOrderedAmount.Text = minimumProductAmountOrderedRule.MinimumProductAmountOrderedAllowed.ToString(); txtRuleMinimumProductsOrderedAmountCustomStartDate.SelectedDate = minimumProductAmountOrderedRule.CustomStartDate; txtRuleMinimumProductsOrderedAmountCustomEndDate.SelectedDate = minimumProductAmountOrderedRule.CustomEndDate; txtRuleMinimumProductsOrderedAmountProductIds.Text = String.Join(",", Array.ConvertAll <int, string>(minimumProductAmountOrderedRule.ProductIds, i => i.ToString())); } } foreach (PromotionDiscountBase discount in promotion.PromotionDiscounts) { ShippingPromotionDiscount shippingDiscount = discount as ShippingPromotionDiscount; if (shippingDiscount != null) { chkRuleShippingDiscount.Checked = true; ddlRuleShippingDiscountType.SelectedValue = ((Int32)shippingDiscount.DiscountType).ToString(); if (shippingDiscount.DiscountType == DiscountType.Fixed) { txtRuleShippingDiscountAmount.Text = shippingDiscount.DiscountAmount.ToString("0.0000"); } else { txtRuleShippingDiscountAmount.Text = (shippingDiscount.DiscountAmount * 100).ToString("0.0000"); } String shipMethodIds = String.Join(",", shippingDiscount.ShippingMethodIds.Select(i => i.ToString()).ToArray()); txtRuleShippingMethodID.Text = shipMethodIds; continue; } GiftProductPromotionDiscount giftProductDiscount = discount as GiftProductPromotionDiscount; if (giftProductDiscount != null && giftProductDiscount.GiftSkus != null) { chkRuleGiftWithPurchase.Checked = true; chkMatchQuantites.Checked = giftProductDiscount.MatchQuantities; txtGiftWithPurchaseDiscountAmount.Text = giftProductDiscount.GiftDiscountPercentage.ToString("0.0000"); } if (giftProductDiscount != null && giftProductDiscount.GiftProductIds != null) { chkRuleGiftWithPurchase.Checked = true; txtRuleGiftWithPurchaseProductId.Text = String.Join(",", Array.ConvertAll <int, string>(giftProductDiscount.GiftProductIds, i => i.ToString())); chkMatchQuantites.Checked = giftProductDiscount.MatchQuantities; txtGiftWithPurchaseDiscountAmount.Text = giftProductDiscount.GiftDiscountPercentage.ToString("0.0000"); } OrderPromotionDiscount orderDiscount = discount as OrderPromotionDiscount; if (orderDiscount != null) { chkRuleOrderDiscount.Checked = true; ddlRuleOrderDiscountType.SelectedValue = ((Int32)orderDiscount.DiscountType).ToString(); if (orderDiscount.DiscountType == DiscountType.Fixed) { txtRuleOrderDiscountAmount.Text = orderDiscount.DiscountAmount.ToString("0.0000"); } else { txtRuleOrderDiscountAmount.Text = (orderDiscount.DiscountAmount * 100).ToString("0.0000"); } continue; } OrderItemPromotionDiscount orderItemDiscount = discount as OrderItemPromotionDiscount; if (orderItemDiscount != null) { chkRuleLineItemDiscount.Checked = true; ddlRuleLineItemDiscountType.SelectedValue = ((Int32)orderItemDiscount.DiscountType).ToString(); if (orderItemDiscount.DiscountType == DiscountType.Fixed) { txtRuleLineItemDiscountAmount.Text = orderItemDiscount.DiscountAmount.ToString("0.0000"); } else { txtRuleLineItemDiscountAmount.Text = (orderItemDiscount.DiscountAmount * 100).ToString("0.0000"); } continue; } } }