protected override void PopulateControl()
 {
     base.PopulateControl();
     if (this.DataSource != null)
     {
         if (Convert.ToString(this.DataSource.Invoiced) == "1")
         {
             this.LockField(this.Product);
             this.LockField(this.StartDate);
             this.LockField(this.EndDate);
             this.LockField(this.Fee);
             this.LockField(this.Vatcode);
             this.AllowDelete = false;
         }
     }
     else
     {
         this.StartDate.FieldValue = DateTime.Now;
         this.Status.FieldValue    = "0";
         string mepId = Request.QueryString["mepid"];
         if (mepId != "")
         {
             MemberPeriod mep = MemberPeriod.FetchById(mepId);
             this.EndDate.FieldValue = mep.End;
         }
     }
 }
Exemplo n.º 2
0
        protected override void PopulateControl()
        {
            base.PopulateControl();


            if (this.DataSource != null && this.DataSource.Id != null)
            {
                this.Address.DataSource     = this.DataSource.MailAddId;
                this.Address.OrganisationId = this.DataSource.OrgIdId;
                this.Address.PersonId       = this.DataSource.Role.PersonId;

                this.LapseDate.Visible = this.DataSource.CancelDate.HasValue;
                if (this.DataSource.RenewLoc == 1)
                {
                    this.RenewLoc.IsReadOnly = UserInterface.ConfigurableBoolean.True;
                }

                if (this.DataSource.Product.MsGroup == 30)
                {
                    this.TotalQty.FieldValue = (this.DataSource.MemberPeriod.Qty2.Value + this.DataSource.MemberPeriod.Qty3.Value + 1).ToString();
                }
                else
                {
                    this.TotalQty.FieldValue = (this.DataSource.MemberPeriod.Qty1.Value).ToString();
                }

                this.AllowDelete = this.DataSource.RenewLoc != 1;
            }
            else
            {
                //New Location record - prepopulate values from Member Period
                string mepId = Request.QueryString["memberPeriodId"];
                if (mepId != "")
                {
                    MemberPeriod mep = MemberPeriod.FetchById(mepId);
                    this.StartDate.FieldValue   = mep.Start;
                    this.EndDate.FieldValue     = mep.End;
                    this.ProductId.FieldValue   = mep.ProductId;
                    this.PriceListId.FieldValue = mep.PriceListId;

                    if (mep.Product.MsGroup == 30)
                    {
                        this.TotalQty.FieldValue = (mep.Qty2.Value + mep.Qty3.Value + 1).ToString();
                    }
                    else
                    {
                        this.TotalQty.FieldValue = (mep.Qty1.Value).ToString();
                    }
                }
                this.LapseDate.Visible = false;
            }

            this.Address.HideAddressContactInfo = true;
        }
        private decimal?CalculateOutstandingBalance(MemberPeriod membership)
        { //calculate Outstanding balance
            decimal?invUnpaidVal = null;

            if (membership.InvHeader.Count > 0)
            {
                decimal totalUnpaidValue = 0;
                foreach (Invoice i in membership.InvHeader)
                {
                    totalUnpaidValue += i.UnpaidValue;
                }
                return(totalUnpaidValue);
            }
            return(invUnpaidVal);
        }
