Exemple #1
0
        private void GeranNfe(VersaoServico versaoServico, ModeloDocumento modelo)
        {
            try
            {
                #region Gerar NFe

                var numero = Funcoes.InpuBox("Criar e Enviar NFe", "Número da Nota:");
                if (string.IsNullOrEmpty(numero))
                {
                    throw new Exception("O Número deve ser informado!");
                }

                _nfe = GetNf(Convert.ToInt32(numero), modelo, versaoServico);
                _nfe.Assina();
                //Descomente a linha abaixo se a SEFAZ de sua UF já habilitou a NT2015.002
                _nfe.infNFeSupl = new infNFeSupl()
                {
                    qrCode = EnderecadorDanfeNfce.ObterUrlQrCode(_nfe, _configuracoes.ConfiguracaoDanfeNfce)
                };
                _nfe.Valida();

                #endregion

                ExibeNfe();

                var dlg = new SaveFileDialog
                {
                    FileName   = _nfe.infNFe.Id.Substring(3),
                    DefaultExt = ".xml",
                    Filter     = "Arquivo XML (.xml)|*.xml"
                };
                var result = dlg.ShowDialog();
                if (result != true)
                {
                    return;
                }
                var arquivoXml = dlg.FileName;
                _nfe.SalvarArquivoXml(arquivoXml);
            }
            catch (Exception ex)
            {
                if (!string.IsNullOrEmpty(ex.Message))
                {
                    Funcoes.Mensagem(ex.Message, "Erro", MessageBoxButton.OK);
                }
            }
        }
Exemple #2
0
        private void BtnEnviaEpec_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                /*
                 * Atenção:
                 * O campo dhEmi da nfe a ser vinculada ao EPEC deve ser exatamente igual ao informado m detevento do EPEC, assim como os demais dados, como emitente, destinatário, etc.
                 * Vide a rejeição código 467 no manual do EPEC
                 */

                #region Enviar EPEC

                var idlote = Funcoes.InpuBox(this, "Enviar EPEC", "Identificador de controle do Lote de envio:");
                if (String.IsNullOrEmpty(idlote))
                {
                    throw new Exception("A Id do Lote deve ser informada!");
                }

                var sequenciaEvento = Funcoes.InpuBox(this, "Enviar EPEC", "Número sequencial do evento:");
                if (String.IsNullOrEmpty(sequenciaEvento))
                {
                    throw new Exception("O número sequencial deve ser informado!");
                }

                var numeronota = Funcoes.InpuBox(this, "Enviar EPEC", "Número da Nota:");
                if (String.IsNullOrEmpty(numeronota))
                {
                    throw new Exception("O Número da Nota deve ser informado!");
                }

                _nfe = GetNf(Convert.ToInt32(numeronota), ModeloDocumento.NFe, _configuracoes.CfgServico.VersaoNFeAutorizacao);

                var servicoNFe  = new ServicosNFe(_configuracoes.CfgServico);
                var retornoEpec = servicoNFe.RecepcaoEventoEPEC(Convert.ToInt16(idlote), Convert.ToInt16(sequenciaEvento), _nfe, "3.10");
                TrataRetorno(retornoEpec);

                #endregion
            }
            catch (Exception ex)
            {
                if (!String.IsNullOrEmpty(ex.Message))
                {
                    Funcoes.Mensagem(ex.Message, "Erro", MessageBoxButton.OK);
                }
            }
        }
        /// <summary>
        ///     Salva os dados de CfgServico em um arquivo XML
        /// </summary>
        /// <param name="arquivo">Arquivo XML onde será salvo os dados</param>
        public void Salvar(string arquivo)
        {
            var camposEmBranco = Funcoes.ObterPropriedadesEmBranco(CfgServico);

            var propinfo = Funcoes.ObterPropriedadeInfo(_cfgServico, c => c.DiretorioSalvarXml);
            camposEmBranco.Remove(propinfo.Name);

            //if (camposEmBranco.Count > 0)
            //    throw new Exception("Informe os dados abaixo antes de salvar as Configurações:" + Environment.NewLine + String.Join(", ", camposEmBranco.ToArray()));

            var dir = Path.GetDirectoryName(arquivo);
            if (dir != null && !Directory.Exists(dir))
            {
                throw new DirectoryNotFoundException("Diretório " + dir + " não encontrado!");
            }

            FuncoesXml.ClasseParaArquivoXml(this, arquivo);
        }
Exemple #4
0
        private void CarregarConfiguracao()
        {
            var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            try
            {
                _configuracoes = !File.Exists(path + ArquivoConfiguracao) ? new ConfiguracaoApp() : FuncoesXml.ArquivoXmlParaClasse <ConfiguracaoApp>(path + ArquivoConfiguracao);
                if (_configuracoes.CfgServico.TimeOut == 0)
                {
                    _configuracoes.CfgServico.TimeOut = 100;//mínimo
                }
            }
            catch (Exception ex)
            {
                if (!String.IsNullOrEmpty(ex.Message))
                {
                    Funcoes.Mensagem(ex.Message, "Erro", MessageBoxButton.OK);
                }
            }
        }
