2009. január 19., hétfő

Fast italian-code-for-companies check function


Problem/Question/Abstract:

In my old article "How to check italian code for companies" there was a explanation how to get information from italian code for companies, but someone recently mailed me that a simply checker version of that function is needed instead. There's the solution.

Answer:

function PartitaIVA(code: string): boolean;
  function ReduceSum(n: Integer): Integer;
  var
    i: Integer;
    s: string;
  begin
    s := inttostr(n);
    if (length(s) = 1) then
    begin
      result := n;
      exit;
    end;
    result := 0;
    for i := 1 to length(s) do
    begin
      result := result + strtointdef(s[i], 0);
    end;
  end;
  function ReduceNum(n: Integer): Integer;
  var
    s: string;
  begin
    result := n;
    s := inttostr(n);
    if (length(s) > 1) then
    begin
      result := strtointdef(s[length(s)], 0)
    end;
  end;
var
  i: Integer;
  c: Integer;
begin
  result := false;
  if (length(code) <> 11) then
    exit;
  for i := 1 to 11 do
    if (not (code[i] in ['0'..'9'])) then
      exit;
  i := strtointdef(copy(code, 8, 3), 0) - 1;
  if ((i < 0) or (i > 102)) then
    exit;
  c := 0;
  for i := 1 to 10 do
  begin
    if ((i mod 2) = 0) then
      inc(c, reducesum(strtointdef(code[i], 0) * 2))
    else
      inc(c, strtointdef(code[i], 0));
  end;
  result := ((10 - ReduceNum(c)) = strtointdef(code[11], -1));
end;

That's all, removed all output parameter variables for a quickly validity check.

Christian Cristofori

Nincsenek megjegyzések:

Megjegyzés küldése