PC SOFT
DEPOT EN LIGNE
POUR WINDEVWEBDEV ET WINDEV MOBILE

[DLL Unimake.DFe] O que é contingência SVC-AN, RS ou SP e como acioná-la automaticamente seu ERP/PDV
Publié par Wandrey
- Non classée
Nouveautés



Description
No 17º episódio do Wandrey Descomplica sobre a DLL Unimake.DFe, aprendemos:

- O que é contingência SVC e como funciona a estrutura de servidores dos ambientes;
- Como entrar em contingência automaticamente;
- Como enviar a CTe no tipo de contingência SVC-RS e SVC-SP;
- Como enviar a NFe no tipo de contingência SVC-RS e SVC-AN.

A seguir, apresento o código em WINDEV para realizar as tarefas descritas acima.

Como enviar a NFe no tipo de contingência SVC-RS e SVC-AN:

```
//CONSULTAR SE O SVC AN OU RS está ativo, se estiver, pode enviar em contingência

//Criar as configurações mínimas necessárias para consumir o serviço
oConfigStatus is Unimake.Business.DFe.Servicos.Configuracao()
oConfigStatus.TipoDFe = TipoDFe.NFe
oConfigStatus.TipoEmissao = TipoEmissao.ContingenciaSVCAN //###
oConfigStatus.CertificadoArquivo = "C:\Projetos\certificados\UnimakePV.pfx"
oConfigStatus.CertificadoSenha = "12345678"

//Criar o XML
oConsStatServ is Unimake.Business.DFe.XML.NFe.ConsStatServ()
oConsStatServ.Versao = "4.00"
oConsStatServ.TpAmb = TipoAmbiente.Producao
oConsStatServ.CUF = UFBrasil.DF //SP ###

EDT_RetornoWSString = ""

lSVCHabilitado is boolean = False

WHEN EXCEPTION IN
//Consumir o serviço
oStatusServico is Unimake.Business.DFe.Servicos.NFe.StatusServico()
oStatusServico.Executar(oConsStatServ, oConfigStatus)

//Ver a string completa do XML retornado pelo WEBSERVICE
EDT_RetornoWSString += "String do XML retornado:" + CR + CR + oStatusServico.RetornoWSString

//Ver o conteúdo da tag CStat e XMotivo
EDT_RetornoWSString += oStatusServico.Result.CStat + " - " + oStatusServico.Result.xMotivo + CR + CR


IF oStatusServico.Result.CStat = 114 //SVCAN Desabilitado
lSVCHabilitado = False
END
IF oStatusServico.Result.CStat = 107 //SVC Habilitado ou Serviço em Operação
lSVCHabilitado = True
END
DO
Info(ExceptionInfo(errFullDetails)) //Ele também pega o conteúdo da Exception do lado do C#, importante envolver o código em Try/Catch
END

//Enviar a nota em contingência sempre dependendo dela estar ativa.

//Criar as configurações mínimas necessárias para consumir o serviço
oConfiguracao is Unimake.Business.DFe.Servicos.Configuracao()
oConfiguracao.TipoDFe = TipoDFe.NFe
oConfiguracao.TipoEmissao = TipoEmissao.ContingenciaSVCAN //###
oConfiguracao.CertificadoArquivo = "C:\Projetos\certificados\UnimakePV.pfx"
oConfiguracao.CertificadoSenha = "12345678"

//Criar o XML

//Criar tag <enviNFe>
oEnviNFe is Unimake.Business.DFe.XML.NFe.EnviNFe()
oEnviNFe.Versao = "4.00"
oEnviNFe.IdLote = "000000000000001"
oEnviNFe.IndSinc = SimNao.Sim

//Criar a tag <NFe>
oNfe is Unimake.Business.DFe.XML.NFe.NFe()

// Criar tag <infNfe>
oInfNFe is Unimake.Business.DFe.XML.NFe.InfNFe()
oInfNFe.Versao = "4.00"

//Criar tag <ide>
oIde is Unimake.Business.DFe.XML.NFe.Ide()
oIde.CUF = UFBrasil.PR
oIde.NatOp = "VENDA PRODUC.DO ESTABELEC"
oIde.Mod = ModeloDFe.NFe
oIde.Serie = 30
oIde.NNF = 8
oIde.DhEmi = SysDateTime()
oIde.DhSaiEnt = SysDateTime()
oIde.TpNF = 1 //TipoOperacao.Saida
oIde.IdDest = 2 //DestinoOperacao.OperacaoInterestadual
oIde.CMunFG = 4118402
oIde.TpImp = 1 //FormatoImpressaoDANFE.NormalRetrato
oIde.TpEmis = TipoEmissao.ContingenciaSVCAN //###
oIde.TpAmb = TipoAmbiente.Homologacao
oIde.FinNFe = FinalidadeNFe.Normal
oIde.IndFinal = SimNao.Sim
oIde.IndPres = IndicadorPresenca.OperacaoPresencial
oIde.ProcEmi = ProcessoEmissao.AplicativoContribuinte
oIde.VerProc = "TESTE 2.00"

//Adicionar a tag Ide dentro da tag InfNFe
oInfNFe.Ide = oIde

//Criar tag Emit
oEmit is Unimake.Business.DFe.XML.NFe.Emit()
oEmit.CNPJ = "06117473000150"
oEmit.XNome = "UNIMAKE SOLUCOES CORPORATIVAS LTDA"
oEmit.XFant = "UNIMAKE - PARANAVAI"
oEmit.IE = "9032000301"
oEmit.IM = "14018"
oEmit.CNAE = "6202300"
oEmit.CRT = 1 //CRT.SimplesNacional

oEnderEmit is Unimake.Business.DFe.XML.NFe.EnderEmit()
oEnderEmit.XLgr = "RUA PAULO ANTONIO COSTA"
oEnderEmit.Nro = "575"
oEnderEmit.XBairro = "CENTRO"
oEnderEmit.CMun = 4118402
oEnderEmit.XMun = "PARANAVAI"
oEnderEmit.UF = 41 //UFBrasil.PR
oEnderEmit.CEP = "87707210"
oEnderEmit.Fone = "04431421010"

//Adicionar a tag EnderEmit dentro da tag Emit
oEmit.EnderEmit = oEnderEmit

//Adicionar a tag Emit dentro da tag InfNfe
oInfNFe.Emit = oEmit

//Criar tag Dest
oDest is Unimake.Business.DFe.XML.NFe.Dest()
oDest.CNPJ = "04218457000128"
oDest.XNome = "NF-E EMITIDA EM AMBIENTE DE HOMOLOGACAO - SEM VALOR FISCAL"
oDest.IndIEDest = 1 //IndicadorIEDestinatario.ContribuinteICMS
oDest.IE = "582614838110"
oDest.Email = "janelaorp@janelaorp.com.br"

oEnderDest is Unimake.Business.DFe.XML.NFe.EnderDest()
oEnderDest.XLgr = "AVENIDA DA SAUDADE"
oEnderDest.Nro = "1555"
oEnderDest.XBairro = "CAMPOS ELISEOS"
oEnderDest.CMun = 3543402
oEnderDest.XMun = "RIBEIRAO PRETO"
oEnderDest.UF = 35 //UFBrasil.SP
oEnderDest.CEP = "14080000"
oEnderDest.Fone = "01639611500"

//Adicionar a tag EnderDest dentro da tag Dest
oDest.EnderDest = oEnderDest

//Adicionar a tag Dest dentro da tag InfNfe
oInfNFe.Dest = oDest

FOR I = 1 TO 3
//criar tag Det
oDet is Unimake.Business.DFe.XML.NFe.Det()
oDet.NItem = I

oProd is Unimake.Business.DFe.XML.NFe.Prod()
oProd.CProd = Complete(NumToString(I), 5, "0")
oProd.CEAN = "SEM GTIN"
oProd.XProd = "NF-E EMITIDA EM AMBIENTE DE HOMOLOGACAO - SEM VALOR FISCAL"
oProd.NCM = "84714900"
oProd.CFOP = "6101"
oProd.UCom = "LU"
oProd.QCom = 1.00
oProd.VUnCom = 84.90
oProd.VProd = 84.90
oProd.CEANTrib = "SEM GTIN"
oProd.UTrib = "LU"
oProd.QTrib = 1.00
oProd.VUnTrib = 84.90
oProd.IndTot = SimNao.Sim //ou 1
oProd.XPed = "300474"
oProd.NItemPed = 1

//Adicionar a tag Prod dentro da tag Det
oDet.Prod = oProd

//Criar tag Imposto
oImposto is Unimake.Business.DFe.XML.NFe.Imposto()
oImposto.VTotTrib = 12.63

//Criar tag Icms
oICMS is Unimake.Business.DFe.XML.NFe.ICMS()

//Criar tag ICMSSN101
oICMSSN101 is Unimake.Business.DFe.XML.NFe.ICMSSN101
oICMSSN101.Orig = 0 //OrigemMercadoria.Nacional
oICMSSN101.PCredSN = 2.8255
oICMSSN101.VCredICMSSN = 2.40

//Adicionar a tag ICMSSN101 dentro da tag ICMS
oICMS.ICMSSN101 = oICMSSN101

//Adicionar a tag ICMS dentro da tag Imposto
oImposto.Icms = oICMS

//Criar tag PIS
oPIS is Unimake.Business.DFe.XML.NFe.PIS()

//Criar tag PISOutr
oPISOutr is Unimake.Business.DFe.XML.NFe.PISOutr()
oPISOutr.CST = "99"
oPISOutr.VBC = 0.00
oPISOutr.PPIS = 0.00
oPISOutr.VPIS = 0.00

//Adicionar a PisOutr dentro da tag Pis
oPIS.PISOutr = oPISOutr

//Adicionar a tag Pis dentro da tag Imposto
oImposto.PIS = oPIS

//Criar tag COFINS
oCOFINS is Unimake.Business.DFe.XML.NFe.COFINS()

//Criar tag COFINSOutr
oCOFINSOutr is Unimake.Business.DFe.XML.NFe.COFINSOutr
oCOFINSOutr.CST = "99"
oCOFINSOutr.VBC = 0.00
oCOFINSOutr.PCOFINS = 0.00
oCOFINSOutr.VCOFINS = 0.00

//Adicionar a COFINSOutr dentro da tag COFINS
oCOFINS.COFINSOutr = oCOFINSOutr

//Adicionar a tag COFINS dentro da tag Imposto
oImposto.COFINS = oCOFINS

//Adicionar a tag Imposto dentro da tag Det
oDet.Imposto = oImposto

//Adicionar a tag Det dentro da tag InfNfe
oInfNFe.AddDet(oDet)
END

//Criar tag Total
oTotal is Unimake.Business.DFe.XML.NFe.Total()

//Criar tag ICMSTot
oICMSTot is Unimake.Business.DFe.XML.NFe.ICMSTot()
oICMSTot.VBC = 0
oICMSTot.VICMS = 0
oICMSTot.VICMSDeson = 0
oICMSTot.VFCP = 0
oICMSTot.VBCST = 0
oICMSTot.VST = 0
oICMSTot.VFCPST = 0
oICMSTot.VFCPSTRet = 0
oICMSTot.VProd = 254.70
oICMSTot.VFrete = 0
oICMSTot.VSeg = 0
oICMSTot.VDesc = 0
oICMSTot.VII = 0
oICMSTot.VIPI = 0
oICMSTot.VIPIDevol = 0
oICMSTot.VPIS = 0
oICMSTot.VCOFINS = 0
oICMSTot.VOutro = 0
oICMSTot.VNF = 254.70
oICMSTot.VTotTrib = 37.89

//Adicionar a tag ICMSTot dentro da tag Total
oTotal.ICMSTot = oICMSTot

//Adicionar a tag Total dentro da tag InfNfe
oInfNFe.Total = oTotal

//Criar a tag Transp
oTransp is Unimake.Business.DFe.XML.NFe.Transp()
oTransp.ModFrete = 0 //ModalidadeFrete.ContratacaoFretePorContaRemetente_CIF

//Criar a tag Vol
oVol is Unimake.Business.DFe.XML.NFe.Vol
oVol.QVol = 1
oVol.Esp = "LU"
oVol.Marca = "UNIMAKE"
oVol.PesoL = 0.000
oVol.PesoB = 0.000

//Adicionar a tag Vol na tag Transp
oTransp.AddVol(oVol)

//Adicionar a tag Transp dentro da tag InfNfe
oInfNFe.Transp = oTransp

//Criar tag Cobr
oCobr is Unimake.Business.DFe.XML.NFe.Cobr()

//Criar tag Fat
oFat is Unimake.Business.DFe.XML.NFe.Fat()
oFat.NFat = "057910"
oFat.VOrig = 254.70
oFat.VDesc = 0
oFat.VLiq = 254.70

//Criar tag Dup (parcela 1)
oDup is Unimake.Business.DFe.XML.NFe.Dup()
oDup.NDup = "001"
oDup.DVenc = SysDateTime()
oDup.VDup = 127.35

//Adicionar a tag Dup dentro da tag Cobr
oCobr.AddDup(oDup)

//Criar tag Dup (parcela 2)
oDup2 is Unimake.Business.DFe.XML.NFe.Dup()
oDup2.NDup = "002"
oDup2.DVenc = SysDateTime()
oDup2.VDup = 127.35

//Adicionar a tag Dup dentro da tag Cobr
oCobr.AddDup(oDup2)

//Adicionar a tag Fat dentro da tag Cobr
oCobr.Fat = oFat

//Adicionar a tag Cobr dentro da tag InfNfe
oInfNFe.Cobr = oCobr

//Criar tag Pag
oPag is Unimake.Business.DFe.XML.NFe.Pag()

//Criar tag DetPag (pode ter mais que uma, só foi criada uma como exemplo)
oDetPag is Unimake.Business.DFe.XML.NFe.DetPag()
oDetPag.IndPag = 0 // IndicadorPagamento.PagamentoVista
oDetPag.TPag = 1 // MeioPagamento.Dinheiro
oDetPag.VPag = 254.70

//Adicionar a tag DetPag dentro da tag Tag
oPag.AddDetPag(oDetPag)

//Adicionar a tag Pag dentro da InfNfe
oInfNFe.Pag = oPag

//Criar tag InfAdic
oInfAdic is Unimake.Business.DFe.XML.NFe.InfAdic()
oInfAdic.InfCpl = "Empresa optante pelo simples nacional, conforme lei compl. 128 de 19/12/2008"

//Adicionar a tag InfAdic dentro da tag InfNfe
oInfNFe.InfAdic = oInfAdic

//Criar tag InfRespTec
oInfRespTec is Unimake.Business.DFe.XML.NFe.InfRespTec()
oInfRespTec.CNPJ = "06117473000150"
oInfRespTec.XContato = "Ze das Couves"
oInfRespTec.Email = "zedascouves@gmail.com"
oInfRespTec.Fone = "04430000000"

//Adicionar a tag InfRespTec dentro da tag InfNfe
oInfNFe.InfRespTec = oInfRespTec

//Adicionar a tag InfNfe dentro da tag Nfe
oNfe.AddInfNFe(oInfNFe)

//Adiconar a tag nfe dentro da tag EnviNfe
oEnviNFe.AddNFe(oNfe)

//Recuperar algumas informações do objeto da NFe

// Criar uma variável dinâmica para armazenar o objeto .NET
oTagNFe is Unimake.Business.DFe.XML.NFe.NFe() <- oEnviNFe.GetNFe(0)
oTagInfNFe is Unimake.Business.DFe.XML.NFe.InfNFe() <- oTagNFe.GetInfNFe(0)

EDT_RetornoWSString += "<chNFe> " + oTagInfNFe.Chave + CR
EDT_RetornoWSString += "<cNF> " + oTagInfNFe.Ide.CNF + CR
EDT_RetornoWSString += "<nNF> " + oTagInfNFe.Ide.NNF + CR

//Não encontrei outra forma de resgatar o código do Enum do C# para o WINDEV, talvez tenha algo que facilite, mas por hora é o que foi possível
CUFInt is int = oTagInfNFe.Ide.CUF

EDT_RetornoWSString += "<ide><cUF>" + CUFInt + CR + CR

WHEN EXCEPTION IN
//Consumir o serviço
oAutorizacao is Unimake.Business.DFe.Servicos.NFe.Autorizacao()
oAutorizacao.SetXMLConfiguracao(oEnviNFe, oConfiguracao)

//Pode-se gravar o conteudo do XML assinado na base de dados antes do envio, caso queira recuperar para futuro tratamento, isso da garantias
notaAssinada is string = oAutorizacao.GetConteudoNFeAssinada(0)

// Demonstrar o XML da nota assinada na tela
EDT_RetornoWSString += "XML nota assinado: " + CR + CR + notaAssinada + CR + CR

// Gravar o XML assinado no HD antes de enviar
chaveNFe is string = oTagInfNFe.Chave
fDelete("d:\testenfe\" + chaveNFe + "-nfe.xml")
fSaveText("d:\testenfe\" + chaveNFe + "-nfe.xml", notaAssinada)

oAutorizacao.Executar(oEnviNFe, oConfiguracao)

//Ver a string do XML retornado pela SEFAZ
EDT_RetornoWSString += "Retorno do webservice: " + CR + CR + oAutorizacao.RetornoWSString + CR + CR

EDT_RetornoWSString += "<cStat Lote> " + oAutorizacao.Result.CStat + CR
EDT_RetornoWSString += "<xMotivo Lote>" + oAutorizacao.Result.XMotivo + CR + CR

falhouEnvioNFe is boolean = False

//Vamos imaginar que por algum motivo enviou a nota e não peguei o retorno, por falha de internet, SEFAZ, etc... como finalizar a nota?
//Codigo dentro do IF a seguir é somente em casos de problema, cada um vai ter que adaptar ele de acordo com a sua rotina de envio de nota.
IF falhouEnvioNFe THEN
//Criar as configurações mínimas necessárias para consumir o serviço
oConfConsSitNFe is Unimake.Business.DFe.Servicos.Configuracao()
oConfConsSitNFe.TipoDFe = TipoDFe.NFe
oConfConsSitNFe.TipoEmissao = TipoEmissao.ContingenciaSVCAN //###
oConfConsSitNFe.CertificadoArquivo = "C:\Projetos\certificados\UnimakePV.pfx"
oConfConsSitNFe.CertificadoSenha = "12345678"

//Criar tag ConsSitNFe
oConsSitNFe is Unimake.Business.DFe.XML.NFe.ConsSitNFe
oConsSitNFe.Versao = "4.00"
oConsSitNFe.TpAmb = TipoAmbiente.Homologacao
oConsSitNFe.ChNFe = chaveNFe

//Consumir o serviço
oConsultaProtocolo is Unimake.Business.DFe.Servicos.NFe.ConsultaProtocolo()
oConsultaProtocolo.Executar(oConsSitNFe, oConfConsSitNFe)

IF oConsultaProtocolo.Result.CStat = 100 //Nota autorizada
oAutorizacao.AddRetConsSitNFes(oConsultaProtocolo.Result)

//Numero do Protocolo da Nota
oRetConsSitNFe is Unimake.Business.DFe.XML.NFe.RetConsSitNFe <- oConsultaProtocolo.Result
protocoloNFe is string = oRetConsSitNFe.ProtNFe.InfProt.NProt

//Gravar o XML de distribuição no HD
oAutorizacao.GravarXmlDistribuicao("d:\testenfe")

//Pegar a string do XML de distribuição para gravar na base de dados
docProcNFe is string = oAutorizacao.GetNFeProcResults(chaveNFe)
EDT_RetornoWSString += "XML de distribuição:" + CR + CR
EDT_RetornoWSString += docProcNFe + CR + CR
ELSE
//Alguma rejeição, fazer tratamentos
EDT_RetornoWSString += "Rejeição consulta protocolo:" + CR + CR
EDT_RetornoWSString += oConsultaProtocolo.Result.CStat + CR
EDT_RetornoWSString += oConsultaProtocolo.Result.XMotivo + CR + CR
END
ELSE
//108 – Serviço Paralisado Temporariamente ###
//109 – Serviço Paralisado e sem previsão de retorno ###

IF oAutorizacao.Result.CStat = 104 // 104 = Lote processado com sucesso
//Ver o conteúdo das tags de retorno da SEFAZ

oRetEnviNFe is Unimake.Business.DFe.XML.NFe.RetEnviNFe() <- oAutorizacao.Result

EDT_RetornoWSString += "<cStat NFe> " + oRetEnviNFe.ProtNFe.InfProt.CStat + CR
EDT_RetornoWSString += "<xMotivo NFe>" + oRetEnviNFe.ProtNFe.InfProt.XMotivo + CR + CR

IF oRetEnviNFe.ProtNFe.InfProt.CStat = 100 // 100 = Autorizado o uso da NF-e
//Gravar o XML de distribuição no HD
oAutorizacao.GravarXmlDistribuicao("d:\testenfe")

//Pegar a string do XML de distribuição para gravar na base de dados
docProcNFe is string = oAutorizacao.GetNFeProcResults(chaveNFe)
EDT_RetornoWSString += "XML de distribuição:" + CR + CR
EDT_RetornoWSString += docProcNFe + CR + CR

//Como pegar o numero do protocolo de autorizacao para gravar na base
EDT_RetornoWSString += "<nProt>" + oRetEnviNFe.ProtNFe.InfProt.NProt + CR + CR
END
END
END

Info("FIM")
DO
Info(ExceptionInfo(errFullDetails)) //Ele também pega o conteúdo da Exception do lado do C#, importante envolver o código em Try/Catch
END
```

Para uma explicação mais detalhada do conteúdo deste post, confira o vídeo no seguinte link:
https://www.youtube.com/watch?v=fRFuG1hjr2k
Illustrations, copies d'écran
none
none
Avis des utilisateurs
(Pour noter la ressource, cliquez sur Ecrire un avis)
Aucun avis ou commentaire ? Soyez le premier !