Author: Christian Cristofori
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:
1 2 function PartitaIVA(code: string): boolean;
3 function ReduceSum(n: Integer): Integer;
4 var5 i: Integer;
6 s: string;
7 begin8 s := inttostr(n);
9 if (length(s) = 1) then10 begin11 result := n;
12 exit;
13 end;
14 result := 0;
15 for i := 1 to length(s) do16 begin17 result := result + strtointdef(s[i], 0);
18 end;
19 end;
20 function ReduceNum(n: Integer): Integer;
21 var22 s: string;
23 begin24 result := n;
25 s := inttostr(n);
26 if (length(s) > 1) then27 begin28 result := strtointdef(s[length(s)], 0)
29 end;
30 end;
31 var32 i: Integer;
33 c: Integer;
34 begin35 result := false;
36 if (length(code) <> 11) then37 exit;
38 for i := 1 to 11 do39 if (not (code[i] in ['0'..'9'])) then40 exit;
41 i := strtointdef(copy(code, 8, 3), 0) - 1;
42 if ((i < 0) or (i > 102)) then43 exit;
44 c := 0;
45 for i := 1 to 10 do46 begin47 if ((i mod 2) = 0) then48 inc(c, reducesum(strtointdef(code[i], 0) * 2))
49 else50 inc(c, strtointdef(code[i], 0));
51 end;
52 result := ((10 - ReduceNum(c)) = strtointdef(code[11], -1));
53 end;
That's all, removed all output parameter variables for a quickly validity check.
Christian Cristofori