8000 Support foreign residents for Romanian CNP · viggo-devries/python-stdnum@505521e · GitHub
[go: up one dir, main page]

Skip to content

Commit 505521e

Browse files
committed
Support foreign residents for Romanian CNP
This supports 7 or 8 as first digits in the CNP which are apparently used to identify foreign residents. This also changes the exception for an incorrect first digit from InvalidFormat to InvalidComponent which is a little clearer. Closes arthurdejong#230
1 parent 51a122d commit 505521e

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

stdnum/ro/cnp.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# cnp.py - functions for handling Romanian CNP numbers
22
# coding: utf-8
33
#
4-
# Copyright (C) 2012-2019 Arthur de Jong
4+
# Copyright (C) 2012-2020 Arthur de Jong
55
#
66
# This library is free software; you can redistribute it and/or
77
# modify it under the terms of the GNU Lesser General Public
@@ -23,12 +23,16 @@
2323
The CNP is a 13 digit number that includes information on the person's
2424
gender, birth date and country zone.
2525
26+
More information:
27+
28+
* https://ro.wikipedia.org/wiki/Cod_numeric_personal
29+
2630
>>> validate('1630615123457')
2731
'1630615123457'
28-
>>> validate('8800101221144') # invalid first digit
32+
>>> validate('0800101221142') # invalid first digit
2933
Traceback (most recent call last):
< E2AB code>3034
...
31-
InvalidFormat: ...
35+
InvalidComponent: ...
3236
>>> validate('1632215123457') # invalid date
3337
Traceback (most recent call last):
3438
...
@@ -52,8 +56,7 @@ def compact(number):
5256

5357

5458
def calc_check_digit(number):
55-
"""Calculate the check digit for personal codes. The number passed
56-
should not have the check digit included."""
59+
"""Calculate the check digit for personal codes."""
5760
# note that this algorithm has not been confirmed by an independent source
5861
weights = (2, 7, 9, 1, 4, 6, 3, 5, 8, 2, 7, 9)
5962
check = sum(w * int(n) for w, n in zip(weights, number)) % 11
@@ -79,9 +82,12 @@ def validate(number):
7982
"""Check if the number is a valid VAT number. This checks the length,
8083
formatting and check digit."""
8184
number = compact(number)
82-
# first digit should be a known one (9=foreigner)
83-
if not isdigits(number) or number[0] not in '1234569':
85+
if not isdigits(number):
8486
raise InvalidFormat()
87+
# first digit should be a known one
88+
# (7,8=foreign resident, 9=other foreigner but apparently only as NIF)
89+
if number[0] not in '123456789':
90+
raise InvalidComponent()
8591
if len(number) != 13:
8692
raise InvalidLength()
8793
# check if birth date is valid

0 commit comments

Comments
 (0)
0