Guest User

Validação de linha digitável de boleto de cobrança

a guest
Apr 26th, 2016
615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.19 KB | None | 0 0
  1. {
  2.  ------------------------------------------------------------------------------
  3.           PROCEDIMENTO PARA EXTRAIR LINHA DIGITAVEL
  4.           NO FORMATO: 00000.00000 00000.000000 00000.000000 0 00000000000000
  5.  ------------------------------------------------------------------------------
  6. }
  7.  
  8. function ExtractLinhaDigitavel(dados: string): string;
  9.  
  10.   function fncValidarDigitoDaLinhaDigitavel(LinhaDigitavel: string): Boolean;
  11.   var
  12.     Verificador, VerificadorCorreto: String;
  13.   begin
  14.     Verificador := LinhaDigitavel;
  15.     Delete(Verificador, 1, Pos(' ', Verificador));
  16.     Delete(Verificador, 1, Pos(' ', Verificador));
  17.     Delete(Verificador, 1, Pos(' ', Verificador));
  18.     Verificador := Copy(Verificador, 1, 1);
  19.     VerificadorCorreto := Modulo11LinhaDigitavel(LinhaDigitavel);
  20.     Result := (VerificadorCorreto = Verificador);
  21.     // MOSTRA O RESULTADO SE ENCONTROU OU NÃO O BOLETO
  22.    // WriteLn(Result);
  23.   end;
  24.  
  25.   function fncValidarLinhaDigitavel(linha: string): LongBool;    
  26.   var
  27.     I: Integer;
  28.   begin
  29.     Result := (linha[6] = '.') and
  30.               (linha[12] = ' ') and
  31.               (linha[18] = '.') and
  32.               (linha[25] = ' ') and
  33.               (linha[31] = '.') and
  34.               (linha[38] = ' ') and
  35.               (linha[40] = ' ');
  36.     if Result then
  37.     begin
  38.       for I:=1 to 54 do
  39.       begin
  40.         if ((I<>6)and(I<>12)and(I<>18)and(I<>25)and(I<>31)and(I<>38)and(I<>40)) then
  41.         begin
  42.           if Pos(linha[I], '0123456789') = 0 then
  43.           begin
  44.             Result := False;
  45.             Break;
  46.           end;
  47.         end;
  48.       end;                                                                                    
  49.     end;
  50.   end;
  51.  
  52. var
  53.   I: Integer;
  54.   SCAN: string;
  55. begin
  56.   Result := '';
  57.   for I:=1 to Length(dados) do
  58.   begin
  59.     SCAN := Copy(dados, I, 54);
  60.     if fncValidarLinhaDigitavel(SCAN) then
  61.     begin
  62.       if fncValidarDigitoDaLinhaDigitavel(SCAN) then
  63.       begin
  64.         // Caso os 54 caracteres contém apenas numeros, pontos, espaços e o digito verificador estiver correto, então é uma linha digitavel.
  65.         Result := SCAN;
  66.       //  WriteLn(Result);
  67.         Break;
  68.       end;
  69.     end;
  70.   end;
  71. end;
Advertisement
Add Comment
Please, Sign In to add comment