Ejemplo n.º 1
0
    def check_vat_nl(self, vat):
        """
        Temporary Netherlands VAT validation to support the new format introduced in January 2020,
        until upstream is fixed.

        Algorithm detail: http://kleineondernemer.nl/index.php/nieuw-btw-identificatienummer-vanaf-1-januari-2020-voor-eenmanszaken

        TODO: remove when fixed upstream
        """

        try:
            from stdnum.util import clean
            from stdnum.nl.bsn import checksum
        except ImportError:
            return True

        vat = clean(vat, ' -.').upper().strip()

        # Remove the prefix
        if vat.startswith("NL"):
            vat = vat[2:]

        if not len(vat) == 12:
            return False

        # Check the format
        match = self.__check_vat_nl_re.match(vat)
        if not match:
            return False

        # Match letters to integers
        char_to_int = {k: str(ord(k) - 55) for k in string.ascii_uppercase}
        char_to_int['+'] = '36'
        char_to_int['*'] = '37'

        # 2 possible checks:
        # - For natural persons
        # - For non-natural persons and combinations of natural persons (company)

        # Natural person => mod97 full checksum
        check_val_natural = '2321'
        for x in vat:
            check_val_natural += x if x.isdigit() else char_to_int[x]
        if int(check_val_natural) % 97 == 1:
            return True

        # Company => weighted(9->2) mod11 on bsn
        vat = vat[:-3]
        if vat.isdigit() and checksum(vat) == 0:
            return True

        return False
Ejemplo n.º 2
0
def validate(number):
    """Check if the number is a valid onderwijsnummer. This checks the length
    and whether the check digit is correct and whether it starts with the
    right sequence."""
    number = compact(number)
    if not number.isdigit() or int(number) <= 0:
        raise InvalidFormat()
    if not number.startswith('10'):
        raise InvalidFormat()
    if len(number) != 9:
        raise InvalidLength()
    if checksum(number) != 5:
        raise InvalidChecksum()
    return number
def validate(number):
    """Check if the number is a valid onderwijsnummer. This checks the length
    and whether the check digit is correct and whether it starts with the
    right sequence."""
    number = compact(number)
    if not number.isdigit() or int(number) <= 0:
        raise InvalidFormat()
    if not number.startswith('10'):
        raise InvalidFormat()
    if len(number) != 9:
        raise InvalidLength()
    if checksum(number) != 5:
        raise InvalidChecksum()
    return number