vat_validator.vies

This module allows interaction with VIES (VAT Information Exchange Service) webservice. It can be used to validate VAT codes and fetch other information from the entity with that VAT.

Usage:

>>> check_vat('PT', '502011378')
CheckVATResult(country_code='PT',
               vat='502011378',
               request_date=datetime.date(2019, 6, 6),
               valid=True,
               name='UNIVERSIDADE DO MINHO',
               address='LG DO PACO\nBRAGA\n4700-320 BRAGA')

See also

VIES on the European Comission website: http://ec.europa.eu/taxation_customs/vies/

The functions hereby described operate on this WSDL url: http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl

class vat_validator.vies.CheckVATResult(country_code, vat, request_date, valid, name, address)[source]

Represents the result obtained by running the function check_vat().

Parameters
  • country_code (str) – IS0 3166 country code.

  • vat (str) – VAT number.

  • request_date (date) – date when this result was queried.

  • valid (bool) – whether this VAT is valid or not.

  • name (Optional[str]) – optional name of the entity associated with this VAT.

  • address (Optional[str]) – optional address of the entity associated with this VAT.

vat_validator.vies.check_vat(country_code, vat)[source]

Checks if given VAT code is valid and (if possible) fetches the name and address of the entity with that code.

Parameters
  • country_code (str) – valid IS0 3166 country code.

  • vat (str) – VAT number to check.

Return type

CheckVATResult

Returns

instance of CheckVATResult.

Raises

ValueError – when the country code is invalid or the VAT is empty.

vat_validator.utils

vat_validator.utils.countries_where_vat_is_valid(vat)[source]

Finds the countries where a given VAT code is valid.

Parameters

vat (str) – VAT number to validate.

Return type

List[str]

Returns

list of country codes where the given VAT is valid.

vat_validator.utils.sanitize_vat(vat)[source]

Sanitizes a given VAT code, removing the country prefix, whitespace and other non alphanumeric characters.

Parameters

vat (str) – VAT code to sanitize.

Return type

str

Returns

sanitized VAT code.

Example

>>> sanitize_vat('PT 502.011.378')
'502011378'
vat_validator.utils.validate_vat(country_code, vat)[source]

Validates a VAT number against the given country VAT format specification. It also takes care of sanitizing the VAT code, removing whitespaces and other invalid characters according to the country rules. It is not required for the VAT to start with the country code, so for example ‘PT 980405319’ and ‘980405319’ will both yield the same result.

Parameters
  • country_code (str) – valid IS0 3166 country code.

  • vat (str) – VAT number to validate.

Return type

bool

Returns

True if the given VAT is valid, False otherwise.

Raises

ValueError – when the country code is invalid.

vat_validator.countries

This module contains a set of functions to validate a VAT number according to a country VAT format rules. Besides the general regex match, the validation functions perform some country specific calculations on the digits using algorithms such as MOD 11 or Lunh’s algorithm.

All of these functions are in the format validate_vat_XX where XX is the IS0 3166 country code. They receive a string representing a VAT number in that country format and return True or False whether that code is valid or not.

Usage:

>>> validate_vat_pt('PT980405319')
True
>>> validate_vat_pt('PT-980 405 319')
True
>>> validate_vat_pt('80405319')
False

Each function is responsible to sanitize the input (remove preciding country code, spaces, punctuation, etc…)

See also

The list of VAT validation algorithms are published here: https://ec.europa.eu/taxation_customs/tin/

This wikipedia page contains a great overview of the different formats: https://en.wikipedia.org/wiki/VAT_identification_number

vat_validator.countries.EU_COUNTRY_CODES = ['AT', 'BE', 'BG', 'HR', 'CY', 'CZ', 'DE', 'DK', 'EE', 'EL', 'ES', 'FI', 'FR', 'GB', 'HU', 'IE', 'IT', 'LT', 'LU', 'LV', 'MT', 'NL', 'PL', 'PT', 'RO', 'SE', 'SI', 'SK']

List of european union country codes

