git-svn-id: https://192.168.0.254/svn/Proyectos.Noviseda_FactuGES2/trunk@8 f33bb606-9f5c-448d-9c99-757f00063c96
105 lines
2.6 KiB
ObjectPascal
105 lines
2.6 KiB
ObjectPascal
unit uCIFNIFUtils;
|
|
|
|
interface
|
|
|
|
function EsCif(Cif : String) : Boolean;
|
|
|
|
implementation
|
|
|
|
uses
|
|
SysUtils;
|
|
|
|
function Letra(Numero: Integer): string;
|
|
begin
|
|
Result:= copy('TRWAGMYFPDXBNJZSQVHLCKET',1 + numero mod 23,1);
|
|
end;
|
|
|
|
function EsNif(NIF: String): Boolean;
|
|
var
|
|
Numero: Integer;
|
|
begin
|
|
Result:= FALSE;
|
|
if TryStrToInt(Copy(NIF,1,Length(NIF)-1),Numero) then
|
|
Result:= Uppercase(Copy(NIF,Length(NIF),1)) = Letra(Numero);
|
|
end;
|
|
|
|
function EsNumero(Num: Char): Boolean;
|
|
begin
|
|
try
|
|
StrToInt(Num);
|
|
Result:=True;
|
|
Except
|
|
result:=False;
|
|
end;
|
|
end;
|
|
|
|
{Cambiar un carácter por otro en una cadena.}
|
|
function CadCambioCar(Cadena, CarOrig, CarCambio : String) : String;
|
|
var
|
|
i : Integer;
|
|
Temp : String;
|
|
begin
|
|
Temp := '';
|
|
for i := 1 to Length(Cadena) do
|
|
if Copy(Cadena, i, 1) = CarOrig then
|
|
Temp := Temp + CarCambio
|
|
else
|
|
Temp := Temp + Copy(Cadena, i, 1);
|
|
Result := Temp;
|
|
end;
|
|
|
|
|
|
{ Validar si un CIF introducido es correcto}
|
|
function EsCif(Cif : String) : Boolean;
|
|
var
|
|
Suma, Control : Integer;
|
|
n : Byte;
|
|
begin
|
|
Result := False;
|
|
|
|
{Se pasa todo a mayúsculas limpio de espacios y de caracteres especiales}
|
|
Cif := UpperCase(Trim(Cif));
|
|
|
|
{Se limpia de los caracteres '-' y '/'. }
|
|
Cif := CadCambioCar(Cif,'-','');
|
|
Cif := CadCambioCar(Cif,'/','');
|
|
|
|
{El cif debe ser de 9 cifras}
|
|
if Length(Cif) = 9 then
|
|
begin
|
|
{Comprobamos que sea un NIF}
|
|
if EsNumero(Cif[1]) then
|
|
Result := EsNif(Cif)
|
|
else
|
|
{Se comprueba que la letra que designa el tipo de cif sea correcta}
|
|
if (Pos(Cif[1], 'ABCDEFGHPQSKLMX') = 0) then
|
|
Result := False
|
|
else
|
|
{Se comprueba si es un extranjero, en ese caso se calcula el nif, cambiando la X, por 0}
|
|
if Cif[1] = 'X' then
|
|
Result := EsNif('0'+Copy(Cif,2,8))
|
|
else
|
|
begin
|
|
Suma:= StrToInt(Cif[3])+StrToInt(Cif[5])+StrToInt(Cif[7]);
|
|
for n := 1 to 4 do
|
|
Suma := Suma + ((2*StrToInt(Cif[2*n])) mod 10)+((2*StrToInt(Cif[2*n])) div 10);
|
|
Control := 10 - (Suma mod 10);
|
|
{Se comprueba si es de tipo 'P' o 'S', es decir, Corporaciones Locales (Ayuntamientos, etc.)
|
|
y Organismos públicos.}
|
|
if Pos(Cif[1],'PS')<>0 then
|
|
{Control tipo letra}
|
|
Result := (Cif[9] = Chr(64+Control))
|
|
else
|
|
{Resto de tipos de CIF}
|
|
begin
|
|
{Control tipo número}
|
|
if Control = 10 then
|
|
Control := 0;
|
|
Result:= ( StrToInt(Cif[9]) = Control);
|
|
end;
|
|
end;
|
|
end;
|
|
end;
|
|
|
|
end.
|