49 lines
1.8 KiB
Python
49 lines
1.8 KiB
Python
"""Deposit identifier algorithm copied from repository alghortim_shenase.py.
|
|
|
|
The input is the customer local mobile number, retaining its leading zero.
|
|
This is a deterministic identifier, not a random or unique-per-notice value.
|
|
"""
|
|
|
|
def normalize_deposit_identifier(value):
|
|
"""Remove leading zeroes from deposit IDs only; retain internal/check digits."""
|
|
value = str(value or '').translate(str.maketrans('۰۱۲۳۴۵۶۷۸۹٠١٢٣٤٥٦٧٨٩', '01234567890123456789')).strip()
|
|
return (value.lstrip('0') or '0') if value else ''
|
|
|
|
|
|
def generate_id_algorithm(id_code: str) -> str:
|
|
"""
|
|
Calculates the verification code based on the reverse-engineered logic:
|
|
1. Iterate digits from right to left (index i = 0, 1, 2...).
|
|
2. Weights:
|
|
- If i < 8: Weight = 8 - i (Sequence: 8, 7, 6, 5, 4, 3, 2, 1)
|
|
- If i >= 8: Weight = i + 1 (Sequence: 9, 10, ...)
|
|
3. Sum = Sum(Digit * Weight)
|
|
4. Check Digit = Sum % 99
|
|
"""
|
|
# تمیزکاری ورودی (حذف فاصلهها و اطمینان از عددی بودن)
|
|
if not id_code.isdigit():
|
|
return "Error: ورودی باید فقط عدد باشد."
|
|
|
|
total_sum = 0
|
|
|
|
# پیمایش از راست به چپ
|
|
# reversed(id_code) ارقام را برعکس میکند
|
|
for i, char in enumerate(reversed(id_code)):
|
|
digit = int(char)
|
|
|
|
# تعیین وزن بر اساس ایندکس (مکان رقم از راست)
|
|
if i < 8:
|
|
weight = 8 - i
|
|
else:
|
|
weight = i + 1
|
|
|
|
total_sum += digit * weight
|
|
|
|
# محاسبه باقیمانده بر 99
|
|
check_digit = total_sum % 99
|
|
|
|
# فرمتدهی دو رقمی (مثلاً 6 میشود 06)
|
|
check_digit_str = f"{check_digit:02}"
|
|
|
|
return normalize_deposit_identifier(f"{id_code}{check_digit_str}")
|