PC SOFT
DEPOT EN LIGNE
POUR WINDEVWEBDEV ET WINDEV MOBILE

[DLL Unimake.DFe] Como gerar o XML da NFe e enviar para a SEFAZ no modo assíncrono em WINDEV.
Publié par Wandrey
dans la catégorie Divers
Nouveautés



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

- Gerar o XML da NFe;
- Enviar a NFe para a SEFAZ no modo assíncrono;
- Consultando recibo do lote de NFe enviado;
- Verificando se a NFe foi autorizada, se sim, gerar o XML de distribuição.

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

```
//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.Normal
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.Nao

//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 = 6
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.Normal
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

//Ver o conteúdo das tags de retorno da SEFAZ
EDT_RetornoWSString += "<cStat> " + oAutorizacao.Result.CStat + CR
EDT_RetornoWSString += "<xMotivo>" + oAutorizacao.Result.XMotivo + CR

IF oAutorizacao.Result.CStat = 103 THEN // 103 = Lote recebido com sucesso
oRetEnviNFe is Unimake.Business.DFe.XML.NFe.RetEnviNFe() <- oAutorizacao.Result
EDT_RetornoWSString += "<nRec>" + oRetEnviNFe.InfRec.NRec + CR + CR

//Criar o objeto de configuração mínimo para consumir o serviço de consulta recibo
oConfigRec is Unimake.Business.DFe.Servicos.Configuracao
oConfigRec.TipoDFe = TipoDFe.NFe
oConfigRec.CertificadoSenha = "12345678"
oConfigRec.CertificadoArquivo = "C:\Projetos\certificados\UnimakePV.pfx"

//Fazer a consulta do recibo do lote enviado para ver se a(s) nota(s) foi(ram) autorizada(s)
oConsReciNFe is Unimake.Business.DFe.XML.NFe.ConsReciNFe
oConsReciNFe.Versao = "4.00"
oConsReciNFe.TpAmb = TipoAmbiente.Homologacao
oConsReciNFe.NRec = oRetEnviNFe.InfRec.NRec

oRetAutorizacao is Unimake.Business.DFe.Servicos.NFe.RetAutorizacao
oRetAutorizacao.Executar(oConsReciNFe, oConfigRec)

//Vamos ver a string do XML retornado pela SEFAZ
EDT_RetornoWSString += "String do XML Retornado da consulta Recibo: " + CR + CR + oRetAutorizacao.RetornoWSString + CR + CR

//Vamos ver o cStat e xMotivo retornado na consulta do recibo
EDT_RetornoWSString += "Status retornado da consulta do recibo:" + CR
EDT_RetornoWSString += "<cStat>" + oRetAutorizacao.Result.CStat + CR
EDT_RetornoWSString += "<xMotivo>" + oRetAutorizacao.Result.XMotivo + CR + CR + CR

//Vamos verificar se o status do lote enviado está como processado com sucesso
IF oRetAutorizacao.Result.CStat = 104 //Lote processado com sucesso
//Informar para o objeto de autorização qual é o resultado das notas. É necessário esta informação para que
//a classe tenha informações para juntar NFe com seu protocolo na hora de gerar o XML de distribuição.
oAutorizacao.RetConsReciNFe = oRetAutorizacao.Result

FOR I = 1 TO oRetAutorizacao.Result.GetProtNFeCount
oProtNFe is Unimake.Business.DFe.XML.NFe.ProtNFe() <- oRetAutorizacao.Result.GetProtNfe(I-1)

IF oProtNFe <> Null //Tivemos retornos de notas
//Vamos ver o cStat e xMotivo retornado na consulta do recibo
EDT_RetornoWSString += "Status de autorização das notas:" + CR
EDT_RetornoWSString += "<cStat>" + oProtNFe.InfProt.CStat + CR
EDT_RetornoWSString += "<xMotivo>" + oProtNFe.InfProt.XMotivo + CR

IF oProtNFe.InfProt.CStat = 100 //Nota autorizada THEN
//Gravar o XML de distribuição da NFe no HD/SSD
oAutorizacao.GravarXmlDistribuicao("d:\testenfe")

//Pegar a string do XML de distribuição para gravar no banco de dados
docProcNFe is string = oAutorizacao.GetNFeProcResults(oProtNFe.InfProt.chNFe);
ELSE
Info("Nota rejeitada: " + NumToString(oProtNFe.InfProt.CStat) + " " + oProtNFe.InfProt.XMotivo)
END
END
END
ELSE
Info("Nota rejeitada: " + NumToString(oRetAutorizacao.Result.CStat) + " " + oRetAutorizacao.Result.XMotivo)
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=YPgsmMWg28w
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 !