Não é uma classe é uma PONTE q pega os arquivos texto q geramos e manda pra SEFAZ e devolve o REToRNO via arquivo Texto pra analisarmos... tipo o Observer da dAruma...
Segue um modelo pra vc ter uma ideia... tirei as partes de Banco de Dados e deixei mais a parte da montagem do arquivo (a grosso modo) e como enviar para o ACBr
procedure GeraNFe()
... 'Confirma a gera‡„o da NF-e', {'Sim','N„o'}
**********************************************************
// Gera Arquivo para NFe na pasta q esta sendo monitorada pelo ACBr
Mensag('Aguarde..., O t‚rmino da gera‡„o da NF-e!!')
ferase('ENTNFe'+cNrMaq+'.TXT')
cArq:='ENTNFe'+cNrMaq+'.CMD'
SET DEVICE TO PRINTER
SET PRINTER TO (cArq)
SET PRINTER ON
nLin :=0
//-- Identifica‡Æo
@ nLin,0 say 'NFE.CriarNFe([Identificacao]' ; nLin++
@ nLin,0 say 'Emissao=' + Dtoc(dDtNFeEmis) ; nLin++
@ nLin,0 say 'Codigo='+cNota ; nLin++
@ nLin,0 say 'Codigo='+cCodigoRandom ; nLin++
@ nLin,0 say 'Numero='+cNota ; nLin++
@ nLin,0 say 'NaturezaOperacao='+ cNaturezaOp ; nLin++
@ nLin,0 say 'Finalidade=' + if( lComplementar, '2', '1')
nLin++
@ nLin,0 say 'Modelo=55' ; nLin++
@ nLin,0 say 'Serie=' + cSerie ; nLin++
@ nLin,0 say 'Saida=' + Dtoc(dDtNFeSaid) ; nLin++
@ nLin,0 say 'hSaiEnt=' + Time()
@ nLin,0 say 'Tipo=' + if( cTipoOPF_ES=='E', '0', '1') ; nLin++
@ nLin,0 say 'idDest=' + if( lInterEstadual, '2', '1') ; nLin++
@ nLin,0 say 'indFinal=' + if( lConsumidorFinal, '1', '0') // 0 Normal e 1 Consumidor Final
@ nLin,0 say '[infNFe]' ; nLin++
@ nLin,0 say 'versao=3.10' ; nLin++
//-- Emitente
@ nLin,0 say '[Emitente]' ; nLin++
@ nLin,0 say 'CNPJ=' + TiraVirgNFe(DadosEmp->CGC ) ; nLin++
@ nLin,0 say 'IE=' + TiraVirgNFe(DadosEmp->InscrEst ) ; nLin++
@ nLin,0 say 'Razao=' + TiraVirgNFe(DadosEmp->Nome ) ; nLin++
@ nLin,0 say 'Fantasia=' + TiraVirgNFe(DadosEmp->Nome ) ; nLin++
@ nLin,0 say 'Fone=' + TiraVirgNFe(DadosEmp->Telefone ) ; nLin++
@ nLin,0 say 'CEP=' + TiraVirgNFe(StrTran(DadosEmp->Cep,'-','')) ; nLin++
@ nLin,0 say 'Logradouro=' + TiraVirgNFe(DadosEmp->Endereco ) ; nLin++
@ nLin,0 say 'Numero=' + TiraVirgNFe(DadosEmp->Numero ) ; nLin++
@ nLin,0 say 'Complemento=' ; nLin++
@ nLin,0 say 'Bairro=' + TiraVirgNFe(DadosEmp->Bairro ) ; nLin++
@ nLin,0 say 'CidadeCod=' + allstr(cCodCidE) ; nLin++
@ nLin,0 say 'Cidade=' + TiraVirgNFe(DadosEmp->Cidade) ; nLin++
@ nLin,0 say 'UF=' + cUFTabE ; nLin++
@ nLin,0 say 'PaisCod=1058' ; nLin++
@ nLin,0 say 'Pais=BRASIL' ; nLin++
@ nLin,0 say 'CRT='+ cTipoCRT ; nLin++
//-- Destinatario
@ nLin,0 say '[Destinatario]' ; nLin++
if 'ISENTO' $ Upper(cDoc2)
@ nLin,0 say 'indIEDest=2' // 2=Contribuinte ISENTO de IE no cadastro do ICMS
else
@ nLin,0 say 'indIEDest=1' // 1=Contribuinte ICMS (informar a IE do destinatario)
// 9=Nao Contribuinte, que pode ou nao possuir IE no Cadastro do ICMS;
// Nota 1: No caso de NFC-e informar indIEDest=9 e nao informar a tag IE do destinatario;
endif
nLin++
if !empty(cDoc1)
@ nLin,0 say 'CNPJ='+if(Sistema->NFeHomolog='S','99999999000191',Alltrim(cDoc1)) ; nLin++
endif
@ nLin,0 say 'IE='+ if(Sistema->NFeHomolog='S',Alltrim(cDoc2),Alltrim(cDoc2)) ; nLin++
@ nLin,0 say 'NomeRazao='+ cNomeCli ; nLin++
@ nLin,0 say 'Fone='+ TiraVirgNFe(Strtran(cFone,'-','')) ; nLin++
@ nLin,0 say 'CEP=' + TiraVirgNFe(StrTran(cCEP,'-','')) ; nLin++
@ nLin,0 say 'Logradouro='+ TiraVirgNFe(cEnd) ; nLin++
@ nLin,0 say 'Numero=' + alltrim(cNumeroDest) ; nLin++
@ nLin,0 say 'Complemento=' ; nLin++
@ nLin,0 say 'Bairro=' + cBairro ; nLin++
@ nLin,0 say 'CidadeCod='+allstr(cCodCid) ; nLin++
@ nLin,0 say 'Cidade=' + cCidade ; nLin++
@ nLin,0 say 'UF=' + cUFTab ; nLin++
@ nLin,0 say 'Email=' + TiraVirgNFe(alltrim(Pessoas->Email)) ; nLin++
@ nLin,0 say 'PaisCod=1058' ; nLin++
@ nLin,0 say 'Pais=BRASIL' ; nLin++
//-- Produto XXXX
Pedido->(DBGOTOP())
while Pedido->(!EOF())
@ nLin,0 say '[Produto'+StrZero(nContador,3)+']' ; nLin++
@ nLin,0 say 'CFOP='+Pedido->CFOP ; nLin++
@ nLin,0 say 'Codigo=' + Pedido->Codigo ; nLin++
@ nLin,0 say 'Descricao='+TiraVirgNFe(Pedido->Descricao) ; nLin++
@ nLin,0 say 'vTotTrib=' + Alltrim( str( Pedido->vTotTrib, 12,2)) ; nLin++
@ nLin,0 say 'NCM=' + StrZero(Val(TiraPontVirg(Pedido->NCM)),8); nLin++
@ nLin,0 say 'CEST=' + alltrim(TiraPontVirg(Pedido->CEST)) ; nLin++
@ nLin,0 say 'EAN=' + if( Len(alltrim(pedido->ean))==13, TiraPontVirg(Pedido->EAN), '') ; nLin++
@ nLin,0 say 'Unidade=' + Pedido->Unidade ; nLin++
@ nLin,0 say 'Quantidade=' + allstr( Pedido->Qtde) ; nLin++
@ nLin,0 say 'ValorUnitario='+ Alltrim(Str( (Pedido->Preco),12,4)) ; nLin++
@ nLin,0 say 'ValorTotal=' + Alltrim(Str((Pedido->Total + Pedido->DescItem),12,3 )) ; nLin++
@ nLin,0 say 'ValorDesconto='+ Alltrim(Str( Pedido->DescItem, 12,3)) ; nLin++
@ nLin,0 say 'vFrete=' + Alltrim( Str( if( (nRestFrete- nVlrFreteRat)>=0, nVlrFreteRat, nRestFrete), 12, 2) ) ; nLin++
@ nLin,0 say 'qTrib=' + allstr( Pedido->Qtde) ; nLin++
@ nLin,0 say 'vUnTrib='+ Alltrim(Str( (Pedido->Preco),12,4)) ; nLin++
@ nLin,0 say 'vOutro=' + Alltrim( Str( (nVOutrasDesp),12,2 ) ) ; nLin++
@ nLin,0 say 'indTot=1' ; nLin++
@ nLin,0 say "[ICMS" + StrZero(nContador,3)+"]" ; nLin++
@ nLin,0 say 'Origem='+ Left(Pedido->CST,1) ; nLin++
@ nLin,0 say 'CSOSN=' + Alltrim(Pedido->CSOSN) ; nLin++
@ nLin,0 say 'ValorBase=0' ; nLin++ //- pMVAST
@ nLin,0 say 'PercentualReducao=0' ; nLin++ //- pRedBC
@ nLin,0 say 'Aliquota=' + Alltrim(str(Pedido->ALIQICMS,10,2)) ; nLin++ //- pICMSST
@ nLin,0 say 'Valor=0' ; nLin++ //- vICMSST
@ nLin,0 say 'ModalidadeST=4' ; nLin++
@ nLin,0 say 'PercentualMargemST=' + Alltrim( str(Pedido->MVA,10,2)) ; nLin++ //- pMVAST
@ nLin,0 say 'PercentualReducaoST=' + Alltrim( str(Pedido->RedBC,10,2)) ; nLin++ //+ allstr(Pedido->RedBC) //- pRedBC
@ nLin,0 say 'ValorBaseST=' + Alltrim( str(Pedido->BCICMST,12,2)) ; nLin++ //- vBCST
@ nLin,0 say 'AliquotaST=' + Alltrim( str(Pedido->ALIQICMSST,10,2)) ; nLin++ //- pICMSST
@ nLin,0 say 'ValorST=' + Pedido->VlrST
if Pedido->VlrIPI>0
@ nLin,0 say "[IPI"+StrZero(nContador,3)+"]" ; nLin++
@ nLin,0 say 'CST='+Right(Pedido->CST,2) ; nLin++
@ nLin,0 say 'ValorBase=' + Alltrim(Str(Pedido->BCICMS, 12,2)) ; nLin++
@ nLin,0 say 'Aliquota=' + Alltrim(Str(Pedido->AliqIPI,10,2)) ; nLin++
@ nLin,0 say 'Valor=' + Alltrim(Str(Pedido->VlrIPI, 12,2)) ; nLin++
endif
*-- Partilha ICMS
if lConsumidorFinal .and. lInterEstadual
@ nLin,0 say "[ICMSUFDest" + StrZero(nContador,3) + "]" ; nLin++
@ nLin,0 say 'vBCUFDest=' + NumACBr(vBCUFDest, 12,2) ; nLin++
@ nLin,0 say 'pFCPUFDest=' + NumACBr(pFCPUFDest, 12, 2) ; nLin++
@ nLin,0 say 'pICMSUFDest=' + NumACBr(pICMSUFDest, 12, 2) ; nLin++
@ nLin,0 say 'pICMSInter=' + NumACBr(pICMSInter, 12, 2) ; nLin++
@ nLin,0 say 'pICMSInterPart='+ NumACBr(pICMSInterPart, 12, 2) ; nLin++
@ nLin,0 say 'vFCPUFDest=' + NumACBr(vFCPUFDest, 12, 2) ; nLin++
@ nLin,0 say 'vICMSUFDest=' + NumACBr(vICMSUFDest, 12, 2) ; nLin++
@ nLin,0 say 'vICMSUFRemet=' + NumACBr(vICMSUFRemet, 12, 2) ; nLin++
endif
*------------------------------------------------------------------------------------------- // Final Grupo ICMS Destino
nContador++
Pedido->(DbSkip())
end
//-- Total da NF-e
@ nLin,0 say "[Total]" ; nLin++
*-- Partilha ICMS
if lConsumidorFinal .and. lInterEstadual
@ nLin,0 say 'vFCPUFDest=' + NumACBr(nTotFCP, 12, 2) ; nLin++
@ nLin,0 say 'vICMSUFDest=' + NumACBr(nTotICMSUFDest, 12, 2) ; nLin++
@ nLin,0 say 'vICMSUFRemet=' + NumACBr(nTotICMSUFRemet,12, 2) ; nLin++
endif
*-------------------------------------------------------------------
@ nLin,0 say 'BaseICMS=' + Alltrim( Str( n_BCICMS, 12, 2) ) ; nLin++
@ nLin,0 say 'ValorICMS=' + Alltrim( Str( n_VlrICMS, 12, 2) ) ; nLin++
@ nLin,0 say 'BaseICMSSubstituicao=' + Alltrim( Str( nT_BCICMSST,12,2) ) ; nLin++
@ nLin,0 say 'ValorICMSSubstituicao=' + Alltrim( Str( nT_VlrICMST, 12, 2) ) ; nLin++
@ nLin,0 say 'ValorIPI=' + Alltrim( Str( nTotIPI,10,2) ) ; nLin++
@ nLin,0 say 'ValorProduto=' + Alltrim( Str( (nTotProd + nDescTot),12,2) ) ; nLin++
@ nLin,0 say 'ValorFrete=' + Alltrim( Str( nVlrFrete,12,2) ) ; nLin++
@ nLin,0 say 'ValorDesconto=' + Alltrim( Str( nDescTot,12,2) ) ; nLin++
@ nLin,0 say 'ValorOutrasDespesas=' + Alltrim( Str( nVOutrasDesp,12,2) ) ; nLin++
@ nLin,0 say 'ValorNota=' + Alltrim( Str( (nVlrNota + nVlrFrete + nTotIPI + nVOutrasDesp),12,2 ) ) ; nLin++
@ nLin,0 say 'vTotTrib=' + Alltrim( str( nTot_vTotTrib, 12,2)) ; nLin++
//-- Transportador
@ nLin,0 say "[Transportador]" ; nLin++
@ nLin,0 say 'FretePorConta='+alltrim(cTipo) ; nLin++
@ nLin,0 say 'NomeRazao='+ alltrim(TiraVirgNFe(cNomeTransp)) ; nLin++
@ nLin,0 say 'CnpjCpf=' + alltrim(TiraVirgNFe(cDoc1Transp)) ; nLin++
@ nLin,0 say 'IE=' + alltrim(TiraVirgNFe(cDoc2Transp)) ; nLin++
@ nLin,0 say 'CFOP=' + alltrim(cCFOPTransp) ; nLin++
@ nLin,0 say 'Endereco=' + alltrim(TiraVirgNFe(cEndeTransp)) ; nLin++
@ nLin,0 say 'CidadeCod='+ allstr(cCodCidTransp) ; nLin++
@ nLin,0 say 'Cidade=' + alltrim(TiraVirgNFe(cCidTransp)) ; nLin++
@ nLin,0 say 'UF=' + alltrim(cEstTransp) ; nLin++
@ nLin,0 say 'UFPlaca=' + alltrim(cEstVeicul) ; nLin++
@ nLin,0 say 'Placa=' + STRTRAN( alltrim(cPlacTransp), '-','') ; nLin++
@ nLin,0 say 'RNTC=' + alltrim(cCodigoANTT) ; nLin++
//--/ Transportador
//--> Volumes
if nPesoVolume == 2
@ nLin,0 say "[Volume001]" ; nLin++
@ nLin,0 say 'Quantidade=' + allstr(nVol_Qtd) ; nLin++
@ nLin,0 say 'Especie=' + Alltrim(cVol_Espec) ; nLin++
@ nLin,0 say 'Marca=' + Alltrim(cVol_Marca) ; nLin++
@ nLin,0 say 'Numeracao=' + Alltrim(cVol_Numer) ; nLin++
@ nLin,0 say 'PesoLiquido=' + Alltrim(Str(nVol_PLiq, 12,3)) ; nLin++
@ nLin,0 say 'PesoBruto=' + Alltrim(Str(nVol_PBrut,12,3)) ; nLin++
endif
//--/ Volumes
//-- Fatura
if nTipoPagto == 2 // A Prazo
@ nLin,0 say '[Fatura]' ; nLin++
@ nLin,0 say 'Numero=' + STRZERO(Val(cNota),9) ; nLin++
@ nLin,0 say 'ValorOriginal=' + Alltrim(Str(nVlrNota, 10,3)) ; nLin++
@ nLin,0 say 'ValorDesconto=0' ; nLin++
@ nLin,0 say 'ValorLiquido=' + Alltrim(Str(nVlrNota, 10,3)) ; nLin++
endif
//-- Dados Adicionais
@ nLin,0 say "[DadosAdicionais]" ; nLin++
@ nLin,0 say 'Complemento='+ cMsgNFe + ')' ; nLin++
DBCLEARREL()
SET PRINTER TO
PRINTOFF()
Aux_NFe_ArqEntACBr( cArq, 'Gerando NF-e', 3) // MANDA ARQUIVO PARA ACBRNFEMONITOR
lSai :=.F.
while .T.
if file( Alltrim(Caminho->PathNFe) + 'SAINFE' + cNrMaq + '.TXT' )
cNFeLog := alltrim(upper(memoread( Alltrim(Caminho->PathNFe) + 'SAINFE' + cNrMaq + '.TXT' )))
if 'OK:' $ cNFeLog
nPosIni := Rat(Alltrim(Caminho->PathNFe) +'RESPOSTA\', cNFeLog)
nPosFim := RAt('XML', cNFeLog) + 3 // devido ter q considerar os caracteres 'XML' tb
nTotSubstr := nPosFim - nPosIni
cRetorno := Substr( cNFeLog, nPosIni, nTotSubstr )
cRetorno := StrTran( cRetorno, 'OK:','')
cRetorno := StrTran( cRetorno, ' ','')
cRetorno := StrTran( cRetorno, 'C:\ACBR\NFE\',Alltrim(Caminho->PathNFe))
cRetorno := StrTran( cRetorno, CHR(13),'')
cRetorno := Alltrim(StrTran( cRetorno, CHR(10),''))
Nota->XMLRETORNO := cRetorno // ,'C', 70, 0},;
Nota->STATUS := 'GERADA' // ,'C', 20, 0},;
cRetorno:= StrTran( cRetorno, Alltrim(Caminho->PathNFe) +'RESPOSTA\','')
lSai := .t.
*-- Valida, Envia e Consulta.
* Validar
lErro:=.F.
Mensag('*** VALIDANDO *** NF-e', 'W+/R')
NFe_ValidarNFe( cRetorno )
*-- Enviar
if lErro
Alert('Erro na Validacao, entre em contato com o Suporte')
else
Mensag('*** ENVIANDO *** NF-e', 'N/GR*')
NFe_EnviarNFe(cRetorno)
if 'NEGAD' $ UPPER(NOTA->STATUS) // NAO CONSULTA
elseif lErro
Alert('Erro no Envio, Inutilize a NF-e SE NAO ESTIVER DUPLICADA ou entre em contato com o Suporte')
else
* Consultar Situacao
Mensag('*** CONSULTANDO *** NF-e', 'W+/G')
Nfe_ConsultarNFe(cRetorno, cEmailx)
endif
endif
elseif 'ERRO' $ cNFeLog
Alert( 'AtencÆo!!! Por problemas de comunica‡„o; n„o foi possivel gerar a NFe;'+ ' Sera necessario refazer a NFe!;;' , {' Pressione Enter '},vcr)
Aux_NFe_MostraRetACBr()
lSai := .F.
endif
FERASE(Alltrim(Caminho->PathNFe) +'SAINFE'+cNrMaq+'.TXT')
EXIT
else
lErro:=.T.
EXIT
endif
end
return 1
********************************************************************************
procedure __________Etapas_NFe
********************************************************************************
return
*************************************************
procedure NFe_ValidarNFe( cXML )
************************************************
FERASE("ENTNFe"+cNrMaq+".TXT")
cArq:="ENTNFe"+cNrMaq+".CMD"
SET DEVICE TO PRINTER
SET PRINTER TO (cArq)
SET PRINTER ON
SETPRC(0,0)
nLin :=0
@ nLin,0 say "NFE.ValidarNFe("+chr(34)+alltrim(nota->xmlretorno)+chr(34)+")"
DBCLEARREL()
SET PRINTER TO ; PRINTOFF()
Aux_NFe_ArqEntACBr(cArq,,,3) // Envia Arq Gerado para ACBrNFeMonitor
While .T.
if file( Alltrim(Caminho->PathNFe) + "SAINFE"+cNrMaq+".TXT" )
cNFeLog := alltrim(upper(memoread( Alltrim(Caminho->PathNFe) + "SAINFE"+cNrMaq+".TXT" )))
if "OK:" $ cNFeLog
lErro:=.F.
NFe_Aux_StatusNota("VALIDADA")
return
elseif "ERRO" $ cNFeLog
lErro:=.T.
Alert( 'Aten‡„o!!! N„o foi poss¡vel Validar a NF-e;' +;
' Verifique o Retorno da SEFAZ;' + ' Sera necess rio validar a NF-e!;;' , {" Pressione Enter "},vcr)
Aux_NFe_MostraRetACBr()
endif
FERASE(Alltrim(Caminho->PathNFe) + "SAINFE"+cNrMaq+".TXT")
EXIT
else
lErro:=.T.
EXIT
endif
END
return
*************************************************************
function NFe_EnviarNFe( cArquivo, nLote, nAssina, nImprime )
local lJaConsultouNFe := .F.
local cEmail := SPACE(50)
FERASE("ENTNFe"+cNrMaq+".TXT")
cArq:="ENTNFe"+cNrMaq+".CMD"
SET DEVICE TO PRINTER
SET PRINTER TO (cArq)
SET PRINTER ON
SETPRC(0,0)
nLin :=0
@ nLin,0 say "NFE.EnviarNFe("+chr(34)+alltrim(nota->xmlretorno)+chr(34)+","+alltrim(nota->nFiscal)+",1,0)"
DBCLEARREL()
SET PRINTER TO
PRINTOFF()
Aux_NFe_ArqEntACBr(cArq, 'Aguarde..., Tentando Envio NF-e / Este processo pode demorar!', 30, , ,'CHAVE',,.T.)
while .T.
if FILE( Alltrim(Caminho->PathNFe) + "SAINFE"+cNrMaq+".TXT" )
cNFeLog := alltrim(upper(memoread( Alltrim(Caminho->PathNFe) + "SAINFE"+cNrMaq+".TXT" )))
cNFeLog := substr( cNFeLog, at("RETORNO",cNFeLog)+7) // Pega a segunda parte do xml Retorno da Sefaz
if ( ("ERRO" $ cNFeLog) .or. ("REJEICAO" $ cNFeLog) .or. ('DENEGADO' $ cNFeLog) .or. ('NEGADA' $ cNFeLog) .or. ('IRREGULARIDADE FISCAL DO DESTINATARIO' $ cNFeLog) )
lErro := .T.
if 'DUPLICIDADE' $ cNFeLog
NFe_Aux_StatusNota("DUPLICIDADE")
elseif ('DENEGADO' $ cNFeLog) .and. ('IRREGULARIDADE FISCAL DO DESTINATARIO' $ cNFeLog)
ALERT('Irregularidade Fiscal do Destinatario, NFe foi DENEGADA!')
NFe_Aux_StatusNota("DENEGADA")
lErro := .F.
NFe_Aux_MudaXMLIrregular() // Aki eu coloco no XML em Dados Adicionais a mensagem que a NFe esta denegada
endif
Alert( "Atencao!!! Nao foi possivel Enviar a NF-e;"+ " Verifique o Retorno da SEFAZ;"+ " Sera necess rio Enviar novamente a NF-e!;;" , {" Pressione Enter "}, vcr)
Aux_NFe_MostraRetACBr()
elseif ("AUTORIZADO" $ cNFeLog)
lErro:=.F.
NFe_Aux_StatusNota("ENVIADA")
endif
FERASE(Alltrim(Caminho->PathNFe) + "SAINFE"+cNrMaq+".TXT")
EXIT
else
lErro:=.T.
Alert( "Atencao!!!;; Ser necess rio refazer o procedimento!;;" , {" Pressione Enter "},vcr)
EXIT
endif
END
return Nil
********************************************************************************
function NFe_ConsultarNFe( cNFe, cEmail, lDuplicada )
********************************************************************************
local i, nRecPosNota, cDefaultPrinter
local cChaveNFe := ''
FERASE("ENTNFe"+cNrMaq+".TXT")
cArq:="ENTNFe"+cNrMaq+".CMD"
SET DEVICE TO PRINTER
SET PRINTER TO (cArq)
SET PRINTER ON
SETPRC(0,0)
nLin :=0
ChaveRetorno:= nota->xmlretorno
ChaveRetorno:= Alltrim(ChaveRetorno)
@ nLin,0 say "NFE.ConsultarNFe("+chr(34)+ChaveRetorno+chr(34)+")"
DBCLEARREL()
SET PRINTER TO ; PRINTOFF()
Aux_NFe_ArqEntACBr( cArq, , ,3)
while .T.
if file( Alltrim(Caminho->PathNFe) + "SAINFE"+cNrMaq+".TXT" )
cNFeLog := alltrim(upper(memoread( Alltrim(Caminho->PathNFe) + "SAINFE"+cNrMaq+".TXT" )))
if "OK:" $ cNFeLog
if ("REJEICAO" $ cNFeLog) .or. ('DENEGADO' $ cNFeLog) .or. ('IRREGULARIDADE FISCAL DO DESTINATARIO' $ cNFeLog)
lErro:=.T.
if 'DUPLICIDADE' $ cNFeLog
Alert( "AtencÆo!!! Foi verificado DUPLICIDADE no retorno do SEFAZ;"+ " Verifique o Retorno da SEFAZ;"+ " Sera necess rio Corrigir a NF-e ou Tentar novamente!;;" , {" Pressione Enter "},vcr)
NFe_Aux_StatusNota("DUPLICIDADE")
else
Alert( "Aten‡„o!!! N„o foi Autorizada a NF-e;"+ " Verifique o Retorno da SEFAZ;"+ " Sera necess rio Corrigir a NF-e ou Tentar novamente!;;" , {" Pressione Enter "},vcr)
Aux_NFe_MostraRetACBr()
endif
else
lErro:=.F.
Nota->STATUS := "AUTORIZADA"
cChaveNFe := NFe_Aux_PegaChave()
NFe_ImprimirDANFE() // Imprimir DANFE
NFe_EnviarEmail( cChaveNFe+'-NFe.XML', cEmailx )
endif
else // cStat Error if ("ERRO" $ cNFeLog) .or. ("REJEICAO" $ cNFeLog) .or. ('DENEGADO' $ cNFeLog) .or. ('IRREGULARIDADE FISCAL DO DESTINATARIO' $ cNFeLog)
lErro:=.T.
if 'DUPLICIDADE' $ cNFeLog
Alert( "Atencäo!!! Foi verificado DUPLICIDADE no retorno do SEFAZ;"+ " Verifique o Retorno da SEFAZ;"+ " Sera necess rio Corrigir a NF-e ou Tentar novamente!;;" , {" Pressione Enter "},vcr)
NFe_Aux_StatusNota("DUPLICIDADE")
else
Alert( "Aten‡„o!!! N„o foi Autorizada a NF-e;"+ " Verifique o Retorno da SEFAZ;"+ " Sera necess rio Corrigir a NF-e ou Tentar novamente!;;" , {" Pressione Enter "},vcr)
Aux_NFe_MostraRetACBr()
endif
endif
FERASE(Alltrim(Caminho->PathNFe) + "SAINFE"+cNrMaq+".TXT")
EXIT
else // Sem retorno ACBr
lErro:=.T.
Alert( "Atencao!!!;; Ser necess rio refazer o procedimento!;;" , {" Pressione Enter "},vcr)
EXIT
endif
END
return NIL
********************************************************************************
function NFe_EnviarEmail( cXML, cEmail )
********************************************************************************
local cChaveNFe := '', cPara, cCopia, cTituloEmail, cTelaCancNfe, getlist:={}
local lSaiLacoEmail:=.F.
FERASE("ENTNFe"+cNrMaq+".TXT")
cArq:="ENTNFe"+cNrMaq+".CMD"
cPara := if( cEmail==NIL, space(40), cEmail)
cPara := PADR(cPara, 40, ' ')
/* if len(alltrim(cPara))<2
cPara:=space(40)
endif
*/
cCopia:=space(40)
cTituloEmail := if( !VarExist('cNomUser'), '', Alltrim(cNomUser)) + " - NF-e " + Nota->NFiscal + Space(11)
While .T.
cTelaCancNfe := SaveScreen(s_geral)
Caixa(07,15,11,69,frame2,vcn,"Enviar E-mail NF-e","W+/R")
@ 08,17 say "Para....: " Get cPara valid !empty(cPara)
@ 09,17 say "C¢pia...: " Get cCopia
read
if LastKey()=27
if Alert('Deseja Sair sem Enviar o Email', {'Nao','Sim'})=2
return NIL
else
Loop
endif
endif
cPara := Lower(Alltrim(cPara))
cCopia := Lower(Alltrim(cCopia))
if "@" $ cPara
lSaiLacoEmail := .T.
else
Alert( " necess rio informar uma e-mail v lido ; para que seja enviada a NF-e!;;" , {" Pressione Enter "},vcr)
RestScreen(s_geral, cTelaCancNfe)
Loop
endif
if "@" $ cCopia
//Blz
else
cCopia:=""
endif
@ 10,17 say "Assunto.: " Get cTituloEmail PICTURE "@K!S40"
read
RestScreen(s_geral, cTelaCancNfe)
if lSaiLacoEmail
Exit
endif
End
SET DEVICE TO PRINTER
SET PRINTER TO (cArq)
SET PRINTER ON
SETPRC(0,0)
nLin :=0
@ nLin,0 say "NFE.EnviarEmail("+cPara+ "," + Alltrim(nota->xmlretorno)+",1,"+ cTituloEmail+","+cCopia+")"
DBCLEARREL()
SET PRINTER TO ; PRINTOFF()
Aux_NFe_ArqEntACBr( cArq, , , 3)
While .T.
if file( Alltrim(Caminho->PathNFe) + "SAINFe"+cNrMaq+".TXT" )
cNFeLog := alltrim(upper(memoread( Alltrim(Caminho->PathNFe) + "SAINFe"+cNrMaq+".TXT" )))
if "OK:" $ cNFeLog
cChaveNFe := NFe_Aux_PegaChave()
Alert( "E-mail Enviado com Sucesso!;"+ "XML - Protocolo Gerado:;"+ cChaveNFe +";;" , + {" Pressione Enter "},vcr)
elseif "ERRO" $ cNFeLog
Alert( "Aten‡„o!!! N„o foi possivel Enviar o E-mail ;"+ " Verifique o Retorno da SEFAZ;"+ " Sera necess rio Tentar novamente!;;" , {" Pressione Enter "},vcr)
Aux_NFe_MostraRetACBr()
endif
FERASE(Alltrim(Caminho->PathNFe) + "SAINFe"+cNrMaq+".TXT")
Exit
endif
END
return NIL
********************************************************************************
function NFe_ImprimirDANFe( cXML )
********************************************************************************
local aTitles := {}
local nQtsCopias
//local cChave := ''
FERASE("ENTNFe"+cNrMaq+".TXT")
cArq:="ENTNFe"+cNrMaq+".CMD"
SET DEVICE TO PRINTER
SET PRINTER TO (cArq)
SET PRINTER ON
SETPRC(0,0)
nLin :=0
ChaveRetorno:= nota->xmlretorno
ChaveRetorno:= Alltrim(ChaveRetorno)
@ nLin,0 say "NFE.ImprimirDANFE("+chr(34)+ChaveRetorno+chr(34)+")"
DBCLEARREL()
SET PRINTER TO ; PRINTOFF()
// O Arquivo foi gerado
nQtsCopias := 1 //Alert('Quantas Copias?', {'1','2'}, vcr)
While nQtsCopias>0
Aux_NFe_ArqEntACBr( cArq,,,3)
While .T.
if file( Alltrim(Caminho->PathNFe) + "SAINFe"+cNrMaq+".TXT" )
cNFeLog := alltrim(upper(memoread( Alltrim(Caminho->PathNFe) + "SAINFe"+cNrMaq+".TXT" )))
if "OK:" $ cNFeLog
if nQtsCopias=2
Exit
endif
FileDelete( cArq )
elseif "ERRO" $ cNFeLog
Alert( "Aten‡„o!!! N„o foi poss¡vel Imprimir o DANFE;"+ " Verifique o Retorno da SEFAZ;"+ " Sera necess rio Tentar novamente!;;" , {" Pressione Enter "},vcr)
Aux_NFe_MostraRetACBr()
endif
FERASE(Alltrim(Caminho->PathNFe) + "SAINFe"+cNrMaq+".TXT")
Exit
endif
END
nQtsCopias--
End
FileDelete( cArq )
return NIL
********************************************************************************
function NFe_CancelarNFe( cNFe )
********************************************************************************
local cChaveNFe := ''
local cProtocolo := ''
local lExtemporaneo := .F.
local getlist:={}, cTelaCanc, nHoraFinal, nHora_1, nHora_2, cJustificativa, cTelaCancNfe
local lRefresh
* 24horas pra cancelar NF
cTelaCanc := SaveScreen(s_geral)
//-- Se data atual menos data emissao nota maior q 1 dia nÆo pode cancelar
if (date() - nota->datasai) > 1
if Alert('Ja faz mais de 24h que esta nota foi emitida; Nao ‚ permitido o cancelamento....;;'+;
'Deseja fazer o CANCELAMENTO EXTEMPORANEO?;; VERIFIQUE SE SUA SITUA€ŽO PERMITE!', {'1. SAIR', '2. CONTINUAR'}, vcw) < 2 // Alert('Ja faz mais de 24h que esta nota foi emitida; Nao ‚ permitido o cancelamento....')
Restscreen(s_geral, cTelaCanc)
return NIL
endif
if !SenhaGer('Cancelamento Extemporaneo',,,,'NF-e')
Restscreen(s_geral, cTelaCanc)
return NIL
endif
lExtemporaneo := .T.
elseif (date() - nota->datasai) == 1 .or. (date() - nota->dataemi) == 0 // Se ta no dia
nHora_1 := timetosec("23:59:59") - timetosec(nota->horaemi)
nHora_2 := timetosec(time()) - timetosec("00:00:01")
nHoraFinal := nHora_1 + nHora_2
if (date() - nota->datasai) == 0
nHoraFinal := timetosec(time()) - timetosec(nota->horaemi)
endif
if nHoraFinal > 86000
if Alert('Ja faz mais de 24h que esta nota foi emitida; Nao ‚ permitido o cancelamento....;;Deseja fazer o CANCELAMENTO EXTEMPORANEO?;; VERIFIQUE SE SUA SITUA€ŽO PERMITE!', {'1. SAIR', '2. CONTINUAR'}, vcw) < 2
Restscreen(s_geral, cTelaCanc)
return NIL
endif
if !SenhaGer('Cancelamento Extemporaneo',,,,'NF-e')
Restscreen(s_geral, cTelaCanc)
return NIL
endif
lExtemporaneo := .T.
endif
elseif (date()- nota->dataemi)<0
Alert('Periodo invalido entre em contato com o suporte!!!')
Restscreen(s_geral, cTelaCanc)
return NIL
endif
FERASE("ENTNFe"+cNrMaq+".TXT")
cArq:="ENTNFe"+cNrMaq+".CMD"
SET DEVICE TO PRINTER
SET PRINTER TO (cArq)
SET PRINTER ON
SETPRC(0,0)
nLin :=0
cChaveNFe := NFe_Aux_PegaChave()
@ nLin,0 say "NFE.CancelarNFE("+chr(34) + cChaveNFe +chr(34)+","+chr(34)+cJustificativa+chr(34)+")"
DBCLEARREL()
SET PRINTER TO ; PRINTOFF()
Aux_NFe_ArqEntACBr(cArq, 'Arguarde..., Tentando Cancelar NF-e / Este processo pode demorar!',30,,,'CHAVE',,.T.)
while .T.
if file( Alltrim(Caminho->PathNFe) + "SAINFE"+cNrMaq+".TXT" )
cNFeLog := alltrim(upper(memoread( Alltrim(Caminho->PathNFe) + "SAINFE"+cNrMaq+".TXT" )))
if "OK:" $ cNFeLog .and. !("REJEICAO" $ cNFeLog)
cProtocolo := Aux_NFe_PegaProtocolo(cNFeLog)
Alert( "Cancelamento da NF-e Homologado com Sucesso!!!;"+ "Protocolo Gerado:;"+ cProtocolo +";;" , + {" Pressione Enter "},vcr)
Aux_NFe_MostraRetACBr()
Nota->STATUS := "CANCELADA"
lRefresh:= .T.
elseif ("ERRO" $ cNFeLog) .or. ("REJEICAO" $ cNFeLog)
Alert( "Aten‡„o!!! N„o foi poss¡vel Efetivar o Cancelamento da NF-e;"+ " Verifique o Retorno da SEFAZ;"+ " Sera necess rio Tentar novamente!;;" , {" Pressione Enter "},vcr)
Aux_NFe_MostraRetACBr()
endif
FERASE(Alltrim(Caminho->PathNFe) + "SAINFE"+cNrMaq+".TXT")
EXIT
endif
END
RESTSCREEN(s_geral, cTelaCanc)
return NIL
********************************************************************************
function NFe_InutilizarNFe( cCNPJ, cJustificativa, nAno, nModelo, nSerie, nNumInicial, nNumFinal )
********************************************************************************
local cProtocolo := '', getList:={}, xTela
FERASE("ENTNFe"+cNrMaq+".TXT")
cArq:="ENTNFe"+cNrMaq+".CMD"
SET DEVICE TO PRINTER
SET PRINTER TO (cArq)
SET PRINTER ON
SETPRC(0,0)
nLin :=0
cCNPJ := TiraVirgNFe(DadosEmp->CGC) //if(cCNPJ=nil,"XXXXXXXXXXXXXX",cCNPJ)
nAno := str(Year(Nota->DataEmi)) //str( nAno )
nModelo := '55' //str( nModelo )
nSerie := Nota->Serie //'1' //str( nSerie )
nNumInicial := str(val(Nota->NFiscal)) //str( nNumInicial )
nNumFinal := str(val(Nota->NFiscal)) //str( nNumfinal )
cCmd := ["]+cCNPJ+[","]+cJustificativa+[",]+nAno+[,]+nModelo+[,]+nSerie+[,]+nNumInicial+[,]+nNumFinal
@ nLin,0 say "NFE.InutilizarNFe("+cCmd+")"
DBCLEARREL()
SET PRINTER TO ; PRINTOFF()
Aux_NFe_ArqEntACBr( cArq, 'Aguarde..., Tentando Inutilizar Nr. NF-e / Este processo pode demorar !', 30,,,'CHAVE',,.T.)
While .T.
if file( Alltrim(Caminho->PathNFe) + "SAINFE"+cNrMaq+".TXT" )
cNFeLog := alltrim(upper(memoread( Alltrim(Caminho->PathNFe) + "SAINFE"+cNrMaq+".TXT" )))
if "OK:" $ cNFeLog
if "REJEICAO" $ cNFeLog
Alert( "Aten‡„o!!! N„o foi poss¡vel Enviar a NF-e;"+ " Verifique o Retorno da SEFAZ;"+ " Sera necess rio Enviar novamente a NF-e!;;" , {" Pressione Enter "},vcr)
Aux_NFe_MostraRetACBr()
else
NFe_Aux_StatusNota("INUTILIZAD")
cProtocolo := Aux_NFe_PegaProtocolo(cNFeLog)
Alert( "Nr. NF-e INUTILIZADO com Sucesso!!!;"+ "Protocolo Gerado:;"+ cProtocolo + ";;" , {" Pressione Enter "},vcr)
Aux_NFe_MostraRetACBr()
endif
elseif "ERRO" $ cNFeLog .or. "REJEICAO" $ cNFeLog
Alert( "Atencao!!! Nao foi poss¡vel Enviar a NF-e;"+ " Verifique o Retorno da SEFAZ;"+ " Sera necess rio Enviar novamente a NF-e!;;" , {" Pressione Enter "},vcr)
Aux_NFe_MostraRetACBr()
endif
FERASE(Alltrim(Caminho->PathNFe) + "SAINFE"+cNrMaq+".TXT")
Exit
else
lErro:=.T.
Alert( "AtencÆo!!!;; Ser necess rio refazer o procedimento!;;" , {" Pressione Enter "},vcr)
EXIT
endif
END
return NIL
********************************************************************************
function Aux_NFe_ArqEntACBr( cArq, cTextoPisc, nTempoPisc, nEsperaInk, nTempMyWait, cIdxMyWait, cDadosMyWait, lWaitCmds )
* Envia Arq Gerado para ACBrNFeMonitor
********************************************************************************
local cDirDoMonitor := ""
default cTextoPisc to ''
default nTempoPisc to 1
default nEsperaInk to 0
default lWaitCmds to .F.
cDirDoMonitor := Alltrim(Caminho->PathNFe) + ''
*-- Apaga Arq Enviado para ACBrNFeMonitor
FERASE( cDirDoMonitor + '\ENTNFE' + cNrMaq + '.TXT' )
if lWaitCmds
MyWaitNFe(2)
endif
*-- Apaga Poss¡vel retorno dao ACBrMonitor
FERASE( cDirDoMonitor + '\SAINFE' + cNrMaq + '.TXT' )
if lWaitCmds
MyWaitNFe(2)
endif
*-- Cria Arq para ACBrNFeMonitor Ler
FILECOPY( cArq, cDirDoMonitor + 'ENTNFe' + cNrMaq + '.CMD' )
if lWaitCmds
MyWaitNFe(2)
endif
*-- Apaga Arq de comando que j foi copiado para o ACBrNFeMonitor
FILEDELETE( cArq )
*-- Renomeia o Arquivo de Comando para a ExtensÆo lida pelo ACBrNFeMonitor
FRENAME( cDirDoMonitor + 'ENTNFe' + cNrMaq + '.CMD',;
Alltrim(Caminho->PathNFe) + 'ENTNFe' + cNrMaq + '.TXT' )
if lWaitCmds
MyWaitNFe(3)
endif
if nEsperaInk > 0
INKEY(nEsperaInk)
endif
if !EMPTY( cTextoPisc )
MensagPisc(cTextoPisc, nTempoPisc)
endif
MyWaitNFe(nTempMyWait, cIdxMyWait, cDadosMyWait)
return NIL
*---------------------------------------------------------------------------*
*-- Executa comando X, parametro Y
function Aux_NFe_Monitor(cComando, xPar)
local lOk := .F.,;
cArquivo := 'ENTNFe' + cNrMaq,;
nLin :=0, ;
cDirDoMonitor := ''
*-- Apaga o arquivo de comando
FERASE( cArquivo + '.TXT')
FERASE( cArquivo + '.CMD')
SET DEVICE TO PRINTER
SET PRINTER TO (cArquivo + '.CMD')
SET PRINTER ON
@ nLin,0 say cComando + '(' + chr(34) + xPar + chr(34) + ')'
DBCLEARREL()
SET PRINTER TO
PRINTOFF()
AbreParam()
*-- Apaga Arquivos na pasta do ACBrNFeMonitor
cDirDoMonitor := Alltrim(Caminho->PathNFe) + ''
FERASE( cDirDoMonitor + '\ENTNFE' + cNrMaq + '.TXT' ) // Envio
FERASE( cDirDoMonitor + '\SAINFE' + cNrMaq + '.TXT' ) // Retorno
inkey(0.5)
*-- Cria Arq para ACBrNFeMonitor Ler
FILECOPY( cArquivo + '.CMD', cDirDoMonitor + cArquivo + '.CMD' ) ; inkey(0.5)
*-- Troca para a ExtensÆo lida pelo ACBrNFeMonitor
FRENAME( cDirDoMonitor + cArquivo + '.CMD', Alltrim(Caminho->PathNFe) + cArquivo + '.TXT' )
INKEY(2)
*-- Apaga Arquivos
FILEDELETE( cArquivo ) // Arquivo Comando local
FERASE( cArquivo + '.TXT') // Arquivo ACBr
FERASE( cArquivo + '.CMD') // Arquivo Comando pasta ACBr
return lOk
********************************************************************************
function Aux_NFe_MostraRetACBr()
* Mostra Retorno SEFAZ caso exista arquivo de retorno
local cTelaMsg := SaveScreen(s_geral), ;
cArqRet := '',;
cConteudoArqRet := ''
if Select('Param') == 0
AbreParam()
endif
cArqRet := Alltrim(Caminho->PathNFe) + 'sainfe' + cNrMaq + '.txt'
if file(cArqRet)
cConteudoArqRet := MemoRead( cArqRet )
cConteudoArqRet := substr(cConteudoArqRet, AT('[RETORNO]', upper(cConteudoArqRet))) // coloquei em fev/2016 para pegar a parte do retorno
endif
if !empty( cConteudoArqRet )
ferase('retacbr'+cNrMaq + '.txt')
memowrit('retacbr'+cNrMaq + '.txt', cConteudoArqRet)
MostraTex('retacbr'+cNrMaq+'.txt', ' RETORNO SEFAZ ', 3,12,20,67) // abre arquivo de Retorno na tela
endif
RestScreen(s_geral, cTelaMsg)
return NIL
* Atenção * Alguns trechos de código foram retirados, então o código acima é pra ter uma noção geral da geração do arquivo para o ACBr e das etapas e uma ideia de como trabalhar.
[]´s