|
| 1 | +# ifu.py - functions for handling Burkina Faso 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 Financier Unique, Burkina Faso tax number). |
| 22 | +
|
| 23 | +This number consists of 9 characters, including a check character. |
| 24 | +
|
| 25 | +More information: |
| 26 | +
|
| 27 | +* https://www.impots.gov.bf/fileadmin/user_upload/storage/fichiers/Loi-058-portant-CODE-GENERAL-DES-IMPOTS-final.pdf |
| 28 | +* http://dgi.impots.gov.bf/services-en-ligne/ifu/page4_7.php |
| 29 | +
|
| 30 | +>>> validate('00014729V') |
| 31 | +'00014729V' |
| 32 | +>>> validate('0000 59 83 T') |
| 33 | +'00005983T' |
| 34 | +>>> validate('12345') |
| 35 | +Traceback (most recent call last): |
| 36 | + ... |
| 37 | +InvalidLength: ... |
| 38 | +>>> format('0000 59 83 T') |
| 39 | +'00005983T' |
| 40 | +""" # noqa: E501 |
| 41 | + |
| 42 | +from stdnum.exceptions import * |
| 43 | +from stdnum.util import clean, isdigits |
| 44 | + |
| 45 | + |
| 46 | +def compact(number): |
| 47 | + """Convert the number to the minimal representation. |
| 48 | +
|
| 49 | + This strips the number of any valid separators and removes surrounding |
| 50 | + whitespace. |
| 51 | + """ |
| 52 | + return clean(number, ' -').upper().strip() |
| 53 | + |
| 54 | + |
| 55 | +def validate(number): |
| 56 | + """Check if the number is a valid Burkina Faso IFU number. |
| 57 | +
|
| 58 | + This checks the length and formatting. |
| 59 | + """ |
| 60 | + number = compact(number) |
| 61 | + if len(number) != 9: |
| 62 | + raise InvalidLength() |
| 63 | + if not isdigits(number[:-1]): |
| 64 | + raise InvalidFormat() |
| 65 | + if not number[-1].isalpha(): |
| 66 | + raise InvalidFormat() |
| 67 | + return number |
| 68 | + |
| 69 | + |
| 70 | +def is_valid(number): |
| 71 | + """Check if the number is a valid Burkina Faso IFU number.""" |
| 72 | + try: |
| 73 | + return bool(validate(number)) |
| 74 | + except ValidationError: |
| 75 | + return False |
| 76 | + |
| 77 | + |
| 78 | +def format(number): |
| 79 | + """Reformat the number to the standard presentation format.""" |
| 80 | + return compact(number) |
0 commit comments