Exemple #5
0
        private void GeranNfe(VersaoServico versaoServico, ModeloDocumento modelo)
        {
            try
            {
                #region Gerar NFe

                var numero = Funcoes.InpuBox(this, "Criar e Enviar NFe", "Número da Nota:");
                if (String.IsNullOrEmpty(numero))
                {
                    throw new Exception("O Número deve ser informado!");
                }

                _nfe = GetNf(Convert.ToInt32(numero), modelo, versaoServico);
                _nfe.Assina().Valida();

                #endregion

                ExibeNfe();

                var dlg = new SaveFileDialog
                {
                    FileName   = _nfe.infNFe.Id.Substring(3),
                    DefaultExt = ".xml",
                    Filter     = "Arquivo XML (.xml)|*.xml"
                };
                var result = dlg.ShowDialog();
                if (result != true)
                {
                    return;
                }
                var arquivoXml = dlg.FileName;
                _nfe.SalvarArquivoXml(arquivoXml);
            }
            catch (Exception ex)
            {
                if (!String.IsNullOrEmpty(ex.Message))
                {
                    Funcoes.Mensagem(ex.Message, "Erro", MessageBoxButton.OK);
                }
            }
        }
Exemple #6
0
        public void Prot(string caminho)
        {
            try
            {
                var arquivoXml = caminho;
                var nfe        = new Classes.NFe().CarregarDeArquivoXml(arquivoXml);
                var chave      = nfe.infNFe.Id.Substring(3);

                if (string.IsNullOrEmpty(chave))
                {
                    throw new Exception("A Chave da NFe não foi encontrada no arquivo!");
                }
                if (chave.Length != 44)
                {
                    throw new Exception("Chave deve conter 44 caracteres!");
                }

                var servicoNFe      = new ServicosNFe(_configuracoes.CfgServico);
                var retornoConsulta = servicoNFe.NfeConsultaProtocolo(chave);
                TrataRetorno(retornoConsulta);

                var nfeproc = new nfeProc
                {
                    NFe     = nfe,
                    protNFe = retornoConsulta.Retorno.protNFe,
                    versao  = retornoConsulta.Retorno.versao
                };
                var novoArquivo = _path + "\\Autorizados\\" + @"\" + nfeproc.protNFe.infProt.chNFe +
                                  "-procNfe.xml";
                FuncoesXml.ClasseParaArquivoXml(nfeproc, novoArquivo);
                //Funcoes.Mensagem("Arquivo salvo em " + novoArquivo, "Atenção", MessageBoxButton.OK);
                Impressora.ImprimirDanferE(novoArquivo);
            }
            catch (Exception ex)
            {
                if (!string.IsNullOrEmpty(ex.Message))
                {
                    Funcoes.Mensagem(ex.Message, "Erro", MessageBoxButton.OK);
                }
            }
        }
Exemple #7
0
        private void BtnStatusServico_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                #region Status do serviço

                var servicoNFe    = new ServicosNFe(_configuracoes.CfgServico);
                var retornoStatus = servicoNFe.NfeStatusServico();

                TrataRetorno(retornoStatus);

                #endregion
            }
            catch (Exception ex)
            {
                if (!String.IsNullOrEmpty(ex.Message))
                {
                    Funcoes.Mensagem(ex.Message, "Erro", MessageBoxButton.OK);
                }
            }
        }
Exemple #8
0
        private void BtnConsultaCadastro_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                #region Consulta Cadastro

                var uf = Funcoes.InpuBox(this, "Consultar Cadastro", "UF do Documento a ser Consultado:");
                if (String.IsNullOrEmpty(uf))
                {
                    throw new Exception("A UF deve ser informada!");
                }
                if (uf.Length != 2)
                {
                    throw new Exception("UF deve conter 2 caracteres!");
                }

                var documento = Funcoes.InpuBox(this, "Consultar Cadastro", "Documento(CPF/CNPJ):");
                if (String.IsNullOrEmpty(documento))
                {
                    throw new Exception("O Documento(CPF/CNPJ) deve ser informado!");
                }
                if (documento.Length != 11 & documento.Length != 14)
                {
                    throw new Exception("O Documento(CPF/CNPJ) deve conter 11 ou 14 caracteres!");
                }

                var servicoNFe      = new ServicosNFe(_configuracoes.CfgServico);
                var retornoConsulta = servicoNFe.NfeConsultaCadastro(uf, documento);
                TrataRetorno(retornoConsulta);

                #endregion
            }
            catch (Exception ex)
            {
                if (!String.IsNullOrEmpty(ex.Message))
                {
                    Funcoes.Mensagem(ex.Message, "Erro", MessageBoxButton.OK);
                }
            }
        }
