Last active
December 19, 2017 12:31
-
-
Save ap-Codkelden/22ba40d06156ccfb2f47ec71a36b3f7d to your computer and use it in GitHub Desktop.
Check digit for EDRPOU code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# edrpou.py | |
""" | |
Перевірка контрольних розрядів: | |
ЄДРПОУ | |
ІПН | |
ТРДПАУ | |
РНОКПП | |
""" | |
# коефіцієнти для контрольного розряду РНОКПП | |
coef_rnokpp = (-1, 5, 7, 9, 4, 6, 10, 5, 7,) | |
def coef_inn(second_step=0): | |
"""Функція для отримання контрольного розряду індивідуального податкового | |
номеру (ІПН, 12 розрядів). | |
Параметри: | |
second_step: | |
Набуває значення True|False (0 або 1 та ін.) - ознака набору | |
коефіцієнтів (першого чи другого), другий застосовується у | |
випадку, якщо контрольна сума дорівнює 10. | |
Повертає: | |
Кортеж коефіцієнтів | |
""" | |
return (11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,) if not second_step \ | |
else (13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53,) | |
def coef_trdpau(second_step=0): | |
"""Аналогічно coef_inn""" | |
return (9, 11, 13, 17, 19, 23, 29, 31) if not second_step \ | |
else (11, 13, 17, 19, 23, 29, 31, 37) | |
class NotEDRPOUValue(Exception): | |
def __init__(self, message): | |
self.message = message | |
print(self.message) | |
def coef_edrpou(edrpou): | |
return (1, 2, 3, 4, 5, 6, 7,) if edrpou < 30000000 or edrpou > 60000000 \ | |
else (7, 1, 2, 3, 4, 5, 6,) | |
def code2list(e): | |
return [int(d) for d in e] | |
def check_sum(e, c): | |
return sum(a*b for a, b in list(zip(e, c))) | |
def is_inn(inn): | |
if isinstance(inn, int): | |
inn = str(inn) | |
else: | |
inn = inn.strip() | |
inn_list = code2list(inn.zfill(12)[:11]) | |
cs = check_sum(inn_list, coef_inn()) % 11 | |
print(cs) | |
# if cs == 10: cs = check_sum(inn_list, coef_inn(1)) % 11 | |
if cs == 10: cs = check_sum(inn_list, coef_inn(1)) % 11 % 10 | |
print(cs) | |
print(inn[-1]) | |
return cs == int(inn[-1]) | |
def is_trdpau(trdpau): | |
if isinstance(trdpau, int): | |
trdpau = str(trdpau) | |
else: | |
trdpau = trdpau.strip() | |
trdpau_list = code2list(trdpau.zfill(9)[:8]) | |
cs = check_sum(trdpau_list, coef_trdpau()) % 11 | |
if cs == 10: cs = check_sum(trdpau_list, coef_trdpau(1)) % 11 | |
return cs == int(trdpau[-1]) | |
def is_rnokpp(rnokpp): | |
if isinstance(rnokpp, int): | |
rnokpp = str(rnokpp) | |
else: | |
rnokpp = rnokpp.strip() | |
rnokpp_list = code2list(rnokpp.zfill(10)[:9]) | |
k = check_sum(rnokpp_list, coef_rnokpp) % 11 | |
cs = 0 if k == 10 else k | |
return cs == int(rnokpp[-1]) | |
def is_edrpou(edrpou): | |
coef = None | |
if isinstance(edrpou, int): | |
coef = coef_edrpou(edrpou) | |
edrpou = str(edrpou) | |
else: | |
if len(edrpou.strip()) > 8: | |
raise NotEDRPOUValue('Given value is not a EDRPOU code') | |
coef = coef_edrpou(int(edrpou)) | |
edrpou_list = code2list(edrpou.zfill(8)[:7]) | |
cs = check_sum(edrpou_list, coef) % 11 | |
if cs > 9: | |
cs = check_sum(edrpou_list, [c+2 for c in coef]) % 11 | |
if cs == 10: | |
cs = 0 | |
return cs == int(edrpou[-1]) | |
if __name__ == "__main__": | |
codes = [ | |
12148228, | |
'02300007', | |
'00000009', | |
31080212, | |
291335012, | |
258063273, | |
87505, | |
] | |
# ДП Криворізька ТЦ, валідний | |
print('00130850 -- ДП Криворізька ТЦ, валідний') | |
print(is_edrpou('00130850')) | |
for c in codes: | |
print(c, '--', is_edrpou(c)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment