fi.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. from __future__ import (
  15. unicode_literals,
  16. print_function,
  17. division
  18. )
  19. import re
  20. from .generic import GenericValidator
  21. class Validator(GenericValidator):
  22. """
  23. For rules see /docs/VIES-VAT Validation Routines-v15.0.doc
  24. """
  25. def __init__(self):
  26. self.regexp = re.compile(r'^\d{8}$')
  27. def validate(self, vat_number):
  28. if super(Validator, self).validate(vat_number) is False:
  29. return False
  30. vat_number = str(vat_number)
  31. checksum = int(vat_number[7])
  32. r = 11 - self.sum_weights([7,9,10,5,8,4,2], vat_number) % 11
  33. if r == 10:
  34. return False
  35. elif (r == 11 and checksum == 0) or checksum == r:
  36. return True
  37. else:
  38. return False