generate random CIF

This handler return a random spanish “Cost, Insurance and Freight” and is easily adaptable to create a CIF validation routine.

OS version: Any

property mayusculas : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
property letrasCIF : "ABCDEFGHKLMPQSXZ"

generateRandomCIF()

on generateRandomCIF() -- A1234567M
	set numCentral to (random number from 1 to 9999999) as string
	if numCentral's length < 7 then -- añadir ceros
		set x to numCentral's length
		repeat (7 - x) times
			set numCentral to "0" & numCentral
		end repeat
	end if
	
	set sumaDePares to 0
	repeat with i from 2 to 6 by 2 -- pares
		set sumaDePares to sumaDePares + ((numCentral's item i) as integer)
	end repeat
	set sumaDeImpares to 0
	repeat with i from 1 to 7 by 2 -- impares
		set temp1 to ((numCentral's item i as integer) * 2) as string
		if temp1's length > 1 then
			set temp2 to (temp1's item 1 as integer) + (temp1's item 2 as integer)
		else
			set temp2 to temp1 as integer
		end if
		set sumaDeImpares to sumaDeImpares + temp2
	end repeat
	
	set temp3 to (sumaDePares + sumaDeImpares) as string
	
	set temp4 to 10 - (temp3's item 2 as integer)
	
	if temp3's length > 1 then
		set temp4 to 10 - (temp3's item 2 as integer)
	else
		set temp4 to 10 - (temp3 as integer)
	end if
	
	if temp4 = 10 then set temp4 to 0
	
	set DCisNum to (random number from 0 to 1) = 0
	if DCisNum then -- generar el dígito de control como número
		set DC to temp4 as string
	else -- generar el dígito de control como letra
		set DC to mayusculas's item temp4
	end if
	
	letrasCIF's item (random number from 1 to letrasCIF's length) & numCentral & DC
end generateRandomCIF