ConsultarDistribuicaoDFe(aCodigoUF, aCNPJCPF : string; aNSU: string; aTipoNSU: TNSUKind)
Este método permite aos atores da NF-e (envolvidos na operação) o acesso aos documentos fiscais eletrônicos (DF-e) e informações resumidas que não tenham sido gerados por ele e que sejam de seu interesse.
O método ConsultarDistribuicaoDFe
Private Sub btnConsultarDfe_Click()
If ((cbbUFConfig.Text <> "") And (txtCNPJConfig <> "") And (cbbCertificado.Text <> "")) Then
strngrdNotas.Text = ""
txtQtdNotas.Text = ""
' Inicia consulta com NSU = "000000000000000"
consultaDFe ("000000000000000")
Else
MsgBox "Campos Certificado ou UF ou CNPJ vazios!"
End If
End Sub
Private Sub consultaDFe(ultNSU As String)
Dim RetDFe, auxXML As String
Dim j, jx, i As Integer
Dim listaXML, nsu As Collection
Set nsu = New Collection
Set listaXML = New Collection
Dim lNodeList, iRSEmitente, iVNF, iSitManifestacao As IXMLDOMNodeList ' tipagem da MSXML6_TLB
' Função de "Consulta da Relação de Documentos a serem distribuidos para determinado CPF ou CNPJ de acordo com a NF-e"
' ConsultarDistribuicaoDFe(aCodigoUF, aCNPJCPF : string; aNSU: string; aTipoNSU: TNSUKind): string;
RetDFe = spdNFe.ConsultarDistribuicaoDFe(trataUF(cbbUFConfig.Text), spdNFe.CNPJ, ultNSU, nkUltimo)
' A função abaixo recebe o XML da consulta
configObjeto (RetDFe)
' Obtém o NSU mais alto e salva em maxNSU
Set lNodeList = lXMLDoc.selectNodes("//retDistDFeInt/maxNSU")
maxNSU = lNodeList.Item(0).Text
' Percorre os NSU e salva os números em nsu
Set lNodeList = lXMLDoc.selectNodes("//retDistDFeInt/loteDistDFeInt/docZip/@NSU")
If lNodeList.length > 0 Then
For i = 0 To lNodeList.length - 1
nsu.Add lNodeList.Item(i).Text
' Atualiza variável nroUltiNSU (último NSU) e nroIniNSU (valor inicial do NSU)
' Cada consulta retorna 50 ítens; ao final da Sub, esses valores serão usados para
' repetir a consulta até atingir o maxNSU
nroUltiNSU = lNodeList.Item(i).Text
nroIniNSU = lNodeList.Item(0).Text
If ultNSU = "000000000000000" Then
txtNSUInicial.Text = nroIniNSU
End If
Next i
End If
' Criando NodeList com XML compactado
Set lNodeList = lXMLDoc.selectNodes("//retDistDFeInt/loteDistDFeInt/docZip")
' Verifica se NodeList não está vazia
If (lNodeList.length > 0) Then
For i = 0 To lNodeList.length - 1
auxXML = lNodeList.Item(i).Text
' Descompacta cada XML e salva o na collection listaXML
listaXML.Add spdNFe.DescompactarXMLZip(auxXML)
Next
End If
' Conta o número de chaves para atualizar o contador
For jx = 1 To listaXML.Count
configObjeto (listaXML(jx))
' Criando NodeList com as chaves
Set lNodeList = lXMLDoc.selectNodes("//resNFe/chNFe")
' Verifica se NodeList está vazia
If lNodeList.length > 0 Then
For i = 0 To lNodeList.length - 1
QtdChaves = QtdChaves + 1
Next
End If
Next
' Atualiza o contador
txtQtdNotas.Text = Str(QtdChaves)
' Loop para carregar as chaves
For j = 1 To listaXML.Count
configObjeto (listaXML.Item(j))
' Selecionando chNFe, xNome e vNF para gerar o relatório
Set lNodeList = lXMLDoc.selectNodes("//resNFe/chNFe")
Set iRSEmitente = lXMLDoc.selectNodes("//resNFe/xNome")
Set iVNF = lXMLDoc.selectNodes("//resNFe/vNF")
' Verifica se NodeList não está vazia
If (lNodeList.length > 0) Then
' Preenche a lista com os resultados da pesquisa
For i = 0 To lNodeList.length - 1
strngrdNotas.Text = strngrdNotas.Text & vbCrLf _
& " * chNota = " & lNodeList.Item(i).Text & vbCrLf _
& " * NSU = " & nsu.Item(j) & vbCrLf _
& " * xNome = " & iRSEmitente.Item(i).Text & vbCrLf _
& " * vNF = " & iVNF.Item(i).Text & vbCrLf _
& "========================================================"
Next
End If
Next
' Implementação recursiva para fazer as consultas
' Caso esteja marcada a checkbox chkConsultaTotal
' Atualiza os campos de texto com valores inicial e final de NSU
txtNroUltiNSU.Text = nroUltiNSU
' Chama a Sub consultaDFe informando o último NSU consultado
' Na próxima consulta, SEFAZ envia a partir do próximo NSU
If (Val(nroUltiNSU) < Val(maxNSU)) And (chkConsultaTotal.Value) Then
consultaDFe nroUltiNSU
End If
End Sub
Sub configObjeto(XML As String)
' Configura objeto lXMLDoc
Set lXMLDoc = New DOMDocument60
lXMLDoc.setProperty "SelectionLanguage", "XPath"
lXMLDoc.async = False
If XML <> "" Then
lXMLDoc.loadXML RemoverNameSpaces(XML)
Else
MsgBox "XML não informado!"
End If
End Sub
Private Function RemoverNameSpaces(XMLString As String) As String
cRemoveNSTransform = "<xsl:stylesheet version=""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"">" _
& "<xsl:output method=""xml"" indent=""no""/>" _
& "<xsl:template match=""/|comment()|processing-instruction()"">" _
& " <xsl:copy>" & " <xsl:apply-templates/>" & " </xsl:copy>" _
& "</xsl:template>" _
& "<xsl:template match=""*"">" & " <xsl:element name=""{local-name()}"">" _
& " <xsl:apply-templates select=""@*|node()""/>" & " </xsl:element>" _
& "</xsl:template>" _
& "<xsl:template match=""@*"">" & " <xsl:attribute name=""{local-name()}"">" _
& " <xsl:value-of select="".""/>" & " </xsl:attribute>" _
& "</xsl:template>" _
& "</xsl:stylesheet>"
Dim Doc, XSL As IXMLDOMDocument2
Dim Res As String
Dim p As Long
Set Doc = New DOMDocument60
Doc.async = False
Set XSL = New DOMDocument60
XSL.async = False
If Doc.loadXML(XMLString) Then
XSL.loadXML (cRemoveNSTransform)
Res = Doc.transformNode(XSL)
p = InStrRev(Res, "?>")
If p <> 0 Then
result = Mid(Res, p + 2)
End If
RemoverNameSpaces = result
Else
MsgBox "Ocorreu um erro!"
End If
End Function
Private Sub btnDescompactar_Click()
If strngrdNotas.Text <> "" Then
strngrdNotas.Text = spdNFe.DescompactarXMLZip(strngrdNotas.Text)
Else
MsgBox "Campo está vazio!"
End If
End Sub
Private Sub btnDownloadNFe_Click()
Dim chaveDownload, xmlCompactado As String
Dim localiza As Integer
chaveDownload = InputBox("Entre o valor da chave")
If chaveDownload <> "" Then
strngrdNotas.Text = ""
strngrdNotas.Text = spdNFe.ConsultarDistribuicaoDFeChave(trataUF(spdNFe.UF), spdNFe.CNPJ, chaveDownload)
' Limpa o retorno, deixando apenas o valor dentro da tag <docZip></docZip>
localiza = InStrRev(strngrdNotas.Text, ".xsd")
If localiza <> 0 Then
xmlCompactado = Mid(strngrdNotas.Text, localiza + 6)
strngrdNotas.Text = Replace(xmlCompactado, "</docZip></loteDistDFeInt></retDistDFeInt>", "")
' Aplica o método DescompactarXMLZip
strngrdNotas.Text = spdNFe.DescompactarXMLZip(strngrdNotas.Text)
End If
Else
MsgBox "Chave da nota não preenchida!"
End If
End Sub
Private Sub btnLoadConfig_Click()
'Arquivo INI a ser manipulado com parametrizações
spdNFe.ConfigINI = App.Path + "\nfeConfig.ini"
ArqIni = App.Path + "\nfeConfig.ini"
spdNFe.LoadConfig (ArqIni) 'Esse metodo faz com que o Componente carregue as configuracoes do INI para as devidas propriedades
' carregar valores
txtCNPJConfig.Text = spdNFe.CNPJ
cbbUFConfig.Text = spdNFe.UF
If spdNFe.NomeCertificado <> "" Then
cbbCertificado.List(0) = spdNFe.NomeCertificado
cbbCertificado.ListIndex = 0
End If
If rbHomologacao.Value = True Then
spdNFe.Ambiente = akHomologacao
Else
spdNFe.Ambiente = akProducao
End If
End Sub
Private Sub Command1_Click()
Form2.Show
End Sub
Private Sub Form_Initialize()
'Instancia o Objeto responsável pela interação com servidores da Receita'
Set spdNFe = New NFeX.spdNFeX
Set spdNFeDataSet = New NFeDataSetX.spdNFeDataSetX
' Método ConfigurarSoftwareHouse
' Método responsável por realizar a validação de licença com a Tecnospeed.
' Chamada : _spdNFeX.ConfigurarSoftwareHouse(aCnpjSH : String, aTokenSH : String)
' aCnpjSH : deve ser informado o CNPJ da Software House cliente Tecnospeed
' aTokenSH : deve ser informado o Token que a Software House possui junto a Tecnospeed
spdNFe.ConfigurarSoftwareHouse "08187168000160", "000000000000000000000"
'Utiliza Método do Componente para Listar Certificados instalado no SO
Dim i As Integer
Dim vetor As Variant
vetor = Split(spdNFe.ListarCertificados("|"), "|")
cbbCertificado.Clear
For i = LBound(vetor) To UBound(vetor)
cbbCertificado.AddItem vetor(i)
Next
End Sub
Comentários
0 comentário
Por favor, entre para comentar.