private void btnOk_Click(object sender, EventArgs e)
        {
            if (acaoSelecionada == AcaoTelaCRUD.Inserir)
            {
                if (txtUsuario.Text != "" && txtSenha.Text != "" && txtRepetirSenha.Text != "")
                {
                    if (txtSenha.Text == txtRepetirSenha.Text)
                    {
                        try
                        {
                            Usuario novoUsuario = new Usuario();
                            if (rdbAdministrador.Checked)
                            {
                                novoUsuario.Acesso = 5;
                            }
                            else if (rdbCaixa.Checked)
                            {
                                novoUsuario.Acesso = 1;
                            }
                            else if (rdbCompras.Checked)
                            {
                                novoUsuario.Acesso = 2;
                            }
                            else if (rdbFinanceiro.Checked)
                            {
                                novoUsuario.Acesso = 3;
                            }

                            novoUsuario.IDFilial = ControleSistema.getFilialLogada().IDFilial;
                            if (pessoaFisicaSelecionada != null)
                            {
                                novoUsuario.IDPessoa = pessoaFisicaSelecionada.IDPessoa;
                            }
                            else
                            {
                                MessageBox.Show("Erro ao definir ID da pessoa física ao usuário");
                                return;
                            }

                            novoUsuario.IDStatus = 1;
                            novoUsuario.Login    = txtUsuario.Text;
                            novoUsuario.Senha    = txtSenha.Text;

                            UsuarioNegocio usuarioNegocio = new UsuarioNegocio();

                            string retorno = usuarioNegocio.Inserir(novoUsuario);

                            int IDUsuario;
                            if (!int.TryParse(retorno, out IDUsuario))
                            {
                                MessageBox.Show("Erro ao inserir novo usuário. Detalhes: " + retorno);
                                return;
                            }
                            else
                            {
                                MessageBox.Show("Usuário inserido com sucesso. ID do no usuário: " + retorno);
                                Close();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Erro ao inserir usuário. Detalhes: " + ex.Message);
                        }
                    }
                    else
                    {
                        MessageBox.Show("As senhas são diferentes.");
                        txtSenha.Clear();
                        txtRepetirSenha.Clear();
                        txtSenha.Focus();
                    }
                }
                else
                {
                    MessageBox.Show("Todos os campos devem ser preenchidos.");
                    txtUsuario.Focus();
                }
            }

            if (acaoSelecionada == AcaoTelaCRUD.Alterar)
            {
                if (txtUsuario.Text != "" && txtSenha.Text != "" && txtRepetirSenha.Text != "")
                {
                    if (txtSenha.Text == txtRepetirSenha.Text)
                    {
                        try
                        {
                            Usuario novoUsuario = new Usuario();
                            if (rdbAdministrador.Checked)
                            {
                                novoUsuario.Acesso = 5;
                            }
                            else if (rdbCaixa.Checked)
                            {
                                novoUsuario.Acesso = 1;
                            }
                            else if (rdbCompras.Checked)
                            {
                                novoUsuario.Acesso = 2;
                            }
                            else if (rdbFinanceiro.Checked)
                            {
                                novoUsuario.Acesso = 3;
                            }

                            novoUsuario.IDFilial = ControleSistema.getFilialLogada().IDFilial;
                            if (usuarioSelecionado != null)
                            {
                                novoUsuario.IDPessoa = usuarioSelecionado.IDPessoa;
                            }
                            else
                            {
                                MessageBox.Show("Erro ao definir ID da pessoa física ao usuário");
                                return;
                            }

                            novoUsuario.IDStatus = 1;
                            novoUsuario.Login    = txtUsuario.Text;
                            novoUsuario.Senha    = txtSenha.Text;

                            UsuarioNegocio usuarioNegocio = new UsuarioNegocio();

                            string retorno = usuarioNegocio.Alterar(novoUsuario);

                            int IDUsuario;
                            if (!int.TryParse(retorno, out IDUsuario))
                            {
                                MessageBox.Show("Erro ao alterar usuário. Detalhes: " + retorno);
                                DialogResult = DialogResult.No;
                                return;
                            }
                            else
                            {
                                MessageBox.Show("Usuário alterado com sucesso. ID do no usuário: " + retorno);
                                DialogResult = DialogResult.Yes;
                                Close();
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Erro ao alterar usuário. Detalhes: " + ex.Message);
                            DialogResult = DialogResult.No;
                        }
                    }
                    else
                    {
                        MessageBox.Show("As senhas são diferentes.");
                        txtSenha.Clear();
                        txtRepetirSenha.Clear();
                        txtSenha.Focus();
                    }
                }
                else
                {
                    MessageBox.Show("Todos os campos devem ser preenchidos.");
                    txtUsuario.Focus();
                }
            }
        }
예제 #2
0
 public void Alterar(UsuarioDTO dto)
 {
     _negocio.Alterar(dto);
 }
        private void btnAlunoCadastrar_Click(object sender, EventArgs e)
        {
            using (UsuarioNegocio usuarioNegocio = new UsuarioNegocio())
            {
                ValidacaoInterfaces validar = new ValidacaoInterfaces();

                // validações do cadastro
                if (validar.validarCampoCadastrar(this.grpIdentificacao.Controls) == true)
                {
                    return;
                }
                else if (validar.ValidarRadioButton(this.rbtCadastrarAtivoSim, this.rbtCadastrarAtivoNao) == true)
                {
                    return;
                }
                else if (validar.ValidarRadioButton(this.rbtCadastrarSexoMasculino, this.rbtCadastrarSexoFeminino) == true)
                {
                    return;
                }
                else if (validar.validarCampoCadastrar(this.grpLocalizacao.Controls) == true)
                {
                    return;
                }
                else if (validar.validarCampoCadastrar(this.grpContato.Controls) == true)
                {
                    return;
                }

                else if (validar.validarCampoCadastrar(this.grpLogin.Controls) == true)
                {
                    return;
                }



                if (acaoNaTelaInformada == AcaoNaTela.Inserir)
                {
                    Usuario usuarioInserir = new Usuario();
                    usuarioInserir.Pessoa = new Pessoa()
                    {
                        Nome                = this.txtCadastrarNome.Text,
                        RG                  = this.txtCadastrarRG.Text,
                        CPF                 = this.mskCadastrarCPF.Text,
                        DataNascimento      = this.dtpCadastrarDataNascimento.Value,
                        Ativo               = this.rbtCadastrarAtivoSim.Checked == true? true: false,
                        Sexo                = this.rbtCadastrarSexoMasculino.Checked == true? true: false,
                        TelefoneCelular     = this.mskCadastrarTelefoneCelular.Text,
                        TelefoneResidencial = this.mskCadastrarTelefoneResidencial.Text,
                        Email               = this.txtCadastrarEmail.Text,
                        Usuario             = this.txtCadastrarUsuario.Text,
                        Senha               = this.txtCadastrarSenha.Text
                    };
                    usuarioInserir.Pessoa.Endereco = new Endereco()
                    {
                        CEP         = this.txtCadastrarCEP.Text,
                        Rua         = this.txtCadastrarRua.Text,
                        Numero      = Convert.ToInt32(this.txtCadastrarNumero.Text),
                        Complemento = this.txtCadastrarComplemento.Text,
                        Bairro      = this.txtCadastrarBairro.Text,
                        Cidade      = this.txtCadastrarCidade.Text,
                        UF          = this.cmbCadastrarUF.Text
                    };

                    usuarioInserir.TipoUsuario = new TipoUsuario()
                    {
                        IDTipoUsuario = Convert.ToInt32(this.cmbCadastrarTipoUsuario.SelectedValue)
                    };
                    string retorno = usuarioNegocio.Inserir(usuarioInserir);
                    try
                    {
                        int codigo = Convert.ToInt32(retorno);
                        MessageBox.Show("Usuário cadastrado com sucesso. Código: " + codigo.ToString(), "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.DialogResult = DialogResult.Yes;
                    }
                    catch
                    {
                        MessageBox.Show("Não foi possível cadastrar usuário. Detalhes: " + retorno, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
                else if (acaoNaTelaInformada == AcaoNaTela.Alterar)
                {
                    Usuario usuarioAlterar = new Usuario();
                    usuarioAlterar.Pessoa = new Pessoa()
                    {
                        IDPessoa            = Convert.ToInt32(this.txtCadastrarCodigo.Text),
                        Nome                = this.txtCadastrarNome.Text,
                        RG                  = this.txtCadastrarRG.Text,
                        CPF                 = this.mskCadastrarCPF.Text,
                        DataNascimento      = this.dtpCadastrarDataNascimento.Value,
                        Ativo               = this.rbtCadastrarAtivoSim.Checked == true? true: false,
                        Sexo                = this.rbtCadastrarSexoMasculino.Checked == true? true: false,
                        TelefoneCelular     = this.mskCadastrarTelefoneCelular.Text,
                        TelefoneResidencial = this.mskCadastrarTelefoneResidencial.Text,
                        Email               = this.txtCadastrarEmail.Text,
                        Usuario             = this.txtCadastrarUsuario.Text,
                        Senha               = this.txtCadastrarSenha.Text
                    };
                    usuarioAlterar.Pessoa.Endereco = new Endereco()
                    {
                        CEP         = this.txtCadastrarCEP.Text,
                        Rua         = this.txtCadastrarRua.Text,
                        Numero      = Convert.ToInt32(this.txtCadastrarNumero.Text),
                        Complemento = this.txtCadastrarComplemento.Text,
                        Bairro      = this.txtCadastrarBairro.Text,
                        Cidade      = this.txtCadastrarCidade.Text,
                        UF          = this.cmbCadastrarUF.Text
                    };

                    usuarioAlterar.TipoUsuario = new TipoUsuario()
                    {
                        IDTipoUsuario = Convert.ToInt32(this.cmbCadastrarTipoUsuario.SelectedValue)
                    };

                    string retorno = usuarioNegocio.Alterar(usuarioAlterar);
                    try
                    {
                        int codigo = Convert.ToInt32(retorno);
                        MessageBox.Show("Usuário alterado com sucesso. Código: " + codigo.ToString(), "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.DialogResult = DialogResult.Yes;
                    }
                    catch
                    {
                        MessageBox.Show("Não foi possível alterar o usuário. Detalhes: " + retorno, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
            }
        }