|
| 1 | +# ifu.py - functions for handling Benin IFU numbers |
| 2 | +# coding: utf-8 |
| 3 | +# |
| 4 | +# Copyright (C) 2023 Leandro Regueiro |
| 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 | +"""IFU (Identifiant Fiscal Unique, Benin tax number). |
| 22 | +
|
| 23 | +This number consists of 13 digits. |
| 24 | +
|
| 25 | +The first digit indicates the type of entity: |
| 26 | + * 1: Individual male |
| 27 | + * 2: Individual female |
| 28 | + * 3: Legal entity / company |
| 29 | + * 4: Legal person / state structure |
| 30 | + * 5: Legal person / international organization and mission diplomatic |
| 31 | + * 6: Legal person / non-governmental organization |
| 32 | +
|
| 33 | +The following four digits give the year. The next six digits are a unique |
| 34 | +identifier within that year. |
| 35 | +
|
| 36 | +The next digit indicates either: |
| 37 | +
|
| 38 | + * 1: a parent company |
| 39 | + * 2-9: subsidiary or agencies |
| 40 | + * 0: other types of person or taxpayer |
| 41 | +
|
| 42 | +The final digit is a check digit, which is used to verify the number was |
| 43 | +correctly typed. |
| 44 | +
|
| 45 | +More information: |
| 46 | +
|
| 47 | +* http://www.finances.bj/sousSites/dgi/wp-content/uploads/2016/12/IFU.pdf |
| 48 | +
|
| 49 | +>>> validate('3201910583176') |
| 50 | +'3201910583176' |
| 51 | +>>> validate('3201 30109 9116') |
| 52 | +'3201301099116' |
| 53 | +>>> validate('12345') |
| 54 | +Traceback (most recent call last): |
| 55 | + ... |
| 56 | +InvalidLength: ... |
| 57 | +>>> format('3201 30109 9116') |
| 58 | +'3201301099116' |
| 59 | +""" # noqa: E501 |
| 60 | + |
| 61 | +from datetime import date |
| 62 | + |
| 63 | +from stdnum.exceptions import * |
| 64 | +from stdnum.util import clean, isdigits |
| 65 | + |
| 66 | + |
| 67 | +def compact(number): |
| 68 | + """Convert the number to the minimal representation. |
| 69 | +
|
| 70 | + This strips the number of any valid separators and removes surrounding |
| 71 | + whitespace. |
| 72 | + """ |
| 73 | + return clean(number, ' -').strip() |
| 74 | + |
| 75 | + |
| 76 | +def validate(number): |
| 77 | + """Check if the number is a valid Benin IFU number. |
| 78 | +
|
| 79 | + This checks the length and formatting. |
| 80 | + """ |
| 81 | + number = compact(number) |
| 82 | + if len(number) != 13: |
| 83 | + raise InvalidLength() |
| 84 | + if not isdigits(number): |
| 85 | + raise InvalidFormat() |
| 86 | + if number[0] not in ('1', '2', '3', '4', '5', '6'): |
| 87 | + raise InvalidComponent() |
| 88 | + if number[1:5] > date.today().strftime('%Y'): |
| 89 | + raise InvalidComponent() |
| 90 | + return number |
| 91 | + |
| 92 | + |
| 93 | +def is_valid(number): |
| 94 | + """Check if the number is a valid Benin IFU number.""" |
| 95 | + try: |
| 96 | + return bool(validate(number)) |
| 97 | + except ValidationError: |
| 98 | + return False |
| 99 | + |
| 100 | + |
| 101 | +def format(number): |
| 102 | + """Reformat the number to the standard presentation format.""" |
| 103 | + return compact(number) |
0 commit comments