Validating Greek VAT code (έλεγχος Α.Φ.Μ)

Sun 17 June 2007 | tags: python
I recently had to validate the VAT code entered in an internetl Django application. Searching around lead me to this site. The implementation there is Visual Basic though, which wouldn’t suit me. So i translated it to Python as best as i could (given i don’t actually know any VB :p )
Here it is:
def isValidAFM(afm):
    if len(afm) != 9:
        return False

    try:
        int(afm)
    except ValueError:
        return False

    AFMpart = afm[:-1]
    afmsum = 0
    multiplier = 2
    for c in AFMpart[::-1]:

        afmsum += ( int(c) * multiplier )
        multiplier *= 2

    if not afmsum:
        return False

    afmmod = afmsum % 11

    lastdigit = int(afm[-1])
    if lastdigit == afmmod:
        return True
    elif (lastdigit == 0) and (afmmod ==10 ):
        return True
    else:
        return False

Caveats:
  • I don’t know if the actual algorithm is valid since my search for official documentation turned up nothing. It seems to be accepted by a large on-line community of accountants though, and that’s good enough for me.
  • I have not tested it much. You at your own risk, as always

Comments !

blogroll

social