diff options
author | Arthur de Jong <arthur@arthurdejong.org> | 2017-08-26 23:44:00 +0200 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2017-08-26 23:44:00 +0200 |
commit | b8389ebd405f74bdbaa1c09c91b4a816fcd0873b (patch) | |
tree | 6adf9690e6230a25d9aaceca21a6740f33d4f3eb /stdnum/ca/bn.py | |
parent | efd2eb98ed51c0866b4f206e93cbc1bc1691ee5a (diff) |
Add Canadian Business Number (BN)
Diffstat (limited to 'stdnum/ca/bn.py')
-rw-r--r-- | stdnum/ca/bn.py | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/stdnum/ca/bn.py b/stdnum/ca/bn.py new file mode 100644 index 0000000..37d44d9 --- /dev/null +++ b/stdnum/ca/bn.py @@ -0,0 +1,80 @@ +# bn.py - functions for handling Canadian Business Numbers (BNs) +# +# Copyright (C) 2017 Arthur de Jong +# +# 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 + +"""BN (Canadian Business Number). + +A Business Number (BN) is a 9-digit identification number for businesses +issued by the Canada Revenue Agency for tax purposes. The 9-digit number can +be followed by two letters (program identifier) and 4 digits (reference +number) to form a program account (or BN15). + +More information: + +* https://www.canada.ca/en/services/taxes/business-number.html +* https://www.ic.gc.ca/app/scr/cc/CorporationsCanada/fdrlCrpSrch.html?locale=en_CA/ + +>>> validate('12302 6635') +'123026635' +>>> validate('12302 6635 RC 0001') +'123026635RC0001' +>>> validate('123456783') +Traceback (most recent call last): + ... +InvalidChecksum: ... +>>> validate('12345678Z') +Traceback (most recent call last): + ... +InvalidFormat: ... +""" + +from stdnum import luhn +from stdnum.exceptions import * +from stdnum.util import clean + + +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): + """Checks to see if the number provided is a valid BN or BN15. This + checks the length, formatting and check digit.""" + number = compact(number) + if len(number) not in (9, 15): + raise InvalidLength() + if not number[:9].isdigit(): + raise InvalidFormat() + luhn.validate(number[:9]) + if len(number) == 15: + if number[9:11] not in ('RC', 'RM', 'RP', 'RT'): + raise InvalidComponent() + if not number[11:].isdigit(): + raise InvalidFormat() + return number + + +def is_valid(number): + """Checks to see if the number provided is a valid BN or BN15. This + checks the length, formatting and check digit.""" + try: + return bool(validate(number)) + except ValidationError: + return False |