2012-06-08 00:08:47 +08:00
|
|
|
from django.utils.encoding import smart_unicode
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2007-04-17 21:37:45 +08:00
|
|
|
def ssn_check_digit(value):
|
|
|
|
"Calculate Italian social security number check digit."
|
|
|
|
ssn_even_chars = {
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8,
|
|
|
|
'9': 9, 'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6, 'H': 7,
|
|
|
|
'I': 8, 'J': 9, 'K': 10, 'L': 11, 'M': 12, 'N': 13, 'O': 14, 'P': 15,
|
|
|
|
'Q': 16, 'R': 17, 'S': 18, 'T': 19, 'U': 20, 'V': 21, 'W': 22, 'X': 23,
|
|
|
|
'Y': 24, 'Z': 25
|
2007-04-17 21:37:45 +08:00
|
|
|
}
|
|
|
|
ssn_odd_chars = {
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
'0': 1, '1': 0, '2': 5, '3': 7, '4': 9, '5': 13, '6': 15, '7': 17, '8':
|
|
|
|
19, '9': 21, 'A': 1, 'B': 0, 'C': 5, 'D': 7, 'E': 9, 'F': 13, 'G': 15,
|
|
|
|
'H': 17, 'I': 19, 'J': 21, 'K': 2, 'L': 4, 'M': 18, 'N': 20, 'O': 11,
|
|
|
|
'P': 3, 'Q': 6, 'R': 8, 'S': 12, 'T': 14, 'U': 16, 'V': 10, 'W': 22,
|
|
|
|
'X': 25, 'Y': 24, 'Z': 23
|
2007-04-17 21:37:45 +08:00
|
|
|
}
|
|
|
|
# Chars from 'A' to 'Z'
|
|
|
|
ssn_check_digits = [chr(x) for x in range(65, 91)]
|
|
|
|
|
|
|
|
ssn = value.upper()
|
|
|
|
total = 0
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
for i in range(0, 15):
|
2007-04-17 21:37:45 +08:00
|
|
|
try:
|
|
|
|
if i % 2 == 0:
|
|
|
|
total += ssn_odd_chars[ssn[i]]
|
|
|
|
else:
|
|
|
|
total += ssn_even_chars[ssn[i]]
|
|
|
|
except KeyError:
|
|
|
|
msg = "Character '%(char)s' is not allowed." % {'char': ssn[i]}
|
|
|
|
raise ValueError(msg)
|
|
|
|
return ssn_check_digits[total % 26]
|
|
|
|
|
|
|
|
def vat_number_check_digit(vat_number):
|
|
|
|
"Calculate Italian VAT number check digit."
|
2012-06-08 00:08:47 +08:00
|
|
|
normalized_vat_number = smart_unicode(vat_number).zfill(10)
|
2007-04-17 21:37:45 +08:00
|
|
|
total = 0
|
|
|
|
for i in range(0, 10, 2):
|
|
|
|
total += int(normalized_vat_number[i])
|
|
|
|
for i in range(1, 11, 2):
|
|
|
|
quotient , remainder = divmod(int(normalized_vat_number[i]) * 2, 10)
|
|
|
|
total += quotient + remainder
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return smart_unicode((10 - total % 10) % 10)
|