Exemple #9
0
        private void BtnCriareEnviarNfce_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                #region Cria e Envia NFe

                var numero = Funcoes.InpuBox(this, "Criar e Enviar NFCe", "Número da NFCe:");
                if (String.IsNullOrEmpty(numero))
                {
                    throw new Exception("O Número deve ser informado!");
                }

                var lote = Funcoes.InpuBox(this, "Criar e Enviar NFCe", "Id do Lote:");
                if (String.IsNullOrEmpty(lote))
                {
                    throw new Exception("A Id do lote deve ser informada!");
                }

                _nfe = GetNf(Convert.ToInt32(numero), ModeloDocumento.NFCe, _configuracoes.CfgServico.VersaoNFeAutorizacao);
                _nfe.Assina(); //não precisa validar aqui, pois o lote será validado em ServicosNFe.NFeAutorizacao
                var servicoNFe   = new ServicosNFe(_configuracoes.CfgServico);
                var retornoEnvio = servicoNFe.NFeAutorizacao(Convert.ToInt32(lote), IndicadorSincronizacao.Assincrono, new List <Classes.NFe> {
                    _nfe
                });

                TrataRetorno(retornoEnvio);

                #endregion
            }
            catch (Exception ex)
            {
                if (!String.IsNullOrEmpty(ex.Message))
                {
                    Funcoes.Mensagem(ex.Message, "Erro", MessageBoxButton.OK);
                }
            }
        }
Exemple #10
0
        private void BtnAdicionaNfeproc_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var arquivoXml = Funcoes.BuscarArquivoXml();
                var nfe        = new Classes.NFe().CarregarDeArquivoXml(arquivoXml);
                var chave      = nfe.infNFe.Id.Substring(3);

                if (String.IsNullOrEmpty(chave))
                {
                    throw new Exception("A Chave da NFe não foi encontrada no arquivo!");
                }
                if (chave.Length != 44)
                {
                    throw new Exception("Chave deve conter 44 caracteres!");
                }

                var servicoNFe      = new ServicosNFe(_configuracoes.CfgServico);
                var retornoConsulta = servicoNFe.NfeConsultaProtocolo(chave);
                TrataRetorno(retornoConsulta);

                var nfeproc = new nfeProc {
                    NFe = nfe, protNFe = retornoConsulta.Retorno.protNFe, versao = retornoConsulta.Retorno.versao
                };
                var novoArquivo = Path.GetDirectoryName(arquivoXml) + @"\" + nfeproc.protNFe.infProt.chNFe + "-procNfe.xml";
                FuncoesXml.ClasseParaArquivoXml(nfeproc, novoArquivo);
                Funcoes.Mensagem("Arquivo salvo em " + novoArquivo, "Atenção", MessageBoxButton.OK);
            }
            catch (Exception ex)
            {
                if (!String.IsNullOrEmpty(ex.Message))
                {
                    Funcoes.Mensagem(ex.Message, "Erro", MessageBoxButton.OK);
                }
            }
        }
Exemple #11
0
        private void CarregaArquivoNfe()
        {
            var arquivoXml = Funcoes.BuscarArquivoXml();

            _nfe = new Classes.NFe().CarregarDeArquivoXml(arquivoXml);
        }
Exemple #12
0
        private void BtnInutiliza_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                #region Inutiliza Numeração

                var ano = Funcoes.InpuBox(this, "Inutilizar Numeração", "Ano");
                if (String.IsNullOrEmpty(ano))
                {
                    throw new Exception("O Ano deve ser informado!");
                }
                if (ano.Length > 2)
                {
                    throw new Exception("O Ano deve ter dois números apenas!");
                }

                var modelostr = Funcoes.InpuBox(this, "Inutilizar Numeração", "Modelo");
                if (String.IsNullOrEmpty(modelostr))
                {
                    throw new Exception("O Modelo deve ser informado!");
                }

                var modelo = (ModeloDocumento)Convert.ToInt16(modelostr);

                var serie = Funcoes.InpuBox(this, "Inutilizar Numeração", "Série");
                if (String.IsNullOrEmpty(serie))
                {
                    throw new Exception("A série deve ser informada!");
                }

                var numeroInicial = Funcoes.InpuBox(this, "Inutilizar Numeração", "Número Inicial");
                if (String.IsNullOrEmpty(numeroInicial))
                {
                    throw new Exception("O Número Inicial deve ser informado!");
                }

                var numeroFinal = Funcoes.InpuBox(this, "Inutilizar Numeração", "Número Final");
                if (String.IsNullOrEmpty(numeroFinal))
                {
                    throw new Exception("O Número Final deve ser informado!");
                }

                var justificativa = Funcoes.InpuBox(this, "Inutilizar Numeração", "Justificativa");
                if (String.IsNullOrEmpty(justificativa))
                {
                    throw new Exception("A Justificativa deve ser informada!");
                }

                var servicoNFe      = new ServicosNFe(_configuracoes.CfgServico);
                var retornoConsulta = servicoNFe.NfeInutilizacao(_configuracoes.Emitente.CNPJ, Convert.ToInt16(ano), modelo, Convert.ToInt16(serie), Convert.ToInt16(numeroInicial),
                                                                 Convert.ToInt16(numeroFinal), justificativa);

                TrataRetorno(retornoConsulta);

                #endregion
            }
            catch (Exception ex)
            {
                if (!String.IsNullOrEmpty(ex.Message))
                {
                    Funcoes.Mensagem(ex.Message, "Erro", MessageBoxButton.OK);
                }
            }
        }
