public int EmitirNotaContingencia(NotaFiscal notaFiscal, string cscId, string csc) { var qrCode = string.Empty; string newNodeXml; const string nFeNamespaceName = "http://www.portalfiscal.inf.br/nfe"; var digVal = string.Empty; var config = _configuracaoService.GetConfiguracao(); notaFiscal.Identificacao.Numero = _configuracaoService.ObterProximoNumeroNotaFiscal(notaFiscal.Identificacao.Modelo); notaFiscal.Identificacao.DataHoraEntradaContigencia = config.DataHoraEntradaContingencia; notaFiscal.Identificacao.JustificativaContigencia = config.JustificativaContingencia; notaFiscal.Identificacao.TipoEmissao = notaFiscal.Identificacao.Modelo == Modelo.Modelo65 ? TipoEmissao.ContigenciaNfce : TipoEmissao.FsDa; notaFiscal.CalcularChave(); X509Certificate2 certificado; var certificadoEntity = _certificadoRepository.GetCertificado(); if (!string.IsNullOrWhiteSpace(certificadoEntity.Caminho)) certificado = _certificateManager.GetCertificateByPath(certificadoEntity.Caminho, RijndaelManagedEncryption.DecryptRijndael(certificadoEntity.Senha)); else certificado = _certificateManager.GetCertificateBySerialNumber(certificadoEntity.NumeroSerial, false); if (notaFiscal.Identificacao.Ambiente == Ambiente.Homologacao) notaFiscal.Produtos[0].Descricao = "NOTA FISCAL EMITIDA EM AMBIENTE DE HOMOLOGACAO - SEM VALOR FISCAL"; var refUri = "#NFe" + notaFiscal.Identificacao.Chave; var xml = Regex.Replace(XmlUtil.GerarXmlLoteNFe(notaFiscal, nFeNamespaceName), "<motDesICMS>1</motDesICMS>", string.Empty); XmlNode node = AssinaturaDigital.AssinarLoteComUmaNota(xml, refUri, certificado, ref digVal); if (notaFiscal.Identificacao.Modelo == Modelo.Modelo65) { qrCode = QrCodeUtil.GerarQrCodeNFe(notaFiscal.Identificacao.Chave, notaFiscal.Destinatario, digVal, notaFiscal.Identificacao.Ambiente, notaFiscal.Identificacao.DataHoraEmissao, notaFiscal.TotalNFe.IcmsTotal.ValorTotalNFe.ToString("F", CultureInfo.InvariantCulture), notaFiscal.TotalNFe.IcmsTotal.ValorTotalIcms.ToString("F", CultureInfo.InvariantCulture), cscId, csc, notaFiscal.Identificacao.TipoEmissao); newNodeXml = node.InnerXml.Replace("<qrCode />", "<qrCode>" + qrCode + "</qrCode>"); } else { newNodeXml = node.InnerXml.Replace("<infNFeSupl><qrCode /></infNFeSupl>", ""); } var document = new XmlDocument(); document.LoadXml(newNodeXml); node = document.DocumentElement; if (node == null) throw new ArgumentException("Xml inválido."); var lote = (TEnviNFe)XmlUtil.Deserialize<TEnviNFe>(node.OuterXml); var nfe = lote.NFe[0]; //salvar nota PreEnvio aqui notaFiscal.Identificacao.Status = Status.CONTINGENCIA; var idNotaCopiaSeguranca = _notaFiscalRepository.SalvarNotaFiscalPendente(notaFiscal, XmlUtil.GerarNfeProcXml(nfe, qrCode), notaFiscal.Identificacao.Ambiente); var notaFiscalEntity = _notaFiscalRepository.GetNotaFiscalById(idNotaCopiaSeguranca, false); notaFiscalEntity.Status = (int)Status.CONTINGENCIA; var nfeProcXml = XmlUtil.GerarNfeProcXml(nfe, qrCode); _notaFiscalRepository.Salvar(notaFiscalEntity, nfeProcXml); notaFiscal.QrCodeUrl = qrCode; return idNotaCopiaSeguranca; }
internal async Task <int> EnviarNotaContingencia(NotaFiscal notaFiscal, string cscId, string csc) { TNFe nfe = null; string qrCode = string.Empty; string newNodeXml = string.Empty; string nFeNamespaceName = "http://www.portalfiscal.inf.br/nfe"; string digVal = string.Empty; int idNotaCopiaSeguranca = 0; var config = ConfiguracaoService.GetConfiguracao(); notaFiscal.Identificacao.DataHoraEntradaContigencia = config.DataHoraEntradaContingencia; notaFiscal.Identificacao.JustificativaContigencia = config.JustificativaContingencia; notaFiscal.Identificacao.TipoEmissao = notaFiscal.Identificacao.Modelo == Modelo.Modelo65 ? TipoEmissao.ContigenciaNfce : TipoEmissao.FsDa; notaFiscal.CalcularChave(); X509Certificate2 certificado; var certificadoEntity = new CertificadoRepository(new NFeContext()).GetCertificado(); if (!string.IsNullOrWhiteSpace(certificadoEntity.Caminho)) { certificado = CertificateManager.GetCertificateByPath(certificadoEntity.Caminho, RijndaelManagedEncryption.DecryptRijndael(certificadoEntity.Senha)); } else { certificado = CertificateManager.GetCertificateBySerialNumber(certificadoEntity.NumeroSerial, false); } if (notaFiscal.Identificacao.Ambiente == Domain.Services.Identificacao.Ambiente.Homologacao) { notaFiscal.Produtos[0].Descricao = "NOTA FISCAL EMITIDA EM AMBIENTE DE HOMOLOGACAO - SEM VALOR FISCAL"; } var refUri = "#NFe" + notaFiscal.Identificacao.Chave; var xml = Regex.Replace(XmlUtil.GerarXmlLoteNFe(notaFiscal, nFeNamespaceName), "<motDesICMS>1</motDesICMS>", string.Empty); XmlNode node = AssinaturaDigital.AssinarLoteComUmaNota(xml, refUri, certificado, ref digVal); var codigoUF = (CodigoUfIbge)Enum.Parse(typeof(CodigoUfIbge), notaFiscal.Emitente.Endereco.UF); if (notaFiscal.Identificacao.Modelo == Modelo.Modelo65) { qrCode = QrCodeUtil.GerarQrCodeNFe(notaFiscal.Identificacao.Chave, notaFiscal.Destinatario, digVal, notaFiscal.Identificacao.Ambiente, notaFiscal.Identificacao.DataHoraEmissao, notaFiscal.TotalNFe.IcmsTotal.ValorTotalNFe.ToString("F", CultureInfo.InvariantCulture), notaFiscal.TotalNFe.IcmsTotal.ValorTotalIcms.ToString("F", CultureInfo.InvariantCulture), cscId, csc, notaFiscal.Identificacao.TipoEmissao); newNodeXml = node.InnerXml.Replace("<qrCode />", "<qrCode>" + qrCode + "</qrCode>"); } else { newNodeXml = node.InnerXml.Replace("<infNFeSupl><qrCode /></infNFeSupl>", ""); } var document = new XmlDocument(); document.LoadXml(newNodeXml); node = document.DocumentElement; TEnviNFe lote = (TEnviNFe)XmlUtil.Deserialize <TEnviNFe>(node.OuterXml); nfe = lote.NFe[0]; //salvar nota PreEnvio aqui notaFiscal.Identificacao.Status = NFe.Repository.Status.CONTINGENCIA; var notaFiscalService = new NotaFiscalService(); idNotaCopiaSeguranca = await notaFiscalService.SalvarNotaFiscalPendenteAsync(notaFiscal, XmlUtil.GerarNfeProcXml(nfe, qrCode), notaFiscal.Identificacao.Ambiente); var notaFiscalEntity = await notaFiscalService.GetNotaFiscalByIdAsync(idNotaCopiaSeguranca, false); notaFiscalEntity.Status = (int)Status.CONTINGENCIA; string nfeProcXml = XmlUtil.GerarNfeProcXml(nfe, qrCode); await notaFiscalService.SalvarAsync(notaFiscalEntity, nfeProcXml); notaFiscal.QrCodeUrl = qrCode; return(idNotaCopiaSeguranca); }