1
1
# vat.py - functions for handling Cypriot VAT numbers
2
2
# coding: utf-8
3
3
#
4
- # Copyright (C) 2012 Arthur de Jong
4
+ # Copyright (C) 2012, 2013 Arthur de Jong
5
5
#
6
6
# This library is free software; you can redistribute it and/or
7
7
# modify it under the terms of the GNU Lesser General Public
25
25
26
26
>>> compact('CY-10259033P')
27
27
'10259033P'
28
- >>> is_valid('CY-10259033P ')
29
- True
30
- >>> is_valid('CY-10259033Z') # invalid check digit
31
- False
28
+ >>> validate('CY-10259033P ')
29
+ '10259033P'
30
+ >>> validate('CY-10259033Z') # invalid check digit
31
+ Traceback (most recent call last):
32
+ ...
33
+ InvalidChecksum: ...
32
34
"""
33
35
36
+ from stdnum .exceptions import *
34
37
from stdnum .util import clean
35
38
36
39
@@ -56,13 +59,25 @@ def calc_check_digit(number):
56
59
) % 26 ]
57
60
58
61
62
+ def validate (number ):
63
+ """Checks to see if the number provided is a valid VAT number. This
64
+ checks the length, formatting and check digit."""
65
+ number = compact (number )
66
+ if not number [:- 1 ].isdigit ():
67
+ raise InvalidFormat ()
68
+ if len (number ) != 9 :
69
+ raise InvalidLength ()
70
+ if number [0 :2 ] == '12' :
71
+ raise InvalidComponent ()
72
+ if number [- 1 ] != calc_check_digit (number [:- 1 ]):
73
+ raise InvalidChecksum ()
74
+ return number
75
+
76
+
59
77
def is_valid (number ):
60
78
"""Checks to see if the number provided is a valid VAT number. This
61
79
checks the length, formatting and check digit."""
62
80
try :
63
- number = compact ( number )
64
- except :
81
+ return bool ( validate ( number ) )
82
+ except ValidationError :
65
83
return False
66
- return len (number ) == 9 and number [:- 1 ].isdigit () and \
67
- number [0 :2 ] != '12' and \
68
- number [- 1 ] == calc_check_digit (number [:- 1 ])
0 commit comments