Exemple #13
0
 private void txt_InputValidationError(object sender, InputValidationErrorEventArgs e)
 {
     Funcoes.Mensagem("Valor Inválido", TituloErro, MessageBoxButton.OK);
 }
Exemple #14
0
 private void BtnArquivoCertificado_Click(object sender, RoutedEventArgs e)
 {
     TxtArquivoCertificado.Text = Funcoes.BuscarArquivoCertificado();
 }
Exemple #15
0
        private void BtnCriareEnviar3_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                #region Cria e Envia NFe

                var numero = Funcoes.InpuBox("Criar e Enviar NFe", "Número da Nota:");
                if (string.IsNullOrEmpty(numero))
                {
                    throw new Exception("O Número deve ser informado!");
                }

                var lote = Funcoes.InpuBox("Criar e Enviar NFe", "Id do Lote:");
                if (string.IsNullOrEmpty(lote))
                {
                    throw new Exception("A Id do lote deve ser informada!");
                }

                _nfe = GetNf(Convert.ToInt32(numero), _configuracoes.CfgServico.ModeloDocumento,
                             _configuracoes.CfgServico.VersaoNFeAutorizacao);
                _nfe.Assina(); //não precisa validar aqui, pois o lote será validado em ServicosNFe.NFeAutorizacao
                //A URL do QR-Code deve ser gerada em um objeto nfe já assinado, pois na URL vai o DigestValue que é gerado por ocasião da assinatura
                //Descomente a linha abaixo se a SEFAZ de sua UF já habilitou a NT2015.002
                _nfe.infNFeSupl = new infNFeSupl()
                {
                    qrCode = EnderecadorDanfeNfce.ObterUrlQrCode(_nfe, _configuracoes.ConfiguracaoDanfeNfce)
                };                                                                                                                               //Define a URL do QR-Code.
                var servicoNFe   = new ServicosNFe(_configuracoes.CfgServico);
                var retornoEnvio = servicoNFe.NFeAutorizacao(Convert.ToInt32(lote), IndicadorSincronizacao.Assincrono, new List <Classes.NFe> {
                    _nfe
                }, true /*Envia a mensagem compactada para a SEFAZ*/);

                TrataRetorno(retornoEnvio);
                if (retornoEnvio.Retorno.infRec.nRec != string.Empty)
                {
                    try
                    {
                        #region Consulta Recibo de lote


                        servicoNFe = new ServicosNFe(_configuracoes.CfgServico);
                        var retornoRecibo = servicoNFe.NFeRetAutorizacao(retornoEnvio.Retorno.infRec.nRec);

                        TrataRetorno(retornoRecibo);
                        //var dlg = new SaveFileDialog
                        //{
                        //    FileName = _nfe.infNFe.Id.Substring(3),
                        //    DefaultExt = ".xml",
                        //    Filter = "Arquivo XML (.xml)|*.xml"
                        //};
                        //var result = dlg.ShowDialog();
                        //if (result != true) return;
                        //var arquivoXml = dlg.FileName;

                        string ch = _nfe.infNFe.Id.Substring(3);
                        _nfe.SalvarArquivoXml(_path + "\\NFCe\\" + ch + ".xml");
                        Prot(_path + "\\NFCe\\" + ch + ".xml");
                        #endregion
                    }
                    catch (Exception ex)
                    {
                        if (!string.IsNullOrEmpty(ex.Message))
                        {
                            Funcoes.Mensagem(ex.Message, "Erro", MessageBoxButton.OK);
                        }
                    }
                }

                #endregion
            }
            catch (Exception ex)
            {
                if (!string.IsNullOrEmpty(ex.Message))
                {
                    Funcoes.Mensagem(ex.Message, "Erro", MessageBoxButton.OK);
                }
            }
        }