diff options
author | Arthur de Jong <arthur@arthurdejong.org> | 2012-02-17 16:36:45 +0100 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2012-02-17 16:36:45 +0100 |
commit | d80344312116ffd7dd62c844288c59a7e11613ac (patch) | |
tree | 2fe1a815bc5387b4b57193f68db45a343f207eb0 /stdnum/cz/dic.py | |
parent | 0d2e4ccb842393e8d215795012e75e0554201075 (diff) |
add a DIČ (Daňové identifikační číslo, Czech VAT number) module
git-svn-id: http://arthurdejong.org/svn/python-stdnum/python-stdnum@132 9dea7c4f-944c-4273-ac1a-574ede026edc
Diffstat (limited to 'stdnum/cz/dic.py')
-rw-r--r-- | stdnum/cz/dic.py | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/stdnum/cz/dic.py b/stdnum/cz/dic.py new file mode 100644 index 0000000..6a61f55 --- /dev/null +++ b/stdnum/cz/dic.py @@ -0,0 +1,86 @@ +# dic.py - functions for handling Czech VAT numbers +# coding: utf-8 +# +# Copyright (C) 2012 Arthur de Jong +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301 USA + +"""Module for handling Czech DIČ (Daňové identifikační číslo, VAT) numbers. + +It is an 8, 9 or 10 digit code that includes a check digit and is used to +uniquely identify taxpayers for VAT (DPH in Czech). The number can refer +to legal entities (8 digit numbers), individuals with a RČ (the 9 or 10 +digit Czech birth number) or individuals without a RČ (9 digit numbers +that begin with a 6). + +>>> compact('CZ 25123891') +'25123891' +>>> is_valid('25123891') # legal entity +True +>>> is_valid('25123890') # incorrect check digit +False +>>> is_valid('7103192745') # RČ +True +>>> is_valid('640903926') # special case +True +""" + +from stdnum.cz import rc +from stdnum.util import clean + + +def compact(number): + """Convert the number to the minimal representation. This strips the + number of any valid separators and removes surrounding whitespace.""" + number = clean(number, ' /').upper().strip() + if number.startswith('CZ'): + number = number[2:] + return number + + +def calc_check_digit_legal(number): + """Calculate the check digit for 8 digit legal entities. The number + passed should not have the check digit included.""" + check = (11 - sum((8 - i) * int(n) for i, n in enumerate(number))) % 11 + return str((check or 1) % 10) + + +def calc_check_digit_special(number): + """Calculate the check digit for special cases. The number passed + should not have the first and last digits included.""" + check = (11 - sum((8 - i) * int(n) for i, n in enumerate(number))) % 11 + return str(9 - check % 10) + + +def is_valid(number): + """Checks to see if the number provided is a valid VAT number. This + checks the length, formatting and check digit.""" + try: + number = compact(number) + except: + return False + if not number.isdigit(): + return False + if len(number) == 8 and not number.startswith('9'): + # legal entities + return calc_check_digit_legal(number[:-1]) == number[-1] + if len(number) == 9 and number.startswith('6'): + # special cases (skip first digit in calculation) + return calc_check_digit_special(number[1:-1]) == number[-1] + if len(number) in (9, 10): + # 9 or 10 digit individual + return rc.is_valid(number) + return False |