Title: How to get italian fiscal code for society informations
Question: In Italy, every society has a numeric fiscal code called "Partita I.V.A." (Imposta sul Valore Aggiunto), that is released only for some cities. Here's the way how to get the serial number, the city of release and if the code is valido or not.
Answer:
Function PartitaIVA( Num : String; Var Progressive : Integer; Var Provincia : String ) : Boolean;
Const
Provincie : Array[ 0..102 ] Of String =
(
'Torino', 'Vercelli', 'Novara', 'Cuneo', 'Asti',
'Alessandria', 'Aosta', 'Imperia', 'Savona', 'Genova',
'La Spezia', 'Varese', 'Como', 'Sondrio', 'Milano',
'Bergamo', 'Brescia', 'Pavia', 'Cremona', 'Mantova',
'Bolzano-Bozen', 'Trento', 'Verona', 'Vicenza', 'Belluno',
'Treviso', 'Venezia', 'Padova', 'Rovigo', 'Udine',
'Gorizia', 'Trieste', 'Piacenza', 'Parma', 'Reggio nell''Emilia',
'Modena', 'Bologna', 'Ferrara', 'Ravenna', 'Forli''-Cesena',
'Pesaro e Urbino', 'Ancona', 'Macerata', 'Ascoli Piceno', 'Massa-Carrara',
'Lucca', 'Pistoia', 'Firenze', 'Livorno', 'Pisa',
'Arezzo', 'Siena', 'Grosseto', 'Perugia', 'Terni',
'Viterbo', 'Rieti', 'Roma', 'Latina', 'Frosinone',
'Caserta', 'Benevento', 'Napoli', 'Avellino', 'Salerno',
'L''Aquila', 'Teramo', 'Pescara', 'Chieti', 'Campobasso',
'Foggia', 'Bari', 'Taranto', 'Brindisi', 'Lecce',
'Potenza', 'Matera', 'Cosenza', 'Catanzaro', 'Reggio di Calabria',
'Trapani', 'Palermo', 'Messina', 'Agrigento', 'Caltanissetta',
'Enna', 'Catania', 'Ragusa', 'Siracusa', 'Sassari',
'Nuoro', 'Cagliari', 'Pordenone', 'Isernia', 'Oristano',
'Biella', 'Lecco', 'Lodi', 'Rimini', 'Prato',
'Crotone', 'Vibo Valentia', 'Verbano-Cusio-Ossola'
);
Function ReduceSum( N : Integer ) : Integer;
Var
IdX : Integer;
tmpStr : String;
Begin
tmpStr := IntToStr( N );
If ( Length( tmpStr ) Result := N;
Exit;
End;
Result := 0;
For IdX := 1 To Length( tmpStr ) Do
Result := Result + StrToInt( tmpStr[ IdX ] );
End;
Function ReduceNum( N : Integer ) : Integer;
Begin
If ( Length( IntToStr( N ) ) Result := N
Else
Result := StrToInt( IntToStr( N )[ Length( IntToStr( N ) ) ] )
End;
Var
IdX : Integer;
SumD : Integer;
SumP : Integer;
Sum : Integer;
Check : Integer;
Begin
If ( Length( Num ) 11 ) Then Begin
Result := False;
Provincia := '11 numeric-characters needed!';
Exit;
End;
For IdX := 1 To 11 Do
If ( Not( Num[ IdX ] In [ '0'..'9' ] ) ) Then Begin
Result := False;
Provincia := '"' + Num[ IdX ] + '" is not a numeric value!';
Exit;
End;
// Returns the town.
IdX := StrToInt( Num[ 8 ] + Num[ 9 ] + Num[ 10 ] ) - 1;
If ( ( IdX = 0 ) And ( IdX // Returns the progressive number.
Progressive := StrToInt( Copy( Num, 1, 7 ) );
// Calculates if is valid.
SumD := 0;
For IdX := 1 To 10 Do
If ( ( IdX Mod 2 ) = 1 ) Then
Inc( SumD, StrToInt( Num[ IdX ] ) );
SumP := 0;
For IdX := 1 To 10 Do
If ( ( IdX Mod 2 ) = 0 ) Then
Inc( SumP, ReduceSum( StrToInt( Num[ IdX ] ) * 2 ) );
ReduceSum( SumP );
Sum := SumD + SumP;
Check := 10 - ReduceNum( Sum );
Result := ( Check = StrToInt( Num[ 11 ] ) );
End;