Created
December 4, 2015 05:26
-
-
Save yogonza524/acb17acd4296d2afdc01 to your computer and use it in GitHub Desktop.
Función para validar un numero de CUIT segun el algoritmo impuesto para tal fin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION public.validate_cuit(cuit character varying) | |
RETURNS boolean AS | |
$BODY$ | |
DECLARE | |
cuit_array text[]; | |
serie integer[]; | |
aux integer; | |
result boolean; | |
BEGIN | |
result = false; | |
--Varifico que el cuit no sea nulo | |
IF (cuit IS NULL) THEN | |
RAISE EXCEPTION 'Debe ingresar un cuit. Solicitud rechazada'; | |
ELSE | |
--Verifico que el cuit este bien formado | |
IF (cuit ~ '^(\d){2}-(\d){8}-(\d)|(\d){11}') THEN | |
--Elimino los '-' si es necesario | |
IF (char_length(cuit) = 13) THEN | |
cuit = regexp_replace(cuit,'-','','g'); | |
END IF; | |
cuit_array = regexp_split_to_array(cuit,''); | |
serie = array[5,4,3,2,7,6,5,4,3,2]; | |
aux = 0; | |
FOR i IN 1..10 LOOP | |
aux = aux + cuit_array[i]::integer * serie[i]; | |
END LOOP; | |
aux = 11 - (aux % 11); | |
IF (aux = 11) THEN | |
aux = 0; | |
ELSE | |
IF (aux = 10) THEN | |
aux = 9; | |
END IF; | |
END IF; | |
IF (aux = cuit_array[11]::integer) THEN | |
result = true; | |
END IF; | |
ELSE | |
RAISE EXCEPTION 'CUIT con formato incorrecto. Solicitud rechazada.'; | |
END IF; | |
END IF; | |
return result; | |
END; | |
$BODY$ | |
LANGUAGE plpgsql VOLATILE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment