Example #1
0
        /// <summary>
        /// Add random coupons to the database
        /// </summary>
        /// <param name="numOfCouponsToAdd"></param>
        /// <param name="r">Random number generator</param>
        /// <param name="connection">Connection object</param>
        /// <param name="txtCouponResults">Text Box to write interesting info into, or null</param>
        /// <param name="lblCouponStatus">Label to write status into, or null</param>
        /// <returns></returns>
        public int GenerateRandomCoupons()
        {
            int couponCount       = 0;
            int couponDetailCount = 0;
            int total             = 0;

            computeDefaultValues();
            CouponParameters cp        = new CouponParameters();
            SqlCommand       cmd       = new SqlCommand();
            bool             keepGoing = true;

            for (int i = 0; i < numOfCouponsToAdd && keepGoing; i++)
            {
                // Add a Coupon record and some Coupon Detail records
                try {
                    computeRandomValuesForCoupon(cp, r);
                    cmd.CommandText = "spAddCouponAndDetail";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Connection  = connection;
                    cmd.Parameters.Clear();
                    // The output parameter must be FIRST in the Parameters collection? https://support.microsoft.com/en-us/kb/308621
                    //cmd.Parameters.Add("@CouponID", SqlDbType.Int);
                    cmd.Parameters.Add(new SqlParameter("CouponID", cp.coupon));
                    cmd.Parameters["CouponID"].Direction = ParameterDirection.Output;
                    cmd.Parameters["CouponID"].SqlDbType = SqlDbType.Int;

                    cmd.Parameters.Add(new SqlParameter("Coupon", cp.coupon));
                    cmd.Parameters.Add(new SqlParameter("CouponSourceID", cp.couponSourceID));
                    cmd.Parameters.Add(new SqlParameter("CouponDescription", cp.couponDescription));
                    cmd.Parameters.Add(new SqlParameter("MaxQtyToPurchase", cp.maxQtyToPurchase));
                    cmd.Parameters.Add(new SqlParameter("MinQtyToPurchase", cp.minQtyToPurchase));
                    cmd.Parameters.Add(new SqlParameter("ProductID", cp.productID));
                    cmd.Parameters.Add(new SqlParameter("DiscountTypeID", cp.discountTypeID));
                    cmd.Parameters.Add(new SqlParameter("StartDate", cp.startDate));
                    cmd.Parameters.Add(new SqlParameter("ThroughDate", cp.throughDate));
                    cmd.Parameters.Add(new SqlParameter("PercentageDiscount", cp.percentageDiscount));
                    cmd.Parameters.Add(new SqlParameter("AmountOff", cp.amountOff));

                    cmd.ExecuteNonQuery();
                    couponCount++; couponDetailCount++;

                    WriteStatus(couponCount, couponDetailCount);
                    cp.couponID = (int)cmd.Parameters["CouponID"].Value;
                    Write("Coupon Added: CouponID = " + cp.couponID + ", Start Date = " + cp.startDate.ToShortDateString() + ", Product = " + Utils.FormatProductForPrint(cp.productID));
                } catch (Exception ex) {
                    Write("Coupon.GenerateRandomCoupon(): " + ex.Message);
                    keepGoing = false;
                }
            }
            return(total);
        }
Example #2
0
        /// <summary>
        /// Compute random record IDs for the random coupon we will add
        /// </summary>
        /// <param name="cp">The set of coupon parameters upon which to operate</param>
        public void computeRandomValuesForCoupon(CouponParameters cp, Random r)
        {
            // We need a unique value in the coupon attribute- it's the natural key
            cp.coupon = Convert.ToString(Guid.NewGuid());
            // Random Product
            cp.productID = (int)Utils.MyDLookup("productID",
                                                "(SELECT ROW_NUMBER() OVER (ORDER BY productID) AS RowNum, * FROM tProduct) sub ",
                                                " RowNum = " + (r.Next(productID_count) + 1),
                                                "");
            cp.couponSourceID = (int)Utils.MyDLookup("couponSourceID",
                                                     "(SELECT ROW_NUMBER() OVER (ORDER BY couponSourceID) AS RowNum, * FROM tCouponSource) sub ",
                                                     " RowNum = " + (r.Next(couponSourceID_count) + 1),
                                                     "");

            cp.discountTypeID = (int)Utils.MyDLookup("discountTypeID",
                                                     "(SELECT ROW_NUMBER() OVER (ORDER BY discountTypeID) AS RowNum, * FROM tdiscountType) sub ",
                                                     " RowNum = " + (r.Next(discountTypeID_count) + 1),
                                                     "");
            int    decrement = r.Next(30);
            int    increment = r.Next(30);
            String startDate, throughDate;

            startDate   = Utils.GetRandomDate(r);
            throughDate = Utils.GetRandomDate(r);
            if (r.Next() % 2 == 0)
            {
                cp.startDate = Convert.ToDateTime(startDate).AddDays(0 - decrement);
            }
            else
            {
                // Once in a while use the start date of the date range rather than a random date
                cp.startDate = Config.startDate.AddDays(0 - decrement);
            }
            cp.throughDate = Convert.ToDateTime(throughDate).AddDays(increment);
            if (cp.startDate > cp.throughDate)
            {
                DateTime tmp = cp.startDate; cp.startDate = cp.throughDate; cp.throughDate = tmp;
            }
            cp.couponDescription  = " ";
            cp.minQtyToPurchase   = 1;
            cp.maxQtyToPurchase   = Convert.ToInt32(1 + (.019 * r.Next(909)));
            cp.percentageDiscount = 1 + r.Next(90);
            try {
                cp.amountOff = Convert.ToDouble(Utils.MyDLookup("InitialPricePerSellableUnit", "tProduct", "ProductID = " + cp.productID, null));
            } catch (Exception ex) { cp.amountOff = .25 + (1.00 * r.NextDouble()); Utils.Log(ex.Message); }  // Default to some random value between .25 and $1.25 if something goes wrong and we can't get a price for this product.
            cp.amountOff = Math.Round(cp.amountOff, 2);
        }