Exemplo n.º 1
0
 private void BuscarProduto()
 {
     if (!string.IsNullOrEmpty(cd_empresa.Text))
     {
         //Prencher variavel codigo de barra
         codigoBarra = cd_produto.Text;
         TpBusca[] filtro = new TpBusca[0];
         if (string.IsNullOrEmpty(cd_produto.Text))
         {
             rProd = UtilPesquisa.BuscarProduto(string.Empty,
                                                cd_empresa.Text,
                                                nm_empresa.Text,
                                                cd_tabelapreco.Text,
                                                new Componentes.EditDefault[] { cd_produto, DS_Produto },
                                                null);
         }
         else if (cd_produto.Text.SoNumero().Trim().Length != cd_produto.Text.Trim().Length)
         {
             rProd = UtilPesquisa.BuscarProduto(cd_produto.Text,
                                                cd_empresa.Text,
                                                nm_empresa.Text,
                                                cd_tabelapreco.Text,
                                                new Componentes.EditDefault[] { cd_produto, DS_Produto },
                                                null);
         }
         else
         {
             Array.Resize(ref filtro, filtro.Length + 2);
             filtro[filtro.Length - 2].vNM_Campo = "isnull(a.st_registro, 'A')";
             filtro[filtro.Length - 2].vOperador = "<>";
             filtro[filtro.Length - 2].vVL_Busca = "'C'";
             filtro[filtro.Length - 1].vNM_Campo = string.Empty;
             filtro[filtro.Length - 1].vOperador = string.Empty;
             filtro[filtro.Length - 1].vVL_Busca = "(a.cd_produto like '%" + cd_produto.Text.Trim() + "') or " +
                                                   "(a.Codigo_Alternativo = '" + (!string.IsNullOrWhiteSpace(cd_produto.TextOld) ? cd_produto.TextOld : cd_produto.Text.Trim()) + "') or " +
                                                   "(exists(select 1 from tb_est_codbarra x " +
                                                   "           where x.cd_produto = a.cd_produto " +
                                                   "           and x.cd_codbarra = '" + cd_produto.Text.Trim() + "'))";
             TList_CadProduto lProd = new TCD_CadProduto().Select(filtro, 0, string.Empty, string.Empty, string.Empty);
             if (lProd.Count > 0)
             {
                 rProd = lProd[0];
             }
         }
         if (rProd != null)
         {
             cd_produto.Text     = rProd.CD_Produto;
             DS_Produto.Text     = rProd.DS_Produto;
             cd_local.Enabled    = !rProd.St_composto;
             bb_local.Enabled    = !rProd.St_composto;
             qtd_entrada.Enabled = !rProd.St_composto;
             vl_entrada.Enabled  = !rProd.St_composto;
             codBarra.Text       = BuscarCodBarra();
             //Buscar Preço Venda
             if (!string.IsNullOrEmpty(cd_produto.Text) && !string.IsNullOrEmpty(cd_tabelapreco.Text))
             {
                 vl_precovenda.Value = TCN_LanPrecoItem.Busca_ConsultaPreco(cd_empresa.Text, cd_produto.Text, cd_tabelapreco.Text, null);
             }
             if (!rProd.St_composto)
             {
                 //Buscar Preço Custo
                 if (!string.IsNullOrEmpty(cd_produto.Text) && !string.IsNullOrEmpty(cd_local.Text))
                 {
                     vl_custo.Value   = TCN_LanEstoque.Vl_MedioLocal(cd_empresa.Text, cd_produto.Text, cd_local.Text, null);
                     vl_entrada.Value = vl_custo.Value;
                 }
                 //Buscar Saldo
                 quantidade.Value = BuscarSaldoLocal();
                 //Total Custo Estoque
                 if (quantidade.Value > 0 && vl_custo.Value > 0)
                 {
                     vl_subtotal.Value = quantidade.Value * vl_custo.Value;
                 }
             }
         }
         else
         {
             cd_produto.Clear();
             cd_produto.Focus();
             //se o produto nao existir com o codigo add ao campo para cadastrar
             if (!string.IsNullOrEmpty(codigoBarra))
             {
                 codBarra.Text = codigoBarra;
                 if (InputBox("Pergunta", "Produto não encontrado com o código de barras!\r\n" +
                              "Deseja incluir código ao novo produto ou relacionar ao existente?") == DialogResult.Yes)
                 {
                     bb_novoProd_Click(this, new EventArgs());
                 }
                 else
                 {
                     BuscarProduto();
                 }
             }
         }
         if (!string.IsNullOrEmpty(cd_local.Text))
         {
             qtd_entrada.Focus();
         }
     }
     else
     {
         MessageBox.Show("Obrigatório selecionar empresa para Buscar o Produto!", "Mensagem", MessageBoxButtons.OK, MessageBoxIcon.Information);
         cd_empresa.Focus();
     }
 }