|
| 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