public bool Cadastrar(EstoquePeca estoquePeca, int idpeca)
 {
     try
     {
         return(_estoquePecasService.Cadastrar(estoquePeca, idpeca));
     }
     catch (RegistroExisteException e)
     {
         throw new RegistroExisteException(e.Message);
     }
     catch (ConcorrenciaBancoException e)
     {
         throw new ConcorrenciaBancoException(e.Message);
     }
 }
        public bool Cadastrar(EstoquePeca estoquepeca)
        {
            DateTime datareg = DateTime.Now;
            string   query   = "INSERT INTO [dbo].[TB_ESTOQUE_PECAS] ([EP_DESCRICAO],[EP_VALORUNIT],[EP_QUANTD],[EP_DATAREGISTRO])" +
                               "VALUES ('" + estoquepeca.Descricao + "', " + estoquepeca.ValorUnit + ", " + estoquepeca.Quantidade + ", '" + datareg.ToShortDateString() + "')";

            try
            {
                return(_banco.ExecutarInstrucao(query));
            }
            catch (ConcorrenciaBancoException e)
            {
                throw new ConcorrenciaBancoException(e.Message);
            }
        }
 public bool Cadastrar(EstoquePeca estoquePeca, int idpeca)
 {
     try
     {
         EstoquePeca obj = _estoquePecasDAL.BuscarEstoquePecas(idpeca); //Falta criar os métodos de busca
         if (obj != null)
         {
             throw new RegistroExisteException("Já existe uma peça com essa Identificação no sistema!");
         }
         return(_estoquePecasDAL.Cadastrar(estoquePeca));
     }
     catch (ConcorrenciaBancoException)
     {
         throw new ConcorrenciaBancoException("Favor tentar novamente mais tarde.");
     }
 }
        private void lblCancelar_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Deseja realmente cancelar manipulação de dados?", "Cancelar", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                txtValor.Text       = "";
                txtDesc.Text        = "";
                nudQuantidade.Value = 0;
                txtid.Text          = _estoquePecasController.PopulaID().ToString();

                btnCadastrarEstoque.Visible = true;
                lblCancelar.Visible         = false;
                btnAlterarEstoque.Enabled   = false;
                btnExcluirEstoque.Enabled   = false;

                EstoquePeca = null;
            }
        }
 public bool Alterar(EstoquePeca estoquePeca, int idpeca)
 {
     try
     {
         EstoquePeca obj = _estoquePecasDAL.BuscarEstoquePecas(idpeca); //Falta criar os métodos de busca
         if (obj == null)
         {
             throw new NaoEncontradoException("Peça não encontrada.");
         }
         _estoquePecasDAL.Alterar(estoquePeca, idpeca);
     }
     catch (ConcorrenciaBancoException)
     {
         throw new ConcorrenciaBancoException("Favor tentar novamente mais tarde.");
     }
     return(true);
 }
        public List <EstoquePeca> Pesquisar(string busca)
        {
            List <EstoquePeca> pecas = new List <EstoquePeca>();
            string             query;

            if (busca == "")
            {
                return(pecas);
            }

            if (DetectaChar(busca) > 0)
            {
                query = "SELECT * FROM [dbo].[TB_ESTOQUE_PECAS] WHERE (EP_DESCRICAO LIKE '%" + busca + "%')";
            }
            else
            {
                query = "SELECT * FROM [dbo].[TB_ESTOQUE_PECAS] WHERE (CONVERT(varchar,EP_ID) LIKE '%" + busca + "%'" +
                        " OR EP_DESCRICAO LIKE '%" + busca + "%')";
            }

            try
            {
                DataTable   dt          = _banco.BuscarRegistro(query);
                EstoquePeca estoquePeca = null;
                DataRow[]   dataRows    = dt.Select();

                foreach (DataRow dr in dataRows)
                {
                    double valor  = double.Parse(dr["EP_VALORUNIT"].ToString());
                    int    qtd    = int.Parse(dr["EP_QUANTD"].ToString());
                    int    idpeca = int.Parse(dr["EP_ID"].ToString());

                    estoquePeca = new EstoquePeca(idpeca, dr["EP_DESCRICAO"].ToString(), valor, qtd);
                    pecas.Add(estoquePeca);
                }
                return(pecas);
            }
            catch (Exception)
            {
                throw new ConcorrenciaBancoException("Erro de concorrência de banco!");
            }
        }
        private void btnConsultarEstoque_Click(object sender, EventArgs e)
        {
            if (txtIDEstoqueConsulta.Text == "")
            {
                MessageBox.Show("Preencha o campo do Identificador!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                try
                {
                    int         id          = int.Parse(txtIDEstoqueConsulta.Text);
                    EstoquePeca estoquePeca = _estoquePecasController.BuscarEstoquePecas(id);
                    if (estoquePeca == null)
                    {
                        MessageBox.Show("Não existe cadastro com esse Identificador!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    else
                    {
                        DataTable dt = new DataTable();
                        dt.Columns.Add("ID", typeof(int));
                        dt.Columns.Add("Descrição", typeof(string));
                        dt.Columns.Add("Valor", typeof(double));
                        dt.Columns.Add("Quantidade", typeof(int));

                        dt.Rows.Add(estoquePeca.Id, estoquePeca.Descricao, estoquePeca.ValorUnit, estoquePeca.Quantidade);

                        dgEstoqueConsulta.DataSource = dt;

                        EstoquePeca       = estoquePeca;
                        ListaEstoquePecas = new List <EstoquePeca>();
                        ListaEstoquePecas.Add(estoquePeca);
                        estoquePeca = null;
                    }
                }
                catch (ConcorrenciaBancoException ex)
                {
                    MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }
        private void btnAlterarEstoque_Click(object sender, EventArgs e)
        {
            if (txtValor.Text == "" || txtDesc.Text == "" || nudQuantidade.Value <= 0)
            {
                MessageBox.Show("Preencha os campos corretamente!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                int         id          = int.Parse(txtid.Text);
                double      valor       = double.Parse(txtValor.Text);
                int         qtd         = Convert.ToInt32(nudQuantidade.Value);
                EstoquePeca estoquePeca = new EstoquePeca(id, txtDesc.Text, valor, qtd);
                try
                {
                    if (_estoquePecasController.Alterar(estoquePeca, id))
                    {
                        MessageBox.Show("Alteração realizada com Sucesso!");
                        txtValor.Text       = "";
                        txtDesc.Text        = "";
                        nudQuantidade.Value = 0;
                        txtid.Text          = _estoquePecasController.PopulaID().ToString();

                        btnCadastrarEstoque.Visible = true;
                        lblCancelar.Visible         = false;
                        btnAlterarEstoque.Enabled   = false;
                        btnExcluirEstoque.Enabled   = false;
                    }
                }
                catch (NaoEncontradoException ex)
                {
                    MessageBox.Show(ex.Message);
                }
                catch (ConcorrenciaBancoException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
        private void btnTrasferirEstoque_Click(object sender, EventArgs e)
        {
            if (EstoquePeca == null)
            {
                MessageBox.Show("Use a função Consultar antes de realizar esta operação!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                txtid.Text          = EstoquePeca.Id.ToString();
                txtValor.Text       = EstoquePeca.ValorUnit.ToString();
                nudQuantidade.Value = EstoquePeca.Quantidade;
                txtDesc.Text        = EstoquePeca.Descricao;

                MessageBox.Show("Dados enviados para a Tela de Cadastro.");

                tbControlEstoque.SelectTab("tbPageCadastroEstoque");
                if (tbControlEstoque.SelectedTab == tbPageCadastroEstoque)
                {
                    EstoquePeca = null;
                    txtIDEstoqueConsulta.Text = "";

                    btnCadastrarEstoque.Visible = false;
                    lblCancelar.Visible         = true;
                    btnAlterarEstoque.Enabled   = true;
                    btnExcluirEstoque.Enabled   = true;

                    if (PerfilAcesso == PerfilAcesso.Atendimento || PerfilAcesso == PerfilAcesso.Operacional)
                    {
                        btnExcluirEstoque.Enabled = false;
                    }
                    else
                    {
                        btnExcluirEstoque.Enabled = true;
                    }
                }
            }
        }
 public EstoquePecas()
 {
     InitializeComponent();
     _estoquePecasController = InstanciarCamadas();
     EstoquePeca             = null;
 }