vat_validator.countries.EU_RULES = {'AT': <function validate_vat_at>, 'BE': <function validate_vat_be>, 'BG': <function validate_vat_bg>, 'CY': <function validate_vat_cy>, 'CZ': <function validate_vat_cz>, 'DE': <function validate_vat_de>, 'DK': <function validate_vat_dk>, 'EE': <function validate_vat_ee>, 'EL': <function validate_vat_el>, 'ES': <function validate_vat_es>, 'FI': <function validate_vat_fi>, 'FR': <function validate_vat_fr>, 'GB': <function validate_vat_gb>, 'HR': <function validate_vat_hr>, 'HU': <function validate_vat_hu>, 'IE': <function validate_vat_ie>, 'IT': <function validate_vat_it>, 'LT': <function validate_vat_lt>, 'LU': <function validate_vat_lu>, 'LV': <function validate_vat_lv>, 'MT': <function validate_vat_mt>, 'NL': <function validate_vat_nl>, 'PL': <function validate_vat_pl>, 'PT': <function validate_vat_pt>, 'RO': <function validate_vat_ro>, 'SE': <function validate_vat_se>, 'SI': <function validate_vat_si>, 'SK': <function validate_vat_sk>}

Maps a country code to the respective country VAT rule

vat_validator.countries.validate_vat_at(vat)[source]

Validates a VAT number against austrian VAT format specification. In Austria is also named “Umsatzsteuer-Identifikationsnummer” (UID). The number must contain the letter ‘U’ followed by 8 digits and the last digit is the check digit.

Parameters

vat (str) – VAT number to validate.

Return type

bool

Returns

True if the given VAT is valid, False otherwise.

vat_validator.countries.validate_vat_be(vat)[source]

Validates a VAT number against belgian VAT format specification. In Belgium is also named “BTW identificatienummer” (BTW-nr). The number must contain 10 digits starting with 0 or 1. The old numbering schema had 9 digits, and is also accepted by this function.

Parameters

vat (str) – VAT number to validate.

Return type

bool

Returns

True if the given VAT is valid, False otherwise.

vat_validator.countries.validate_vat_bg(vat)[source]

Validates a VAT number against bulgarian VAT format specification. In Bulgary is also named “Identifikacionen nomer po DDS” (ДДС номер ). The number must contain 9 or 10 digits. It assumes one of four formats: “legal entities”, “physical persons”, “foreigners” and “miscellaneous”.

Parameters

vat (str) – VAT number to validate.

Return type

bool

Returns

True if the given VAT is valid, False otherwise.

vat_validator.countries.validate_vat_cy(vat)[source]

Validates a VAT number against cyprus VAT format specification. In Cyprus is also named “Arithmós Engraphḗs phi. pi. a.” (ΦΠΑ). The number must contain 8 digits followed by a letter that is used to check the number.

Parameters

vat (str) – VAT number to validate.

Return type

bool

Returns

True if the given VAT is valid, False otherwise.

vat_validator.countries.validate_vat_cz(vat)[source]

Validates a VAT number against czech republic VAT format specification. In Czech Republic is also named “Daňové identifikační číslo” (DIČ). The number must contain 8 to 10 digits depending if it is a legar entity or individual.

Parameters

vat (str) – VAT number to validate.

Return type

bool

Returns

True if the given VAT is valid, False otherwise.

vat_validator.countries.validate_vat_de(vat)[source]

Validates a VAT number against german VAT format specification. In Germany is also named “Umsatzsteuer-Identifikationsnummer” (USt-IdNr). The number must contain 9 digits and the first one cannot be 0.

Parameters

vat (str) – VAT number to validate.

Return type

bool

Returns

True if the given VAT is valid, False otherwise.

vat_validator.countries.validate_vat_dk(vat)[source]

Validates a VAT number against dannish VAT format specification. In Denmark is also named “Momsregistreringsnummer” (CVR). The number must contain 8 digits and the last digit is the check digit. It uses MOD 11 algorithm to calculate the check digit.

Parameters

vat (str) – VAT number to validate.

Return type

bool

Returns

