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
- 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