Exemplo n.º 4
0
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                var mp = new MemberPeriod();

                if (MemberPeriodID > 0)
                {
                    mp = repo.Single <MemberPeriod>(MemberPeriodID);
                }

                if (!string.IsNullOrEmpty(tbMemberID.Text.Trim()))
                {
                    mp.MemberID = Convert.ToInt32(tbMemberID.Text.Trim());
                }
                else
                {
                    throw new Exception("Member ID can't be empty");
                }

                mp.MemberName = tbMemberName.Text.Trim();
                mp.IsActive   = cbIsActive.Checked;

                if (!string.IsNullOrEmpty(ddlMemberClass.SelectedValue))
                {
                    mp.MemberClass =
                        (MemberClassType)Enum.Parse(typeof(MemberClassType), ddlMemberClass.SelectedValue);
                }
                else
                {
                    throw new Exception("MemberClass can't be empty");
                }

                mp.MemberCardNo = tbMemberCardNo.Text.Trim();

                if (!string.IsNullOrEmpty(tbOrderID.Text.Trim()))
                {
                    mp.OrderID = Convert.ToInt32(tbOrderID.Text.Trim());
                }
                else
                {
                    mp.OrderID = null;
                }

                mp.StartDate   = Convert.ToDateTime(tbStartDate.Text.Trim());
                mp.EndDate     = Convert.ToDateTime(tbEndDate.Text.Trim());
                mp.Description = tbDescription.Text.Trim();
                mp.Remark      = tbRemark.Text.Trim();

                if (MemberPeriodID > 0)
                {
                    repo.Update(mp);

                    ClientScript.RegisterClientScriptBlock(typeof(string), "succeed",
                                                           "alert('更新成功');window.location.href=window.location.href", true);
                }
                else
                {
                    // Whether this MemberPeriod Exists
                    var list = repo.Query <MemberPeriod>(x => x.MemberID == mp.MemberID);

                    if (list.Any(x => x.StartDate <= mp.StartDate && x.EndDate >= mp.EndDate))
                    {
                        throw new Exception($"The Member Period in active for this Member(No.{mp.MemberID})");
                    }

                    repo.Insert(mp);

                    ClientScript.RegisterClientScriptBlock(typeof(string), "succeed",
                                                           "alert('添加成功');window.location.href = 'AdminMemberPeriod.aspx'", true);
                }
            }
            catch (Exception ex)
            {
                ClientScript.RegisterClientScriptBlock(typeof(string), "failed", $"alert('{ex.Message}')", true);
            }
        }
