Example #1
0
        public static bool HasQualifyingItemGiftProductPromotion(IRuleContext ruleContext)
        {
            bool isQualifyingItemGiftProductPromotion = false;
            PromotionController promotionController   = CreatePromotionController();

            foreach (PromotionsData.PromotionUsage promotionUsage in PromotionsData.DataContextProvider.Current.PromotionUsages.Where(pu => pu.CustomerId == ruleContext.CustomerId && pu.Complete == false))
            {
                PromotionsData.Promotion promotion = PromotionsData.DataContextProvider.Current.Promotions.FirstOrDefault(p => p.Id == promotionUsage.PromotionId);
                ruleContext.PromotionId = promotion.Id;

                if (promotionController.ValidatePromotion(promotion, ruleContext, AppLogic.CustomerLevelAllowsCoupons(ruleContext.CustomerLevel)).All(vr => vr.IsValid))
                {
                    foreach (PromotionDiscountBase pd in promotion.PromotionDiscounts.Where(p => p.GetType() == typeof(GiftProductPromotionDiscount)))
                    {
                        CategoryPromotionRule     categories    = new CategoryPromotionRule();
                        SectionPromotionRule      sections      = new SectionPromotionRule();
                        ManufacturerPromotionRule manufacturers = new ManufacturerPromotionRule();
                        ProductIdPromotionRule    productids    = new ProductIdPromotionRule();

                        if (promotion.PromotionRules.Where(pr => (pr.GetType() == typeof(CategoryPromotionRule)) || (pr.GetType() == typeof(SectionPromotionRule)) || (pr.GetType() == typeof(ManufacturerPromotionRule)) || (pr.GetType() == typeof(ProductIdPromotionRule))).Count() > 0)
                        {
                            isQualifyingItemGiftProductPromotion = true;
                        }
                    }
                }
            }

            return(isQualifyingItemGiftProductPromotion);
        }
Example #2
0
        public static void AssignPromotion(int customerId, string promotionCode)
        {
            PromotionsData.Promotion promotion = GetPromotionByCode(promotionCode, true);
            if (promotion == null)
            {
                return;
            }

            AssignPromotion(customerId, promotion.Id);
        }
    private void DeleteForm(Int32 id)
    {
        AspDotNetStorefront.Promotions.Data.Promotion promotion = AspDotNetStorefront.Promotions.Data.DataContextProvider.Current.Promotions.FirstOrDefault(l => l.Id == id);
        if (promotion == null)
        {
            return;
        }

        AspDotNetStorefront.Promotions.Data.DataContextProvider.Current.PromotionUsages.DeleteAllOnSubmit(AspDotNetStorefront.Promotions.Data.DataContextProvider.Current.PromotionUsages.Where(pu => pu.PromotionId == promotion.Id));
        AspDotNetStorefront.Promotions.Data.DataContextProvider.Current.Promotions.DeleteOnSubmit(promotion);
        AspDotNetStorefront.Promotions.Data.DataContextProvider.Current.SubmitChanges();

        BindData();
        CancelForm();
    }
Example #4
0
        public static IEnumerable <IPromotionValidationResult> ValidatePromotion(string promotionCode, IRuleContext ruleContext)
        {
            PromotionsData.Promotion promotion = GetPromotionByCode(promotionCode, false);
            if (promotion == null)
            {
                return new[] { new SimplePromotionValidationResult(false, "Promotion.Reason.DoesNotExist") }
            }
            ;
            else if (!promotion.Active)
            {
                return new[] { new SimplePromotionValidationResult(false, "Promotion.Reason.InactivePromotion") }
            }
            ;

            ruleContext.PromotionId = promotion.Id;

            return(CreatePromotionController().ValidatePromotion(promotion, ruleContext, AppLogic.CustomerLevelAllowsCoupons(ruleContext.CustomerLevel)));
        }
Example #5
0
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        int        promotionId;
        LinkButton btn = (LinkButton)sender;

        if (int.TryParse(btn.CommandArgument, out promotionId))
        {
            AspDotNetStorefront.Promotions.Data.Promotion promotion = AspDotNetStorefront.Promotions.Data.DataContextProvider.Current.Promotions.FirstOrDefault(l => l.Id == promotionId);
            if (promotion == null)
            {
                return;
            }

            AspDotNetStorefront.Promotions.Data.DataContextProvider.Current.PromotionUsages.DeleteAllOnSubmit(AspDotNetStorefront.Promotions.Data.DataContextProvider.Current.PromotionUsages.Where(pu => pu.PromotionId == promotion.Id));
            AspDotNetStorefront.Promotions.Data.DataContextProvider.Current.Promotions.DeleteOnSubmit(promotion);
            AspDotNetStorefront.Promotions.Data.DataContextProvider.Current.SubmitChanges();
        }
    }
Example #6
0
        static bool IsGiftProductDiscountValid(PromotionsData.Promotion promotion, IRuleContext ruleContext, IDiscountContext discountContext)
        {
            // If the store isn't concerned with quantity on hand, all
            if (!AppLogic.AppConfigBool("Inventory.LimitCartToQuantityOnHand") &&
                Store.StoreCount == 1)
            {
                return(true);
            }

            var giftDiscounts = promotion
                                .PromotionDiscounts
                                .OfType <GiftProductPromotionDiscount>();

            // If this promotion doesn't contain gift product discounts, return it as valid.
            if (!giftDiscounts.Any())
            {
                return(true);
            }

            // If there's inventory for all gifted products then return the promotion as valid.
            return(giftDiscounts.All(giftDiscount =>
            {
                // Determine the quantity of gifted products.
                var quantityToGift = giftDiscount.MatchQuantities
                                        ? discountContext.DiscountableItems.Sum(s => s.Quantity)
                                        : 1;

                // this promotion
                return giftDiscount
                .GiftProductIds
                .All(giftProductId =>
                {
                    return AppLogic.GetInventory(
                        productId: giftProductId,
                        variantId: AppLogic.GetDefaultProductVariant(giftProductId),
                        chosenSize: string.Empty,
                        chosenColor: string.Empty) >= AppLogic.AppConfigNativeInt("HideProductsWithLessThanThisInventoryLevel") &&
                    new Product(giftProductId).IsMappedToStore();
                });
            }));
        }
    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;
            }
        }
    }
