Skip to content

Commit 2478483

Browse files
oboratavarthurdejong
authored andcommitted
Add British Columbia PHN
Closes #421
1 parent 58d6283 commit 2478483

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

stdnum/ca/bc_phn.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# bc_phn.py - functions for handling British Columbia Personal Health Numbers (PHNs)
2+
# coding: utf-8
3+
#
4+
# Copyright (C) 2023 Ömer Boratav
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+
"""BC PHN (British Columbia Personal Health Number).
22+
23+
A unique, numerical, lifetime identifier used in the specific identification
24+
of an individual client or patient who has had any interaction with the
25+
British Columbia health system. It is assigned only to and used by one person
26+
and will not be assigned to any other person.
27+
28+
The existence of a PHN does not imply eligibility for health care services in
29+
BC or provide any indication of an individual’s benefit status.
30+
31+
The PNH is a 10-digit number where the first digit is always 9, and the last
32+
digit is a MOD-11 check digit.
33+
34+
More information:
35+
36+
* https://www2.gov.bc.ca/gov/content/health/health-drug-coverage/msp/bc-residents/personal-health-identification
37+
* https://www2.gov.bc.ca/assets/gov/health/practitioner-pro/software-development-guidelines/conformance-standards/vol-4b-app-rules-client-registry.pdf
38+
39+
40+
>>> validate('9698 658 215')
41+
'9698658215'
42+
>>> format('9698658215')
43+
'9698 658 215'
44+
>>> validate('9698648215')
45+
Traceback (most recent call last):
46+
...
47+
InvalidChecksum: ...
48+
>>> validate('5736504210')
49+
Traceback (most recent call last):
50+
...
51+
InvalidComponent: ...
52+
>>> validate('9736A04212')
53+
Traceback (most recent call last):
54+
...
55+
InvalidFormat: ...
56+
""" # noqa: E501
57+
58+
59+
from stdnum.exceptions import *
60+
from stdnum.util import clean, isdigits
61+
62+
63+
def compact(number):
64+
"""Convert the number to the minimal representation. This strips the
65+
number of any valid separators and removes surrounding whitespace."""
66+
return clean(number, '- ').strip()
67+
68+
69+
def calc_check_digit(number):
70+
"""Calculate the check digit. The number passed should not have the check
71+
digit included."""
72+
weights = (2, 4, 8, 5, 10, 9, 7, 3)
73+
s = sum((w * int(n)) % 11 for w, n in zip(weights, number))
74+
return str((11 - s) % 11)
75+
76+
77+
def validate(number):
78+
"""Check if the number is a valid PHN. This checks the length,
79+
formatting and check digit."""
80+
number = compact(number)
81+
if len(number) != 10:
82+
raise InvalidLength()
83+
if not isdigits(number):
84+
raise InvalidFormat()
85+
if number[0] != '9':
86+
raise InvalidComponent()
87+
if number[9] != calc_check_digit(number[1:9]):
88+
raise InvalidChecksum()
89+
return number
90+
91+
92+
def is_valid(number):
93+
"""Check if the number is a valid PHN."""
94+
try:
95+
return bool(validate(number))
96+
except ValidationError:
97+
return False
98+
99+
100+
def format(number):
101+
"""Reformat the number to the standard presentation format."""
102+
number = compact(number)
103+
return ' '.join((number[0:4], number[4:7], number[7:]))

0 commit comments

Comments
 (0)