Add isINN and isSnils

This commit is contained in:
cmp167
2022-09-02 10:45:07 +12:00
parent e5565f89ec
commit 4f6a0b17ed

View File

@@ -207,10 +207,15 @@
"output" => $out
);
$err = @file("/tmp/fgExec.err");
$ff = "/tmp/fgExec.err";
$err = @file($ff);
// var_export($err);
for($i = 0; $i < count($err); $i++) {
if(!is_array($err)) {
$a["stderr"][] = "Can't read file: $ff";
}
else for($i = 0; $i < count($err); $i++) {
$str = trim($err[$i], "\r\n");
// $this->d($str);
@@ -322,6 +327,85 @@
return null;
}
function isINN($inn) {
$inn = (string) $inn;
if(!preg_match("/^[0-9]{10,12}$/", $inn)) {
return null;
}
switch(strlen($inn)) {
case 10:
$n = 0;
foreach([2, 4, 10, 3, 5, 9, 4, 6, 8] as $i => $k) {
$n += $k * (int) $inn[$i];
}
$n10 = $n % 11 % 10;
if($n10 === (int) $inn[9])
return true;
return false;
case 12:
$n = 0;
foreach([7, 2, 4, 10, 3, 5, 9, 4, 6, 8] as $i => $k) {
$n += $k * (int) $inn[$i];
}
$n11 = $n % 11 % 10;
$n = 0;
foreach([3, 7, 2, 4, 10, 3, 5, 9, 4, 6, 8] as $i => $k) {
$n += $k * (int) $inn[$i];
}
$n12 = $n % 11 % 10;
if(($n11 === (int) $inn[10]) && ($n12 === (int) $inn[11]))
return true;
return false;
}
return false;
}
function isSnils($snils) {
$snils = (string) $snils;
if(!preg_match("/^[0-9]{11}$/", $snils)) {
return null;
}
$sum = 0;
for($i = 0; $i < 9; $i++) {
$sum += (int) $snils[$i] * (9 - $i);
}
$cs = 0;
if ($sum < 100) {
$cs = $sum;
} elseif ($sum > 101) {
$cs = $sum % 101;
if ($cs === 100) {
$cs = 0;
}
}
if($cs === (int) substr($snils, -2)) {
return true;
}
return false;
}
// END
}