Skip to content

Add European Excise Number #424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions stdnum/eu/excise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# excise.py - functions for handling EU Excise numbers
# coding: utf-8
#
# Copyright (C) 2023 Cédric Krier
#
# 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

"""Excise Number

The Excise Number is the identification number issued by the competent
authority in respect of the person or premises.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a bit more detail on what this number is and what it's used for?


The first two letters are the ISO country code of the Member State where the
operator is located (e.g. LU);
The next 11 alphanumeric characters are the identifier of the operator.
The identifier must include 11 digits, shorter identifiers must be padded to
the left with zeroes (e.g. 00000987ABC)

More information:

* https://ec.europa.eu/taxation_customs/dds2/seed/help/seedhedn.jsp

>>> compact('LU 00000987ABC')
'LU00000987ABC'
>>> validate('LU00000987ABC')
'LU00000987ABC'
"""

from stdnum.eu.vat import MEMBER_STATES
from stdnum.exceptions import *
from stdnum.util import clean, get_cc_module, get_soap_client


_country_modules = dict()

seed_wsdl = 'https://ec.europa.eu/taxation_customs/dds2/seed/services/excise/verification?wsdl'
"""The WSDL URL of the System for Exchange of Excise Data (SEED)."""


def _get_cc_module(cc):
"""Get the Excise number module based on the country code."""
cc = cc.lower()
if cc not in MEMBER_STATES:
raise InvalidComponent()
if cc not in _country_modules:
_country_modules[cc] = get_cc_module(cc, 'excise')
return _country_modules[cc]


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()
return number


def validate(number):
"""Check if the number is a valid Excise number."""
number = clean(number, ' ').upper().strip()
cc = number[:2]
if len(number) != 13:
raise InvalidLength()
module = _get_cc_module(cc)
if module:
module.validate(number)
return number


def is_valid(number):
"""Check if the number is a valid Excise number."""
try:
return bool(validate(number))
except ValidationError:
return False


def check_seed(number, timeout=30): # pragma: no cover (not part of normal test suite)
"""Query the online European Commission System for Exchange of Excise Data
(SEED) 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."""
number = compact(number)
client = get_soap_client(seed_wsdl, timeout)
return client.verifyExcise(number)
3 changes: 2 additions & 1 deletion stdnum/fr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@

"""Collection of French numbers."""

# provide vat as an alias
# provide excise and vat as an alias
from stdnum.fr import accise as excise # noqa: F401
from stdnum.fr import tva as vat # noqa: F401
95 changes: 95 additions & 0 deletions stdnum/fr/accise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# accise.py - functions for handling French Accise numbers
# coding: utf-8
#
# Copyright (C) 2023 Cédric Krier
#
# 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

"""n° d'accise.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you improve this docstring a bit? For example: "n° d'accise (French number for ...)".


The n° d'accise always start by FR0 following by the 2 ending digits of the
year, 3 number of customs office, one letter for the type and an ordering
number of 4 digits.

>>> compact('FR0 23 004 N 9448')
'FR023004N9448'
>>> validate('FR023004N9448')
'FR023004N9448'
>>> validate('FR012907E0820')
'FR012907E0820'

>>> validate('FR012345')
Traceback (most recent call last):
...
InvalidLength: ...
>>> validate('FR0XX907E0820')
Traceback (most recent call last):
...
InvalidFormat: ...
>>> validate('FR012XXXE0820')
Traceback (most recent call last):
...
InvalidFormat: ...
>>> validate('FR012907A0820')
Traceback (most recent call last):
...
InvalidFormat: ...
>>> validate('FR012907EXXXX')
Traceback (most recent call last):
...
InvalidFormat: ...
"""

from stdnum.exceptions import *
from stdnum.util import clean, isdigits


OPERATORS = set(['E', 'N', 'C', 'B'])


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()
return number


def validate(number):
"""Check if the number is a valid accise number. This checks the length,
formatting."""
number = clean(number, ' ').upper().strip()
code = number[:3]
if code != 'FR0':
raise InvalidFormat()
if len(number) != 13:
raise InvalidLength()
if not isdigits(number[3:5]):
raise InvalidFormat()
if not isdigits(number[5:8]):
raise InvalidFormat()
if number[8] not in OPERATORS:
raise InvalidFormat()
if not isdigits(number[9:12]):
raise InvalidFormat()
return number


def is_valid(number):
"""Check if the number is a valid accise number."""
try:
return bool(validate(number))
except ValidationError:
return False
64 changes: 64 additions & 0 deletions tests/test_eu_excise.doctest
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
test_eu_excise.doctest - more detailed doctests for the stdnum.eu.excise module

Copyright (C) 2023 Cédric Krier

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

This file contains more detailed doctests for the stdnum.eu.excise module. It
tries to validate a number of Excise numbers that have been found online.

>>> from stdnum.eu import excise
>>> from stdnum.exceptions import *

These have been found online and should all be valid numbers.

>>> numbers = '''
...
... LU 00000987ABC
... FR012907E0820
... '''
>>> [x for x in numbers.splitlines() if x and not excise.is_valid(x)]
[]

The following numbers are wrong in one way or another. First we need a
function to be able to determine the kind of error.

>>> def caught(number, exception):
... try:
... excise.validate(number)
... return False
... except exception:
... return True
...


These numbers should be mostly valid except that they have the wrong length.

>>> numbers = '''
...
... LU987ABC
... '''
>>> [x for x in numbers.splitlines() if x and not caught(x, InvalidLength)]
[]

These numbers should be mostly valid except that they have the wrong prefix

>>> numbers = '''
...
... XX00000987ABC
... '''
>>> [x for x in numbers.splitlines() if x and not caught(x, InvalidComponent)]
[]
45 changes: 45 additions & 0 deletions tests/test_eu_excise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# test_eu_excise.py - functions for testing the online SEED validation
# coding: utf-8
#
# Copyright (C) 2023 Cédric Krier
#
# 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

# This is a separate test file because it should not be run regularly
# because it could negatively impact the SEED service.

"""Extra tests for the stdnum.eu.excise module."""

import os
import unittest

from stdnum.eu import excise


@unittest.skipIf(
not os.environ.get('ONLINE_TESTS'),
'Do not overload online services')
class TestSeed(unittest.TestCase):
"""Test the SEED web service provided by the European commission for
validation Excise numbers of European countries."""

def test_check_seed(self):
"""Test stdnum.eu.excise.check_seed()"""
result = excise.check_seed('FR012907E0820')
self.assertTrue('errorDescription' not in result)
self.assertTrue(len(result['result']) > 0)
first = result['result'][0]
self.assertEqual(first['excise'], 'FR012907E0820')