|
| 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 national des Entreprises et Associations, |
| 22 | +Senegal tax number). |
| 23 | +
|
| 24 | +This number consists of 7 digits (in practive it is sometimes 9 digits, the |
| 25 | +first two being zeroes). It is usually followed by a Tax Identification Code |
| 26 | +called COFI. |
| 27 | +
|
| 28 | +The COFI consists of 3 alphanumeric characters. The first one is a digit: |
| 29 | +
|
| 30 | +* 0: taxpayer subject to the real scheme, not subject to VAT. |
| 31 | +* 1: taxpayer subject to the single global contribution (TOU). |
| 32 | +* 2: taxpayer subject to the real scheme and subject to VAT. |
| 33 | +
|
| 34 | +The second character is a letter that indicates the tax center of attachment: |
| 35 | +
|
| 36 | +* A: Dakar Plateau 1. |
| 37 | +* B: Dakar Plateau 2. |
| 38 | +* C: Grand Dakar. |
| 39 | +* D: Pikine. |
| 40 | +* E: Rufisque. |
| 41 | +* F: Thiès. |
| 42 | +* G: Big Business Center. |
| 43 | +* H: Luga. |
| 44 | +* J: Diourbel. |
| 45 | +* K: Saint-Louis. |
| 46 | +* L: Tambacounda. |
| 47 | +* M: Kaolack. |
| 48 | +* N: Fatick. |
| 49 | +* P: A. |
| 50 | +* Q: Kolda. |
| 51 | +* R: remediated Parcels. |
| 52 | +* S: Liberal Professions. |
| 53 | +* T: Guédiawaye. |
| 54 | +* U: Dakar-Medina. |
| 55 | +* V: Dakar Freedom. |
| 56 | +* W: Matam. |
| 57 | +* Z: Medium Business Centre. |
| 58 | +
|
| 59 | +The third character is a digit that indicates the legal form of the taxpayer: |
| 60 | +
|
| 61 | +* 1: Individual-Natural person. |
| 62 | +* 2: SARL. |
| 63 | +* 3: SA. |
| 64 | +* 4: Simple Limited Partnership. |
| 65 | +* 5: Share Sponsorship Company. |
| 66 | +* 6: GIE. |
| 67 | +* 7: Civil Society. |
| 68 | +* 8: Partnership. |
| 69 | +* 9: Cooperative Association. |
| 70 | +* 0: Other. |
| 71 | +
|
| 72 | +More information: |
| 73 | +
|
| 74 | +* https://www.nkac-audit.com/en/comment-lire-un-numero-d-identifiant-fiscal-unique-ninea-au-senegal/ |
| 75 | +* https://audifiscsn.com/en/2021/12/11/savoir-bien-lire-le-ninea-peut-vous-eviter-des-redressements-fiscaux/ |
| 76 | +* https://www.creationdentreprise.sn/rechercher-une-societe |
| 77 | +
|
| 78 | +>>> validate('3067221') |
| 79 | +'3067221' |
| 80 | +>>> validate('30672212G2') |
| 81 | +'30672212G2' |
| 82 | +>>> validate('306 7221') |
| 83 | +'306 7221' |
| 84 | +>>> validate('3067221 2G2') |
| 85 | +'3067221 2G2' |
| 86 | +>>> validate('12345') |
| 87 | +Traceback (most recent call last): |
| 88 | + ... |
| 89 | +InvalidLength: ... |
| 90 | +>>> format('30672212G2') |
| 91 | +'3067221 2G2' |
| 92 | +""" # noqa: E501 |
| 93 | + |
| 94 | +from stdnum.exceptions import * |
| 95 | +from stdnum.util import clean, isdigits |
| 96 | + |
| 97 | + |
| 98 | +TAX_CENTERS = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', |
| 99 | + 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'Z') |
| 100 | + |
| 101 | + |
| 102 | +def compact(number): |
| 103 | + """Convert the number to the minimal representation. |
| 104 | +
|
| 105 | + This strips the number of any valid separators and removes surrounding |
| 106 | + whitespace. |
| 107 | + """ |
| 108 | + return clean(number, ' -/,').upper().strip() |
| 109 | + |
| 110 | + |
| 111 | +def validate(number): |
| 112 | + """Check if the number is a valid Senegal NINEA number. |
| 113 | +
|
| 114 | + This checks the length and formatting. |
| 115 | + """ |
| 116 | + number = compact(number) |
| 117 | + if len(number) not in (7, 9, 10, 12): |
| 118 | + raise InvalidLength() |
| 119 | + if len(number) in (7, 9): |
| 120 | + if not isdigits(number): |
| 121 | + raise InvalidFormat() |
| 122 | + return number |
| 123 | + if not isdigits(number[:-3]): |
| 124 | + raise InvalidFormat() |
| 125 | + if number[-3] not in ('0', '1', '2'): |
| 126 | + raise InvalidComponent() |
| 127 | + if number[-2] not in TAX_CENTERS: |
| 128 | + raise InvalidComponent() |
| 129 | + if not isdigits(number[-1]): |
| 130 | + raise InvalidComponent() |
| 131 | + return number |
| 132 | + |
| 133 | + |
| 134 | +def is_valid(number): |
| 135 | + """Check if the number is a valid Senegal NINEA number.""" |
| 136 | + try: |
| 137 | + return bool(validate(number)) |
| 138 | + except ValidationError: |
| 139 | + return False |
| 140 | + |
| 141 | + |
| 142 | +def format(number): |
| 143 | + """Reformat the number to the standard presentation format.""" |
| 144 | + number = compact(number) |
| 145 | + if len(number) in (7, 9): |
| 146 | + return number |
| 147 | + return ' '.join([number[:-3], number[-3:]]) |
0 commit comments