Skip to content

Add support for Algeria TIN number #313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions stdnum/dz/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# __init__.py - collection of Algerian numbers
# coding: utf-8
#
# Copyright (C) 2022 Leandro Regueiro
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA

"""Collection of Algerian numbers."""

# provide vat as an alias
from stdnum.dz import nif as vat # noqa: F401
99 changes: 99 additions & 0 deletions stdnum/dz/nif.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# pin.py - functions for handling Algeria NIF numbers
# coding: utf-8
#
# Copyright (C) 2022 Leandro Regueiro
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA

"""NIF, sometimes N.I.F. (Numéro d'Identification Fiscale, Algeria tax number).

The NIF was adopted by the Algerian tax authorities on 2006, replacing the NIS
number.

The NIF applies to physical persons, legal persons, legal entities,
administrative entities, local branches for foreign companies, associations,
professional organizations, etc.

The NIF consists of 15 digits, but sometimes it can be 20 digits long in order
to represent branches or secondary stablishments.

More information:

* http://www.jecreemonentreprise.dz/index.php?option=com_content&view=article&id=612&Itemid=463&lang=fr
* https://www.mf.gov.dz/index.php/fr/fiscalite
* https://cnrcinfo.cnrc.dz/numero-didentification-fiscale-nif/
* https://nifenligne.mfdgi.gov.dz/
* http://nif.mfdgi.gov.dz/nif.asp

>>> validate('416001000000007')
'416001000000007'
>>> validate('408 020 000 150 039')
'408020000150039'
>>> validate('41201600000606600001')
'41201600000606600001'
>>> validate('000 216 001 808 337 13010')
'00021600180833713010'
>>> validate('12345')
Traceback (most recent call last):
...
InvalidLength: ...
>>> validate('X1600100000000V')
Traceback (most recent call last):
...
InvalidFormat: ...
>>> format('408 020 000 150 039')
'408020000150039'
>>> format('000 216 001 808 337 13010')
'00021600180833713010'
"""

from stdnum.exceptions import *
from stdnum.util import clean, isdigits


def compact(number):
"""Convert the number to the minimal representation.

This strips the number of any valid separators, removes surrounding
whitespace.
"""
return clean(number, ' ')


def validate(number):
"""Check if the number is a valid Algeria NIF number.

This checks the length and formatting.
"""
number = compact(number)
if len(number) not in (15, 20):
raise InvalidLength()
if not isdigits(number):
raise InvalidFormat()
return number


def is_valid(number):
"""Check if the number is a valid Algeria NIF number."""
try:
return bool(validate(number))
except ValidationError:
return False


def format(number):
"""Reformat the number to the standard presentation format."""
return compact(number)
231 changes: 231 additions & 0 deletions tests/test_dz_nif.doctest
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
test_dz_nif.doctest - more detailed doctests for stdnum.dz.nif module

Copyright (C) 2022 Leandro Regueiro

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA


This file contains more detailed doctests for the stdnum.dz.nif module. It
tries to test more corner cases and detailed functionality that is not really
useful as module documentation.

>>> from stdnum.dz import nif


Tests for some corner cases.

>>> nif.validate('416001000000007')
'416001000000007'
>>> nif.validate('408 020 000 150 039')
'408020000150039'
>>> nif.validate('41201600000606600001')
'41201600000606600001'
>>> nif.validate('000 216 001 808 337 13010')
'00021600180833713010'
>>> nif.validate('12345')
Traceback (most recent call last):
...
InvalidLength: ...
>>> nif.validate('X1600100000000V')
Traceback (most recent call last):
...
InvalidFormat: ...
>>> nif.format('408 020 000 150 039')
'408020000150039'
>>> nif.format('000 216 001 808 337 13010')
'00021600180833713010'


These have been found online and should all be valid numbers.

>>> numbers = '''
...
... 416001000000007
... 000116001560982
... 000416096544827
... 408 020 000 150 039
... 000016001381120
... 000 216 001 808 337
... 408020000060031
... 000216002104442
... 000216001808337
... 000007019004069
... 000 216 001 808 337 13010
... 001125069042347
... 408020001100031
... 411020000240004
... 408002000000065
... 000616097268506
... 000716097805474
... 408020000020098
... 41201600000606600001
... 408020000290087
... 000416096547023
... 001309109031646
... 001431039004061
... 000116180849545
... 408020000310039
... 420016000090015
... 408020000210125
... 001116098865826
... 000716097425528
... 408020000160863
... 000016001159195
... 098919015000337
... 00131 6099242493
... 410007000000046
... 000216299033049
... 420110400000042
... 001519019003549
... 412014000190014
... 408008000100033
... 001816104587248
... 001125019041843
... 099816000344301
... 417180000000088
... 000848019007735
... 40802100000204900000
... 000316096228742
... 001616104328611
... 002131011846676
... 408015000017094
... 00021 600 180 833 716 001
... 410011000000012
... 198015500011640
... 001707024366917
... 099925006295010
... 099916000816810
... 000716097425528
... 000816097716198
... 000347019004646
... 181050400060195
... 099815019058902
... 099915004348016
... 164151000512151
... 001609019033838
... 099919008329067
... 000 116 180 807 261
... 157092600693479
... 408004000000082
... 002016101606088
... 27216010518015900000
... 179423501014107
... 169470500236104
... 001730019009056
... 198847070005037
... 408015000043003
... 099807024211756
... 000607024268702
... 000516097078853
... 000016001358124
... 000416096783683
... 099915004292220
... 099916000598324
... 000135072346001
... 001225006964374
... 000735072494667
... 001025006891914
... 00001600137674493006
... 00001600137674493019
... 00001600137674493014
... 00001600137674493005
... 00001600137674493002
... 001206018759104
... 000336068252339
... 000423036376676
... 000307024248170
... 000906018632115
... 099931010410317
... 099747086204339
... 000825006783595
... 000624038263530
... 000434046319884
... 0000 1600 15 01 289
... 196 816 040 011 445
... 001 109 199 007 345
... 195 916 010 107 542
... 194 916 010 095 431
... 197 919 010 321 535
... 165 431 400 300 157
... 797 435 379 003 601
... 097524019047421
... 000245006625193
... 152431400682135
... 000505022347781
... 180240104351164
... 000724038267478
... 000824038269319
... 000428056270862
... 197305380027237
... 001341050285855
... 198805420009824
... 099824038206619
... 099816000499785
... 001513026489736
... 287160700030197
... 000116001567707
... 000016019034356
... 197016180012728
... 001316100750533
... 295161704436174
... 197816180459711
... 195213040012847
... 000619019037344
... 182162000621184
... 000116001524660
... 099916001064029
... 287160700030597
... 000516096872520
... 198018210116342
... 000116001567707
... 153160105528127
... 099935072285348
... 165161701380190
... 001216209014745
... 000516100938845
... 000016001300865
... 287160700030197
... 000216002133054
... 189161702283168
... 001316099262097
... 185160201610152
... 295161704436174
... 000816097696555
... 001216209014175
... 001116099033052
... 000916098321803
... 193160600100148
... 001216098929656
... 000516096872520
... 153160105528127
... 000516096825183
... 099916029015224
... 169164800432122
... 000016001300865
... 000516096980536
... 00131609936785400000
... 001316100750513
... 099716000280672
... 000616097459024
... 099916000745814
... 001716104401413
... 196416140076038
... 000816097696555
... 171161800210177
...
... '''
>>> [x for x in numbers.splitlines() if x and not nif.is_valid(x)]
[]