Jump to content
Fivewin Brasil

validar chave da NF-e, tem como


marcioe

Recommended Posts

Existe uma forma de Validar a chave da NF-e,
tem cliente que Digita a Chave para Fazer a baixa do XML, 
E o "abençoado" as vezes digita os 44 numeros( que o sistema nosso já verifica ) se for menor que 44 posiçoes dá msg de erro
Porem as vezes o "Abençoado" digita os 44 numeros e erra um bendito no meio, dai liga no suporte, etc...
É um pela Saco, será que tem como (já mandei ele comprar um leitor), mas já viu como é cliente.
Se tiver uma forma de Validar seria bem util

 

 

Link to comment
Share on other sites

segue o código para voce testar

 

Local cIndice           := '4329876543298765432987654329876543298765432'                             
Local nDigito           
Local nContador         
Local nSoma             := 0                                                       


                                                                                              
   For nContador := 1 to 43                                                                   
       nSoma += (Val(SubStr(cCodigoChave, nContador, 1)) * Val(SubStr(cIndice, nContador, 1)))
   Next                                                                                       
                                                                                              
   nDigito := (nSoma % 11)                                                                    
                                                                                              
   If nDigito == 0 .Or. nDigito == 1                                                          
      nDigito := 0                                                                            
   Else                                                                                       
      nDigito := (11 - nDigito)                                                               
   EndIf                                                                                      
                                                                                              
   If SubStr(cCodigoChave, 44, 1) != LTrim(Str(nDigito, 1))                                   
     MsgStop('A chave informada é inválida!', 'Atenção!')                                     
     Return .f.                                                                               
   EndIf                                                                                      

 

Link to comment
Share on other sites

Segue...
 

#include "toya2k20.ch"

//------------------------------------------------------------------------------
//	if NFe_validar_chave( "32191205570714000825550010062282521138571606" )
//		msginfo( "Chave correta" )
//	else	
//		msginfo( "Chave incorreta" )
//	endif
//------------------------------------------------------------------------------

function NFe_validar_chave( pcChave )
// #toya: 17/09/2020 - 20:25:01 - mrb_dfe-2020 - Release .001.001

local Result := .t.
local aChave[09]
local cChave := ""
local cData := dtoc( date() ) 
local cAAMM := ""
local cAno := ""
local cMes := ""
local lRetorno := .f.
local lOk := .t.
local i := 0

DEFAULT pcChave := ""

cChave := SoNumeros( pcChave )
if len( cChave ) <> 44
	Result := .f.
else
	cAAMM := substr( cData, 7, 2 ) + substr( cData, 3, 2 )
	if substr( cChave, 5, 2 ) < substr( cAAMM, 1, 2 )
		cMes := "\01\02\03\04\05\06\07\08\09\10\11\12\"
	elseif substr( cChave, 5, 2 ) == substr( cAAMM, 1, 2 )
		cMes := "\"
		for i = 1 to val( substr( cAAMM, 3, 2 ) )
			cMes += strzero( i, 2, 0 ) + "\"
		next
	else
		Result := .f.
	endif
