|
| 1 | +# steuernummer.py - functions for handling German tax numbers |
| 2 | +# coding: utf-8 |
| 3 | +# |
| 4 | +# Copyright (C) 2017 Holvi Payment Services |
| 5 | +# Copyright (C) 2018 Arthur de Jong |
| 6 | +# |
| 7 | +# This library is free software; you can redistribute it and/or |
| 8 | +# modify it under the terms of the GNU Lesser General Public |
| 9 | +# License as published by the Free Software Foundation; either |
| 10 | +# version 2.1 of the License, or (at your option) any later version. |
| 11 | +# |
| 12 | +# This library is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | +# Lesser General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU Lesser General Public |
| 18 | +# License along with this library; if not, write to the Free Software |
| 19 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 20 | +# 02110-1301 USA |
| 21 | + |
| 22 | +"""St.-Nr. (Steuernummer, German tax number). |
| 23 | +
|
| 24 | +The Steuernummer (St.-Nr.) is a tax number assigned by regional tax offices |
| 25 | +to taxable individuals and organisations. The number is being replaced by the |
| 26 | +Steuerliche Identifikationsnummer (IdNr). |
| 27 | +
|
| 28 | +The number has 10 or 11 digits for the regional form (per Bundesland) and 13 |
| 29 | +digits for the number that is unique within Germany. The number consists of |
| 30 | +(part of) the Bundesfinanzamtsnummer (BUFA-Nr.), a district number, a serial |
| 31 | +number and a check digit. |
| 32 | +
|
| 33 | +More information: |
| 34 | +
|
| 35 | +* https://de.wikipedia.org/wiki/Steuernummer |
| 36 | +
|
| 37 | +>>> validate(' 181/815/0815 5') |
| 38 | +'18181508155' |
| 39 | +>>> validate('201/123/12340', 'Sachsen') |
| 40 | +'20112312340' |
| 41 | +>>> validate('4151081508156', 'Thuringen') |
| 42 | +'4151081508156' |
| 43 | +>>> validate('4151181508156', 'Thuringen') |
| 44 | +Traceback (most recent call last): |
| 45 | + ... |
| 46 | +InvalidFormat: ... |
| 47 | +>>> validate('136695978') |
| 48 | +Traceback (most recent call last): |
| 49 | + ... |
| 50 | +InvalidLength: ... |
| 51 | +""" |
| 52 | + |
| 53 | +import re |
| 54 | + |
| 55 | +from stdnum.exceptions import * |
| 56 | +from stdnum.util import clean |
| 57 | + |
| 58 | + |
| 59 | +# The number formats per region (regional and country-wide format) |
| 60 | +_number_formats_per_region = { |
| 61 | + 'Baden-Württemberg': ['FFBBBUUUUP', '28FF0BBBUUUUP'], |
| 62 | + 'Bayern': ['FFFBBBUUUUP', '9FFF0BBBUUUUP'], |
| 63 | + 'Berlin': ['FFBBBUUUUP', '11FF0BBBUUUUP'], |
| 64 | + 'Brandenburg': ['0FFBBBUUUUP', '30FF0BBBUUUUP'], |
| 65 | + 'Bremen': ['FFBBBUUUUP', '24FF0BBBUUUUP'], |
| 66 | + 'Hamburg': ['FFBBBUUUUP', '22FF0BBBUUUUP'], |
| 67 | + 'Hessen': ['0FFBBBUUUUP', '26FF0BBBUUUUP'], |
| 68 | + 'Mecklenburg-Vorpommern': ['0FFBBBUUUUP', '40FF0BBBUUUUP'], |
| 69 | + 'Niedersachsen': ['FFBBBUUUUP', '23FF0BBBUUUUP'], |
| 70 | + 'Nordrhein-Westfalen': ['FFFBBBBUUUP', '5FFF0BBBBUUUP'], |
| 71 | + 'Rheinland-Pfalz': ['FFBBBUUUUP', '27FF0BBBUUUUP'], |
| 72 | + 'Saarland': ['0FFBBBUUUUP', '10FF0BBBUUUUP'], |
| 73 | + 'Sachsen': ['2FFBBBUUUUP', '32FF0BBBUUUUP'], |
| 74 | + 'Sachsen-Anhalt': ['1FFBBBUUUUP', '31FF0BBBUUUUP'], |
| 75 | + 'Schleswig-Holstein': ['FFBBBUUUUP', '21FF0BBBUUUUP'], |
| 76 | + 'Thüringen': ['1FFBBBUUUUP', '41FF0BBBUUUUP'], |
| 77 | +} |
| 78 | + |
| 79 | +REGIONS = sorted(_number_formats_per_region.keys()) |
| 80 | +"""Valid regions recognised by this module.""" |
| 81 | + |
| 82 | + |
| 83 | +def _clean_region(region): |
| 84 | + """Convert the region name to something that we can use for comparison |
| 85 | + without running into encoding issues.""" |
| 86 | + return ''.join( |
| 87 | + x for x in region.lower() |
| 88 | + if x in 'abcdefghijklmnopqrstvwxyz') |
| 89 | + |
| 90 | + |
| 91 | +class _Format(object): |
| 92 | + |
| 93 | + def __init__(self, fmt): |
| 94 | + self._fmt = fmt |
| 95 | + self._re = re.compile('^%s$' % re.sub( |
| 96 | + r'([FBUP])\1*', |
| 97 | + lambda x: r'(\d{%d})' % len(x.group(0)), fmt)) |
| 98 | + |
| 99 | + def match(self, number): |
| 100 | + return self._re.match(number) |
| 101 | + |
| 102 | + def replace(self, f, b, u, p): |
| 103 | + items = iter([f, b, u, p]) |
| 104 | + return re.sub(r'([FBUP])\1*', lambda x: next(items), self._fmt) |
| 105 | + |
| 106 | + |
| 107 | +# Convert the structure to something that we can easily use |
| 108 | +_number_formats_per_region = dict( |
| 109 | + (_clean_region(region), [ |
| 110 | + region, _Format(formats[0]), _Format(formats[1])]) |
| 111 | + for region, formats in _number_formats_per_region.items()) |
| 112 | + |
| 113 | + |
| 114 | +def _get_formats(region=None): |
| 115 | + """Return the formats for the region.""" |
| 116 | + if region: |
| 117 | + region = _clean_region(region) |
| 118 | + if region not in _number_formats_per_region: |
| 119 | + raise InvalidComponent() |
| 120 | + return [_number_formats_per_region[region]] |
| 121 | + return _number_formats_per_region.values() |
| 122 | + |
| 123 | + |
| 124 | +def compact(number): |
| 125 | + """Convert the number to the minimal representation. This strips the |
| 126 | + number of any valid separators and removes surrounding whitespace.""" |
| 127 | + return clean(number, ' -./,').strip() |
| 128 | + |
| 129 | + |
| 130 | +def validate(number, region=None): |
| 131 | + """Check if the number is a valid tax number. This checks the length and |
| 132 | + formatting. The region can be supplied to verify that the number is |
| 133 | + assi
F438
gned in that region.""" |
| 134 | + number = compact(number) |
| 135 | + if not number.isdigit(): |
| 136 | + raise InvalidFormat() |
| 137 | + if len(number) not in (10, 11, 13): |
| 138 | + raise InvalidLength() |
| 139 | + if not any(region_fmt.match(number) or country_fmt.match(number) |
| 140 | + for region, region_fmt, country_fmt in _get_formats(region)): |
| 141 | + raise InvalidFormat() |
| 142 | + return number |
| 143 | + |
| 144 | + |
| 145 | +def is_valid(number, region=None): |
| 146 | + """Check if the number is a valid tax number. This checks the length and |
| 147 | + formatting. The region can be supplied to verify that the number is |
| 148 | + assigned in that region.""" |
| 149 | + try: |
| 150 | + return bool(validate(number, region)) |
| 151 | + except ValidationError: |
| 152 | + return False |
| 153 | + |
| 154 | + |
| 155 | +def guess_regions(number): |
| 156 | + """Return a list of regions this number is valid for.""" |
| 157 | + number = compact(number) |
| 158 | + return sorted( |
| 159 | + region for region, region_fmt, country_fmt in _get_formats() |
| 160 | + if region_fmt.match(number) or country_fmt.match(number)) |
| 161 | + |
| 162 | + |
| 163 | +def to_regional_number(number): |
| 164 | + """Convert the number to a regional (10 or 11 digit) number.""" |
| 165 | + number = compact(number) |
| 166 | + for region, region_fmt, country_fmt in _get_formats(): |
| 167 | + m = country_fmt.match(number) |
| 168 | + if m: |
| 169 | + return region_fmt.replace(*m.groups()) |
| 170 | + raise InvalidFormat() |
| 171 | + |
| 172 | + |
| 173 | +def to_country_number(number, region=None): |
| 174 | + """Convert the number to the nationally unique number. The region is |
| 175 | + needed if the number is not only valid for one particular region.""" |
| 176 | + number = compact(number) |
| 177 | + formats = ( |
| 178 | + (region_fmt.match(number), country_fmt) |
| 179 | + for region, region_fmt, country_fmt in _get_formats(region)) |
| 180 | + formats = [ |
| 181 | + (region_match, country_fmt) |
| 182 | + for region_match, country_fmt in formats |
| 183 | + if region_match] |
| 184 | + if not formats: |
| 185 | + raise InvalidFormat() |
| 186 | + if len(formats) != 1: |
| 187 | + raise InvalidComponent() |
| 188 | + return formats[0][1].replace(*formats[0][0].groups()) |
| 189 | + |
| 190 | + |
| 191 | +def format(number, region=None): |
| 192 | + """Reformat the passed number to the standard format.""" |
| 193 | + number = compact(number) |
| 194 | + for region, region_fmt, country_fmt in _get_formats(region): |
| 195 | + m = region_fmt.match(number) |
| 196 | + if m: |
| 197 | + f, b, u, p = m.groups() |
| 198 | + return region_fmt.replace(f + '/', b + '/', u, p) |
| 199 | + return number |
0 commit comments