|
| 1 | +# ninea.py - functions for handling Senegal NINEA 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 | +"""NINEA (Numéro d'Identification Nationale des Entreprises et Associations, Senegal tax number). |
| 22 | +
|
| 23 | +The National Identification Number for Businesses and Associations (NINEA) is |
| 24 | +a unique tax identifier for tax purposes in Senegal. |
| 25 | +
|
| 26 | +This number consists of 7 digits and is usually followed by a 3 digit tax |
| 27 | +identification code called COFI (Code d’Identification Fiscale) that is used |
| 28 | +to indicate the company's tax status and legal structure. |
| 29 | +
|
| 30 | +More information: |
| 31 | +
|
| 32 | +* https://www.wikiprocedure.com/index.php/Senegal_-_Obtain_a_Tax_Identification_Number |
| 33 | +* https://nkac-audit.com/comprendre-le-ninea-votre-guide-de-lecture-simplifie/ |
| 34 | +* https://www.creationdentreprise.sn/rechercher-une-societe |
| 35 | +* https://www.dci-sn.sn/index.php/obtenir-son-ninea |
| 36 | +* https://e-ninea.ansd.sn/search_annuaire |
| 37 | +
|
| 38 | +>>> validate('306 7221') |
| 39 | +'3067221' |
| 40 | +>>> validate('30672212G2') |
| 41 | +'30672212G2' |
| 42 | +>>> validate('3067221 2G2') |
| 43 | +'30672212G2' |
| 44 | +>>> validate('1234567 0AZ') |
| 45 | +Traceback (most recent call last): |
| 46 | + ... |
| 47 | +InvalidComponent: ... |
| 48 | +>>> format('30672212G2') |
| 49 | +'3067221 2G2' |
| 50 | +""" # noqa: E501 |
| 51 | + |
| 52 | +from stdnum.exceptions import * |
| 53 | +from stdnum.util import clean, isdigits |
| 54 | + |
| 55 | + |
| 56 | +def compact(number: str) -> str: |
| 57 | + """Convert the number to the minimal representation. |
| 58 | +
|
| 59 | + This strips the number of any valid separators and removes surrounding |
| 60 | + whitespace. |
| 61 | + """ |
| 62 | + return clean(number, ' -/,').upper().strip() |
| 63 | + |
| 64 | + |
| 65 | +def _validate_cofi(number: str) -> None: |
| 66 | + # The first digit of the COFI indicates the tax status |
| 67 | + # 0: taxpayer subject to the real scheme, not subject to VAT. |
| 68 | + # 1: taxpayer subject to the single global contribution (TOU). |
| 69 | + # 2: taxpayer subject to the real scheme and subject to VAT. |
| 70 | + if number[0] not in '012': |
| 71 | + raise InvalidComponent() |
| 72 | + # The second character is a letter that indicates the tax centre: |
| 73 | + # A: Dakar Plateau 1 |
| 74 | + # B: Dakar Plateau 2 |
| 75 | + # C: Grand Dakar |
| 76 | + # D: Pikine |
| 77 | + # E: Rufisque |
| 78 | + # F: Thiès |
| 79 | + # G: Centre des grandes Entreprises |
| 80 | + # H: Louga |
| 81 | + # J: Diourbel |
| 82 | + # K: Saint-Louis |
| 83 | + # L: Tambacounda |
| 84 | + # M: Kaolack |
| 85 | + # N: Fatick |
| 86 | + # P: Ziguinchor |
| 87 | + # Q: Kolda |
| 88 | + # R: Prarcelles assainies |
| 89 | + # S: Professions libérales |
| 90 | + # T: Guédiawaye |
| 91 | + # U: Dakar-Medina |
| 92 | + # V: Dakar liberté |
| 93 | + # W: Matam |
| 94 | + # Z: Centre des Moyennes Entreprises |
| 95 | + if number[1] not in 'ABCDEFGHJKLMNPQRSTUVWZ': |
| 96 | + raise InvalidComponent() |
| 97 | + # The third character is a digit that indicates the legal form: |
| 98 | + # 1: Individual-Natural person |
| 99 | + # 2: SARL |
| 100 | + # 3: SA |
| 101 | + # 4: Simple Limited Partnership |
| 102 | + # 5: Share Sponsorship Company |
| 103 | + # 6: GIE |
| 104 | + # 7: Civil Society |
| 105 | + # 8: Partnership |
| 106 | + # 9: Cooperative Association |
| 107 | + # 0: Other |
| 108 | + if number[2] not in '0123456789': |
| 109 | + raise InvalidComponent() |
| 110 | + |
| 111 | + |
| 112 | +def validate(number: str) -> str: |
| 113 | + """Check if the number is a valid Senegal NINEA. |
| 114 | +
|
| 115 | + This checks the length and formatting. |
| 116 | + """ |
| 117 | + cofi = '' |
| 118 | + number = compact(number) |
| 119 | + if len(number) > 9: |
| 120 | + cofi = number[-3:] |
| 121 | + number = number[:-3] |
| 122 | + if len(number) not in (7, 9): |
| 123 | + raise InvalidLength() |
| 124 | + if not isdigits(number): |
| 125 | + raise InvalidFormat() |
| 126 | + if cofi: |
| 127 | + _validate_cofi(cofi) |
| 128 | + return number + cofi |
| 129 | + |
| 130 | + |
| 131 | +def is_valid(number: str) -> bool: |
| 132 | + """Check if the number is a valid Senegal NINEA.""" |
| 133 | + try: |
| 134 | + return bool(validate(number)) |
| 135 | + except ValidationError: |
| 136 | + return False |
| 137 | + |
| 138 | + |
| 139 | +def format(number: str) -> str: |
| 140 | + """Reformat the number to the standard presentation format.""" |
| 141 | + number = compact(number) |
| 142 | + if len(number) in (7, 9): |
| 143 | + return number |
| 144 | + return ' '.join([number[:-3], number[-3:]]) |
0 commit comments