at.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Copyright 2018 Agile Geeks
  2. # Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  3. # and associated documentation files (the "Software"), to deal in the Software without restriction,
  4. # including without limitation the rights to use, copy, modify, merge, publish, distribute,
  5. # sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
  6. # is furnished to do so, subject to the following conditions:
  7. # The above copyright notice and this permission notice shall be included in all copies or substantial
  8. # portions of the Software.
  9. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
  10. # LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  11. # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  12. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  13. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  14. import re
  15. from .generic import GenericValidator
  16. class Validator(GenericValidator):
  17. """
  18. For rules see /docs/VIES-VAT Validation Routines-v15.0.doc
  19. """
  20. def __init__(self):
  21. self.regexp = re.compile(r'^U\d{8}$', re.IGNORECASE)
  22. def validate(self, vat_number):
  23. if super(Validator, self).validate(vat_number) is False:
  24. return False
  25. vat_number = str(vat_number)
  26. checknum = int (vat_number[8])
  27. r = 0
  28. for i in range(3,8,2):
  29. digit = int (vat_number[i-1])
  30. r = r + ( int( digit/5 ) + (digit*2)%10 )
  31. c = 4
  32. for i in range(2,9,2):
  33. c = c + int (vat_number[i-1])
  34. c9 = (10 - (r+c)%10) % 10
  35. return checknum == c9