endif
if Result
	Result := .f.
	cAno := "\"
	for i = 10 to val( substr( cAAMM, 1, 2 ) )
	  cAno += strzero( i, 2, 0 ) + "\"
	next
	if substr( cChave, 1, 2 ) $ "\12\27\16\13\29\23\53\32\52\21\51\50\31\15\25\41\26\22\33\24\43\11\14\42\35\28\17\"
		if substr( cChave, 3, 2 ) $ cAno .and. substr( cChave, 5, 2 ) $ cMes
			if ValidaCNPJ_CPF( "J", substr( cChave, 7, 14 ) )
				if substr( cChave, 21, 2 ) == "55"
					if ( val( substr( cChave, 23, 3 ) ) >= 0 .and.;
					   val( substr( cChave, 23, 3 ) ) <= 899 .and.;
					   substr( cChave, 35, 1 ) == "1" ) .or.;
					   ( val( substr( cChave, 23, 3 ) ) >= 900 .and.;
					   val( substr( cChave, 23, 3 ) ) <= 999 .and.;
					   substr( cChave, 35,1 ) $ "\2\3\4\5\" )
						if val( substr( cChave, 26, 9 ) ) > 0
							if substr( cChave, 35, 1 ) $ "\1\2\3\4\5\"
								if val( substr( cChave, 36, 8 ) ) > 0
									if substr( cChave, 44, 1 ) == NFe_Modulo11( substr( cChave, 1, 43 ) )
										Result:=.t.
									endif
								endif
							endif
						endif
					endif
				endif
			endif
		endif
	endif
endif

return Result

//------------------------------------------------------------------------------

static function NFe_Modulo11( pcNumero, aSeq, nLen )

local Result := "%"
local cNumero := ""
local nTotal := 0
local i := 0

DEFAULT aSeq := { 4,3,2,9,8,7,6,5,4,3,2,9,8,7,6,5,4,3,2,9,8,7,6,5,4,3,2,9,8,7,6,5,4,3,2,9,8,7,6,5,4,3,2 }
DEFAULT nLen := 43

cNumero := iif( valtype( pcNumero ) == "N", padl( alltrim( str( pcNumero, nLen ) ), nLen ), padl( pcNumero, nLen ) )
for i = 1 to nLen
    nTotal += val( substr( cNumero, i, 1 ) ) * aSeq[i]
next
Result := iif( nTotal % 11 < 2, "0", str( 11 - ( nTotal % 11 ), 1 ) )

return Result

//------------------------------------------------------------------------------

static function Sonumeros( cString, cExcecao )

local Result := ""
local i := 0

DEFAULT  cExcecao := ""

for i = 1 to len( cString )
	if substr( cString, i, 1 ) $ "1234567890" .or. substr( cString, i, 1 ) $ cExcecao
		Result += substr( cString, i, 1 )
	endif
next

return Result

//------------------------------------------------------------------------------

static function ValidaCNPJ_CPF( cTipo, cDados, lMsg )

local Result := .f.

DEFAULT cTipo := "J"
DEFAULT lMsg  := .t.

if cTipo == "F"
	Result := ValidarCPF( cDados, lMsg )
else
   Result := ValidarCNPJ( cDados, lMsg )
endif

return Result

//------------------------------------------------------------------------------

static function ValidarCPF( cCPF, lMsg )

local Result := .t.
local i
local j
local soma
local controle
local digito
local num
local wCPF := iif( ValType( cCPF ) == "U", "", cCPF )

DEFAULT lMsg := .t.

wCPF := strtran( wCPF, ".", "" )
wCPF := strtran( wCPF, "-", "" )
if empty( wCPF )
	Result := .t.
else
	if len( wCPF ) < 11
		Result  := .f.
	else
		controle := ""
		num = 10
		for j = 1 to 2
			soma=0
			for i = 1 to 9
				soma += ( val( substr( wCPF, i, 1 ) ) * num )
				num--
			next
			if j = 2
				soma += ( 2 * val( controle ) )
			endif
			digito = soma - ( int( soma / 11 ) * 11 )
			if digito == 0 .or. digito == 1
				controle := controle + "0"
			else
				controle := controle + str( 11 - digito, 1 )
			endif
			num := 11
		next
		if controle <> substr( wCPF, 10, 2 )
			Result := .f.
		endif
	endif
	if !Result
		if lMsg
			msginfo( "CPF Incorreto ou Digito Invalido...", "ATENCAO" )
		endif	
	endif
endif

return Result

//------------------------------------------------------------------------------

static function ValidarCNPJ( cCNPJ, lMsg )

local Result := .t.
local i
local j
local soma
local controle
local digito
local num
local wCNPJ := iif( valtype( cCNPJ ) == "U", "", cCNPJ )

DEFAULT lMsg := .t.

wCNPJ := charrem( ".-/", wCNPJ )
if empty( wCNPJ )
	Result := .t.
else
	iF len( wCNPJ ) < 14
		Result := .f.
	else
		controle := ""
		num := 5
		for j = 1 to 2
			soma := 0
			for i = 1 to 12
				soma += ( val( substr( wCNPJ, i, 1 ) ) * num )
				num--
				if num == 1
					num := 9
				endif
			next
			if j == 2
				soma += ( 2 * val( controle ) )
			endif
			digito := soma - ( int( soma / 11 ) * 11 )
			if digito == 0 .or. digito == 1
				controle := controle + "0"
			else
				controle := controle + str( 11 - digito, 1 )
			endif
			num := 6
		next
		if controle <> substr( wCNPJ,13,2)
			Result := .f.
		endif
	endif
	if !Result
		if lMsg
			msginfo( "CNPJ Incorreto ou Digito Invalido..." + " [" + controle + "]" )
		endif
	endif
endif

return Result

//------------------------------------------------------------------------------

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...