Exemplo n.º 1
0
        public CouponBaseDTO GetCouponBaseToken(int couponInstanceId, out string error)
        {
            error = string.Empty;

            var inst = CouponInstanceRepository.GetById(couponInstanceId);

            if (inst == null)
            {
                error = "coupon instance not found";
                return(null);
            }

            var coupon = CouponRepository.GetById(inst.CouponId);

            if (coupon == null)
            {
                error = "coupon not found";
                return(null);
            }

            return(coupon.Entity2CouponBaseDTO());
        }
Exemplo n.º 2
0
        public CouponValidationToken ValidateCoupon(int priceLineId, int couponOwnerId, int?courseId, int?bundleId, int couponInstanceId)
        {
            var result = new CouponValidationToken
            {
                IsValid    = false
                , Discount = 0
            };

            var inst = CouponInstanceRepository.GetById(couponInstanceId);

            if (inst == null)
            {
                result.Message = "coupon instance not found";
                return(result);
            }

            var coupon = CouponRepository.GetById(inst.CouponId).Entity2AuthorCouponDTO();

            if (coupon == null)
            {
                result.Message = "coupon not found";
                return(result);
            }

            var priceToken = GetItemPriceToken(priceLineId);

            if (priceToken == null)
            {
                result.Message = "Price Line not found";
                return(result);
            }

            decimal basePrice;

            if (courseId != null)
            {
                var course = CourseRepository.GetById((int)courseId);

                if (course == null)
                {
                    result.Message = "course not found";
                    return(result);
                }

                if (coupon.CourseId != null)
                {
                    if (coupon.CourseId != courseId)
                    {
                        result.Message = "coupon not allowed to this course";
                        return(result);
                    }
                }
                else
                {
                    if (coupon.OwnerUserId == null || (coupon.OwnerUserId != course.AuthorUserId))
                    {
                        result.Message = "coupon not allowed to this course";
                        return(result);
                    }
                }

                var itemPrice = priceToken.Price;

                if (itemPrice == 0)
                {
                    result.Message = "invalid price";
                    return(result);
                }

                basePrice = itemPrice;
            }
            else if (bundleId != null)
            {
                var bundle = BundleRepository.GetById((int)bundleId);

                if (bundle == null)
                {
                    result.Message = "bundle not found";
                    return(result);
                }

                if (coupon.BundleId != null)
                {
                    if (coupon.BundleId != bundleId)
                    {
                        result.Message = "coupon not allowed to this bundle";
                        return(result);
                    }
                }
                else
                {
                    if (coupon.OwnerUserId == null || (coupon.OwnerUserId != bundle.AuthorId))
                    {
                        result.Message = "coupon not allowed to this bundle";
                        return(result);
                    }
                }

                var itemPrice = priceToken.Price;

                if (itemPrice == 0)
                {
                    result.Message = "invalid price";
                    return(result);
                }

                basePrice = itemPrice;
            }
            else
            {
                result.Message = "course or bundle required";
                return(result);
            }

            var objectName = courseId != null ? "course" : "bundle";

            result.OriginalPrice = basePrice;
            result.FinalPrice    = basePrice;



            if ((coupon.CourseId.HasValue && coupon.CourseId != courseId))
            {
                result.Message = "This coupon is not allowed for this " + objectName;
            }
            else if (coupon.ExpirationDate < DateTime.Now.AddDays(-1))
            {
                result.Message = "This coupon is expired";
            }
            else if (inst.UsageLimit != -1 && inst.SALE_OrderLines.Count >= inst.UsageLimit)
            {
                result.Message = "This coupon is no longer valid";
            }
            else
            {
                if (priceToken.PriceType == BillingEnums.ePricingTypes.SUBSCRIPTION && coupon.Type != CourseEnums.CouponType.SUBSCRIPTION)
                {
                    result.Message = "This coupon is not allowed for this " + objectName;
                    return(result);
                }

                result.IsValid = true;

                switch (coupon.Type)
                {
                case CourseEnums.CouponType.PERCENT:
                case CourseEnums.CouponType.SUBSCRIPTION:
                    result.Discount   = (decimal)(coupon.Amount != null ? basePrice * (coupon.Amount / 100) : 0);
                    result.FinalPrice = CalculateDiscountedPrice(basePrice, coupon.Amount ?? 0, CourseEnums.CouponType.PERCENT);
                    result.IsFree     = result.FinalPrice == 0;
                    result.Message    = priceToken.PriceType == BillingEnums.ePricingTypes.SUBSCRIPTION ? String.Format("The price of this {2} is now {1:0.00} for the initial {0} months of your subscription. After that, regular rates shall apply", coupon.SubscriptionMonths ?? 0, result.FinalPrice, objectName) :
                                        String.Format("Coupon code is valid for a discount of {0}%. Your price is: {1} {2:0.00}", coupon.Amount, priceToken.Currency.ISO, result.FinalPrice);
                    break;

                case CourseEnums.CouponType.FIXED:
                    if (priceToken.PriceType == BillingEnums.ePricingTypes.SUBSCRIPTION)
                    {
                        result.IsValid = false;
                        result.Message = "Fixed amount coupon can't be applied to subscription";
                    }
                    else
                    {
                        result.Discount   = coupon.Amount ?? 0;
                        result.FinalPrice = CalculateDiscountedPrice(basePrice, coupon.Amount ?? 0, CourseEnums.CouponType.FIXED);
                        result.IsFree     = result.FinalPrice == 0;
                        result.Message    = string.Format("Coupon code is valid for a discount of {1} {0:0.00}, Your price is: {1} {2:0.00}", result.Discount, priceToken.Currency.ISO, result.FinalPrice);
                    }

                    break;

                case CourseEnums.CouponType.FREE:
                    result.Discount   = basePrice;
                    result.FinalPrice = 0;
                    result.IsFree     = true;
                    result.Message    = "This coupon entitles you to get this course for free. Click the button below to complete the process.";
                    break;
                }
            }

            return(result);
        }