Ola!
ALLguém já tem essa rotina pronta ?
E pode compartilhar ?
Saudações,
Itamar M. Lins Jr.

Moderador: Moderadores
Function Main()
Local x, xy, cPath, cFile, aFilesXml:={}
Local cTagItem, cBloco, nValorNota, nValorTotal:=0, nValorProds:=0
//cPath := "C:\MyFolder\" // com pasta definida
cPath := "" // na pasta local
aFilesXml := Directory( cPath + "*.Xml" )
For x = 1 To Len( aFilesXml )
cFile := cPath + aFilesXml[ x, 1 ]
cFile := Hb_MemoRead( cFile )
cBloco := XmlNode( cFile , "total" ) //Para somar os valores totais da notas
nValorTag := XmlNode( cBloco , "vNF" )
nValorTotal += Val( nValorTag )
For xy = 1 To 999 //Para somar os valores totais dos produtos
cTagItem := [det nItem="] + Hb_NToS( xy ) + ["]
cBloco := XmlNode( cFile , cTagItem )
If !Empty( cBloco )
nValorTag := XmlNode( cBloco , "vProd" )
nValorProds += Val( nValorTag )
Else
Exit // sem mais produtos, sai do loop
EndIf
Next
Next
Clear Screen
?
? "Valor total das notas :", nValorTotal
? "Valor total dos Produtos:", nValorProds
?
Return Nil
FUNCTION XmlNode( cXml, cNode, lComTag )
LOCAL nInicio, nFim, cResultado := ""
hb_Default( @lComTag, .F. )
nInicio := At( "<" + cNode + ">", cXml )
IF " " $ cNode
cNode := Substr( cNode, 1, At( " ", cNode ) - 1 )
ENDIF
IF nInicio != 0
IF ! lComTag
nInicio := nInicio + Len( cNode ) + 2
IF nInicio != 1 .AND. Substr( cXml, nInicio - 1, 1 ) != ">" // Quando tem elementos no bloco
nInicio := hb_At( ">", cXml, nInicio ) + 1
ENDIF
ENDIF
nFim := hb_At( "</" + cNode + ">", cXml, nInicio )
IF nFim != 0
nFim -=1
IF lComTag
nFim := nFim + Len( cNode ) + 3
ENDIF
cResultado := Substr( cXml, nInicio, nFim - nInicio + 1 )
ENDIF
ENDIF
RETURN cResultado
****************************************
STATIC FUNCTION IMPORTAXML( )
****************************************
#include "common.ch"
#include "fileio.ch"
#include "hwgui.ch"
LOCAL oProduto, aXmlEnt, oXmlEnt
cDirBase:=HB_Dirbase()
cArquivoXML :=hwg_SelectFile( {"XML Files( *.XML)"," ALL Files( *.*)"},{ "*.xml","*.*"},curdrive()+":\"+CURDIR()+"\DATABASE","Selecione Arquivo XML a Importar para Nota de Entrada")
IF DirChange( @cDirBase ) # 0
hwg_MsgInfo(DiskName() + ':\' + CurDir())
ENDIF
IF EMPTY(cArquivoXML)
RETURN NIL
ENDIF
DBSELECTAREA("DADOS")
Ordsetfocus(1)
DBGOTOP( )
aXmlEnt := memoread(cArquivoXML)
oXmlEnt := XmlToDoc(aXmlEnt) && carrega as variaveis da SEFAZCLASS
IF RTRIM(FLAG->n_CNPJ) # TIRAPONTO(oXmlEnt:Destinatario:Cnpj)
IF ! hwg_MsgNoYes("NF-E TEM CNPJ DIFERENTE "+ HB_EOL()+"DO CNPJ USUARIO DO SISTEMA"+ HB_EOL()+ HB_EOL() + "SISTEMA: "+RTRIM(FLAG->n_CNPJ)+ HB_EOL() + "NF-E......: "+TIRAPONTO(oXmlEnt:Destinatario:Cnpj)+ HB_EOL() + HB_EOL() + "deseja continuar ?","Importar XML")
RETURN NIL
ENDIF
ENDIF
DBSELECTAREA("DADOS")
Ordsetfocus(4)
DBGOTOP( )
IF DBSEEK(SUBSTR(oXmlEnt:cChave,7,28))
hwg_MsgInfo("NF-E DE ENTRADA JA FOI IMPORTADA"+ HB_EOL() + "NÃO É PERMITIDO IMPORTAR NOVAMENTE")
RETURN NIL
ELSE
Ordsetfocus(1)
cPrefixo := hb_Dirbase()+"XMLENTRADA\" + oXmlEnt:cChave
hb_MemoWrit( cPrefixo + ".xml", aXmlEnt )
oSpedDa := hbNfeDaGeral():New()
oSpedDa:ToPdf( aXmlEnt, cPrefixo + ".PDF" )
DBAPPEND()
DADOS->a_Id := oXmlEnt:cChave
DADOS->b_nNF := oXmlEnt:cNumDoc
DADOS->b_serie := oXmlEnt:cSerie
DADOS->b_mod := oXmlEnt:cModFis
DADOS->b_dhEmi := oXmlEnt:DataEmissao
DADOS->b_natOp := oXmlEnt:NaturezaOperacao
DADOS->c_CNPJ := TIRAPONTO(oXmlEnt:Emitente:Cnpj)
DADOS->c_xNome := oXmlEnt:Emitente:Nome
DADOS->b_natOp := oXmlEnt:NaturezaOperacao
DADOS->c_xLgr := oXmlEnt:Emitente:Endereco
DADOS->c_nro := oXmlEnt:Emitente:Numero
DADOS->c_xCpl := oXmlEnt:Emitente:Complemento
DADOS->c_xBairro := oXmlEnt:Emitente:Bairro
DADOS->c_cMun := oXmlEnt:Emitente:CidadeIbge
DADOS->c_xMun := oXmlEnt:Emitente:Cidade
DADOS->c_UF := oXmlEnt:Emitente:Uf
DADOS->c_CEP := oXmlEnt:Emitente:Cep
DADOS->c_fone := oXmlEnt:Emitente:Telefone
DADOS->c_IE := oXmlEnt:Emitente:InscricaoEstadual
DADOS->DATMOV := M->DAT_HOJE
DADOS->ST := .F.
DADOS->e_CNPJ := oXmlEnt:Destinatario:Cnpj
DADOS->e_xNome := oXmlEnt:Destinatario:Nome
DADOS->e_IE := oXmlEnt:Destinatario:InscricaoEstadual
DADOS->e_xLgr := oXmlEnt:Destinatario:Endereco
DADOS->e_nro := oXmlEnt:Destinatario:Numero
DADOS->e_xCpl := oXmlEnt:Destinatario:Complemento
DADOS->e_xBairr := oXmlEnt:Destinatario:Bairro
DADOS->e_cMun := oXmlEnt:Destinatario:CidadeIbge
DADOS->e_xMun := oXmlEnt:Destinatario:Cidade
DADOS->e_UF := oXmlEnt:Destinatario:Uf
DADOS->e_CEP := oXmlEnt:Destinatario:Cep
DADOS->e_fone := oXmlEnt:Destinatario:Telefone
DADOS->w_vIPI := oXmlEnt:Totais:IpiVal
DADOS->w_vII := oXmlEnt:Totais:IIVal
DADOS->w_vBC := oXmlEnt:Totais:IcmBas
DADOS->w_vICMS := oXmlEnt:Totais:IcmVal
DADOS->w_vBCST := oXmlEnt:Totais:SubBas
DADOS->w_vST := oXmlEnt:Totais:SubVal
DADOS->w_vPIS := oXmlEnt:Totais:PisVal
DADOS->w_vCOFINS := oXmlEnt:Totais:CofVal
DADOS->w_vPROD := oXmlEnt:Totais:ValPro
DADOS->w_vSeg := oXmlEnt:Totais:ValSeg
DADOS->w_vFrete := oXmlEnt:Totais:ValFre
DADOS->w_vDesc := oXmlEnt:Totais:ValDesc
DADOS->w_vOutro := oXmlEnt:Totais:ValOut
DADOS->w_vNF := oXmlEnt:Totais:ValNot
DADOS->w_vTotTrib := oXmlEnt:Totais:ValTrib
DBSELECTAREA("FORNECEDOR")
Ordsetfocus(4)
IF DBSEEK(DADOS->c_CNPJ+" ")
DADOS->UFORNEC := FORNECEDOR->UFORNEC
ELSE
// hwg_MsgInfo("NAO ACHEI")
Ordsetfocus(1)
DBGOBOTTOM()
mUFORNEC := VAL(FORNECEDOR->UFORNEC) + 1
DBAPPEND()
FORNECEDOR->UFORNEC :=STRZERO(mUFORNEC,4,0)
DADOS->UFORNEC := STRZERO(mUFORNEC,4,0)
FORNECEDOR->TIPCLI :="1"
FORNECEDOR->NRAZ_SOC :=TIRACENTO(upper(DADOS->c_xNome))
FORNECEDOR->CPFCGC :=DADOS->c_CNPJ
FORNECEDOR->INSEST :=DADOS->c_IE
// FORNECEDOR->EMAIL :=LOWER(mEMAIL)
FORNECEDOR->CEP :=DADOS->c_CEP
FORNECEDOR->ENDER :=TIRACENTO(UPPER(DADOS->c_xLgr))
FORNECEDOR->NUMERO :=VAL(DADOS->c_nro)
FORNECEDOR->COMPLEM :=TIRACENTO(UPPER(DADOS->c_xCpl))
FORNECEDOR->BAIR :=TIRACENTO(UPPER(DADOS->c_xBairro))
FORNECEDOR->CODPAIS :="1058"
FORNECEDOR->PAIS :="BRASIL"
FORNECEDOR->UF :=DADOS->c_UF
FORNECEDOR->CODMUN :=DADOS->c_cMun
FORNECEDOR->CIDADE :=DADOS->c_xMun
FORNECEDOR->FONE :=DADOS->c_fone
FORNECEDOR->DATCAD :=M->DAT_HOJE
FORNECEDOR->DATALT :=M->DAT_HOJE
FORNECEDOR->USUALT :=M->OPERADOR
ENDIF
DBSELECTAREA("ITENS")
MEMVAR->i_nItem := 1
nCont := 1
FOR EACH oProduto IN oXmlEnt:Produto
DBAPPEND()
ITENS->i_Id := oXmlEnt:cChave
ITENS->i_nItem := STRZERO(MEMVAR->i_nItem++,3,0)
ITENS->i_cProd := oProduto:Codigo
ITENS->i_cEAN := oProduto:GTIN
ITENS->i_cEANTrib := oProduto:GTINTrib
ITENS->i_xProd := oProduto:Nome
ITENS->i_NCM := oProduto:NCM
ITENS->i_CFOP := TIRAPONTO(oProduto:CFOP)
ITENS->i_CEST := oProduto:CEST
ITENS->i_uCom := oProduto:Unidade
ITENS->i_uTrib := oProduto:UnidTrib
ITENS->i_qCom := oProduto:Qtde
ITENS->i_qTrib := oProduto:QtdeTrib
ITENS->i_vUnCom := oProduto:ValorUnitario
ITENS->i_vUnTrib := STR(oProduto:ValUnitTrib)
ITENS->i_vProd := oProduto:ValorTotal
ITENS->i_vDesc := oProduto:Desconto
// ITENS->i_xPed := oProduto:Pedido
// ITENS-> := oProduto:InfAdicional
ITENS->o_vBC := oProduto:Ipi:Base
ITENS->o_pIPI := oProduto:Ipi:Aliquota
ITENS->o_vIPI := oProduto:Ipi:Valor
ITENS->n_CST := oProduto:Icms:Cst
ITENS->n_orig := oProduto:Icms:Cst
ITENS->n_vBC := oProduto:Icms:Base
// IF oProduto:Icms:Aliquota # 0.0000
// ITENS->n_pICMS := oProduto:Icms:Aliquota
// ELSE
// ITENS->n_pICMS := oProduto:IcmsSt:Aliquota
// ENDIF
ITENS->n_vICMS := oProduto:Icms:Valor
ITENS->n_vICMSST := oProduto:IcmsSt:Valor
ITENS->q_CST := oProduto:Pis:Cst
ITENS->q_vBC := oProduto:Pis:Base
ITENS->q_pPIS := oProduto:Pis:Aliquota
ITENS->q_vPIS := oProduto:Pis:Valor
ITENS->s_CST := oProduto:Cofins:Cst
ITENS->s_vBC := oProduto:Cofins:Base
ITENS->s_pCOFINS := oProduto:Cofins:Aliquota
ITENS->s_vCOFINS := oProduto:Cofins:Valor
DBCOMMIT()
DBSELECTAREA("PRODUTOS")
Ordsetfocus(7)
IF LEN(oProduto:GTIN) > 0
IF .NOT. DBSEEK(VAL(ITENS->i_cEANTrib))
IF .NOT. DBSEEK(VAL(ITENS->i_cEAN))
ITENS->CODPRO := ""
ENDIF
ELSE
ITENS->CODPRO := PRODUTOS->CODPRO
ITENS->DESPRO := PRODUTOS->DESPRO
ENDIF
ELSE
DBSELECTAREA("FORPRO")
Ordsetfocus(4)
// hwg_MsgInfo("["+DADOS->UFORNEC +"-"+ oProduto:Codigo+"]")
IF .NOT. DBSEEK(DADOS->UFORNEC + oProduto:Codigo)
// hwg_MsgInfo("NAO ENCONTREI")
ITENS->CODPRO := ""
ELSE
// hwg_MsgInfo("ENCONTREI ["+FORPRO->UFORNEC+"-"+RTRIM(FORPRO->i_cProd)+"-"+FORPRO->CODPRO+"]")
ITENS->CODPRO := FORPRO->CODPRO
ENDIF
ENDIF
DBSELECTAREA("ITENS")
nCont++
NEXT
DBCOMMITALL()
ENDIF
DBSELECTAREA("DADOS")
Ordsetfocus(3)
DBGOTOP( )
RETURN NIL
Retornar para Legislação Fiscal e Tributária
Usuários vendo este fórum: Nenhum usuário registrado online e 8 visitantes