protected void btnEditOption_Click(object sender, EventArgs e)
 {
     int user = Convert.ToInt32(userLogin.UserId);
     clientbus = new ClientBUS();
     DataTable dtClient = new DataTable();
     dtClient = clientbus.GetByID(user);
     DateTime dateexpire = Convert.ToDateTime(dtClient.Rows[0]["expireDate"].ToString());
     int registerId = Convert.ToInt32(dtClient.Rows[0]["registerId"].ToString());
     int clientId = Convert.ToInt32(dtClient.Rows[0]["clientId"].ToString());
     clientRegister = new ClientRegisterBUS();
     DataTable dtClientRegister = new DataTable();
     dtClientRegister = clientRegister.GetbyID(registerId);
     int packageid = Convert.ToInt32(dtClientRegister.Rows[0]["packageId"].ToString());
     int limitId = Convert.ToInt32(ddlLimitPackage.SelectedValue.ToString());
     pkgBus = new PackageBUS();
     DataTable dtpackage = new DataTable();
     dtpackage = pkgBus.GetById(Convert.ToInt32(packageid));
     int SubAccount = Convert.ToInt32(dtpackage.Rows[0]["subAccontCount"].ToString());
     int totalFee = Convert.ToInt32(lblSumCost.Text);
     int registerType = 3;
     int packagetimeid = Convert.ToInt32(dtClientRegister.Rows[0]["packageTimeId"].ToString());
     string from = Convert.ToDateTime(dtClientRegister.Rows[0]["from"].ToString()).ToString("dd/MM/yyyy");
     string to = Convert.ToDateTime(dtClientRegister.Rows[0]["to"].ToString()).ToString("dd/MM/yyyy");
     string lastRegisterFrom = Convert.ToDateTime(dtClientRegister.Rows[0]["lastRegisterFrom"].ToString()).ToString("dd/MM/yyyy");
     string lastRegisterTo = Convert.ToDateTime(dtClientRegister.Rows[0]["lastRegisterTo"].ToString()).ToString("dd/MM/yyyy");
     int lastRegisterFee = Convert.ToInt32(dtClientRegister.Rows[0]["lastRegisterFee"].ToString());
     int lastRegisterFeeRemain = Convert.ToInt32(dtClientRegister.Rows[0]["lastRegisterFeeRemain"].ToString());
     int newregisterid = clientRegister.UpdateUpgrade(clientId, packageid, limitId, SubAccount, totalFee, registerType, packagetimeid, from, to, lastRegisterFrom, lastRegisterTo, lastRegisterFee, lastRegisterFeeRemain);
     clientbus.UpdateRegiterId(clientId, from, to, registerId, newregisterid);
     foreach (RepeaterItem rptItems in rptListOptions.Items)
     {
         CheckBox chk = (CheckBox)rptItems.FindControl("chkOptions");
         Label lbl = (Label)rptItems.FindControl("lblID");
         int functionId = Convert.ToInt32(lbl.Text);
         if (chk.Checked == true)
         {
             clientFunction = new ClientFunctionBUS();
             clientFunction.UpdateFunction(newregisterid, clientId, functionId);
         }
     }
     btnEditOption.Enabled = false;
     btnEditOption.CssClass = "button round text-upper";
     btnEditOption.Text = "Đã điều chỉnh chức năng gói!!!";
 }
 protected void btnGiahan_Click(object sender, EventArgs e)
 {
     if (Session["expireDays"] != null)
     {
         int user = Convert.ToInt32(userLogin.UserId);
         clientbus = new ClientBUS();
         DataTable dtClient = new DataTable();
         dtClient = clientbus.GetByID(user);
         int registerId = Convert.ToInt32(dtClient.Rows[0]["registerId"].ToString());
         clientRegister = new ClientRegisterBUS();
         DataTable dtClientRegister = new DataTable();
         dtClientRegister = clientRegister.GetbyID(registerId);
         int clientId = Convert.ToInt32(dtClientRegister.Rows[0]["clientId"].ToString());
         int packageId = Convert.ToInt32(dtClientRegister.Rows[0]["packageId"].ToString());
         int limitId = Convert.ToInt32(dtClientRegister.Rows[0]["limitId"].ToString());
         int SubAccountCount = Convert.ToInt32(dtClientRegister.Rows[0]["subAccontCount"].ToString());
         int emailCount = 0;
         if(int.TryParse(dtClientRegister.Rows[0]["emailCount"].ToString(),out emailCount));
         int totalFee = Convert.ToInt32(lblExtendCost.Text);
         int registerType = Convert.ToInt32(dtClientRegister.Rows[0]["registerType"].ToString());
         int packageTimeId = Convert.ToInt32(dtClientRegister.Rows[0]["packageTimeId"].ToString());
         DateTime activeDate = DateTime.Now;
         DateTime expireDate = Convert.ToDateTime(Session["expireDays"].ToString());
         string from = activeDate.ToString("dd/MM/yyyy");
         string to = expireDate.ToString("dd/MM/yyyy") ;
         string lastRegisterFrom = Convert.ToDateTime(dtClientRegister.Rows[0]["from"].ToString()).ToString("dd/MM/yyyy");
         string lastRegisterTo = Convert.ToDateTime(dtClientRegister.Rows[0]["to"].ToString()).ToString("dd/MM/yyyy");
         int lastRegisterFee = Convert.ToInt32(dtClientRegister.Rows[0]["totalFee"].ToString());
         int lastRegisterFeeRemain=0;
         int newregisterid = clientRegister.UpdateUpgrade(clientId, packageId, limitId, SubAccountCount, totalFee, registerType, packageId, from, to, lastRegisterFrom, lastRegisterTo, lastRegisterFee, lastRegisterFeeRemain);
         clientbus.UpdateRegiterId(clientId, from, to, registerId, newregisterid);
         clientFunction = new ClientFunctionBUS();
         DataTable dtRegisOldFunction = clientFunction.GetByregisterIdandclientId(registerId, clientId);
         foreach (DataRow drRegisOldFunction in dtRegisOldFunction.Rows)
         {
             int functionId = Convert.ToInt32(drRegisOldFunction["functionId"].ToString());
             clientFunction.UpdateFunction(newregisterid, clientId, functionId);
         }
         #region oldcode
         expireDatesession = Session["expireDays"].ToString();
         clientbus.UpdateExtendLicense(clientId.ToString(), activeDate, expireDate);
         #endregion
     }
     LoadData();
 }
 protected void btnUpgrade_Click(object sender, EventArgs e)
 {
     //int limitId = Convert.ToInt32(ddlUpgradeServices.SelectedValue.ToString());
     int packageid = Convert.ToInt32(ddlUpgradeServices.SelectedValue.ToString());
     int user = Convert.ToInt32(userLogin.UserId);
     clientbus = new ClientBUS();
     DataTable dtClient = new DataTable();
     dtClient = clientbus.GetByID(user);
     DateTime dateexpire = Convert.ToDateTime(dtClient.Rows[0]["expireDate"].ToString());
     int registerId = Convert.ToInt32(dtClient.Rows[0]["registerId"].ToString());
     int clientId = Convert.ToInt32(dtClient.Rows[0]["clientId"].ToString());
     DateTime dayactive = Convert.ToDateTime(dtClient.Rows[0]["activeDate"].ToString());
     DateTime dayexpire = Convert.ToDateTime(dtClient.Rows[0]["expireDate"].ToString());
     clientRegister = new ClientRegisterBUS();
     DataTable dtClientRegister = new DataTable();
     dtClientRegister = clientRegister.GetbyID(registerId);
     int limitId = Convert.ToInt32(dtClientRegister.Rows[0]["limitId"].ToString());
     string lastRegisterFrom = Convert.ToDateTime(dtClientRegister.Rows[0]["from"].ToString()).ToString("dd/MM/yyyy"); ;
     string lastRegisterTo = Convert.ToDateTime(dtClientRegister.Rows[0]["to"].ToString()).ToString("dd/MM/yyyy");
     clientRegister = new ClientRegisterBUS();
     DataTable dtpackage = new DataTable();
     pkgBus = new PackageBUS();
     dtpackage = pkgBus.GetById(Convert.ToInt32(packageid));
     int SubAccount = Convert.ToInt32(dtpackage.Rows[0]["subAccontCount"].ToString());
     int packagelimitid = Convert.ToInt32(dtpackage.Rows[0]["limitid"].ToString());
     DataTable dtlimit = new DataTable();
     pkglimitBus = new PackageLimitBUS();
     dtlimit = pkglimitBus.GetByUserIdPackageLimit(limitId);
     pkgCostBus = new PackageCostBUS();
     DataTable dtCost = pkgCostBus.GetPackageCost(packageid);
     int cost = Convert.ToInt32(dtCost.Rows[0]["cost"].ToString());
     int numberoftime = Convert.ToInt32(ddlUpgradeTime.SelectedValue);
     int value = 0;
     int totalFee = Convert.ToInt32(lblFee.Text);
     string status = Request.QueryString["type"].ToString();
     int registerType;
     if (status == "extend")
     {
         registerType = 1;
     }
     else if (status == "upgrade")
     {
         registerType = 2;
     }
     else if (status == "addfunction")
     {
         registerType = 3;
     }
     else
     {
         registerType = 0;
     }
     string today = DateTime.Now.ToShortDateString();
     int numberofday = Convert.ToInt32(ddlUpgradeTime.SelectedValue.ToString());
     //DateTime expiredays = Convert.ToDateTime(today).AddMonths(numberofday);
     //DateTime activedays = Convert.ToDateTime(today);
     string registerDate = DateTime.Now.ToString("dd/MM/yyyy");
     string registerTime = DateTime.Now.ToString("dd/MM/yyyy");
     string from = DateTime.Now.ToString("dd/MM/yyyy");
     string to = DateTime.Parse(Session["expireDays"].ToString()).ToString("dd/MM/yyyy");
     int lastRegisterFee = Convert.ToInt32(dtClientRegister.Rows[0]["totalFee"].ToString());
     int lastRegisterFeeRemain = Convert.ToInt32(lblFeeRemain.Text);
     int packagetimeid = Convert.ToInt32(ddlUpgradeServices.SelectedValue);
     int newregisterid = clientRegister.UpdateUpgrade(clientId, packageid, limitId, SubAccount, totalFee, registerType, packagetimeid, from, to, lastRegisterFrom, lastRegisterTo, lastRegisterFee, lastRegisterFeeRemain);
     clientbus.UpdateRegiterId(clientId, from, to, registerId, newregisterid);
     foreach (RepeaterItem rptItems in rptListOptionsUpgrade.Items)
     {
         CheckBox chk = (CheckBox)rptItems.FindControl("chkOptionsUpgrade");
         Label lbl = (Label)rptItems.FindControl("lblIDUpgrade");
         int functionId = Convert.ToInt32(lbl.Text);
         if (chk.Checked == true)
         {
             clientFunction = new ClientFunctionBUS();
             clientFunction.UpdateFunction(newregisterid, clientId, functionId);
         }
     }
     btnUpgrade.Enabled = false;
     btnUpgrade.CssClass = "button round text-upper";
     btnUpgrade.Text = "Đã nâng cấp gói thành công!!!";
 }
    private void LoadData()
    {
        #region LoadData
        int id = Convert.ToInt32(userLogin.UserId);
        if (id != null)
        {
            //int user = Convert.ToInt32(Request.QueryString["user"].ToString());
            int user = id;
            clientbus = new ClientBUS();
            DataTable dtClient = new DataTable();
            dtClient = clientbus.GetByID(user);
            string email = dtClient.Rows[0]["email"] + "";
            int registerId = Convert.ToInt32(dtClient.Rows[0]["registerId"].ToString());
            int clientId = Convert.ToInt32(dtClient.Rows[0]["clientId"].ToString());
            clientRegister = new ClientRegisterBUS();
            DataTable dtClientRegister = new DataTable();
            dtClientRegister = clientRegister.GetbyID(registerId);
            string packageid = dtClientRegister.Rows[0]["packageId"].ToString();
            DataTable dtpackage = new DataTable();
            pkgBus = new PackageBUS();
            dtpackage = pkgBus.GetById(Convert.ToInt32(packageid));
            int packagelimitid = Convert.ToInt32(dtClientRegister.Rows[0]["limitid"].ToString());
            DataTable dtlimit = new DataTable();
            pkglimitBus = new PackageLimitBUS();
            dtlimit = pkglimitBus.GetByUserIdPackageLimit(packagelimitid);
            string SoluongEmail = "";
            if (dtpackage.Rows.Count > 0)
            {
                lblTenGoi.Text = dtpackage.Rows[0]["packageName"].ToString();
                SoluongEmail = dtpackage.Rows[0]["isUnlimit"].ToString();
                if (SoluongEmail.Trim() != "True")
                {
                    SoluongEmail = dtpackage.Rows[0]["emailCount"].ToString();
                    lblSoLuongEmail.Text = SoluongEmail;
                }
                else
                {
                    lblSoLuongEmail.Text = "Không giới hạn";
                }
            }
            lblNgayKichHoat.Text = dtClient.Rows[0]["activeDate"].ToString();
            lblNgayDenHan.Text = dtClient.Rows[0]["expireDate"].ToString();
            lblSoTaiKhoanCon.Text = dtClientRegister.Rows[0]["subAccontCount"].ToString();

            string GioihanEmail = dtlimit.Rows[0]["isUnlimit"].ToString();
            if (GioihanEmail.Trim() != "True")
            {
                GioihanEmail = dtlimit.Rows[0]["under"].ToString();
                lblGioiHanEmail.Text = GioihanEmail;
            }
            else
            {
                lblGioiHanEmail.Text = "Không giới hạn";
            }
            clientFunction = new ClientFunctionBUS();
            DataTable dtfunction = new DataTable();
            dtfunction = clientFunction.GetByregisterIdandclientId(registerId, clientId);
            if (dtfunction != null && dtfunction.Rows.Count > 0)
            {
                DataTable dtTemp = new DataTable();
                dtTemp.Columns.Add("functionName", typeof(String));
                foreach (DataRow drfunction in dtfunction.Rows)
                {
                    DataRow dr = dtTemp.NewRow();
                    function = new FunctionBUS();
                    string idFunction = drfunction["functionId"].ToString();
                    DataTable dtTemp2 = function.GetByFunctionId(Convert.ToInt32(idFunction));
                    if (dtTemp2 != null && dtTemp2.Rows.Count > 0)
                    {
                        string FunctionName = dtTemp2.Rows[0]["functionName"].ToString();
                        dr["functionName"] = FunctionName;
                        dtTemp.Rows.Add(dr);
                    }
                }

                rptFunction.DataSource = dtTemp;
                rptFunction.DataBind();
            }
            txtHoTen.Text = dtClient.Rows[0]["clientName"].ToString();
            txtDiaChi.Text = dtClient.Rows[0]["address"].ToString();
            txtSoDienThoai.Text = dtClient.Rows[0]["phone"].ToString();
            DateTime d = DateTime.Now;// Convert.ToDateTime(dtClient.Rows[0]["dateofbirth"].ToString());
            if (DateTime.TryParse(dtClient.Rows[0]["dateofbirth"] + "", out d))
                txtDateofBirth.Text = d.ToString("dd/MM/yyyy");
            lblEmail.Text = email;
            string todays = DateTime.Now.ToString("yyyy-MM-dd");
            DateTime today = Convert.ToDateTime(todays);

            DateTime dateexpire = DateTime.Now;

            if (DateTime.TryParse(dtClient.Rows[0]["expireDate"] + "", out dateexpire) && dateexpire < today)
            {
                btnGiahan.Enabled = true;
                btnGiahan.Text = "Gia hạn (Đã hết hạn " + (today - dateexpire).TotalDays + " ngày)";
            }
            else
            {
                btnGiahan.Enabled = false; ;
                btnGiahan.CssClass = "button round image-right ic-add text-upper";
                btnGiahan.Text = "Gia hạn (Còn lại " + (dateexpire - today).TotalDays + " ngày)";
            }
        }
        else
        {

        }
        #endregion
    }