|
| 1 | +"""Russia INN.""" |
| 2 | + |
| 3 | +from validators.utils import validator |
| 4 | + |
| 5 | + |
| 6 | +@validator |
| 7 | +def ru_inn(value: str): |
| 8 | + """Validate a Russian INN (Taxpayer Identification Number). |
| 9 | +
|
| 10 | + The INN can be either 10 digits (for companies) or 12 digits (for individuals). |
| 11 | + The function checks both the length and the control digits according to Russian tax rules. |
| 12 | +
|
| 13 | + Examples: |
| 14 | + >>> ru_inn('500100732259') # Valid 12-digit INN |
| 15 | + True |
| 16 | + >>> ru_inn('7830002293') # Valid 10-digit INN |
| 17 | + True |
| 18 | + >>> ru_inn('1234567890') # Invalid INN |
| 19 | + ValidationFailure(func=ru_inn, args={'value': '1234567890'}) |
| 20 | +
|
| 21 | + Args: |
| 22 | + value: Russian INN string to validate. Can contain only digits. |
| 23 | +
|
| 24 | + Returns: |
| 25 | + (Literal[True]): If `value` is a valid Russian INN. |
| 26 | + (ValidationError): If `value` is an invalid Russian INN. |
| 27 | +
|
| 28 | + Note: |
| 29 | + The validation follows the official algorithm: |
| 30 | + - For 10-digit INN: checks 10th control digit |
| 31 | + - For 12-digit INN: checks both 11th and 12th control digits |
| 32 | + """ |
| 33 | + if not value: |
| 34 | + return False |
| 35 | + |
| 36 | + try: |
| 37 | + digits = list(map(int, value)) |
| 38 | + # company |
| 39 | + if len(digits) == 10: |
| 40 | + weight_coefs = [2, 4, 10, 3, 5, 9, 4, 6, 8, 0] |
| 41 | + control_number = sum([d * w for d, w in zip(digits, weight_coefs)]) % 11 |
| 42 | + return ( |
| 43 | + (control_number % 10) == digits[-1] |
| 44 | + if control_number > 9 |
| 45 | + else control_number == digits[-1] |
| 46 | + ) |
| 47 | + # person |
| 48 | + elif len(digits) == 12: |
| 49 | + weight_coefs1 = [7, 2, 4, 10, 3, 5, 9, 4, 6, 8, 0, 0] |
| 50 | + control_number1 = sum([d * w for d, w in zip(digits, weight_coefs1)]) % 11 |
| 51 | + weight_coefs2 = [3, 7, 2, 4, 10, 3, 5, 9, 4, 6, 8, 0] |
| 52 | + control_number2 = sum([d * w for d, w in zip(digits, weight_coefs2)]) % 11 |
| 53 | + print(control_number1, control_number2, value) |
| 54 | + return ( |
| 55 | + (control_number1 % 10) == digits[-2] |
| 56 | + if control_number1 > 9 |
| 57 | + else control_number1 == digits[-2] and (control_number2 % 10) == digits[-1] |
| 58 | + if control_number2 > 9 |
| 59 | + else control_number2 == digits[-1] |
| 60 | + ) |
| 61 | + else: |
| 62 | + return False |
| 63 | + except ValueError: |
| 64 | + return False |
0 commit comments