8000 Fix detection of natural RUC values · mjsignup/python-stdnum@1a0e613 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1a0e613

Browse files
committed
Fix detection of natural RUC values
A natural RUC is the CI plus an establishment number. Both the natural RUC and the public RUC can have a third digit with the value 6. Closes arthurdejong#267
1 parent 8071444 commit 1a0e613

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

stdnum/ec/ruc.py

Lines changed: 34 additions & 13 deletions
< 107CD td data-grid-cell-id="diff-3f26c3fb4fac5680b90ca8608f1a287bc039268c70c4abd916897a80fc766120-85-106-2" data-line-anchor="diff-3f26c3fb4fac5680b90ca8608f1a287bc039268c70c4abd916897a80fc766120R106" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);padding-right:24px" tabindex="-1" valign="top" class="focusable-grid-cell diff-text-cell right-side-diff-cell left-side">
return number
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# coding: utf-8
33
#
44
# Copyright (C) 2014 Jonathan Finlay
5-
# Copyright (C) 2014-2015 Arthur de Jong
5+
# Copyright (C) 2014-2021 Arthur de Jong
66
#
77
# This library is free software; you can redistribute it and/or
88
# modify it under the terms of the GNU Lesser General Public
@@ -53,6 +53,32 @@ def _checksum(number, weights):
5353
return sum(w * int(n) for w, n in zip(weights, number)) % 11
5454

5555

56+
def _validate_natural(number):
57+
"""Check if the number is a valid natural RUC (CI plus establishment)."""
58+
if number[-3:] == '000':
59+
raise InvalidComponent() # establishment number wrong
60+
ci.validate(number[:10])
61+
return number
62+
63+
64+
def _validate_public(number):
65+
"""Check if the number is a valid public RUC."""
66+
if number[-4:] == '0000':
67+
raise InvalidComponent() # establishment number wrong
68+
if _checksum(number[:9], (3, 2, 7, 6, 5, 4, 3, 2, 1)) != 0:
69+
raise InvalidChecksum()
70+
return number
71+
72+
73+
def _validate_juridical(number):
74+
"""Check if the number is a valid juridical RUC."""
75+
if number[-3:] == '000':
76+
raise InvalidComponent() # establishment number wrong
77+
if _checksum(number[:10], (4, 3, 2, 7, 6, 5, 4, 3, 2, 1)) != 0:
78+
raise InvalidChecksum()
79+
return number
80+
81+
5682
def validate(number):
5783
"""Check if the number provided is a valid RUC number. This checks the
5884
length, formatting, check digit and check sum."""
@@ -65,21 +91,16 @@ def validate(number):
6591
raise InvalidComponent() # invalid province code
6692
if number[2] < '6':
6793
# 0..5 = natural RUC: CI plus establishment number
68-
if number[-3:] == '000':
69-
raise InvalidComponent() # establishment number wrong
70-
ci.validate(number[:10])
94+
_validate_natural(number)
7195
elif number[2] == '6':
72-
# 6 = public RUC
73-
if number[-4:] == '0000':
74-
raise InvalidComponent() # establishment number wrong
75-
if _checksum(number[:9], (3, 2, 7, 6, 5, 4, 3, 2, 1)) != 0:
76-
raise InvalidChecksum()
96+
# 6 = public RUC (or natural RUC)
97+
try:
98+
_validate_public(number)
99+
except ValidationError:
100+
_validate_natural(number)
77101
elif number[2] == '9':
78102
# 9 = juridical RUC
79-
if number[-3:] == '000':
80-
raise InvalidComponent() # establishment number wrong
81-
if _checksum(number[:10], (4, 3, 2, 7, 6, 5, 4, 3, 2, 1)) != 0:
82-
raise InvalidChecksum()
103+
_validate_juridical(number)
83104
else:
84105
raise InvalidComponent() # third digit wrong
85106

tests/test_ec_ruc.doctest

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
test_ec_ruc.doctest - more detailed doctests for stdnum.ec.ruc module
22

33
Copyright (C) 2014 Jonathan Finlay
4-
Copyright (C) 2014-2015 Arthur de Jong
4+
Copyright (C) 2014-2021 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
@@ -27,13 +27,15 @@ as module documentation.
2727
>>> from stdnum.exceptions import *
2828

2929

30-
Normal natural RUC values (third digit less than 6) that should just work.
30+
Normal natural RUC values (third digit less than or equal to 6) that should
31+
just work.
3132

3233
>>> numbers = '''
3334
... 0101016905001
3435
... 0602910945001
3536
... 0910005917001
3637
... 0926687856001
38+
... 0962467429001
3739
... 1001152287001
3840
... 1102755442001
3941
... 1104552037001
@@ -127,6 +129,7 @@ Normal public RUC values (third digit is 6) that should just work.
127129
... 0960006420001
128130
... 0968529830001
129131
... 0968566440001
132+
... 0968599020001
130133
... 1060000180001
131134
... 1060000260001
132135
... 1060000340001

0 commit comments

Comments
 (0)
0