protected void ApplyPromoClick(object sender, EventArgs e)
        {
            rowMessage.Visible      = false;
            msrp.Visible            = false;
            MessageLabel.Text       = string.Empty;
            LitDisclaimer.Text      = string.Format(Message.TermWithPromo, string.Empty);
            discountInfoRow.Visible = false;
            bool isLimit;

            if (string.IsNullOrEmpty(PromoText.Text))
            {
                InitDefaultPromo(Message.InvalidOrExpiredPromo, false);
                return;
            }

            var  giftCard   = _customerCreditRepository.GetGiftCardByCode(PromoText.Text.Trim());
            bool isGiftCard = giftCard != null;

            if (giftCard != null)
            {
                if (PublicCustomerCredits == null)
                {
                    InitDefaultPromo(ErrorMessage.YouMustLogIn, false);
                    return;
                }

                if (giftCard.Status == (short)Enums.GiftCardType.Used)
                {
                    InitDefaultPromo(ErrorMessage.GiftCardHasBeenUsed, false);
                    return;
                }

                if (!string.IsNullOrEmpty(giftCard.EmailAddress) &&
                    !PublicCustomerInfos.EmailAddress.Equals(giftCard.EmailAddress, StringComparison.OrdinalIgnoreCase))
                {
                    InitDefaultPromo(ErrorMessage.PleaseCheckYourGiftCardWithCurrentAccount, false);
                    return;
                }

                _customerCreditRepository.AddGiftCard(PublicCustomerCredits, PromoText.Text.Trim());
                PublicCustomerCredits = _customerCreditRepository.Refresh(PublicCustomerCredits);
                CacheLayer.Clear(CacheKeys.GiftCardCacheKey);
                CacheLayer.Clear(CacheKeys.CustomerCreditsCacheKey);
                CacheLayer.Clear(CacheKeys.CustomerCreditLogsCacheKey);
                BindSubscriptionInfos();
                InitDefaultPromo(ErrorMessage.GiftCardHasBeenApplied);
                BookProductUpdatePanel.Update();
            }

            if (!isGiftCard)
            {
                var param = new GetDiscountValidByCodeParams
                {
                    Code           = PromoText.Text.Trim(),
                    SubscriptionId = PublicSubscription.Id,
                    CustomerId     = PublicCustomerInfos != null ? PublicCustomerInfos.CustomerId : 0,
                    IsAdmin        = false,
                    BookingId      = 0
                };

                Discounts discounts = _discountRepository.GetDiscountValidByCode(param, out isLimit);
                if (discounts == null)
                {
                    InitDefaultPromo(isLimit ? Message.ExceedLimit : Message.InvalidOrExpiredPromo, false);
                    return;
                }

                if (discounts.MinAmount > PublicSubscription.Price * TotalTickets)
                {
                    InitDefaultPromo(ErrorMessage.MinAmountNotMatch, false);
                    return;
                }

                double actualPrice = NormalPrice;
                actualPrice = Helper.CalculateDiscount(discounts, actualPrice, TotalTickets);

                if (!actualPrice.Equals(NormalPrice))
                {
                    msrp.Visible       = true;
                    msrpPrice.Text     = Helper.FormatPrice(NormalPrice * TotalTickets);
                    LitDisclaimer.Text = string.Format(Message.TermWithPromo, Message.PeriodString);
                }

                moneyPrice.Text    = Helper.FormatPrice(actualPrice * TotalTickets);
                perMoneyPrice.Text = Helper.FormatPrice(actualPrice);

                string json = JsonConvert.SerializeObject(discounts, CustomSettings.SerializerSettings());
                Session[_discountKey] = json;
                BindSubscriptionInfos();
            }
        }
