Skip to content

Commit a870db1

Browse files
committed
Add accise - the french number for excise
1 parent 498c9c7 commit a870db1

File tree

3 files changed

+107
-3
lines changed

3 files changed

+107
-3
lines changed

stdnum/eu/excise.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,25 @@
4141

4242
from stdnum.eu.vat import MEMBER_STATES
4343
from stdnum.exceptions import *
44-
from stdnum.util import clean, get_soap_client
44+
from stdnum.util import clean, get_cc_module, get_soap_client
4545

4646

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

5052

53+
def _get_cc_module(cc):
54+
"""Get the Excise number module based on the country code."""
55+
cc = cc.lower()
56+
if cc not in MEMBER_STATES:
57+
raise InvalidComponent()
58+
if cc not in _country_modules:
59+
_country_modules[cc] = get_cc_module(cc, 'excise')
60+
return _country_modules[cc]
61+
62+
5163
def compact(number):
5264
"""Convert the number to the minimal representation. This strips the number
5365
of any valid separators and removes surrounding whitespace."""
@@ -59,10 +71,11 @@ def validate(number):
5971
"""Check if the number is a valid Excise number."""
6072
number = clean(number, ' ').upper().strip()
6173
cc = number[:2]
62-
if cc.lower() not in MEMBER_STATES:
63-
raise InvalidComponent()
6474
if len(number) != 13:
6575
raise InvalidLength()
76+
module = _get_cc_module(cc)
77+
if module:
78+
module.validate(number)
6679
return number
6780

6881

stdnum/fr/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@
2222

2323
# provide vat as an alias
2424
from stdnum.fr import tva as vat # noqa: F401
25+
# provide excise as an alias
26+
from stdnum.fr import accise as excise

stdnum/fr/accise.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# accise.py - functions for handling French Accise numbers
2+
# coding: utf-8
3+
#
4+
# Copyright (C) 2023 Cédric Krier
5+
#
6+
# This library is free software; you can redistribute it and/or
7+
# modify it under the terms of the GNU Lesser General Public
8+
# License as published by the Free Software Foundation; either
9+
# version 2.1 of the License, or (at your option) any later version.
10+
#
11+
# This library is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
# Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public
17+
# License along with this library; if not, write to the Free Software
18+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19+
# 02110-1301 USA
20+
21+
"""n° d'accise.
22+
23+
The n° d'accise always start by FR0 following by the 2 ending digits of the
24+
year, 3 number of customs office, one letter for the type and an ordering
25+
number of 4 digits.
26+
27+
>>> compact('FR0 23 004 N 9448')
28+
'FR023004N9448'
29+
>>> validate('FR023004N9448')
30+
'FR023004N9448'
31+
>>> validate('FR012907E0820')
32+
'FR012907E0820'
33+
34+
>>> validate('FR012345')
35+
Traceback (most recent call last):
36+
...
37+
InvalidLength: ...
38+
>>> validate('FR0XX907E0820')
39+
Traceback (most recent call last):
40+
...
41+
InvalidFormat: ...
42+
>>> validate('FR012XXXE0820')
43+
Traceback (most recent call last):
44+
...
45+
InvalidFormat: ...
46+
>>> validate('FR012907A0820')
47+
Traceback (most recent call last):
48+
...
49+
InvalidFormat: ...
50+
>>> validate('FR012907EXXXX')
51+
Traceback (most recent call last):
52+
...
53+
InvalidFormat: ...
54+
"""
55+
56+
from stdnum.exceptions import *
57+
from stdnum.util import clean, isdigits
58+
59+
OPERATORS = set(['E', 'N', 'C', 'B'])
60+
61+
def compact(number):
62+
"""Convert the number to the minimal representation. This strips the number
63+
of any valid separators and removes surrounding whitespace."""
64+
number = clean(number, ' ').upper().strip()
65+
return number
66+
67+
def validate(number):
68+
number = clean(number, ' ').upper().strip()
69+
code = number[:3]
70+
if code != 'FR0':
71+
raise InvalidFormat()
72+
if len(number) != 13:
73+
raise InvalidLength()
74+
if not isdigits(number[3:5]):
75+
raise InvalidFormat()
76+
if not isdigits(number[5:8]):
77+
raise InvalidFormat()
78+
if number[8] not in OPERATORS:
79+
raise InvalidFormat()
80+
if not isdigits(number[9:12]):
81+
raise InvalidFormat()
82+
return number
83+
84+
def is_valid(number):
85+
"""Check if the number is a valid accise number."""
86+
try:
87+
return bool(validate(number))
88+
except ValidationError:
89+
return False

0 commit comments

Comments
 (0)