async Task ConsultaCliente() { if (string.IsNullOrEmpty(userCodeEntry.Text)) { btnCadastraCliente.IsEnabled = false; btnCadastraCliente.Opacity = 0.5; await DisplayAlert("Alerta!", "Preencha o CPF ou CNPJ.", "OK"); userCodeEntry.Focus(); return; } string buscaCliente = Util.RemoveSpecialCharacters(userCodeEntry.Text.Trim()); if (buscaCliente.Length != 11 && buscaCliente.Length != 14) { LimpaCampos(); btnCadastraCliente.IsEnabled = false; btnCadastraCliente.Opacity = 0.5; await DisplayAlert("Alerta!", "O CPF ou CNPJ não é válido.", "OK"); return; } if (buscaCliente.Length == 11) { isCnpj = false; if (!Util.IsCpf(buscaCliente)) { LimpaCampos(); btnCadastraCliente.IsEnabled = false; btnCadastraCliente.Opacity = 0.5; await DisplayAlert("Alerta!", "O CPF não é válido.", "OK"); return; } } if (buscaCliente.Length == 14) { isCnpj = true; if (!Util.IsCnpj(buscaCliente)) { LimpaCampos(); btnCadastraCliente.Opacity = 0.5; btnCadastraCliente.IsEnabled = false; await DisplayAlert("Alerta!", "O CNPJ não é válido.", "OK"); return; } } //Verifica se já existe CNPJ cadastrado IsBusy = true; ClienteService clienteLogado = new ClienteService(); ClienteModel cliente = new ClienteModel(); cliente = await clienteLogado.BuscaClientePorCnpj(buscaCliente); IsBusy = false; if (cliente != null && cliente.IdCliente > 0) { GlobalVariables.GlobalClientePedido = cliente; lblRazaoSocial.Text = cliente.RazaoSocial.ToUpper(); lblNomeFantasia.Text = cliente.NomeFantasia.ToUpper(); lblTelefone.Text = cliente.Fone; lblEmail.Text = cliente.Email; lblEndereco.Text = cliente.Endereco; lblNumero.Text = cliente.Numero; lblBairro.Text = cliente.Bairro; lblMunicipio.Text = cliente.Cidade; lblEstado.Text = cliente.Estado; lblCep.Text = cliente.Cep; if (isCnpj) { lblIE.Text = cliente.IE; } else { lblIE.Text = ""; } btnCadastraCliente.IsEnabled = false; } else { if (isCnpj) { var answer = await DisplayAlert("Cliente não cadastrado!", "Deseja consultar o CNPJ na base da Receita Federal?", "Sim", "Não"); if (answer) { await ConsultaCNPJReceita(buscaCliente); } else { btnCadastraCliente.Opacity = 1; btnCadastraCliente.IsEnabled = true; } } else { await DisplayAlert("Info", "Cliente não cadastrado!", "OK"); btnCadastraCliente.Opacity = 1; btnCadastraCliente.IsEnabled = true; return; } } }
async Task ConsultaCNPJ(object sender, EventArgs e) { try { if (!CrossConnectivity.Current.IsConnected) { await DisplayAlert("Alerta!", "Sem conexão com à Internet.", "OK"); return; } if (string.IsNullOrEmpty(userCodeEntry.Text)) { await DisplayAlert("Alerta!", "Preencha o CPF ou CNPJ.", "OK"); btnProximoPedido.IsVisible = false; return; } string buscaCliente = Util.RemoveSpecialCharacters(userCodeEntry.Text.Trim()); if (buscaCliente.Length != 11 && buscaCliente.Length != 14) { await DisplayAlert("Alerta!", "O CPF ou CNPJ não é válido.", "OK"); btnProximoPedido.IsVisible = false; return; } if (buscaCliente.Length == 11) { if (!Util.IsCpf(buscaCliente)) { await DisplayAlert("Alerta!", "O CPF não é válido.", "OK"); btnProximoPedido.IsVisible = false; return; } } if (buscaCliente.Length == 14) { if (!Util.IsCnpj(buscaCliente)) { await DisplayAlert("Alerta!", "O CNPJ não é válido.", "OK"); btnProximoPedido.IsVisible = false; return; } } //Verifica se já existe CNPJ cadastrado this.IsBusy = true; ClienteService clienteLogado = new ClienteService(); ClienteModel cliente = new ClienteModel(); cliente = await clienteLogado.BuscaClientePorCnpj(buscaCliente); this.IsBusy = false; if (cliente != null && cliente.IdCliente > 0) { GlobalVariables.GlobalClientePedido = cliente; lblRazaoSocial.IsVisible = true; lblCnpjCpf.IsVisible = true; lblCep.IsVisible = true; lblEndereco.IsVisible = true; lblRazaoSocial.Text = cliente.RazaoSocial; lblCnpjCpf.Text = cliente.CnpjCpf; lblCep.Text = cliente.Cep; lblEndereco.Text = cliente.Endereco; lblNaoEncontrado.Text = ""; btnProximoPedido.IsVisible = true; NovoPedido(sender, e); return; } else { await DisplayAlert("Alerta", "Cliente não cadastrado!", "OK"); lblRazaoSocial.IsVisible = false; lblCnpjCpf.IsVisible = false; lblCep.IsVisible = false; lblEndereco.IsVisible = false; lblRazaoSocial.Text = ""; lblCnpjCpf.Text = ""; lblCep.Text = ""; lblEndereco.Text = ""; lblNaoEncontrado.Text = "Cliente não encontrado"; btnProximoPedido.IsVisible = false; return; } } catch (Exception) { } }