Example #9
0
        public static List <DiscountableItem> GetDiscountableItems(IRuleContext ruleContext, int promoId)
        {
            List <DiscountableItem> discountableItems   = new List <DiscountableItem>();
            List <Int32>            nonDiscountableIds  = GetNonDiscountableShoppingCartIds(ruleContext);
            PromotionController     promotionController = CreatePromotionController();

            PromotionsData.Promotion promotion = PromotionsData.DataContextProvider.Current.Promotions.FirstOrDefault(p => p.Active && p.Id == promoId);
            ruleContext.PromotionId = promotion.Id;

            if (promotionController.ValidatePromotion(promotion, ruleContext, AppLogic.CustomerLevelAllowsCoupons(ruleContext.CustomerLevel)).All(vr => vr.IsValid))
            {
                foreach (PromotionDiscountBase pd in promotion.PromotionDiscounts.Where(p => p.GetType() == typeof(OrderItemPromotionDiscount) || p.GetType() == typeof(GiftProductPromotionDiscount) || p.GetType() == typeof(OrderPromotionDiscount)))
                {
                    CategoryPromotionRule     categoryIdRule     = new CategoryPromotionRule();
                    SectionPromotionRule      sectionIdRule      = new SectionPromotionRule();
                    ManufacturerPromotionRule manufacturerIdRule = new ManufacturerPromotionRule();
                    ProductIdPromotionRule    productIdRule      = new ProductIdPromotionRule();

                    if (promotion.PromotionRules.Where(pr => pr.GetType() == typeof(CategoryPromotionRule)).Count() > 0)
                    {
                        categoryIdRule = promotion.PromotionRules.Where(pr => pr.GetType() == typeof(CategoryPromotionRule)).First() as CategoryPromotionRule;
                    }

                    if (promotion.PromotionRules.Where(pr => pr.GetType() == typeof(SectionPromotionRule)).Count() > 0)
                    {
                        sectionIdRule = promotion.PromotionRules.Where(pr => pr.GetType() == typeof(SectionPromotionRule)).First() as SectionPromotionRule;
                    }

                    if (promotion.PromotionRules.Where(pr => pr.GetType() == typeof(ManufacturerPromotionRule)).Count() > 0)
                    {
                        manufacturerIdRule = promotion.PromotionRules.Where(pr => pr.GetType() == typeof(ManufacturerPromotionRule)).First() as ManufacturerPromotionRule;
                    }

                    if (promotion.PromotionRules.Where(pr => pr.GetType() == typeof(ProductIdPromotionRule)).Count() > 0)
                    {
                        productIdRule = promotion.PromotionRules.Where(pr => pr.GetType() == typeof(ProductIdPromotionRule)).First() as ProductIdPromotionRule;
                    }

                    foreach (ShoppingCartItem cartItem in ruleContext.ShoppingCartItems)
                    {
                        bool qualifies = true;
                        if (categoryIdRule.CategoryIds != null && categoryIdRule.CategoryIds.Count() > 0 && cartItem.CategoryIds != null && cartItem.CategoryIds.Intersect(categoryIdRule.CategoryIds).Count() == 0)
                        {
                            qualifies = false;
                        }

                        if (sectionIdRule.SectionIds != null && sectionIdRule.SectionIds.Count() > 0 && cartItem.SectionIds != null && cartItem.SectionIds.Intersect(sectionIdRule.SectionIds).Count() == 0)
                        {
                            qualifies = false;
                        }

                        if (manufacturerIdRule.ManufacturerIds != null && manufacturerIdRule.ManufacturerIds.Count() > 0 && cartItem.ManufacturerIds != null && cartItem.ManufacturerIds.Intersect(manufacturerIdRule.ManufacturerIds).Count() == 0)
                        {
                            qualifies = false;
                        }

                        if (productIdRule.ProductIds != null && !productIdRule.ProductIds.Contains(cartItem.ProductId))
                        {
                            qualifies = false;
                        }

                        if (nonDiscountableIds.Contains(cartItem.ShoppingCartRecordId))
                        {
                            qualifies = false;
                        }

                        if (qualifies)
                        {
                            if (pd.GetType() == typeof(OrderItemPromotionDiscount) || pd.GetType() == typeof(GiftProductPromotionDiscount))
                            {
                                DiscountableItem discountableItem = new DiscountableItem();
                                discountableItem.CartPrice            = cartItem.CartPrice;
                                discountableItem.ProductId            = cartItem.ProductId;
                                discountableItem.Quantity             = cartItem.Quantity;
                                discountableItem.ShoppingCartRecordId = cartItem.ShoppingCartRecordId;
                                discountableItem.Sku       = cartItem.Sku;
                                discountableItem.Subtotal  = cartItem.Subtotal;
                                discountableItem.VariantId = cartItem.VariantId;
                                discountableItems.Add(discountableItem);
                            }
                        }
                    }
                }
            }
            return(discountableItems);
        }