True if the given VAT is valid, False otherwise.

vat_validator.countries.validate_vat_ee(vat)[source]

Validates a VAT number against estoniaon VAT format specification. In Estonia is also named “Käibemaksukohustuslase number” (KMKR). The number must contain 9 digits.

Parameters

vat (str) – VAT number to validate.

Return type

bool

Returns

True if the given VAT is valid, False otherwise.

vat_validator.countries.validate_vat_el(vat)[source]

Validates a VAT number against greece VAT format specification. In Greece is also named “Arithmós Forologikou Mētrṓou” (ΑΦΜ). The number must contain 9 digits and the last digit is the check digit. It uses MOD 11 algorithm to calculate the check digit.

Parameters

vat (str) – VAT number to validate.

Return type

bool

Returns

True if the given VAT is valid, False otherwise.

vat_validator.countries.validate_vat_fi(vat)[source]

Validates a VAT number against finnish VAT format specification. In Finland is also named “Arvonlisäveronumero” (AVL nro). The number must contain 8 digits and the last digit is the check digit. It uses MOD 11-2 algorithm to calculate the check digit.

Parameters

vat (str) – VAT number to validate.

Return type

bool

Returns

True if the given VAT is valid, False otherwise.

vat_validator.countries.validate_vat_fr(vat)[source]

Validates a VAT number against french VAT format specification. In France is also named “Numéro de TVA intracommunautaire” (TVA). The number must contain 2 control characters followed by 9 digits.

Parameters

vat (str) – VAT number to validate.

Return type

bool

Returns

True if the given VAT is valid, False otherwise.

vat_validator.countries.validate_vat_hr(vat)[source]

Validates a VAT number against croatian VAT format specification. In Croatia is also named “PDV Id. Broj OIB” (PDV-ID; OIB). The number must contain 11 digits and the last digit is the check digit. It uses MOD 11-10 algorithm to calculate the check digit.

Parameters

vat (str) – VAT number to validate.

Return type

bool

Returns

True if the given VAT is valid, False otherwise.

vat_validator.countries.validate_vat_hu(vat)[source]

Validates a VAT number against hungarian VAT format specification. In Hungary is also named “Közösségi adószám” (ΑNUM). The number must contain 8 digits and the last digit is the check digit.

Parameters

vat (str) – VAT number to validate.

Return type

bool

Returns

True if the given VAT is valid, False otherwise.

vat_validator.countries.validate_vat_ie(vat)[source]

Validates a VAT number against irish VAT format specification. In Ireland is also named “Value added tax identification no.” (VAT/CBL). The number must be in one of the following formats:

  • 7 digits and 1 letter, optionally followed by another letter

  • 1 digit, 1 letter or “+”, “*” and 5 digits and 1 letter

The number must contain 8 digits and the last digit is the check digit.

Parameters

vat (str) – VAT number to validate.

Return type

bool

Returns

True if the given VAT is valid, False otherwise.

vat_validator.countries.validate_vat_it(vat)[source]

Validates a VAT number against italien VAT format specification. In Italy is also named “Partita IVA” (P.IVA). The number must contain 11 digits and the last digit is the check digit. It uses Luhn Algorithm to calculate the check digit.

Parameters

vat (str) – VAT number to validate.

Return type

bool

Returns

True if the given VAT is valid, False otherwise.

vat_validator.countries.validate_vat_lt(vat)[source]

Validates a VAT number against lithuanian VAT format specification. In Lithuania is also named “Pridėtinės vertės mokestis” (PVM KODAS). The number must contain 9 or 12 digits.

Parameters

vat (str) – VAT number to validate.

Return type

bool

Returns

True if the given VAT is valid, False otherwise.

vat_validator.countries.validate_vat_lu(vat)[source]

Validates a VAT number against luxembourg VAT format specification. In Luxembourg is also named “Numéro d’identification à la taxe sur la valeur ajoutée” (No. TVA). The number must contain 8 digits and the last two digits are the check digits.

Parameters

vat (str) – VAT number to validate.

Return type

bool

Returns

True if the given VAT is valid, False otherwise.

vat_validator.countries.validate_vat_lv(vat)[source]

Validates a VAT number against latvian VAT format specification. In Latvia is also named “Pievienotās vērtības nodokļa” (PVN). The number must contain 11 digits and can be in one of the following formats:

  • 1st digit is bigger than 3, and so use a MOD 11 algorithm

  • 1st digit is 3, 2nd digit is 2, followed by 9 digits

  • 2 digits represent a day of month, followed by 2 digits that represent the month, followed by 2 digits that represent the year, followed by 1 digit equals to 0, 1 or 2, followed by 4 digits

Parameters

vat (str) – VAT number to validate.

Return type

bool

Returns

True if the given VAT is valid, False otherwise.

vat_validator.countries.validate_vat_mt(vat)[source]

Validates a VAT number against maltese VAT format specification. In Malta is also named “Vat reg. no.” (VAT No.). The number must contain 8 digits and the last two digits are the check digits. It uses MOD 37 algorithm to calculate the check digits.

Parameters

vat (str) – VAT number to validate.

Return type

bool

Returns

True if the given VAT is valid, False otherwise.

vat_validator.countries.validate_vat_nl(vat)[source]

Validates a VAT number against netherlands VAT format specification. In Netherlands is also named “Btw-nummer” Btw-nr.). The number must contain 9 digits followed by the letter ‘B’ followed by 2 digits. It uses MOD 11 algorithm to calculate the check digit.

Parameters

vat (str) – VAT number to validate.

Return type

bool

Returns

True if the given VAT is valid, False otherwise.

vat_validator.countries.validate_vat_pl(vat)[source]

Validates a VAT number against polish VAT format specification. In Poland is also named “numer identyfikacji podatkowej” (NIP). The number must contain 10 digits and the last digit is the check digit. It uses MOD 11 algorithm to calculate the check digit.

Parameters

vat (str) – VAT number to validate.

Return type

bool

Returns

True if the given VAT is valid, False otherwise.

vat_validator.countries.validate_vat_pt(vat)[source]

Validates a VAT number against portuguese VAT format specification. In Portugal is also named “Número de Identificação Fiscal” (NIF). The number must contain 9 digits and the last digit is the check digit. It uses MOD 11 algorithm to calculate the check digit.

Parameters

vat (str) – VAT number to validate.

Return type

bool

Returns

True if the given VAT is valid, False otherwise.

See also

https://pt.wikipedia.org/wiki/Número_de_identificação_fiscal

vat_validator.countries.validate_vat_ro(vat)[source]

Validates a VAT number against romanian VAT format specification. In Romania is also named “Codul de identificare fiscală” (CIF). The number must contain 2 to 10 digits and the last digit is the check digit. It uses MOD 11 algorithm to calculate the check digit.

Parameters

vat (str) – VAT number to validate.

Return type

bool

Returns

True if the given VAT is valid, False otherwise.

vat_validator.countries.validate_vat_se(vat)[source]

Validates a VAT number against swedish VAT format specification. In Sweden is also named “momsregistreringsnummer” (Momsnr). The number must contain 12 digits.

Parameters

vat (str) – VAT number to validate.

Return type

bool

Returns

True if the given VAT is valid, False otherwise.

vat_validator.countries.validate_vat_si(vat)[source]

Validates a VAT number against slovenian VAT format specification. In Slovenia is also named “Davčna številka” (ID za DDV). The number must contain 8 digits and the last digit is the check digit. It uses MOD 11 algorithm to calculate the check digit.

Parameters

vat (str) – VAT number to validate.

Return type

bool

Returns

True if the given VAT is valid, False otherwise.

vat_validator.countries.validate_vat_sk(vat)[source]

Validates a VAT number against slovakian VAT format specification. In Slovakia is also named “Identifikačné číslo pre daň z pridanej hodnoty” (IČ DPH). The number must contain 10 digits and be divisible by 11.

Parameters

vat (str) – VAT number to validate.

Return type

bool

Returns

True if the given VAT is valid, False otherwise.