|
| 1 | +# nuit.py - functions for handling Mozambique NUIT numbers |
| 2 | +# coding: utf-8 |
| 3 | +# |
| 4 | +# Copyright (C) 2023 Leandro Regueiro |
| 5 | +# Copyright (C) 2025 Luca Sicurello |
| 6 | +# |
| 7 | +# This library is free software; you can redistribute it and/or |
| 8 | +# modify it under the terms of the GNU Lesser General Public |
| 9 | +# License as published by the Free Software Foundation; either |
| 10 | +# version 2.1 of the License, or (at your option) any later version. |
| 11 | +# |
| 12 | +# This library is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | +# Lesser General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU Lesser General Public |
| 18 | +# License along with this library; if not, write to the Free Software |
| 19 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 20 | +# 02110-1301 USA |
| 21 | + |
| 22 | +"""NUIT (Número Único de Identificação Tributaria, Mozambique tax number). |
| 23 | +
|
| 24 | +This number consists of 9 digits, sometimes separated in three groups of three |
| 25 | +digits using whitespace to make it easier to read. |
| 26 | +
|
| 27 | +The first digit indicates the type of entity. The next seven digits are a |
| 28 | +sequential number. The last digit is the check digit, which is used to verify |
| 29 | +the number was correctly typed. |
| 30 | +
|
| 31 | +More information: |
| 32 | +
|
| 33 | +* https://www.mobilize.org.mz/nuit-numero-unico-de-identificacao-tributaria/ |
| 34 | +* https://www.at.gov.mz/por/Perguntas-Frequentes2/NUIT |
| 35 | +
|
| 36 | +>>> validate('400339910') |
| 37 | +'400339910' |
| 38 | +>>> validate('400 005 834') |
| 39 | +'400005834' |
| 40 | +>>> validate('12345') |
| 41 | +Traceback (most recent call last): |
| 42 | + ... |
| 43 | +InvalidLength: ... |
| 44 | +>>> format('400339910') |
| 45 | +'400 339 910' |
| 46 | +""" |
| 47 | + |
| 48 | +from __future__ import annotations |
| 49 | + |
| 50 | +from stdnum.exceptions import * |
| 51 | +from stdnum.util import clean, isdigits |
| 52 | + |
| 53 | + |
| 54 | +def compact(number: str) -> str: |
| 55 | + """Convert the number to the minimal representation.""" |
| 56 | + return clean(number, ' -.').strip() |
| 57 | + |
| 58 | + |
| 59 | +def calc_check_digit(number: str) -> str: |
| 60 | + """Calculate the check digit.""" |
| 61 | + weights = (8, 9, 4, 5, 6, 7, 8, 9) |
| 62 | + check = sum(w * int(n) for w, n in zip(weights, number)) % 11 |
| 63 | + return '01234567891'[check] |
| 64 | + |
| 65 | + |
| 66 | +def validate(number: str) -> str: |
| 67 | + """Check if the number is a valid Mozambique NUIT number.""" |
| 68 | + number = compact(number) |
| 69 | + if len(number) != 9: |
| 70 | + raise InvalidLength() |
| 71 | + if not isdigits(number): |
| 72 | + raise InvalidFormat() |
| 73 | + if calc_check_digit(number[:-1]) != number[-1]: |
| 74 | + raise InvalidChecksum() |
| 75 | + return number |
| 76 | + |
| 77 | + |
| 78 | +def is_valid(number: str) -> bool: |
| 79 | + """Check if the number is a valid Mozambique NUIT number.""" |
| 80 | + try: |
| 81 | + return bool(validate(number)) |
| 82 | + except ValidationError: |
| 83 | + return False |
| 84 | + |
| 85 | + |
| 86 | +def format(number: str) -> str: |
| 87 | + """Reformat the number to the standard presentation format.""" |
| 88 | + number = compact(number) |
| 89 | + return ' '.join([number[:3], number[3:-3], number[-3:]]) |
0 commit comments