Fonte: www.activedelphi.com.br
function ValidarChaveNFe(const ChaveNFe: string):boolean;
const
PESO : Array[0..43] of Integer = (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,0); /// Aqui corrigido para o Delphi XE
var
Retorno : boolean;
aChave : Array[0..43] of Char;
Soma : Integer;
Verif : Integer;
I : Integer;
begin
Retorno := false;
try
try
if not Length(ChaveNFe) = 44 then
raise Exception.Create('');
StrPCopy(aChave,StringReplace(ChaveNFe,' ', '',[rfReplaceAll]));
Soma := 0;
for I := Low(aChave) to High(aChave) do
Soma := Soma + (StrToInt(aChave[i]) * PESO[i]);
if Soma = 0 then
raise Exception.Create('');
Soma := Soma - (11 * (Trunc(Soma / 11)));
if (Soma = 0) or (Soma = 1) then
Verif := 0
else
Verif := 11 - Soma;
Retorno := Verif = StrToInt(aChave[43]);
except
Retorno := false;
end;
finally
Result := Retorno;
end;
const
PESO : Array[0..43] of Integer = (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,0); /// Aqui corrigido para o Delphi XE
var
Retorno : boolean;
aChave : Array[0..43] of Char;
Soma : Integer;
Verif : Integer;
I : Integer;
begin
Retorno := false;
try
try
if not Length(ChaveNFe) = 44 then
raise Exception.Create('');
StrPCopy(aChave,StringReplace(ChaveNFe,' ', '',[rfReplaceAll]));
Soma := 0;
for I := Low(aChave) to High(aChave) do
Soma := Soma + (StrToInt(aChave[i]) * PESO[i]);
if Soma = 0 then
raise Exception.Create('');
Soma := Soma - (11 * (Trunc(Soma / 11)));
if (Soma = 0) or (Soma = 1) then
Verif := 0
else
Verif := 11 - Soma;
Retorno := Verif = StrToInt(aChave[43]);
except
Retorno := false;
end;
finally
Result := Retorno;
end;
Nenhum comentário:
Postar um comentário