|
| 1 | +import re |
| 2 | +import pycountry |
| 3 | +from .registries import ViesRegistry |
| 4 | + |
| 5 | + |
| 6 | +WHITESPACE_EXPRESSION = re.compile('[\s\-]+') |
| 7 | +"""Whitespace expression. |
| 8 | +
|
| 9 | +Used for cleaning VAT numbers. |
| 10 | +""" |
| 11 | + |
| 12 | + |
| 13 | +VAT_NUMBER_EXPRESSIONS = { |
| 14 | + 'AT': re.compile(r'^\d{9}$'), |
| 15 | + 'BE': re.compile(r'^\d{10}$'), |
| 16 | + 'BG': re.compile(r'^\d{9,10}$'), |
| 17 | + 'CY': re.compile(r'^\d{8}[a-z]$', re.IGNORECASE), |
| 18 | + 'CZ': re.compile(r'^\d{8,10}$'), |
| 19 | + 'DE': re.compile(r'^\d{9}$'), |
| 20 | + 'DK': re.compile(r'^\d{8}$'), |
| 21 | + 'EE': re.compile(r'^\d{9}$'), |
| 22 | + 'EL': re.compile(r'^\d{9}$'), |
| 23 | + 'ES': re.compile(r'^[\da-z]\d{7}[\da-z]$', re.IGNORECASE), |
| 24 | + 'FI': re.compile(r'^\d{8}$'), |
| 25 | + 'FR': re.compile(r'^[\da-z]{2}\d{9}$', re.IGNORECASE), |
| 26 | + 'GB': re.compile(r'^(\d{9})|(\d{12})|(GD\d{3})|(HA\d{3})$', re.IGNORECASE), |
| 27 | + 'HU': re.compile(r'^\d{8}$'), |
| 28 | + 'IE': re.compile(r'^\d[\+\*\da-z]\d{5}[a-z]$', re.IGNORECASE), |
| 29 | + 'IT': re.compile(r'^\d{11}$'), |
| 30 | + 'LT': re.compile(r'^(\d{9})|(\d{12})$'), |
| 31 | + 'LU': re.compile(r'^\d{8}$'), |
| 32 | + 'LV': re.compile(r'^\d{11}$'), |
| 33 | + 'MT': re.compile(r'^\d{8}$'), |
| 34 | + 'NL': re.compile(r'^\d{9}B\d{3}$', re.IGNORECASE), |
| 35 | + 'PL': re.compile(r'^\d{10}$'), |
| 36 | + 'PT': re.compile(r'^\d{9}$'), |
| 37 | + 'RO': re.compile(r'^\d{2,10}$'), |
| 38 | + 'SE': re.compile(r'^\d{12}$'), |
| 39 | + 'SK': re.compile(r'^\d{10}$'), |
| 40 | +} |
| 41 | +"""VAT number expressions. |
| 42 | +
|
| 43 | +Mapping form ISO 3166-1-alpha-2 country codes to the whitespace-less expression |
| 44 | +a valid VAT number from the given country must match excluding the country code |
| 45 | +prefix. |
| 46 | +
|
| 47 | +EU VAT number structures are retrieved from `VIES |
| 48 | +<http://ec.europa.eu/taxation_customs/vies/faqvies.do>`_. |
| 49 | +""" |
| 50 | + |
| 51 | + |
| 52 | +VIES_REGISTRY = ViesRegistry() |
| 53 | +"""VIES registry instance. |
| 54 | +""" |
| 55 | + |
| 56 | + |
| 57 | +VAT_REGISTRIES = { |
| 58 | + 'AT': VIES_REGISTRY, |
| 59 | + 'BE': VIES_REGISTRY, |
| 60 | + 'BG': VIES_REGISTRY, |
| 61 | + 'CY': VIES_REGISTRY, |
| 62 | + 'CZ': VIES_REGISTRY, |
| 63 | + 'DE': VIES_REGISTRY, |
| 64 | + 'DK': VIES_REGISTRY, |
| 65 | + 'EE': VIES_REGISTRY, |
| 66 | + 'EL': VIES_REGISTRY, |
| 67 | + 'ES': VIES_REGISTRY, |
| 68 | + 'FI': VIES_REGISTRY, |
| 69 | + 'FR': VIES_REGISTRY, |
| 70 | + 'GB': VIES_REGISTRY, |
| 71 | + 'HU': VIES_REGISTRY, |
| 72 | + 'IE': VIES_REGISTRY, |
| 73 | + 'IT': VIES_REGISTRY, |
| 74 | + 'LT': VIES_REGISTRY, |
| 75 | + 'LU': VIES_REGISTRY, |
| 76 | + 'LV': VIES_REGISTRY, |
| 77 | + 'MT': VIES_REGISTRY, |
| 78 | + 'NL': VIES_REGISTRY, |
| 79 | + 'PL': VIES_REGISTRY, |
| 80 | + 'PT': VIES_REGISTRY, |
| 81 | + 'RO': VIES_REGISTRY, |
| 82 | + 'SE': VIES_REGISTRY, |
| 83 | + 'SK': VIES_REGISTRY, |
| 84 | +} |
| 85 | +"""VAT registries. |
| 86 | +
|
| 87 | +Mapping from ISO 3166-1-alpha-2 country codes to the VAT registry capable of |
| 88 | +validating the VAT number. |
| 89 | +""" |
| 90 | + |
| 91 | + |
| 92 | +def decompose_vat_number(vat_number, |
| 93 | + country_code=None): |
| 94 | + """Decompose a VAT number and an optional country code. |
| 95 | +
|
| 96 | + :param vat_number: VAT number. |
| 97 | + :param country_code: |
| 98 | + Optional country code. Default ``None`` prompting detection from the |
| 99 | + VAT number. |
| 100 | + :returns: |
| 101 | + a :class:`tuple` containing the VAT number and country code or |
| 102 | + ``(None, None)`` if decomposition failed. |
| 103 | + """ |
| 104 | + |
| 105 | + # Clean the VAT number. |
| 106 | + vat_number = WHITESPACE_EXPRESSION.sub('', vat_number).upper() |
| 107 | + |
| 108 | + # Attempt to determine the country code of the VAT number if possible. |
| 109 | + if not country_code: |
| 110 | + country_code = vat_number[0:2] |
| 111 | + if not pycountry.countries.get(alpha2=country_code): |
| 112 | + return (None, None) |
| 113 | + vat_number = vat_number[2:] |
| 114 | + elif vat_number[0:2] == country_code: |
| 115 | + vat_number = vat_number[2:] |
| 116 | + |
| 117 | + return vat_number, country_code |
| 118 | + |
| 119 | + |
| 120 | +def is_vat_number_format_valid(vat_number, |
| 121 | + country_code=None): |
| 122 | + """Test if the format of a VAT number is valid. |
| 123 | +
|
| 124 | + :param vat_number: VAT number to validate. |
| 125 | + :param country_code: |
| 126 | + Optional country code. Should be supplied if known, as there is no |
| 127 | + guarantee that naively entered VAT numbers contain the correct alpha-2 |
| 128 | + country code prefix for EU countries just as not all non-EU countries |
| 129 | + have a reliable country code prefix. Default ``None`` prompting |
| 130 | + detection. |
| 131 | + :returns: |
| 132 | + ``True`` if the format of the VAT number can be fully asserted as valid |
| 133 | + or ``False`` if not, otherwise ``None`` indicating that the VAT number |
| 134 | + format may or may not be valid. |
| 135 | + """ |
| 136 | + |
| 137 | + # Decompose the VAT number. |
| 138 | + vat_number, country_code = decompose_vat_number(vat_number, country_code) |
| 139 | + if not vat_number or not country_code: |
| 140 | + return False |
| 141 | + |
| 142 | + # Test the VAT number against an expression if possible. |
| 143 | + if not country_code in VAT_NUMBER_EXPRESSIONS: |
| 144 | + return None |
| 145 | + |
| 146 | + if not VAT_NUMBER_EXPRESSIONS[country_code].match(vat_number): |
| 147 | + return False |
| 148 | + |
| 149 | + return True |
| 150 | + |
| 151 | + |
| 152 | +def is_vat_number_valid(vat_number, |
| 153 | + country_code=None): |
| 154 | + """Test if a VAT number is valid. |
| 155 | +
|
| 156 | + If possible, the VAT number will be checked against available registries. |
| 157 | +
|
| 158 | + :param vat_number: VAT number to validate. |
| 159 | + :param country_code: |
| 160 | + Optional country code. Should be supplied if known, as there is no |
| 161 | + guarantee that naively entered VAT numbers contain the correct alpha-2 |
| 162 | + country code prefix for EU countries just as not all non-EU countries |
| 163 | + have a reliable country code prefix. Default ``None`` prompting |
| 164 | + detection. |
| 165 | + :returns: |
| 166 | + ``True`` if the VAT number can be fully asserted as valid or ``False`` |
| 167 | + if not, otherwise ``None`` indicating that the VAT number may or may |
| 168 | + not be valid. |
| 169 | + """ |
| 170 | + |
| 171 | + # Decompose the VAT number. |
| 172 | + vat_number, country_code = decompose_vat_number(vat_number, country_code) |
| 173 | + if not vat_number or not country_code: |
| 174 | + return False |
| 175 | + |
| 176 | + # Test the VAT number format. |
| 177 | + format_result = is_vat_number_format_valid(vat_number, country_code) |
| 178 | + if format_result is not True: |
| 179 | + return format_result |
| 180 | + |
| 181 | + # Attempt to check the VAT number against a registry. |
| 182 | + if country_code not in VAT_REGISTRIES: |
| 183 | + return None |
| 184 | + |
| 185 | + return VAT_REGISTRIES[country_code].is_vat_number_valid(vat_number, |
| 186 | + country_code) |
| 187 | + |
| 188 | + |
| 189 | +__all__ = ('is_vat_number_format_valid', 'is_vat_number_valid', ) |
0 commit comments