Exemplo n.º 5
0
        public bool CheckMemberCanPurchase(MemberPeriod mp)
        {
            // Check Member Class for Purchase the MatchTicket
            bool retValue;

            if (AllowMemberClass.HasValue)
            {
                retValue = mp != null && mp.IsActive && (int)mp.MemberClass >= AllowMemberClass.Value;
            }
            else
            {
                retValue = true;
            }

            return retValue;
        }
        private void InitForm()
        {
            try
            {
                lblMemberName.Text = $"<b>{MemberName}</b> (<em>NO.{Mid}</em>)";

                if (OrderID > 0)
                {
                    var o = (OrdrTicket)Order.Select(OrderID);

                    // For Vincent Song to View the MatchTickets Confirmation Page
                    if (ConfigGlobal.IsPluginAdmin(Uid) || (Uid.Equals(33067) && (int)o.Status >= 3))
                    {
                        lblMemberName.Text = $"<b>{o.MemberName}</b> (<em>NO.{o.MemberID}</em>)";
                    }
                    else
                    {
                        if (o == null || !o.MemberID.Equals(Mid) || !o.IsActive)
                        {
                            throw new Exception("此订单无效或非当前用户订单");
                        }
                    }

                    #region Bind OrderView Status Workflow

                    if (ucPortalWorkflowInfo != null)
                    {
                        ucPortalWorkflowInfo.JSONOrderStatusList = $"[ {string.Join(",", o.StatusWorkflowInfo)} ]";
                        ucPortalWorkflowInfo.CurrOrderStatus     = o.Status;
                    }

                    #endregion

                    var m = _repo.Single <Member>(o.MemberID);

                    lblOrderMobile.Text = $"<em>{o.Mobile}</em>";

                    #region Set Member Nation & Region

                    if (!string.IsNullOrEmpty(m.Nation))
                    {
                        if (m.Nation.Equals("中国"))
                        {
                            lblMemberRegion.Text = "中国 ";

                            var region = m.Region.Split('|');

                            foreach (var t in region)
                            {
                                int regionId;

                                if (int.TryParse(t, out regionId))
                                {
                                    lblMemberRegion.Text += DictionaryItem.Cache.Load(regionId).Name + " ";
                                }
                            }
                        }
                        else
                        {
                            lblMemberRegion.Text = m.Nation;
                        }
                    }
                    else
                    {
                        lblMemberRegion.Text = "无";
                    }

                    #endregion

                    lblMemberIDCardNo.Text     = m.IDCardNo;
                    lblMemberPassportNo.Text   = m.PassportNo;
                    lblMemberPassportName.Text = m.PassportName;
                    lblMemberWeChat.Text       = $"<em>{m.WeChat}</em>";
                    lblMemberEmail.Text        = $"<em>{m.Email}</em>";

                    lblOrderID.Text          = $"<em>{o.ID}</em>";
                    lblOrderCreateTime.Text  = o.CreateTime.ToString("yyyy-MM-dd HH:mm");
                    lblOrderDescription.Text = o.Description;

                    if (!string.IsNullOrEmpty(o.Remark))
                    {
                        lblOrderRemark.Text   = o.Remark.Replace("\r\n", "<br />");
                        phOrderRemark.Visible = true;
                    }
                    else
                    {
                        phOrderRemark.Visible = false;
                    }

                    // Should be Calculator in this Page
                    double price;
                    string priceInfo;

                    var oiMatchTicket = o.OIMatchTicket;

                    // Get Order MatchTicket Info

                    bool isMemberCouldPurchase;

                    if (oiMatchTicket != null && oiMatchTicket.IsActive)
                    {
                        var mt = MatchTicket.Cache.Load(oiMatchTicket.MatchGuid);

                        if (mt == null)
                        {
                            throw new Exception("无相关比赛信息,请联系管理员");
                        }

                        var mp = MemberPeriod.GetCurrentMemberPeriodByMemberID(Mid);

                        isMemberCouldPurchase = mt.CheckMemberCanPurchase(mp);

                        var p = Product.Cache.Load(mt.ProductCode);

                        if (p == null)
                        {
                            throw new Exception("无相关商品信息,请联系管理员");
                        }

                        lblMatchTicketInfo.Text =
                            $"<em>【{mt.LeagueName}】{mt.TeamName}({Arsenal_Team.Cache.Load(mt.TeamGuid).TeamEnglishName})</em>";
                        lblMatchTicketPlayTime.Text =
                            $"<em>【伦敦】{mt.PlayTimeLocal.ToString("yyyy-MM-dd HH:mm")}</em>";

                        var strRank = mt.ProductInfo.Trim();
                        if (lblMatchTicketRank != null && !string.IsNullOrEmpty(strRank))
                        {
                            lblMatchTicketRank.Text = $"<em>{strRank.Substring(strRank.Length - 7, 7)}</em>";
                        }

                        if (mt.AllowMemberClass.HasValue && mt.AllowMemberClass.Value == 2)
                        {
                            lblMatchTicketAllowMemberClass.Text = "<em>只限高级会员 (Premier) </em>";
                        }
                        else if (mt.AllowMemberClass.HasValue && mt.AllowMemberClass == 1)
                        {
                            lblMatchTicketAllowMemberClass.Text = "<em>普通会员 (Core) 以上</em>";
                        }
                        else
                        {
                            lblMatchTicketAllowMemberClass.Text = "无";
                        }

                        ucPortalMatchInfo.MatchGuid = mt.ID;

                        lblOrderItem_TravelDate.Text = oiMatchTicket.TravelDate.ToString("yyyy年MM月dd日");


                        // Set Order Price

                        price     = oiMatchTicket.TotalPrice;
                        priceInfo = string.Format("<合计> {2}:{0} × {1}", oiMatchTicket.UnitPrice.ToString("f2"),
                                                  oiMatchTicket.Quantity,
                                                  Product.Cache.Load(oiMatchTicket.ProductGuid).DisplayName);

                        tbOrderPrice.Text  = price.ToString(CultureInfo.CurrentCulture);
                        lblOrderPrice.Text = $"{priceInfo} = <em>{price.ToString("f2")}</em>元";

                        if (o.Status.Equals(OrderStatusType.Draft))
                        {
                            btnSubmit.Visible = true;
                            btnModify.Visible = true;
                            btnCancel.Visible = true;

                            if (!isMemberCouldPurchase)
                            {
                                lblOrderRemark.Text =
                                    $@"<em style='line-height: 1.8'>由于球票供应有限,所有主场球票预订均只向(Core/Premier)会员开放。<br />
                                        <a href='ServerMembershipCheck.ashx?OrderID={OrderID}' target='_blank' style='background: #fff48d'>
                                        【点击这里】请在续费或升级会员资格后,才能提交订单。</a></em>";
                                phOrderRemark.Visible = true;

                                btnSubmit.Visible = false;
                            }
                        }
                        else if (o.Status.Equals(OrderStatusType.Submitted))
                        {
                            btnSubmit.Visible = false;
                            btnModify.Visible = false;
                            btnCancel.Visible = true;

                            ucPortalProductQrCode.QrCodeUrl      = p.QrCodeUrl;
                            ucPortalProductQrCode.QrCodeProvider = "淘宝";
                        }
                        else
                        {
                            btnSubmit.Visible = false;
                            btnModify.Visible = false;
                            btnCancel.Visible = false;
                        }
                    }
                    else
                    {
                        throw new Exception("此订单未填写订票信息");
                    }
                }
                else
                {
                    throw new Exception("此订单不存在");
                }
            }
            catch (Exception ex)
            {
                ClientScript.RegisterClientScriptBlock(typeof(string), "failed",
                                                       $"alert('{ex.Message}');window.location.href = 'iArsenalOrder.aspx'", true);
            }
        }
        public void ProcessRequest(HttpContext context)
        {
            using (var conn = new SqlConnection(DataAccess.ConnectString))
            {
                conn.Open();
                var trans = conn.BeginTransaction();

                try
                {
                    var strOrderId = context.Request.QueryString["OrderID"];
                    int id;

                    IRepository repo = new Repository();

                    if (!string.IsNullOrEmpty(strOrderId) && int.TryParse(strOrderId, out id))
                    {
                        // Get Current MatchTicket Order
                        var oTicket = (OrdrTicket)Order.Select(id);

                        // Get Order MatchTicket Info
                        var oiMatchTicket = oTicket?.OIMatchTicket;

                        if (oiMatchTicket != null && oiMatchTicket.IsActive)
                        {
                            var mt = MatchTicket.Cache.Load(oiMatchTicket.MatchGuid);
                            var mp = MemberPeriod.GetCurrentMemberPeriodByMemberID(oTicket.MemberID);

                            // isMemberCouldPurchase, should be false
                            if (!mt.CheckMemberCanPurchase(mp))
                            {
                                var currProductType = mt.AllowMemberClass == 2 ?
                                                      ProductType.MembershipPremier : ProductType.MembershipCore;

                                var p = Product.Cache.Load(currProductType).Find(x => x.IsActive);

                                var isUpgrade = (mp != null && mp.MemberClass.Equals(MemberClassType.Core) &&
                                                 mt.AllowMemberClass.HasValue && mt.AllowMemberClass == 2);

                                #region isRenew
                                var isRenew = false;

                                var mpLast = MemberPeriod.GetCurrentMemberPeriodByMemberID(oTicket.MemberID, -1);

                                if (mpLast != null && mpLast.MemberClass.Equals(MemberClassType.Core))
                                {
                                    isRenew = currProductType.Equals(ProductType.MembershipCore);
                                }
                                if (mpLast != null && mpLast.MemberClass.Equals(MemberClassType.Premier))
                                {
                                    isRenew = currProductType.Equals(ProductType.MembershipPremier);
                                }
                                #endregion

                                #region new membership order

                                var o = new Order
                                {
                                    Mobile      = oTicket.Mobile,
                                    UpdateTime  = DateTime.Now,
                                    Description = string.Empty,
                                    OrderType   = OrderBaseType.Membership,
                                    MemberID    = oTicket.MemberID,
                                    MemberName  = oTicket.MemberName,
                                    Address     = oTicket.Address,
                                    Payment     = string.Empty,
                                    Price       = 0,
                                    Sale        = null,
                                    Deposit     = null,
                                    Status      = OrderStatusType.Submitted,
                                    Rate        = 0,
                                    CreateTime  = DateTime.Now,
                                    IsActive    = true,
                                    Remark      = string.Empty
                                };

                                //Get the Order ID after Insert new one
                                object key;
                                repo.Insert(o, out key, trans);
                                var newId = Convert.ToInt32(key);
                                #endregion

                                #region new membership orderItem
                                //New Order Items
                                var oi = new OrdrItmMembership();

                                // Set AlterMethod & MemberCardNo
                                if (isUpgrade)
                                {
                                    oi.AlterMethod  = "Upgrade";
                                    oi.MemberCardNo = mp.MemberCardNo;
                                }
                                else if (isRenew)
                                {
                                    oi.AlterMethod  = "Renew";
                                    oi.MemberCardNo = mpLast.MemberCardNo;
                                }
                                else
                                {
                                    oi.AlterMethod = string.Empty;

                                    var rand = new Random(Guid.NewGuid().GetHashCode());
                                    oi.MemberCardNo = rand.Next(100, 999).ToString();
                                }

                                oi.EndDate = CurrSeasonDeadline;

                                oi.OrderID  = newId;
                                oi.Quantity = 1;

                                #region set membership sale
                                var pPremier = Product.Cache.Load("iMS2");
                                var pCore    = Product.Cache.Load("iMS1");

                                if (isUpgrade)
                                {
                                    oi.Sale = pPremier.PriceCNY - pCore.PriceCNY;
                                }
                                else if (isRenew)
                                {
                                    oi.Sale = currProductType.Equals(ProductType.MembershipPremier)
                                        ? Convert.ToDouble(Math.Floor(pPremier.PriceCNY * 0.88)) : pCore.PriceCNY;
                                }
                                else
                                {
                                    oi.Sale = null;
                                }
                                #endregion

                                oi.Place(Member.Cache.Load(o.MemberID), p, trans);

                                #endregion

                                trans.Commit();

                                context.Response.Redirect($"~/iArsenalOrderView_Membership.aspx?OrderID={newId}", false);
                                context.ApplicationInstance.CompleteRequest();
                            }
                        }
                    }
                }
                catch
                {
                    context.Response.Redirect("iArsenalOrder.aspx", false);
                    context.ApplicationInstance.CompleteRequest();
                }
            }
        }
        protected void btnGenMemberPeriod_Click(object sender, EventArgs e)
        {
            using (var conn = new SqlConnection(DataAccess.ConnectString))
            {
                conn.Open();
                var trans = conn.BeginTransaction();

                try
                {
                    if (OrderID > 0)
                    {
                        var o = (OrdrMembership)Order.Select(OrderID);

                        if (ConfigGlobal.IsPluginAdmin(Uid) && o != null && o.Status.Equals(OrderStatusType.Confirmed))
                        {
                            // Whether Core or Premier Membership
                            OrdrItmMembership oiMembership;

                            if (o.OIMembershipCore != null && o.OIMembershipCore.IsActive)
                            {
                                oiMembership = o.OIMembershipCore;
                            }
                            else if (o.OIMembershipPremier != null && o.OIMembershipPremier.IsActive)
                            {
                                oiMembership = o.OIMembershipPremier;
                            }
                            else
                            {
                                throw new Exception("此订单未登记会籍信息");
                            }

                            var p = Product.Cache.Load(oiMembership.ProductGuid);

                            if (p == null)
                            {
                                throw new Exception("无相关会籍可申请,请联系管理员");
                            }

                            // Get all Member Period of current season
                            var list = _repo.Query<MemberPeriod>(x =>
                                x.StartDate <= DateTime.Now && x.EndDate >= DateTime.Now)
                                .FindAll(x => x.IsActive);

                            var updateFlag = false;

                            // Valiate the Member Period Information
                            if (list.Any())
                            {
                                if (list.Exists(x =>
                                    x.MemberID.Equals(o.MemberID) && x.MemberName.Equals(o.MemberName)
                                    && p.ProductType.Equals(ProductType.MembershipCore)))
                                {
                                    throw new Exception("此会员当前赛季已经有会籍信息");
                                }
                                if (list.Exists(x =>
                                    x.MemberID.Equals(o.MemberID) && x.MemberName.Equals(o.MemberName)
                                    && x.MemberClass.Equals(MemberClassType.Core))
                                    && p.ProductType.Equals(ProductType.MembershipPremier))
                                {
                                    updateFlag = true;
                                }

                                if (!updateFlag && list.Exists(x => !x.MemberID.Equals(o.MemberID)
                                                                     &&
                                                                     x.MemberCardNo.Equals(oiMembership.MemberCardNo,
                                                                         StringComparison.OrdinalIgnoreCase)))
                                {
                                    throw new Exception("此会员卡号已被其他会员占用");
                                }
                            }

                            if (updateFlag &&
                                oiMembership.AlterMethod.Equals("Upgrade", StringComparison.OrdinalIgnoreCase))
                            {
                                // Level up the core member to premier for current season
                                var mpCore = list.SingleOrDefault(x =>
                                    x.MemberID.Equals(o.MemberID) && x.MemberName.Equals(o.MemberName)
                                    && x.MemberClass.Equals(MemberClassType.Core));

                                if (mpCore != null)
                                {
                                    mpCore.MemberClass = MemberClassType.Premier;

                                    // not update MemberCardNo of the core member
                                    oiMembership.MemberCardNo = mpCore.MemberCardNo;

                                    mpCore.EndDate = oiMembership.EndDate;

                                    mpCore.Description =
                                        $"Season {oiMembership.Season} 于 {DateTime.Now.ToString("yyyy-MM-dd HH:mm")} 升级为【{mpCore.MemberClass}】会籍,原会籍订单号:{mpCore.OrderID}";

                                    mpCore.OrderID = OrderID;

                                    _repo.Update(mpCore, trans);
                                }
                            }
                            else
                            {
                                // Insert new Member Period for current season
                                var mp = new MemberPeriod();

                                mp.MemberID = o.MemberID;
                                mp.MemberName = o.MemberName;
                                mp.MemberCardNo = oiMembership.MemberCardNo;

                                if (p.ProductType.Equals(ProductType.MembershipCore))
                                {
                                    mp.MemberClass = MemberClassType.Core;
                                }
                                else if (p.ProductType.Equals(ProductType.MembershipPremier))
                                {
                                    mp.MemberClass = MemberClassType.Premier;
                                }
                                else
                                {
                                    throw new Exception("此订单无相关会籍信息");
                                }

                                mp.OrderID = OrderID;
                                mp.StartDate = DateTime.Now;
                                mp.EndDate = oiMembership.EndDate;
                                mp.IsActive = true;
                                mp.Description = $"Season {oiMembership.Season}";
                                mp.Remark = string.Empty;

                                _repo.Insert(mp, trans);
                            }

                            // Update Order Status
                            o.Status = OrderStatusType.Delivered;
                            o.UpdateTime = DateTime.Now;

                            _repo.Update(o, trans);

                            trans.Commit();

                            ClientScript.RegisterClientScriptBlock(typeof(string), "succeed",
                                $"alert('【{p.ProductType}】会籍 (卡号:{oiMembership.MemberCardNo}) 保存成功');window.location.href = window.location.href",
                                true);
                        }
                        else
                        {
                            throw new Exception("此订单无效,请联系管理员");
                        }
                    }
                    else
                    {
                        throw new Exception("此订单无效,请联系管理员");
                    }
                }
                catch (Exception ex)
                {
                    trans.Rollback();

                    ClientScript.RegisterClientScriptBlock(typeof(string), "failed", $"alert('{ex.Message}')", true);
                }

                //conn.Close();
            }
        }
