Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CTeDistribuicaoDFe #1469

Merged
merged 1 commit into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 42 additions & 13 deletions CTe.Servicos/DistribuicaoDFe/ServicoCTeDistribuicaoDFe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,40 @@



using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using System.Xml;
using CTe.Classes;
using CTe.Classes.Servicos.DistribuicaoDFe;
using CTe.Servicos.Factory;
using CTe.Utils.DistribuicaoDFe;
using CTe.Utils;
using CTe.Wsdl.DistribuicaoDFe;
using DFe.Utils;
using Shared.DFe.Utils;
using System;
using System.IO;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using System.Xml;
using Compressao = DFe.Utils.Compressao;


namespace CTe.Servicos.DistribuicaoDFe
{
public class ServicoCTeDistribuicaoDFe
{
public ServicoCTeDistribuicaoDFe()
{

}

private readonly ConfiguracaoServico _configuracaoServico;
private readonly X509Certificate2 _certificado;
public ServicoCTeDistribuicaoDFe(ConfiguracaoServico configuracaoServico, X509Certificate2 certificado = null)
{
_configuracaoServico = configuracaoServico;
_certificado = certificado ?? configuracaoServico.X509Certificate2;
}


/// <summary>
/// Serviço destinado à distribuição de informações resumidas e documentos fiscais eletrônicos de interesse de um ator, seja este pessoa física ou jurídica.
/// </summary>
Expand All @@ -63,7 +78,7 @@ public class ServicoCTeDistribuicaoDFe
/// <returns>Retorna um objeto da classe CTeDistDFeInteresse com os documentos de interesse do CNPJ/CPF pesquisado</returns>
public RetornoCteDistDFeInt CTeDistDFeInteresse(string ufAutor, string documento, string ultNSU = "0", string nSU = "0", ConfiguracaoServico configuracaoServico = null)
{
var configServico = configuracaoServico ?? ConfiguracaoServico.Instancia;
var configServico = configuracaoServico ?? _configuracaoServico ?? ConfiguracaoServico.Instancia;
distDFeInt pedDistDFeInt;
XmlDocument dadosConsulta;
var ws = InicializaCTeDistDFeInteresse(documento, ultNSU, nSU, out pedDistDFeInt, out dadosConsulta, configServico);
Expand All @@ -82,7 +97,7 @@ public RetornoCteDistDFeInt CTeDistDFeInteresse(string ufAutor, string documento
{
for (int i = 0; i < retConsulta.loteDistDFeInt.Length; i++)
{
string conteudo = Compressao.Unzip(retConsulta.loteDistDFeInt[i].XmlNfe);
string conteudo = Compressao.Unzip(retConsulta.loteDistDFeInt[i].XmlNfe).RemoverDeclaracaoXml();
string chCTe = string.Empty;

if (conteudo.StartsWith("<cteProc"))
Expand All @@ -95,6 +110,15 @@ public RetornoCteDistDFeInt CTeDistDFeInteresse(string ufAutor, string documento
var procEventoNFeConteudo = FuncoesXml.XmlStringParaClasse<Classes.Servicos.DistribuicaoDFe.Schemas.procEventoCTe>(conteudo);
chCTe = procEventoNFeConteudo.eventoCTe.infEvento.chCTe;
}
else if (conteudo.StartsWith("<cteOSProc"))
{
var retConteudo = FuncoesXml.XmlStringParaClasse<CTe.CTeOSDocumento.CTe.CTeOS.Retorno.cteOSProc>(conteudo);
chCTe = retConteudo.protCTe.infProt.chCTe;
}
else
{

}

string[] schema = retConsulta.loteDistDFeInt[i].schema.Split('_');
if (chCTe == string.Empty)
Expand All @@ -111,7 +135,7 @@ public RetornoCteDistDFeInt CTeDistDFeInteresse(string ufAutor, string documento

public async Task<RetornoCteDistDFeInt> CTeDistDFeInteresseAsync(string ufAutor, string documento, string ultNSU = "0", string nSU = "0", ConfiguracaoServico configuracaoServico = null)
{
var configServico = configuracaoServico ?? ConfiguracaoServico.Instancia;
var configServico = configuracaoServico ?? _configuracaoServico ?? ConfiguracaoServico.Instancia;
distDFeInt pedDistDFeInt;
XmlDocument dadosConsulta;
var ws = InicializaCTeDistDFeInteresse(documento, ultNSU, nSU, out pedDistDFeInt, out dadosConsulta, configServico);
Expand All @@ -130,7 +154,7 @@ public async Task<RetornoCteDistDFeInt> CTeDistDFeInteresseAsync(string ufAutor,
{
for (int i = 0; i < retConsulta.loteDistDFeInt.Length; i++)
{
string conteudo = Compressao.Unzip(retConsulta.loteDistDFeInt[i].XmlNfe);
string conteudo = Compressao.Unzip(retConsulta.loteDistDFeInt[i].XmlNfe).RemoverDeclaracaoXml();
string chCTe = string.Empty;

if (conteudo.StartsWith("<cteProc"))
Expand All @@ -143,6 +167,11 @@ public async Task<RetornoCteDistDFeInt> CTeDistDFeInteresseAsync(string ufAutor,
var procEventoNFeConteudo = FuncoesXml.XmlStringParaClasse<Classes.Servicos.DistribuicaoDFe.Schemas.procEventoCTe>(conteudo);
chCTe = procEventoNFeConteudo.eventoCTe.infEvento.chCTe;
}
else if (conteudo.StartsWith("<cteOSProc"))
{
var retConteudo = FuncoesXml.XmlStringParaClasse<CTe.CTeOSDocumento.CTe.CTeOS.Retorno.cteOSProc>(conteudo);
chCTe = retConteudo.protCTe.infProt.chCTe;
}

string[] schema = retConsulta.loteDistDFeInt[i].schema.Split('_');
if (chCTe == string.Empty)
Expand All @@ -164,7 +193,7 @@ private CTeDistDFeInteresse InicializaCTeDistDFeInteresse(string documento, stri

#region Cria o objeto wdsl para consulta

var ws = WsdlFactory.CriaWsdlCTeDistDFeInteresse(configuracaoServico);
var ws = WsdlFactory.CriaWsdlCTeDistDFeInteresse(configuracaoServico, _certificado);

#endregion

Expand All @@ -183,11 +212,11 @@ private CTeDistDFeInteresse InicializaCTeDistDFeInteresse(string documento, stri
pedDistDFeInt.CNPJ = documento;


pedDistDFeInt.distNSU = new distNSU {ultNSU = ultNSU.PadLeft(15, '0')};
pedDistDFeInt.distNSU = new distNSU { ultNSU = ultNSU.PadLeft(15, '0') };

if (!nSU.Equals("0"))
{
pedDistDFeInt.consNSU = new consNSU {NSU = nSU.PadLeft(15, '0')};
pedDistDFeInt.consNSU = new consNSU { NSU = nSU.PadLeft(15, '0') };
pedDistDFeInt.distNSU = null;
}

Expand Down
45 changes: 23 additions & 22 deletions CTe.Servicos/Factory/WsdlFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,109 +46,110 @@
using CTe.Wsdl.ConsultaProtocolo.V4;
using CTe.Wsdl.Evento.V4;
using CTe.Wsdl.Recepcao.Sincrono;
using System.Security.Cryptography.X509Certificates;

namespace CTe.Servicos.Factory
{
public class WsdlFactory
{
public static CteStatusServico CriaWsdlCteStatusServico(ConfiguracaoServico configuracaoServico = null)
public static CteStatusServico CriaWsdlCteStatusServico(ConfiguracaoServico configuracaoServico = null, X509Certificate2 certificado = null)
{
var url = UrlHelper.ObterUrlServico(configuracaoServico).CteStatusServico;

var configuracaoWsdl = CriaConfiguracao(url, configuracaoServico);
var configuracaoWsdl = CriaConfiguracao(url, configuracaoServico, certificado);

return new CteStatusServico(configuracaoWsdl);
}

public static CteConsulta CriaWsdlConsultaProtocolo(ConfiguracaoServico configuracaoServico = null)
public static CteConsulta CriaWsdlConsultaProtocolo(ConfiguracaoServico configuracaoServico = null, X509Certificate2 certificado = null)
{
var url = UrlHelper.ObterUrlServico(configuracaoServico).CteConsulta;

var configuracaoWsdl = CriaConfiguracao(url, configuracaoServico);
var configuracaoWsdl = CriaConfiguracao(url, configuracaoServico, certificado);

return new CteConsulta(configuracaoWsdl);
}

public static CteConsultaV4 CriaWsdlConsultaProtocoloV4(ConfiguracaoServico configuracaoServico = null)
public static CteConsultaV4 CriaWsdlConsultaProtocoloV4(ConfiguracaoServico configuracaoServico = null, X509Certificate2 certificado = null)
{
var url = UrlHelper.ObterUrlServico(configuracaoServico).CteConsulta;

var configuracaoWsdl = CriaConfiguracao(url, configuracaoServico);
var configuracaoWsdl = CriaConfiguracao(url, configuracaoServico, certificado);

return new CteConsultaV4(configuracaoWsdl);
}

public static CteInutilizacao CriaWsdlCteInutilizacao(ConfiguracaoServico configuracaoServico = null)
public static CteInutilizacao CriaWsdlCteInutilizacao(ConfiguracaoServico configuracaoServico = null, X509Certificate2 certificado = null)
{
var url = UrlHelper.ObterUrlServico(configuracaoServico).CteInutilizacao;

var configuracaoWsdl = CriaConfiguracao(url, configuracaoServico);
var configuracaoWsdl = CriaConfiguracao(url, configuracaoServico, certificado);

return new CteInutilizacao(configuracaoWsdl);
}

public static CteRetRecepcao CriaWsdlCteRetRecepcao(ConfiguracaoServico configuracaoServico = null)
public static CteRetRecepcao CriaWsdlCteRetRecepcao(ConfiguracaoServico configuracaoServico = null, X509Certificate2 certificado = null)
{
var url = UrlHelper.ObterUrlServico(configuracaoServico).CteRetRecepcao;

var configuracaoWsdl = CriaConfiguracao(url, configuracaoServico);
var configuracaoWsdl = CriaConfiguracao(url, configuracaoServico, certificado);

return new CteRetRecepcao(configuracaoWsdl);
}

public static CteRecepcao CriaWsdlCteRecepcao(ConfiguracaoServico configuracaoServico = null)
public static CteRecepcao CriaWsdlCteRecepcao(ConfiguracaoServico configuracaoServico = null, X509Certificate2 certificado = null)
{
var url = UrlHelper.ObterUrlServico(configuracaoServico).CteRecepcao;

var configuracaoWsdl = CriaConfiguracao(url, configuracaoServico);
var configuracaoWsdl = CriaConfiguracao(url, configuracaoServico, certificado);

return new CteRecepcao(configuracaoWsdl);
}

public static CteRecepcaoSincronoV4 CriaWsdlCteRecepcaoSincronoV4(ConfiguracaoServico configuracaoServico = null)
public static CteRecepcaoSincronoV4 CriaWsdlCteRecepcaoSincronoV4(ConfiguracaoServico configuracaoServico = null, X509Certificate2 certificado = null)
{
var url = UrlHelper.ObterUrlServico(configuracaoServico).CteRecepcaoSinc;

var configuracaoWsdl = CriaConfiguracao(url, configuracaoServico);
var configuracaoWsdl = CriaConfiguracao(url, configuracaoServico, certificado);

return new CteRecepcaoSincronoV4(configuracaoWsdl);
}

public static CteRecepcaoEvento CriaWsdlCteEvento(ConfiguracaoServico configuracaoServico = null)
public static CteRecepcaoEvento CriaWsdlCteEvento(ConfiguracaoServico configuracaoServico = null, X509Certificate2 certificado = null)
{
var url = UrlHelper.ObterUrlServico(configuracaoServico).CteRecepcaoEvento;

var configuracaoWsdl = CriaConfiguracao(url, configuracaoServico);
var configuracaoWsdl = CriaConfiguracao(url, configuracaoServico, certificado);

return new CteRecepcaoEvento(configuracaoWsdl);
}

public static CteRecepcaoEventoV4 CriaWsdlCteEventoV4(ConfiguracaoServico configuracaoServico = null)
public static CteRecepcaoEventoV4 CriaWsdlCteEventoV4(ConfiguracaoServico configuracaoServico = null, X509Certificate2 certificado = null)
{
var url = UrlHelper.ObterUrlServico(configuracaoServico).CteRecepcaoEvento;

var configuracaoWsdl = CriaConfiguracao(url, configuracaoServico);
var configuracaoWsdl = CriaConfiguracao(url, configuracaoServico, certificado);

return new CteRecepcaoEventoV4(configuracaoWsdl);
}


public static CTeDistDFeInteresse CriaWsdlCTeDistDFeInteresse(ConfiguracaoServico configuracaoServico = null)
public static CTeDistDFeInteresse CriaWsdlCTeDistDFeInteresse(ConfiguracaoServico configuracaoServico = null, X509Certificate2 certificado = null)
{
var url = UrlHelper.ObterUrlServico(configuracaoServico).CTeDistribuicaoDFe;

var configuracaoWsdl = CriaConfiguracao(url, configuracaoServico);
var configuracaoWsdl = CriaConfiguracao(url, configuracaoServico, certificado);

return new CTeDistDFeInteresse(configuracaoWsdl);
}


private static WsdlConfiguracao CriaConfiguracao(string url, ConfiguracaoServico configuracaoServico = null)
private static WsdlConfiguracao CriaConfiguracao(string url, ConfiguracaoServico configuracaoServico, X509Certificate2 certificado)
{
var configServico = configuracaoServico ?? ConfiguracaoServico.Instancia;

var codigoEstado = configServico.cUF.GetCodigoIbgeEmString();
var certificadoDigital = configServico.X509Certificate2;
var certificadoDigital = certificado ?? configServico.X509Certificate2;
var versaoEmString = configServico.VersaoLayout.GetString();
var timeOut = configServico.TimeOut;

Expand Down
17 changes: 16 additions & 1 deletion DFe.Utils/StringExtencoes.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.RegularExpressions;
using System;
using System.Text.RegularExpressions;

namespace Shared.DFe.Utils
{
Expand All @@ -24,5 +25,19 @@ public static string RemoverAcentos(this string valor)

return valor;
}

public static string RemoverDeclaracaoXml(this string xml)
{
if (string.IsNullOrEmpty(xml))
return xml;

var posIni = xml.IndexOf("<?", StringComparison.Ordinal);
if (posIni < 0)
return xml;

var posFinal = xml.IndexOf("?>", StringComparison.Ordinal);
return posFinal < 0 ? xml : xml.Remove(posIni, (posFinal + 2) - posIni);
}

}
}
Loading