lists.arthurdejong.org
RSS feed

Bug in Ecuador validation

[Date Prev][Date Next] [Thread Prev][Thread Next]

Bug in Ecuador validation



Hello

I try to validate the number: 1760143022001
This is a valid RUC number in my country, but your code not work with this number.

I attached the my code that has worked .

///////////////////////////////////////////////

def validar_ruc(ruc):
    if not ruc:
        raise ValidationError(u'Debe ingresar un RUC')
   
    if not len(ruc) == 13:
        raise ValidationError(u'RUC debe tener 13 digitos')

    # Consumidor Final    
    if ruc == '9999999999999':
        return True

    seis = False
    if ruc[2:3] == '9':
        coef = [4, 3, 2, 7, 6, 5, 4, 3, 2]
        verificador = int(ruc[9:10])
    elif ruc[2:3] == '6':
        seis = True
        coef = [3, 2, 7, 6, 5, 4, 3, 2]
        verificador = int(ruc[8:9])
    elif int(ruc[2:3]) <= 5:
        return validar_cedula(ruc[:10])
    else:
        raise ValidationError(u'RUC: Código verificador error')

    suma = 0
    for i in range(len(coef)):
        suma += int(ruc[i]) * coef[i]

    result = 11 - (suma > 0 and suma % 11 or 11)
    if result == verificador:
        return True
    else:
        if seis:
            # tratarlo como Persona natural
            return validar_cedula(ruc[:10])
        raise ValidationError(u'RUC: Código verificador error')

def validar_cedula(cedula):
    # print("Check Cedula")
    if not cedula:
        raise ValidationError(u'Debe ingresar una Cédula')

    if not len(cedula) == 10:
        raise ValidationError(u'Cédula debe tener 10 digitos')

    # Consumidor Final
    if cedula == '9999999999':
        return True

    coef = [2, 1, 2, 1, 2, 1, 2, 1, 2]
    ultimo = int(cedula[9:10])
    cedula = cedula[:9]
    suma = 0
    for i in range(len(coef)):
        val = int(cedula[i]) * coef[i]
        suma += val > 9 and val - 9 or val
    result = 10 - ((suma % 10) != 0 and suma % 10 or 10)
    if result == ultimo:
        return True
    else:
        raise ValidationError(u'Cédula: Código verificador error')