Exemplo n.º 9
0
        protected void btnGenMemberPeriod_Click(object sender, EventArgs e)
        {
            using (var conn = new SqlConnection(DataAccess.ConnectString))
            {
                conn.Open();
                var trans = conn.BeginTransaction();

                try
                {
                    if (OrderID > 0)
                    {
                        var o = (OrdrMembership)Order.Select(OrderID);

                        if (ConfigGlobal.IsPluginAdmin(Uid) && o != null && o.Status.Equals(OrderStatusType.Confirmed))
                        {
                            // Whether Core or Premier Membership
                            OrdrItmMembership oiMembership;

                            if (o.OIMembershipCore != null && o.OIMembershipCore.IsActive)
                            {
                                oiMembership = o.OIMembershipCore;
                            }
                            else if (o.OIMembershipPremier != null && o.OIMembershipPremier.IsActive)
                            {
                                oiMembership = o.OIMembershipPremier;
                            }
                            else
                            {
                                throw new Exception("此订单未登记会籍信息");
                            }

                            var p = Product.Cache.Load(oiMembership.ProductGuid);

                            if (p == null)
                            {
                                throw new Exception("无相关会籍可申请,请联系管理员");
                            }

                            // Get all Member Period of current season
                            var list = _repo.Query <MemberPeriod>(x =>
                                                                  x.StartDate <= DateTime.Now && x.EndDate >= DateTime.Now)
                                       .FindAll(x => x.IsActive);

                            var updateFlag = false;

                            // Valiate the Member Period Information
                            if (list.Any())
                            {
                                if (list.Exists(x =>
                                                x.MemberID.Equals(o.MemberID) && x.MemberName.Equals(o.MemberName) &&
                                                p.ProductType.Equals(ProductType.MembershipCore)))
                                {
                                    throw new Exception("此会员当前赛季已经有会籍信息");
                                }
                                if (list.Exists(x =>
                                                x.MemberID.Equals(o.MemberID) && x.MemberName.Equals(o.MemberName) &&
                                                x.MemberClass.Equals(MemberClassType.Core)) &&
                                    p.ProductType.Equals(ProductType.MembershipPremier))
                                {
                                    updateFlag = true;
                                }

                                if (!updateFlag && list.Exists(x => !x.MemberID.Equals(o.MemberID)
                                                               &&
                                                               x.MemberCardNo.Equals(oiMembership.MemberCardNo,
                                                                                     StringComparison.OrdinalIgnoreCase)))
                                {
                                    throw new Exception("此会员卡号已被其他会员占用");
                                }
                            }

                            if (updateFlag &&
                                oiMembership.AlterMethod.Equals("Upgrade", StringComparison.OrdinalIgnoreCase))
                            {
                                // Level up the core member to premier for current season
                                var mpCore = list.SingleOrDefault(x =>
                                                                  x.MemberID.Equals(o.MemberID) && x.MemberName.Equals(o.MemberName) &&
                                                                  x.MemberClass.Equals(MemberClassType.Core));

                                if (mpCore != null)
                                {
                                    mpCore.MemberClass = MemberClassType.Premier;

                                    // not update MemberCardNo of the core member
                                    oiMembership.MemberCardNo = mpCore.MemberCardNo;

                                    mpCore.EndDate = oiMembership.EndDate;

                                    mpCore.Description =
                                        $"Season {oiMembership.Season} 于 {DateTime.Now.ToString("yyyy-MM-dd HH:mm")} 升级为【{mpCore.MemberClass}】会籍,原会籍订单号:{mpCore.OrderID}";

                                    mpCore.OrderID = OrderID;

                                    _repo.Update(mpCore, trans);
                                }
                            }
                            else
                            {
                                // Insert new Member Period for current season
                                var mp = new MemberPeriod();

                                mp.MemberID     = o.MemberID;
                                mp.MemberName   = o.MemberName;
                                mp.MemberCardNo = oiMembership.MemberCardNo;

                                if (p.ProductType.Equals(ProductType.MembershipCore))
                                {
                                    mp.MemberClass = MemberClassType.Core;
                                }
                                else if (p.ProductType.Equals(ProductType.MembershipPremier))
                                {
                                    mp.MemberClass = MemberClassType.Premier;
                                }
                                else
                                {
                                    throw new Exception("此订单无相关会籍信息");
                                }

                                mp.OrderID     = OrderID;
                                mp.StartDate   = DateTime.Now;
                                mp.EndDate     = oiMembership.EndDate;
                                mp.IsActive    = true;
                                mp.Description = $"Season {oiMembership.Season}";
                                mp.Remark      = string.Empty;

                                _repo.Insert(mp, trans);
                            }

                            // Update Order Status
                            o.Status     = OrderStatusType.Delivered;
                            o.UpdateTime = DateTime.Now;

                            _repo.Update(o, trans);

                            trans.Commit();

                            ClientScript.RegisterClientScriptBlock(typeof(string), "succeed",
                                                                   $"alert('【{p.ProductType}】会籍 (卡号:{oiMembership.MemberCardNo}) 保存成功');window.location.href = window.location.href",
                                                                   true);
                        }
                        else
                        {
                            throw new Exception("此订单无效,请联系管理员");
                        }
                    }
                    else
                    {
                        throw new Exception("此订单无效,请联系管理员");
                    }
                }
                catch (Exception ex)
                {
                    trans.Rollback();

                    ClientScript.RegisterClientScriptBlock(typeof(string), "failed", $"alert('{ex.Message}')", true);
                }

                //conn.Close();
            }
        }
        protected override void PopulateControl()
        {
            base.PopulateControl();

            decimal balOutstanding  = (this.DataSource.Id != null && this.DataSource.Total.HasValue) ? this.DataSource.Total.Value : 0;
            int     numInstallments = (this.DataSource.Id != null && this.DataSource.NoOfInstallments.HasValue) ? this.DataSource.NoOfInstallments.Value : 1;

            if (this.DataSource.Id == null)
            {
                if (!string.IsNullOrEmpty(this.Request.QueryString["memberPeriodId"]))
                //new direct debit created from member page
                //relate to the member period id and the memberperiod table (271)
                //and set defaults from the member period
                {
                    //set defaults
                    this.RecDate.FieldValue        = DateTime.Now;
                    this.EffectiveDate.FieldValue  = DateTime.Now.AddDays(14);
                    this.Type.FieldValue           = "0";
                    this.DayOfMonth.FieldValue     = "1";
                    this.CollectionType.FieldValue = "1";
                    this.Status.FieldValue         = "0";
                    this.TableNum.FieldValue       = "271";

                    MemberPeriod membership = MemberPeriod.FetchById(this.Request.QueryString["memberPeriodId"]);
                    balOutstanding = membership.Fee;

                    if (membership != null)
                    {
                        var unpaidInvoiceValue = CalculateOutstandingBalance(membership);
                        if (unpaidInvoiceValue.HasValue)
                        {
                            balOutstanding = unpaidInvoiceValue.Value;
                        }
                    }

                    if (membership.Product.DdNoOfInstallments.HasValue)
                    {
                        numInstallments = membership.Product.DdNoOfInstallments.Value;
                    }
                    this.MepStartDate.FieldValue   = membership.Start.Value.ToString("dd MMM yyyy");
                    this.MepEndDate.FieldValue     = this.EndDate.FieldValue = membership.End.Value.ToString("dd MMM yyyy");
                    this.DueDate.FieldValue        = CalculateFirstDueDate(DateTime.Now, membership.Start, 1).ToString("dd MMM yyyy");
                    this.MemberPeriodId.FieldValue = (this.Request.QueryString["memberPeriodId"]);
                    this.AccountName.FieldValue    = membership.InvoiceRole.PersonName;
                    this.Frequency.FieldValue      = membership.Product.DdFrequency.HasValue ? membership.Product.DdFrequency.Value.ToString() : "0";
                    this.Installments.FieldValue   = numInstallments.ToString();
                    this.DDExist.FieldValue        = DirectDebit.FetchAllByMemberPeriodId(membership.Id).Where(dd => dd.Status < 7).Count().ToString();
                }
                //initialise max value
                this.MaximumValue.FieldValue = (balOutstanding / numInstallments).ConvertTo <String>();
                this.TotalVal.FieldValue     = balOutstanding.ConvertTo <String>();
            }
            else
            {
                this.Status.DataSource = PopulateStatusList(false);
                this.Status.FieldValue = this.DataSource.Status.ToString();
                //can't update status or rec date in edit mode
                this.Status.IsReadOnly  = UserInterface.ConfigurableBoolean.True;
                this.RecDate.IsReadOnly = UserInterface.ConfigurableBoolean.True;

                if (this.DataSource.MemberPeriod != null)
                {
                    var unpaidInvoiceValue = CalculateOutstandingBalance(this.DataSource.MemberPeriod);
                    if (this.DataSource.MemberPeriodId != null && this.DataSource.TableNum == 271 && unpaidInvoiceValue.HasValue)
                    {
                        balOutstanding = unpaidInvoiceValue.Value;
                    }
                }
            }

            this.BalanceOutstanding.FieldValue = balOutstanding.ConvertTo <String>();
        }