示例#1
0
        /// <summary>
        /// Function to check if the coupon code is valid
        /// </summary>
        /// <param name="couponCode">Coupon code</param>
        /// <param name="userID">User ID</param>
        /// <returns>Coupon validation flag</returns>
        public int CheckCouponCode(string couponCode, string userID)
        {
            DataTable dt = objDAL.CheckCouponCode(couponCode);

            /*Code for coupon validation
             * 0. Error
             * 1. Valid
             * 2. Invalid coupon: Already used.
             * 3. Invalid coupon: Coupon not applicable.
             * 4. Wrong coupon code.*/
            if (dt.Rows.Count > 0)
            {
                DataRow row = dt.Rows[0];

                /*Coupon types
                 * 1. Normal coupon: Used 1 per user.
                 * 2. First time coupon: Used only for first order.
                 */
                Coupon coupon = new Coupon
                {
                    CouponID   = Convert.ToString(row["CouponID"]),
                    CouponCode = Convert.ToString(row["CouponCode"]),
                    CouponType = Convert.ToInt32(row["CouponTYpe"]),
                    Amount     = Math.Round(Convert.ToDecimal(row["Amount"]), 2)
                };

                if (coupon.CouponType == 1) //Normal coupon.
                {
                    //Check if the coupon has been already used by the user.
                    bool isValid = objDAL.CheckCouponCode(coupon.CouponID, userID);

                    if (isValid)
                    {
                        return(1);
                    }
                    else
                    {
                        return(1);
                    }
                }
                else if (coupon.CouponType == 2) //First time coupon
                {
                    //Check if the user is ordering for the first time.
                    bool isValid = objDAL.CheckOrder(userID);

                    if (isValid)
                    {
                        return(1);
                    }
                    else
                    {
                        return(3);
                    }
                }
            }
            else
            {
                return(4);
            }

            return(0);
        }