Ejemplo n.º 1
0
    protected void btnSave_Click(object sender, EventArgs e)
    {
        DataTable table = null;
        string message = "";
        try
        {
            if (hdfId.Value == null || hdfId.Value == "")//them moi
            {
                message = checkInput();
            }
            else
            {
                message = checkPassword();
            }
            int status = 0;
            if (message == "")
            {

                ulBus = new UserLoginBUS();
                UserLoginDTO ulDto = new UserLoginDTO();
                ulDto.Username = txtUsername.Text;
                ulDto.Password = Common.GetMd5Hash(txtPassword.Text);
                ulDto.Email = txtEmail.Text;
                ulDto.Is_Block = this.chkBlock.Checked;
                ulDto.UserType = 2;
                //tai khoan con ID =3
                ulDto.UserId = 3;
                ConnectionData.OpenMyConnection();
                if (hdfId.Value == null || hdfId.Value == "")//them moi
                {

                    if (getUserLogin().DepartmentId == 3)
                    {
                        table = ulBus.GetClientIdSub(getUserLogin().UserId);
                    }
                    else
                    {
                        table = ulBus.GetClientId(getUserLogin().UserId);
                    }

                    int clienID = int.Parse(table.Rows[0]["clientId"].ToString());
                    ulDto.ClientID = clienID;

                    int statusclient = int.Parse(table.Rows[0]["Status"].ToString());

                    DataTable countSubClient = ulBus.GetCountSubClient(ulDto.ClientID);
                    int countSub = int.Parse(countSubClient.Rows[0]["numberSub"].ToString());

                    DataTable subAccount = ulBus.GetSubAccountCount(ulDto.ClientID);
                    int SubAccount = int.Parse(subAccount.Rows[0]["subAccontCount"].ToString());

                    DateTime NgayHetHan = Convert.ToDateTime(table.Rows[0]["expireDate"].ToString());
                    string todays = DateTime.Now.ToString("yyyy-MM-dd");
                    DateTime today = Convert.ToDateTime(todays);
                    DateTime expireDay = Convert.ToDateTime(NgayHetHan);
                    DataTable checkEmail = ulBus.GetEmailByUser(txtEmail.Text.Trim());
                    if (statusclient == 2 || expireDay < today)
                    {
                        status = 3;
                    }
                    else
                    {
                        if (countSub < SubAccount)
                        {
                            if (checkEmail.Rows.Count > 0)
                            {
                                status = 5;
                            }
                            else
                            {
                                ulBus.tblUserLoginSubClient_insert(ulDto);
                                //lay UserID
                                DataTable dt = ulBus.GetUserIDByUserName(txtEmail.Text);
                                int userID = int.Parse(dt.Rows[0]["UserId"].ToString());
                                ulDto.UserId = userID;
                                ulBus.tblSubClient_insert(ulDto);
                                status = 1;
                            }
                        }
                        else
                        {
                            status = 4;
                        }
                    }

                }
                else
                {

                    ulDto.SubId = int.Parse(hdfId.Value);
                    DataTable checkEmail = ulBus.GetEmailByUser(txtEmail.Text.Trim());
                    if (checkEmail.Rows.Count > 0)
                    {
                        status = 5;
                    }
                    else
                    {
                        ulBus.tblSubClient_Update(ulDto);
                        // DataTable table1 = ulBus.GetUserIdBySubID(ulDto.SubId);
                        // int userID = int.Parse(table1.Rows[0]["UserID"].ToString());
                        DataTable tablesub = ulBus.GetBySubId(ulDto.SubId);
                        string Username = tablesub.Rows[0]["subEmail"].ToString();
                        DataTable dtIsBlock = ulBus.GetIsBlockByUserId(Username);
                        bool Is_Block_check = chkBlock.Checked;
                        ulBus.tblUserLoginSub_Update(Username, Is_Block_check);
                        status = 2;
                    }

                }

                ConnectionData.CloseMyConnection();
                pnSuccess.Visible = true;
                pnError.Visible = false;
                LoadData();
                if (status == 5)
                {
                    pnSuccess.Visible = false;
                    pnError.Visible = true;
                    lblError.Text = "Email đã được sử dụng. Vui lòng chọn email khác !";
                    this.txtEmail.Focus();
                }
                if (status == 4)
                {
                    lblError.Text = "Tạo tài khoản con vượt quá giới hạn cho phép!";
                    pnSuccess.Visible = false;
                    pnError.Visible = true;
                }
                if (status == 3)
                {
                    lblError.Text = "Không cho phép tạo tài khoản con.Liên hệ quản trị!";
                    pnSuccess.Visible = false;
                    pnError.Visible = true;
                }
                if (status == 1)
                {
                    lblSuccess.Text = "Thêm thành công !";
                }
                else
                    if (status == 2)
                    {
                        lblSuccess.Text = "Bạn vừa cập nhật thành công chức năng !";
                        txtUsername.Enabled = true;
                    }

            }
            else
            {
                pnSuccess.Visible = false;
                pnError.Visible = true;
                lblError.Text = message;
            }

        }
        catch (Exception ex)
        {
            pnSuccess.Visible = false;
            pnError.Visible = true;
            lblError.Text = "Kiểm tra lại dữ liệu nhập vào !";
            logs.Error(userLogin.Username + "subClient-Save", ex);
        }
    }