Skip to content

Add support for Guinea TIN #386

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/gn/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# __init__.py - collection of Guinea numbers
# coding: utf-8
#
# Copyright (C) 2023 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 Guinea numbers."""

# provide aliases
from stdnum.gn import nifp as vat # noqa: F401
90 changes: 90 additions & 0 deletions stdnum/gn/nifp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# nifp.py - functions for handling Guinea NIFp numbers
# coding: utf-8
#
# Copyright (C) 2023 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

"""NIFp (Numéro d'Identification Fiscale Permanent, Guinea tax number).

This number consists of 9 digits, usually separated into three groups
using hyphens to make it easier to read, like XXX-XXX-XXX.

The first eight digits are a sequence assigned in a pseudo-aleatory manner.

The last digit is the check digit, which is used to verify the number
was correctly typed, using the Luhn algorithm.

More information:

* https://dgi.gov.gn/wp-content/uploads/2022/09/N%C2%B0-12-Cahier-de-Charges-NIF-p.pdf

>>> validate('693770885')
'693770885'
>>> validate('693-770-885')
'693770885'
>>> validate('12345')
Traceback (most recent call last):
...
InvalidLength: ...
>>> validate('693770880')
Traceback (most recent call last):
...
InvalidChecksum: ...
>>> format('693770885')
'693-770-885'
""" # noqa: E501

from stdnum import luhn
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 and removes surrounding
whitespace.
"""
return clean(number, ' -').strip()


def validate(number):
"""Check if the number is a valid Guinea NIFp number.

This checks the length, formatting and check digit.
"""
number = compact(number)
if len(number) != 9:
raise InvalidLength()
if not isdigits(number):
raise InvalidFormat()
luhn.validate(number)
return number


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


def format(number):
"""Reformat the number to the standard presentation format."""
number = compact(number)
return '-'.join([number[:3], number[3:-3], number[-3:]])
278 changes: 278 additions & 0 deletions tests/test_gn_nifp.doctest
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
test_gn_nifp.doctest - more detailed doctests for stdnum.gn.nifp module

Copyright (C) 2023 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.gn.nifp module. It
tries to test more corner cases and detailed functionality that is not really
useful as module documentation.

>>> from stdnum.gn import nifp


Tests for some corner cases.

>>> nifp.validate('693770885')
'693770885'
>>> nifp.validate('693-770-885')
'693770885'
>>> nifp.format('693770885')
'693-770-885'
>>> nifp.validate('12345')
Traceback (most recent call last):
...
InvalidLength: ...
>>> nifp.validate('VV3456789')
Traceback (most recent call last):
...
InvalidFormat: ...
>>> nifp.validate('693770880')
Traceback (most recent call last):
...
InvalidChecksum: ...


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

>>> numbers = '''
...
... 864098611
... 255270027
... 664763828
... 833139827
... 203906862
... 982469827
... 568054498
... 834426504
... 975743923
... 793177023
... 762478154
... 938291994
... 699530101
... 677783854
... 229656939
... 447777913
... 619597412
... 457445468
... 286200803
... 171849813
... 370302309
... 645335456
... 796930261
... 410726301
... 735630923
... 875159337
... 579042839
... 780806089
... 410793046
... 168219525
... 299579169
... 565042967
... 489733675
... 493948053
... 842993172
... 242694412
... 276587276
... 896053261
... 589015205
... 291581551
... 249071515
... 139207336
... 892371576
... 784170151
... 515556629
... 376815395
... 390899623
... 519065411
... 316865609
... 765808340
... 739474005
... 775437791
... 123034571
... 203352125
... 526179486
... 585086473
... 172818015
... 890027683
... 265163162
... 249566852
... 438888018
... 165178989
... 899116669
... 314321431
... 163512015
... 853101202
... 258490101
... 698036902
... 146412911
... 946163276
... 739179950
... 806927760
... 991436239
... 475199857
... 703457069
... 270905136
... 852622687
... 374188258
... 512173220
... 294503776
... 914351234
... 617816558
... 447159617
... 547234278
... 358273308
... 339107195
... 177755154
... 634628101
... 925867079
... 433727930
... 775347206
... 273094276
... 343940110
... 178057717
... 587773839
... 281973404
... 257754374
... 722864204
... 215895707
... 326241312
... 461366700
... 672620879
... 908810518
... 102193364
... 616688727
... 381668276
... 234705127
... 877008771
... 647585900
... 658615315
... 407497502
... 429527492
... 597754480
... 415146935
... 969695261
... 311132112
... 664138476
... 126686484
... 955449905
... 540187069
... 538556184
... 912232667
... 137598496
... 201815446
... 270746290
... 739984243
... 271422958
... 435560800
... 549137867
... 330284803
... 104249479
... 113906614
... 137855094
... 157550823
... 185330651
... 197259856
... 258620392
... 261409544
... 289136574
... 289281768
... 290216472
... 322943275
... 617240353
... 626945182
... 630262566
... 632225991
... 633490883
... 634726517
... 639191436
... 657222618
... 666959549
... 720469097
... 786587667
... 787449685
... 806025516
... 826931636
... 830178893
... 875480923
... 877661041
... 887516623
... 909632895
... 927432484
... 958819708
... 999269178
... 538787201
... 157700758
... 196897417
... 202702692
... 242818490
... 336760889
... 333066967
... 369089545
... 379503667
... 422626143
... 496666249
... 530081389
... 582894515
... 602377368
... 622719409
... 656468998
... 681340105
... 728558354
... 812780294
... 824956379
... 834514606
... 902765809
... 960943835
... 964456891
... 972555296
... 977428416
... 326916780
... 286497227
... 200419646
... 309877405
... 839709987
... 436016539
... 780527677
... 841526031
... 442058939
... 218904506
... 416379998
... 962345336
... 687525204
... 572385698
... 908179583
... 458646338
... 331933168
... 569056062
... 107805368
... 853102234
... 102932480
... 402644983
... 436933428
... 281697813
... 925648545
... 691380299
... 645709031
... 638198374
... 999707235
... 653873455
... 645709031
...
... '''
>>> [x for x in numbers.splitlines() if x and not nifp.is_valid(x)]
[]