Exemple #2
0
        public Discounts GetDiscountValidByCode(GetDiscountValidByCodeParams param, out bool isLimit)
        {
            isLimit = false;

            // Products
            var discount = (from p1 in DiscountProductList
                            join p in DiscountList on p1.DiscountId equals p.Id
                            where p.Code.ToLower() == param.Code.Trim().ToLower() &&
                            !p.IsDelete &&
                            p1.ProductId == param.ProductId &&
                            p.StartDate.HasValue &&
                            p.EndDate.HasValue &&
                            (param.IsAdmin || (p.StartDate.Value.Date <= DateTime.UtcNow.ToLosAngerlesTimeWithTimeZone(param.TimezoneId).Date &&
                                               p.EndDate.Value.Date >= DateTime.UtcNow.ToLosAngerlesTimeWithTimeZone(param.TimezoneId).Date))
                            select p).FirstOrDefault();

            // Subscription
            if (param.SubscriptionId > 0)
            {
                discount = (from ds in DiscountSubscriptionList
                            join d in DiscountOfSubscriptionList on ds.DiscountId equals d.Id
                            where d.Code.ToLower() == param.Code.Trim().ToLower() &&
                            !d.IsDelete &&
                            ds.SubscriptionId == param.SubscriptionId &&
                            d.StartDate.HasValue &&
                            d.EndDate.HasValue &&
                            d.StartDate.Value.Date <= DateTime.UtcNow.ToLosAngerlesTimeWithTimeZone(param.TimezoneId).Date &&
                            d.EndDate.Value.Date >= DateTime.UtcNow.ToLosAngerlesTimeWithTimeZone(param.TimezoneId).Date
                            select d).FirstOrDefault();

                if (discount == null)
                {
                    discount = (from ds in DiscountSubscriptionList
                                join d in DiscountList on ds.DiscountId equals d.Id
                                where d.Code.ToLower() == param.Code.Trim().ToLower() &&
                                !d.IsDelete &&
                                ds.SubscriptionId == param.SubscriptionId &&
                                d.StartDate.HasValue &&
                                d.EndDate.HasValue &&
                                d.StartDate.Value.Date <= DateTime.UtcNow.ToLosAngerlesTimeWithTimeZone(param.TimezoneId).Date &&
                                d.EndDate.Value.Date >= DateTime.UtcNow.ToLosAngerlesTimeWithTimeZone(param.TimezoneId).Date
                                select d).FirstOrDefault();
                }
            }

            // Discount AllProduct with Code Required
            if (discount == null)
            {
                discount = DiscountList.FirstOrDefault(x => x.IsAllProducts &&
                                                       x.Code.ToLower() == param.Code.Trim().ToLower() &&
                                                       x.CodeRequired &&
                                                       !x.IsDelete &&
                                                       x.StartDate.HasValue &&
                                                       x.EndDate.HasValue &&
                                                       (param.IsAdmin || (x.StartDate.Value.Date <= DateTime.UtcNow.ToLosAngerlesTimeWithTimeZone(param.TimezoneId).Date &&
                                                                          x.EndDate.Value.Date >= DateTime.UtcNow.ToLosAngerlesTimeWithTimeZone(param.TimezoneId).Date)));
            }

            // Discount AllProduct with Code Not Required
            if (discount == null)
            {
                discount = DiscountList.FirstOrDefault(x => x.IsAllProducts &&
                                                       x.Code.ToLower() == param.Code.Trim().ToLower() &&
                                                       !x.IsDelete &&
                                                       x.StartDate.HasValue &&
                                                       x.EndDate.HasValue &&
                                                       (param.IsAdmin || (x.StartDate.Value.Date <= DateTime.UtcNow.ToLosAngerlesTimeWithTimeZone(param.TimezoneId).Date &&
                                                                          x.EndDate.Value.Date >= DateTime.UtcNow.ToLosAngerlesTimeWithTimeZone(param.TimezoneId).Date)));
            }

            if (discount != null && param.CustomerId > 0)
            {
                var bookings = (from b in BookingList
                                join db in DiscountBookingList on b.BookingId equals db.BookingId
                                join ci in CustomerInfoList on b.CustomerId equals ci.CustomerId
                                where ci.CustomerId == param.CustomerId && db.DiscountId == discount.Id &&
                                b.PassStatus != (int)Enums.BookingStatus.Refunded &&
                                b.BookingId != param.BookingId
                                select b).Count();

                if (discount.MaxPurchases != 0 && discount.MaxPurchases > bookings)
                {
                    return(discount);
                }
                isLimit = true;
                return(null);
            }

            return(discount);
        }