it.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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{11}$')
  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[10])
  32. c8910 = int(vat_number[7:10])
  33. if c8910 not in range(1, 102) and c8910 not in [120, 121, 999, 888]:
  34. return False
  35. s1 = 0
  36. for i in range(0,9,2):
  37. s1 = s1 + int(vat_number[i])
  38. s2 = 0
  39. for i in range(1,10,2):
  40. n = int(vat_number[i])
  41. s2 = s2 + int(n/5) + (2 * n) % 10
  42. checkval = (10 - (s1 + s2) % 10) % 10
  43. return checksum == checkval