Skip to content

Instantly share code, notes, and snippets.

@kastaneda
Created January 17, 2022 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kastaneda/5790888115750c4502146b0ac3cd0dd5 to your computer and use it in GitHub Desktop.
Save kastaneda/5790888115750c4502146b0ac3cd0dd5 to your computer and use it in GitHub Desktop.
Реєстраційний номер облікової картки платника податків
#!/usr/bin/env php
<?php
$code = $argv[1] ?? '1010101017';
if (!is_string($code) || strlen($code) != 10 || !is_numeric($code)) {
echo 'РНОКПП має бути довжиною 10 цифр' . PHP_EOL;
die();
}
echo 'Що відомо про РНОКПП ' . $code . PHP_EOL;
$sex = (int) $code[8] % 2 ? 'чоловіча' : 'жіноча';
echo 'Стать: ' . $sex . PHP_EOL;
$days = 'P' . substr($code, 0, 5) . 'D';
$dob = (new \DateTime('1899-12-31'))->add(new \DateInterval($days));
echo 'Дата народження: ' . $dob->format('Y-m-d') . PHP_EOL;
$magic = fn($i) => [-1, 5, 7, 9, 4, 6, 10, 5, 7][$i] * (int) $code[$i];
$control_digit = array_sum(array_map($magic, range(0, 8))) % 11 % 10;
$is_valid = (int) $code[9] === $control_digit;
if ($is_valid) {
echo 'Контрольна сума збігається' . PHP_EOL;
} else {
echo 'Контрольна сума не збігається' . PHP_EOL;
echo 'Остання цифра має бути ' . $control_digit . PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment