arthurdejong / python-stdnum

A Python library to provide functions to handle, parse and validate standard numbers.
https://arthurdejong.org/python-stdnum/
GNU Lesser General Public License v2.1
495 stars 205 forks source link

Flip values greece "gr" to "el" at VIES validation #256

Closed bapons closed 3 years ago

bapons commented 3 years ago

Flip greek codes "gr" -> "el"

As we know VIES greek code accepted is EL For that would be nice to flip values if we detect the code is GR before making the request to VIES wsdl.

The library already does something similar to get the module from the country of Greece but on the contrary.

Related code

def _get_cc_module(cc):
    """Get the VAT number module based on the country code."""
    # Greece uses a "wrong" country code
    cc = cc.lower()
    if cc == 'el':
        cc = 'gr'
    if cc not in MEMBER_STATES:
        return
    if cc == 'xi':
        cc = 'gb'
    if cc not in _country_modules:
        _country_modules[cc] = get_cc_module(cc, 'vat')
    return _country_modules[cc]

I was thinking of something like this Related code

def check_vies(number, timeout=30):  # pragma: no cover (not part of normal test suite)
    """Query the online European Commission VAT Information Exchange System
    (VIES) for validity of the provided number. Note that the service has
    usage limitations (see the VIES website for details). The timeout is in
    seconds. This returns a dict-like object."""
    # this function isn't automatically tested because it would require
    # network access for the tests and unnecessarily load the VIES website
    number = compact(number)
    if number[:2] == "GR":
        number = f"EL{number[2:]}"
    client = get_soap_client(vies_wsdl, timeout)
    return client.checkVat(number[:2], number[2:])
arthurdejong commented 3 years ago

I don't think that for example GR039868210 is a valid Greek VAT number while the same number starting with EL is considered valid. If a VAT number starting with GR is documented to be valid in some cases we should probably modify the compact() function to replace GR with EL

bapons commented 3 years ago

Totally true about GR03986821, is not a valid VAT number. I just thought could be a good idea to change values before call VIES service, just for Greece. Because it is an exception compared with the other countries.