Esempio n. 1
0
    private HashSet <string> GetExistingCodes()
    {
        // Prepare query for codes cache
        var existingQuery = CouponCodeInfoProvider.GetCouponCodes(SiteContext.CurrentSiteName).Column("CouponCodeCode").Distinct();

        // Restrict cache if prefix specified
        if (!string.IsNullOrEmpty(prefix))
        {
            existingQuery.Where("CouponCodeCode", QueryOperator.Like, prefix + "%");
        }

        // Create cache of this site coupon codes
        var existingCodes = new HashSet <string>();

        using (DbDataReader reader = existingQuery.ExecuteReader())
        {
            while (reader.Read())
            {
                existingCodes.Add(reader.GetString(0));
            }
        }

        return(existingCodes);
    }
    /// <summary>
    /// Returns dictionary of discount coupon use count and limit. Key of the dictionary is the ID of discount.
    /// </summary>
    /// <param name="type">Object type (ignored).</param>
    /// <param name="discountIDs">IDs of discount which the dictionary is to be filled with.</param>
    private SafeDictionary <int, IDataContainer> GetCountsDataHandler(string type, IEnumerable <int> discountIDs)
    {
        DataSet counts = CouponCodeInfoProvider.GetCouponCodeUseCount(discountIDs);

        return(counts.ToDictionaryById("CouponCodeDiscountID"));
    }
    protected object ugDiscounts_OnExternalDataBound(object sender, string sourceName, object parameter)
    {
        var discountRow  = parameter as DataRowView;
        var discountInfo = GetDiscountInfo(discountRow?.Row);

        if (discountInfo == null)
        {
            return(String.Empty);
        }

        switch (sourceName.ToLowerInvariant())
        {
        // Append to a value field char '%' or site currency in case of flat discount
        case "value":
            return(GetDiscountGridValue(discountInfo));

        // Display discount status
        case "status":
            // Ensure correct values for unigrid export
            if (sender == null)
            {
                return(discountInfo.DiscountStatus.ToLocalizedString("com.discountstatus"));
            }

            return(new DiscountStatusTag(couponCountsDataProvider, discountInfo));

        case "discountorder":
            // Ensure correct values for unigrid export
            if ((sender == null) || !ECommerceContext.IsUserAuthorizedToModifyDiscount())
            {
                return(discountInfo.DiscountOrder);
            }

            return(new PriorityInlineEdit
            {
                PrioritizableObject = discountInfo,
                Unigrid = ugDiscounts
            });

        case "application":
            // Display blank value if discount don't use coupons
            if (!discountInfo.DiscountUsesCoupons)
            {
                return("&mdash;");
            }

            return(discountInfo.HasCoupons ? CouponCodeInfoProvider.GetCouponUsageInfoMessage(discountInfo, true) : GetString("com.discount.notcreated"));

        case "orderamount":
            decimal totalPriceInMainCurrency = ValidationHelper.GetDecimal(discountInfo.DiscountItemMinOrderAmount, 0m);

            // Display blank value in the discount listing if order amount is not configured
            if (totalPriceInMainCurrency == 0)
            {
                return(string.Empty);
            }

            // Format currency
            string priceInMainCurrencyFormatted = CurrencyInfoProvider.GetFormattedPrice(totalPriceInMainCurrency, SiteContext.CurrentSiteID);
            return(HTMLHelper.HTMLEncode(priceInMainCurrencyFormatted));
        }

        return(parameter);
    }
Esempio n. 4
0
    private void GenerateCodes(object parameter)
    {
        try
        {
            // Construct cache for code uniqueness check
            var existingCodes = GetExistingCodes();

            CouponCodeInfo couponCode = null;

            using (CMSActionContext context = new CMSActionContext())
            {
                // Do not touch parent for all codes
                context.TouchParent = false;
                context.LogEvents   = false;

                // Create generator
                RandomCodeGenerator generator = new RandomCodeGenerator(pattern, prefix);
                // Use cache for checking code uniqueness
                generator.CodeChecker = code => !existingCodes.Contains(code);

                for (int i = 0; i < count; i++)
                {
                    // Get new code
                    string code = generator.GenerateCode();

                    couponCode = new CouponCodeInfo
                    {
                        CouponCodeUseLimit   = numberOfUses,
                        CouponCodeDiscountID = discountId,
                        CouponCodeCode       = code,
                        CouponCodeUseCount   = 0
                    };

                    // Save coupon code
                    CouponCodeInfoProvider.SetCouponCodeInfo(couponCode);

                    // Log that coupon was created
                    AddLog(string.Format(GetString("com.couponcode.generated"), HTMLHelper.HTMLEncode(ResHelper.LocalizeString(code))));
                }
            }

            // Touch parent (one for all)
            if (couponCode != null)
            {
                couponCode.Generalized.TouchParent();
            }

            // Log information that coupons were generated
            EventLogProvider.LogEvent(EventType.INFORMATION, "Discounts", "GENERATECOUPONCODES",
                                      string.Format("{0} coupon codes for discount '{1}' successfully generated.", count, ParentDiscount.DiscountDisplayName),
                                      userId: CurrentUser.UserID,
                                      userName: CurrentUser.UserName,
                                      siteId: SiteContext.CurrentSiteID
                                      );
        }
        catch (Exception ex)
        {
            CurrentError = GetString("com.couponcode.generateerror");
            EventLogProvider.LogException("Discounts", "GENERATECOUPONCODES", ex);
        }
    }