Skip to content

Commit

Permalink
Merge pull request #50 from haroflow/mdfeconfiguracao-singleton
Browse files Browse the repository at this point in the history
Transformada classe MDFeConfiguracao de static para singleton
  • Loading branch information
danilobreda authored Nov 22, 2024
2 parents 19e48cd + b9393ea commit dc9ecea
Show file tree
Hide file tree
Showing 39 changed files with 358 additions and 266 deletions.
36 changes: 18 additions & 18 deletions MDFe.AppTeste/MDFeTesteModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ public void CriarEnviar()
#endregion dados emitente (emit)

#region modal
if (MDFeConfiguracao.VersaoWebService.VersaoLayout == VersaoServico.Versao100)
if (MDFeConfiguracao.Instancia.VersaoWebService.VersaoLayout == VersaoServico.Versao100)
{
mdfe.InfMDFe.InfModal.Modal = new MDFeRodo
{
Expand Down Expand Up @@ -607,7 +607,7 @@ public void CriarEnviar()
}


if (MDFeConfiguracao.VersaoWebService.VersaoLayout == VersaoServico.Versao300)
if (MDFeConfiguracao.Instancia.VersaoWebService.VersaoLayout == VersaoServico.Versao300)
{
mdfe.InfMDFe.InfModal.Modal = new MDFeRodo
{
Expand Down Expand Up @@ -689,7 +689,7 @@ public void CriarEnviar()
};


if (MDFeConfiguracao.VersaoWebService.VersaoLayout == VersaoServico.Versao300)
if (MDFeConfiguracao.Instancia.VersaoWebService.VersaoLayout == VersaoServico.Versao300)
{
mdfe.InfMDFe.InfDoc.InfMunDescarga[0].InfCTe[0].Peri = new List<MDFePeri>
{
Expand All @@ -705,7 +705,7 @@ public void CriarEnviar()

#region seg

if (MDFeConfiguracao.VersaoWebService.VersaoLayout == VersaoServico.Versao300)
if (MDFeConfiguracao.Instancia.VersaoWebService.VersaoLayout == VersaoServico.Versao300)
{
mdfe.InfMDFe.Seg = new List<MDFeSeg>();

Expand Down Expand Up @@ -733,7 +733,7 @@ public void CriarEnviar()

#region Produto Predominante

if (MDFeConfiguracao.VersaoWebService.VersaoLayout == VersaoServico.Versao300)
if (MDFeConfiguracao.Instancia.VersaoWebService.VersaoLayout == VersaoServico.Versao300)
{
mdfe.InfMDFe.prodPred = new prodPred
{
Expand Down Expand Up @@ -865,7 +865,7 @@ public void GerarESalvar()
#endregion dados emitente (emit)

#region modal
if (MDFeConfiguracao.VersaoWebService.VersaoLayout == VersaoServico.Versao100)
if (MDFeConfiguracao.Instancia.VersaoWebService.VersaoLayout == VersaoServico.Versao100)
{
mdfe.InfMDFe.InfModal.VersaoModal = MDFeVersaoModal.Versao100;
mdfe.InfMDFe.InfModal.Modal = new MDFeRodo
Expand Down Expand Up @@ -894,7 +894,7 @@ public void GerarESalvar()
}


if (MDFeConfiguracao.VersaoWebService.VersaoLayout == VersaoServico.Versao300)
if (MDFeConfiguracao.Instancia.VersaoWebService.VersaoLayout == VersaoServico.Versao300)
{
mdfe.InfMDFe.InfModal.VersaoModal = MDFeVersaoModal.Versao300;
mdfe.InfMDFe.InfModal.Modal = new MDFeRodo
Expand Down Expand Up @@ -978,7 +978,7 @@ public void GerarESalvar()
};


if (MDFeConfiguracao.VersaoWebService.VersaoLayout == VersaoServico.Versao300)
if (MDFeConfiguracao.Instancia.VersaoWebService.VersaoLayout == VersaoServico.Versao300)
{
mdfe.InfMDFe.InfDoc.InfMunDescarga[0].InfCTe[0].Peri = new List<MDFePeri>
{
Expand All @@ -994,7 +994,7 @@ public void GerarESalvar()

#region seg

if (MDFeConfiguracao.VersaoWebService.VersaoLayout == VersaoServico.Versao300)
if (MDFeConfiguracao.Instancia.VersaoWebService.VersaoLayout == VersaoServico.Versao300)
{
mdfe.InfMDFe.Seg = new List<MDFeSeg>();

Expand Down Expand Up @@ -1346,17 +1346,17 @@ private static void CarregarConfiguracoesMDFe(Configuracao config)
ManterDadosEmCache = config.CertificadoDigital.ManterEmCache,
};

MDFeConfiguracao.ConfiguracaoCertificado = configuracaoCertificado;
MDFeConfiguracao.CaminhoSchemas = config.ConfigWebService.CaminhoSchemas;
MDFeConfiguracao.CaminhoSalvarXml = config.DiretorioSalvarXml;
MDFeConfiguracao.IsSalvarXml = config.IsSalvarXml;
MDFeConfiguracao.Instancia.ConfiguracaoCertificado = configuracaoCertificado;
MDFeConfiguracao.Instancia.CaminhoSchemas = config.ConfigWebService.CaminhoSchemas;
MDFeConfiguracao.Instancia.CaminhoSalvarXml = config.DiretorioSalvarXml;
MDFeConfiguracao.Instancia.IsSalvarXml = config.IsSalvarXml;

MDFeConfiguracao.VersaoWebService.VersaoLayout = config.ConfigWebService.VersaoLayout;
MDFeConfiguracao.Instancia.VersaoWebService.VersaoLayout = config.ConfigWebService.VersaoLayout;

MDFeConfiguracao.VersaoWebService.TipoAmbiente = config.ConfigWebService.Ambiente;
MDFeConfiguracao.VersaoWebService.UfEmitente = config.ConfigWebService.UfEmitente;
MDFeConfiguracao.VersaoWebService.TimeOut = config.ConfigWebService.TimeOut;
MDFeConfiguracao.IsAdicionaQrCode = true;
MDFeConfiguracao.Instancia.VersaoWebService.TipoAmbiente = config.ConfigWebService.Ambiente;
MDFeConfiguracao.Instancia.VersaoWebService.UfEmitente = config.ConfigWebService.UfEmitente;
MDFeConfiguracao.Instancia.VersaoWebService.TimeOut = config.ConfigWebService.TimeOut;
MDFeConfiguracao.Instancia.IsAdicionaQrCode = true;
}

protected virtual void OnSucessoSync(RetornoEEnvio e)
Expand Down
47 changes: 25 additions & 22 deletions MDFe.Classes/Extensoes/ExtMDFe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@ namespace MDFe.Classes.Extencoes
{
public static class ExtMDFe
{
public static MDFEletronico Valida(this MDFEletronico mdfe)
public static MDFEletronico Valida(this MDFEletronico mdfe, MDFeConfiguracao cfgMdfe = null)
{
var config = cfgMdfe ?? MDFeConfiguracao.Instancia;
if (mdfe == null) throw new ArgumentException("Erro de assinatura, MDFe esta null");

var xmlMdfe = FuncoesXml.ClasseParaXmlString(mdfe);

switch (MDFeConfiguracao.VersaoWebService.VersaoLayout)
switch (config.VersaoWebService.VersaoLayout)
{
case VersaoServico.Versao100:
Validador.Valida(xmlMdfe, "mdfe_v1.00.xsd");
Validador.Valida(xmlMdfe, "mdfe_v1.00.xsd", config);
break;
case VersaoServico.Versao300:
Validador.Valida(xmlMdfe, "mdfe_v3.00.xsd");
Validador.Valida(xmlMdfe, "mdfe_v3.00.xsd", config);
break;
}

Expand All @@ -39,63 +40,64 @@ public static MDFEletronico Valida(this MDFEletronico mdfe)

if (tipoModal == typeof (MDFeRodo))
{
switch (MDFeConfiguracao.VersaoWebService.VersaoLayout)
switch (config.VersaoWebService.VersaoLayout)
{
case VersaoServico.Versao100:
Validador.Valida(xmlModal, "mdfeModalRodoviario_v1.00.xsd");
Validador.Valida(xmlModal, "mdfeModalRodoviario_v1.00.xsd", config);
break;
case VersaoServico.Versao300:
Validador.Valida(xmlModal, "mdfeModalRodoviario_v3.00.xsd");
Validador.Valida(xmlModal, "mdfeModalRodoviario_v3.00.xsd", config);
break;
}
}

if (tipoModal == typeof (MDFeAereo))
{
switch (MDFeConfiguracao.VersaoWebService.VersaoLayout)
switch (config.VersaoWebService.VersaoLayout)
{
case VersaoServico.Versao100:
Validador.Valida(xmlModal, "mdfeModalAereo_v1.00.xsd");
Validador.Valida(xmlModal, "mdfeModalAereo_v1.00.xsd", config);
break;
case VersaoServico.Versao300:
Validador.Valida(xmlModal, "mdfeModalAereo_v3.00.xsd");
Validador.Valida(xmlModal, "mdfeModalAereo_v3.00.xsd", config);
break;
}
}

if (tipoModal == typeof (MDFeAquav))
{
switch (MDFeConfiguracao.VersaoWebService.VersaoLayout)
switch (config.VersaoWebService.VersaoLayout)
{
case VersaoServico.Versao100:
Validador.Valida(xmlModal, "mdfeModalAquaviario_v1.00.xsd");
Validador.Valida(xmlModal, "mdfeModalAquaviario_v1.00.xsd", config);
break;
case VersaoServico.Versao300:
Validador.Valida(xmlModal, "mdfeModalAquaviario_v3.00.xsd");
Validador.Valida(xmlModal, "mdfeModalAquaviario_v3.00.xsd", config);
break;
}
}

if (tipoModal == typeof (MDFeFerrov))
{
switch (MDFeConfiguracao.VersaoWebService.VersaoLayout)
switch (config.VersaoWebService.VersaoLayout)
{
case VersaoServico.Versao100:
Validador.Valida(xmlModal, "mdfeModalFerroviario_v1.00.xsd");
Validador.Valida(xmlModal, "mdfeModalFerroviario_v1.00.xsd", config);
break;
case VersaoServico.Versao300:
Validador.Valida(xmlModal, "mdfeModalFerroviario_v3.00.xsd");
Validador.Valida(xmlModal, "mdfeModalFerroviario_v3.00.xsd", config);
break;
}
}

return mdfe;
}

public static MDFEletronico Assina(this MDFEletronico mdfe, EventHandler<string> eventHandlerChaveMdfe = null, object quemInvocouEventoChaveMDFe = null)
public static MDFEletronico Assina(this MDFEletronico mdfe, EventHandler<string> eventHandlerChaveMdfe = null, object quemInvocouEventoChaveMDFe = null, MDFeConfiguracao cfgMdfe = null)
{
if(mdfe == null) throw new ArgumentException("Erro de assinatura, MDFe esta null");

var config = cfgMdfe ?? MDFeConfiguracao.Instancia;
var modeloDocumentoFiscal = mdfe.InfMDFe.Ide.Mod;
var tipoEmissao = (int) mdfe.InfMDFe.Ide.TpEmis;
var codigoNumerico = mdfe.InfMDFe.Ide.CMDF;
Expand All @@ -118,10 +120,10 @@ public static MDFEletronico Assina(this MDFEletronico mdfe, EventHandler<string>
if (eventHandlerChaveMdfe != null)
eventHandlerChaveMdfe.Invoke(quemInvocouEventoChaveMDFe, dadosChave.Chave);

mdfe.InfMDFe.Versao = MDFeConfiguracao.VersaoWebService.VersaoLayout;
mdfe.InfMDFe.Versao = config.VersaoWebService.VersaoLayout;
mdfe.InfMDFe.Ide.CDV = dadosChave.DigitoVerificador;

var assinatura = AssinaturaDigital.Assina(mdfe, mdfe.InfMDFe.Id, MDFeConfiguracao.X509Certificate2);
var assinatura = AssinaturaDigital.Assina(mdfe, mdfe.InfMDFe.Id, config.X509Certificate2);

mdfe.Signature = assinatura;

Expand All @@ -133,12 +135,13 @@ public static string XmlString(this MDFEletronico mdfe)
return FuncoesXml.ClasseParaXmlString(mdfe);
}

public static void SalvarXmlEmDisco(this MDFEletronico mdfe, string nomeArquivo = null)
public static void SalvarXmlEmDisco(this MDFEletronico mdfe, string nomeArquivo = null, MDFeConfiguracao cfgMdfe = null)
{
if (MDFeConfiguracao.NaoSalvarXml()) return;
var config = cfgMdfe ?? MDFeConfiguracao.Instancia;
if (config.NaoSalvarXml()) return;

if (string.IsNullOrEmpty(nomeArquivo))
nomeArquivo = Path.Combine(MDFeConfiguracao.CaminhoSalvarXml, mdfe.Chave() + "-mdfe.xml");
nomeArquivo = Path.Combine(config.CaminhoSalvarXml, mdfe.Chave() + "-mdfe.xml");

FuncoesXml.ClasseParaArquivoXml(mdfe, nomeArquivo);
}
Expand Down
16 changes: 9 additions & 7 deletions MDFe.Classes/Extensoes/ExtMDFeConsReciMDFe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ namespace MDFe.Classes.Extencoes
{
public static class ExtMDFeConsReciMDFe
{
public static void ValidaSchema(this MDFeConsReciMDFe consReciMDFe)
public static void ValidaSchema(this MDFeConsReciMDFe consReciMDFe, MDFeConfiguracao cfgMdfe = null)
{
var config = cfgMdfe ?? MDFeConfiguracao.Instancia;
var xmlValidacao = consReciMDFe.XmlString();

switch (MDFeConfiguracao.VersaoWebService.VersaoLayout)
switch (config.VersaoWebService.VersaoLayout)
{
case VersaoServico.Versao100:
Validador.Valida(xmlValidacao, "consReciMDFe_v1.00.xsd");
Validador.Valida(xmlValidacao, "consReciMDFe_v1.00.xsd", config);
break;
case VersaoServico.Versao300:
Validador.Valida(xmlValidacao, "consReciMDFe_v3.00.xsd");
Validador.Valida(xmlValidacao, "consReciMDFe_v3.00.xsd", config);
break;
}
}
Expand All @@ -38,11 +39,12 @@ public static XmlDocument CriaRequestWs(this MDFeConsReciMDFe consReciMDFe)
return request;
}

public static void SalvarXmlEmDisco(this MDFeConsReciMDFe consReciMDFe)
public static void SalvarXmlEmDisco(this MDFeConsReciMDFe consReciMDFe, MDFeConfiguracao cfgMdfe = null)
{
if (MDFeConfiguracao.NaoSalvarXml()) return;
var config = cfgMdfe ?? MDFeConfiguracao.Instancia;
if (config.NaoSalvarXml()) return;

var caminhoXml = MDFeConfiguracao.CaminhoSalvarXml;
var caminhoXml = config.CaminhoSalvarXml;

var arquivoSalvar = Path.Combine(caminhoXml, consReciMDFe.NRec + "-ped-rec.xml");

Expand Down
16 changes: 9 additions & 7 deletions MDFe.Classes/Extensoes/ExtMDFeConsSitMDFe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ namespace MDFe.Classes.Extencoes
{
public static class ExtMDFeConsSitMDFe
{
public static void ValidarSchema(this MDFeConsSitMDFe consSitMdfe)
public static void ValidarSchema(this MDFeConsSitMDFe consSitMdfe, MDFeConfiguracao cfgMdfe = null)
{
var config = cfgMdfe ?? MDFeConfiguracao.Instancia;
var xmlEnvio = consSitMdfe.XmlString();

switch (MDFeConfiguracao.VersaoWebService.VersaoLayout)
switch (config.VersaoWebService.VersaoLayout)
{
case VersaoServico.Versao100:
Validador.Valida(xmlEnvio, "consSitMDFe_v1.00.xsd");
Validador.Valida(xmlEnvio, "consSitMDFe_v1.00.xsd", config);
break;
case VersaoServico.Versao300:
Validador.Valida(xmlEnvio, "consSitMDFe_v3.00.xsd");
Validador.Valida(xmlEnvio, "consSitMDFe_v3.00.xsd", config);
break;
}
}
Expand All @@ -38,11 +39,12 @@ public static XmlDocument CriaRequestWs(this MDFeConsSitMDFe consSitMdfe)
return request;
}

public static void SalvarXmlEmDisco(this MDFeConsSitMDFe consSitMdfe)
public static void SalvarXmlEmDisco(this MDFeConsSitMDFe consSitMdfe, MDFeConfiguracao cfgMdfe = null)
{
if (MDFeConfiguracao.NaoSalvarXml()) return;
var config = cfgMdfe ?? MDFeConfiguracao.Instancia;
if (config.NaoSalvarXml()) return;

var caminhoXml = MDFeConfiguracao.CaminhoSalvarXml;
var caminhoXml = config.CaminhoSalvarXml;

var arquivoSalvar = Path.Combine(caminhoXml, consSitMdfe.ChMDFe + "-ped-sit.xml");

Expand Down
16 changes: 9 additions & 7 deletions MDFe.Classes/Extensoes/ExtMDFeConsStatServMDFe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ namespace MDFe.Classes.Extencoes
{
public static class ExtMDFeConsStatServMDFe
{
public static void ValidarSchema(this MDFeConsStatServMDFe consStatServMDFe)
public static void ValidarSchema(this MDFeConsStatServMDFe consStatServMDFe, MDFeConfiguracao cfgMdfe = null)
{
var config = cfgMdfe ?? MDFeConfiguracao.Instancia;
var xmlValidacao = consStatServMDFe.XmlString();

switch (MDFeConfiguracao.VersaoWebService.VersaoLayout)
switch (config.VersaoWebService.VersaoLayout)
{
case VersaoServico.Versao100:
Validador.Valida(xmlValidacao, "consStatServMDFe_v1.00.xsd");
Validador.Valida(xmlValidacao, "consStatServMDFe_v1.00.xsd", config);
break;
case VersaoServico.Versao300:
Validador.Valida(xmlValidacao, "consStatServMDFe_v3.00.xsd");
Validador.Valida(xmlValidacao, "consStatServMDFe_v3.00.xsd", config);
break;
}
}
Expand All @@ -38,11 +39,12 @@ public static XmlDocument CriaRequestWs(this MDFeConsStatServMDFe consStatServMd
return request;
}

public static void SalvarXmlEmDisco(this MDFeConsStatServMDFe consStatServMdFe)
public static void SalvarXmlEmDisco(this MDFeConsStatServMDFe consStatServMdFe, MDFeConfiguracao cfgMdfe = null)
{
if (MDFeConfiguracao.NaoSalvarXml()) return;
var config = cfgMdfe ?? MDFeConfiguracao.Instancia;
if (config.NaoSalvarXml()) return;

var caminhoXml = MDFeConfiguracao.CaminhoSalvarXml;
var caminhoXml = config.CaminhoSalvarXml;

var arquivoSalvar = Path.Combine(caminhoXml, "-pedido-status-servico.xml");

Expand Down
16 changes: 9 additions & 7 deletions MDFe.Classes/Extensoes/ExtMDFeCosMDFeNaoEnc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ public static string XmlString(this MDFeCosMDFeNaoEnc consMDFeNaoEnc)
return FuncoesXml.ClasseParaXmlString(consMDFeNaoEnc);
}

public static void ValidarSchema(this MDFeCosMDFeNaoEnc consMdFeNaoEnc)
public static void ValidarSchema(this MDFeCosMDFeNaoEnc consMdFeNaoEnc, MDFeConfiguracao cfgMdfe = null)
{
var config = cfgMdfe ?? MDFeConfiguracao.Instancia;
var xmlValidacao = consMdFeNaoEnc.XmlString();

switch (MDFeConfiguracao.VersaoWebService.VersaoLayout)
switch (config.VersaoWebService.VersaoLayout)
{
case VersaoServico.Versao100:
Validador.Valida(xmlValidacao, "consMDFeNaoEnc_v1.00.xsd");
Validador.Valida(xmlValidacao, "consMDFeNaoEnc_v1.00.xsd", config);
break;
case VersaoServico.Versao300:
Validador.Valida(xmlValidacao, "consMDFeNaoEnc_v3.00.xsd");
Validador.Valida(xmlValidacao, "consMDFeNaoEnc_v3.00.xsd", config);
break;
}
}
Expand All @@ -38,11 +39,12 @@ public static XmlDocument CriaRequestWs(this MDFeCosMDFeNaoEnc cosMdFeNaoEnc)
return request;
}

public static void SalvarXmlEmDisco(this MDFeCosMDFeNaoEnc cosMdFeNaoEnc)
public static void SalvarXmlEmDisco(this MDFeCosMDFeNaoEnc cosMdFeNaoEnc, MDFeConfiguracao cfgMdfe = null)
{
if (MDFeConfiguracao.NaoSalvarXml()) return;
var config = cfgMdfe ?? MDFeConfiguracao.Instancia;
if (config.NaoSalvarXml()) return;

var caminhoXml = MDFeConfiguracao.CaminhoSalvarXml;
var caminhoXml = config.CaminhoSalvarXml;

var arquivoSalvar = Path.Combine(caminhoXml, cosMdFeNaoEnc.CNPJ + "-ped-sit.xml");

Expand Down
Loading

0 comments on commit dc9ecea

Please sign in to comment.