-
Notifications
You must be signed in to change notification settings - Fork 214
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
cedk
wants to merge
2
commits into
arthurdejong:master
Choose a base branch
from
cedk:eu-excise-number
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] | ||
[] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?