2
Good afternoon friends,
by chance someone would have some procedure similar to that and that you could post here?
It will be of great use for me and future readers of this post.
I await return.
This was my last attempt to translate this Delphi code, but it’s wrong.
private static bool Ok = true;
public static string RemoveFirst(this string source, string remove)
{
int index = source.IndexOf(remove);
return (index < 0)
? source
: source.Remove(index, remove.Length);
}
/// <summary>
/// Valida o dígito verificador da linha
/// </summary>
/// <param name="chaveAcesso"></param>
/// <returns></returns>
public static string RetornaDigitoVerificadorCteModulo11(string chaveAcesso)
{
int peso = 2;
int soma = 0;
try
{
chaveAcesso.ToCharArray()
.Reverse()
.ToList()
.ForEach(f =>
{
soma += (Convert.ToInt32(f.ToString()) * peso);
peso = (peso == 9) ? 2 : peso + 1;
});
return (11 - (soma % 11)) <= 1 ? "0" : (11 - (soma % 11)).ToString();
}
catch
{
return "ERRO: A chave de acesso deve conter apenas números.";
}
}
/// <summary>
/// Valida de forma generalizada o DV da linha digitável
/// </summary>
/// <param name="linhaDigtavel"></param>
/// <returns></returns>
private static bool ValidacaoGeralDV(string linhaDigtavel)
{
string Dv;
string DvVerificado;
Dv = linhaDigtavel;
RemoveFirst(Dv, " ");
RemoveFirst(Dv, " ");
RemoveFirst(Dv, " ");
Dv = Dv.Substring(0, Dv.Length);
DvVerificado = RetornaDigitoVerificadorCteModulo11(linhaDigtavel);
return (DvVerificado == Dv) ? true : false;
}
/// <summary>
/// Validação geral da linha digitável
/// </summary>
/// <param name="linha"></param>
/// <returns></returns>
private static void ValidacaoGeralLinha(string linha)
{
char[] caracteresLinha = linha.ToCharArray();
bool correto = (caracteresLinha[6] == '.') &&
(caracteresLinha[12] == ' ') &&
(caracteresLinha[18] == '.') &&
(caracteresLinha[25] == ' ') &&
(caracteresLinha[31] == '.') &&
(caracteresLinha[38] == ' ') &&
(caracteresLinha[40] == ' ');
if (correto)
{
for (int i = 1; i <= 54; i++)
{
if ( (i != 6) &&
(i != 12) &&
(i != 18) &&
(i != 25) &&
(i != 31) &&
(i != 38) &&
(i != 40) )
{
if (!linha.Contains(i.ToString()))
{
Ok = false;
break;
}
}
}
}
}
/// <summary>
/// Retorna a linha digitável já validada no formato: 00000.00000 00000.000000 00000.000000 0 00000000000000
/// </summary>
/// <param name="dado"></param>
/// <returns></returns>
private static string RetornaLinhaValidada(string dado)
{
string resultado = "";
for (int i = 1; i <= dado.Length; i++)
{
resultado = dado.Substring(0, dado.Length);
ValidacaoGeralLinha(resultado);
if (Ok)
{
if (ValidacaoGeralDV(resultado))
break;
}
}
return "";
}
static void Main(string[] args)
{
Console.WriteLine(RetornaLinhaValidada("10490.05539 03698.700006 00091.449587 5 55490000028531"));
Console.ReadKey();
}
Each bank has a different way, has to ask for their documentation. In fact, I think it’s not a matter of programming itself.
– Maniero
Developing this will be your least problem. As already stated by the bigown, each bench has a different pattern. You need to have the bank’s documentation to work on this.
– Jéf Bueno
You want to validate only the format or you want to validate the code?
– Randrade
The Randrade format as well as the Delphi code.
– Jonas Silva
Let’s go from the top @Jonassilva. What exactly do you need to do? Do you want to pass the digitable line and validate exactly what?
– Jéf Bueno
I think this library can help you: https://github.com/BoletoNet/boletonet
– Leonardo Hofling
@jbueno, I want to validate check digit and line format for the pattern in question.
– Jonas Silva
@Jonassilva If you want to validate the checker digit, you need an implementation for each bank. It is not possible to make an implementation that encompasses all banks.
– Jéf Bueno
@jbueno, the famous Módulo11, would no longer be a "global" validation regardless of which bank is?
– Jonas Silva
@jbueno, including this function there of Module 11 in Delphi returns the correctly independent digit of the database :D
– Jonas Silva