Skip to content

Instantly share code, notes, and snippets.

@EthraZa
Last active May 23, 2016 17:43
Show Gist options
  • Save EthraZa/6dda54010e5661728d51ed43f1db0dfc to your computer and use it in GitHub Desktop.
Save EthraZa/6dda54010e5661728d51ed43f1db0dfc to your computer and use it in GitHub Desktop.
Funções de validação de Inscrição Estadual
<?php
/*
* Funções de validação de Inscrição Estadual
*
* Fórmulas dos Fiscos das 27 UF's no Site do SINTEGRA:
* http://www.sintegra.gov.br/insc_est.html
*/
/**
* Checa Inscrição Estadual
*
* $sI (String) I.E. sem mascara - ex. preg_replace('/[A-Za-z_\W+ ]/', '', $sI);
* $sU (String) Codigo UF IBGE ou Sigla UF
*
* return boolean
*/
function isIE($sI, $sU) {
$sI = (string) $sI;
if ($sU && (($sI == 'ISENTO') || ($sI == '1850170') || ($sI == '1350110')))
return true;
switch(strtoupper($sU)) {
//Norte
case '11':
case 'RO':
return isIeRO($sI);
case '12':
case 'AC':
return isIeAC($sI.'');
case '13':
case 'AM':
return isIeAM($sI);
case '14':
case 'RR':
return isIeRR($sI);
case '15':
case 'PA':
return isIePA($sI);
case '16':
case 'AP':
return isIeAP($sI);
case '17':
case 'TO':
return isIeTO($sI);
//Nordeste
case '21':
case 'MA':
return isIeMA($sI);
case '22':
case 'PI':
return isIePI($sI);
case '23':
case 'CE':
return isIeCE($sI);
case '24':
case 'RN':
return isIeRN($sI);
case '25':
case 'PB':
return isIePB($sI);
case '26':
case 'PE':
return isIePE($sI);
case '27':
case 'AL':
return isIeAL($sI);
case '28':
case 'SE':
return isIeSE($sI);
case '29':
case 'BA':
return isIeBA($sI);
//Sudeste
case '31':
case 'MG':
return isIeMG($sI);
case '32':
case 'ES':
return isIeES($sI);
case '33':
case 'RJ':
return isIeRJ($sI);
case '35':
case 'SP':
return isIeSP($sI);
//Sul
case '41':
case 'PR':
return isIePR($sI);
case '42':
case 'SC':
return isIeSC($sI);
case '43':
case 'RS':
return isIeRS($sI);
//Centro-Oeste
case '50':
case 'MS':
return isIeMS($sI);
case '51':
case 'MT':
return isIeMT($sI);
case '52':
case 'GO':
return isIeGO($sI);
case '53':
case 'DF':
return isIeDF($sI);
default:
return false;
}
}
//Acre
function isIeAC($sI){
$ret = false;
$aI = str_split($sI);
if (count($aI) == 13 && $aI[0] == '0' && $aI[1] == '1') {
$s = (4 * $aI[0]) + (3 * $aI[1]) + (2 * $aI[2]) + (9 * $aI[3]) + (8 * $aI[4]) + (7 * $aI[5]) + (6 * $aI[6]) + (5 * $aI[7]) + (4 * $aI[8]) + (3 * $aI[9]) + (2 * $aI[10]);
$da = 11 - ($s % 11);
if ($da > 9) $da = 0;
$s = (5 * $aI[0]) + (4 * $aI[1]) + (3 * $aI[2]) + (2 * $aI[3]) + (9 * $aI[4]) + (8 * $aI[5]) + (7 * $aI[6]) + (6 * $aI[7]) + (5 * $aI[8]) + (4 * $aI[9]) + (3 * $aI[10]) + (2 * $aI[11]);
$db = 11 - ($s % 11);
if ($db > 9) $db = 0;
$ret = (($aI[11] == $da) && ($aI[12] == $db));
}
return $ret;
}
//Alagoas
function isIeAL($sI){
$ret = false;
$aI = str_split($sI);
if (count($aI) == 9) {
$s = (2 * $aI[0]) + (3 * $aI[1]) + (4 * $aI[2]) + (5 * $aI[3]) + (6 * $aI[4]) + (7 * $aI[5]) + (8 * $aI[6]) + (9 * $aI[7]);
$da = $s % 11;
if ($da > 9) $da = 0;
$ret = ($aI[8] == $da);
}
return $ret;
}
//Amapa
function isIeAP($sI){
$ret = false;
$aI = str_split($sI);
if (count($aI) == 9 && $aI[0] == '0' && $aI[1] == '3') {
$p = substr($sI, 0, -1);
if ($p < '03017001') {
$p = 5;
$d = 0;
} else if ($p > '03017000' && $p < '03019023') {
$p = 9;
$d = 1;
} else {
$p = 0;
$d = 0;
}
$s = $p + (9 * $aI[0]) + (8 * $aI[1]) + (7 * $aI[2]) + (6 * $aI[3]) + (5 * $aI[4]) + (4 * $aI[5]) + (3 * $aI[6]) + (2 * $aI[7]);
$da = 11 - ($s % 11);
if ($da == 10) {
$da = 0;
} else if ($da > 10) {
$da = $d;
}
$ret = ($aI[8] == $da);
}
return $ret;
}
//Amazonas
function isIeAM($sI){
$ret = false;
$aI = str_split($sI);
if (count($aI) == 9) {
$s = ($aI[0] * 9) + ($aI[1] * 8) + ($aI[2] * 7) + ($aI[3] * 6) + ($aI[4] * 5) + ($aI[5] * 4) + ($aI[6] * 3) + ($aI[7] * 2);
if ($s < 11) {
$da = 11 - $s;
} else {
$da = $s % 11;
if ($da <= 1) {
$da = 0;
} else {
$da = 11 - $da;
}
}
$ret = ($aI[8] == $da);
}
return $ret;
}
//Bahia
function isIeBA($sI){
$ret = false;
$aI = str_split($sI);
$c = count($aI);
if ($c == 9 && $aI[0] == 0)
$aI = array_shift($aI);
if ($c == 8) {
$m = (($aI[0] >= 0 && $aI[0] <= 5) || $aI[0] == 8)? 10 : 11;
$s = (7 * $aI[0]) + (6 * $aI[1]) + (5 * $aI[2]) + (4 * $aI[3]) + (3 * $aI[4]) + (2 * $aI[5]);
$da = $s % $m;
if ($da > 0) $da = $m - $da;
$s = (8 * $aI[0]) + (7 * $aI[1]) + (6 * $aI[2]) + (5 * $aI[3]) + (4 * $aI[4]) + (3 * $aI[5]) + (2 * $aI[7]);
$db = $s % $m;
if ($db < 2) {
$db = 0;
} else {
$db = $m - $db;
}
$ret = (($aI[7] == $da) && ($aI[6] == $db));
} else if ($c == 9) {
$m = (($aI[0] >= 0 && $aI[0] <= 5) || $aI[0] == 8)? 10 : 11;
$s = (8 * $aI[0]) + (7 * $aI[1]) + (6 * $aI[2]) + (5 * $aI[3]) + (4 * $aI[4]) + (3 * $aI[5]) + (2 * $aI[6]);
$da = $s % $m;
if ($da > 0) $da = $m - $da;
$s = (9 * $aI[0]) + (8 * $aI[1]) + (7 * $aI[2]) + (6 * $aI[3]) + (5 * $aI[4]) + (4 * $aI[5]) + (3 * $aI[6]) + (2 * $aI[8]);
$db = $s % $m;
if ($db < 2) {
$db = 0;
} else {
$db = $m - $db;
}
$ret = (($aI[8] == $da) && ($aI[7] == $db));
}
return $ret;
}
//Ceara
function isIeCE($sI){
$ret = false;
$aI = str_split($sI);
if (count($aI) == 9) {
$s = (9 * $aI[0]) + (8 * $aI[1]) + (7 * $aI[2]) + (6 * $aI[3]) + (5 * $aI[4]) + (4 * $aI[5]) + (3 * $aI[6]) + (2 * $aI[7]);
$da = 11 - ($s % 11);
if ($da > 9) $da = 0;
$ret = ($aI[8] == $da);
}
return $ret;
}
//Distrito Federal
function isIeDF($sI){
$ret = false;
$aI = str_split($sI);
if (count($aI) == 13 && $aI[0] == '0' && $aI[1] == '7') {
$s = (4 * $aI[0]) + (3 * $aI[1]) + (2 * $aI[2]) + (9 * $aI[3]) + (8 * $aI[4]) + (7 * $aI[5]) + (6 * $aI[6]) + (5 * $aI[7]) + (4 * $aI[8]) + (3 * $aI[9]) + (2 * $aI[10]);
$da = 11 - ($s % 11);
if ($da > 9) $da = 0;
$s = (5 * $aI[0]) + (4 * $aI[1]) + (3 * $aI[2]) + (2 * $aI[3]) + (9 * $aI[4]) + (8 * $aI[5]) + (7 * $aI[6]) + (6 * $aI[7]) + (5 * $aI[8]) + (4 * $aI[9]) + (3 * $aI[10]) + (2 * $aI[11]);
$db = 11 - ($s % 11);
if ($db > 9) $db = 0;
$ret = (($aI[11] == $da) && ($aI[12] == $db));
}
return $ret;
}
//Espirito Santo
function isIeES($sI){
$ret = false;
$aI = str_split($sI);
if (count($aI) == 9) {
$s = (9 * $aI[0]) + (8 * $aI[1]) + (7 * $aI[2]) + (6 * $aI[3]) + (5 * $aI[4]) + (4 * $aI[5]) + (3 * $aI[6]) + (2 * $aI[7]);
$da = $s % 11;
if ($da < 1) {
$da = 0;
} else {
$da = 11 - $da;
}
$ret = ($aI[8] == $da);
}
return $ret;
}
//Goias
function isIeGO($sI){
$ret = false;
$aI = str_split($sI);
if (count($aI) == 9 && $aI[0] == '1' && ($aI[1] == '0' || $aI[1] == '1' || $aI[1] == '5')) {
$s = (9 * $aI[0]) + (8 * $aI[1]) + (7 * $aI[2]) + (6 * $aI[3]) + (5 * $aI[4]) + (4 * $aI[5]) + (3 * $aI[6]) + (2 * $aI[7]);
$da = $s % 11;
if ($da == 0) {
$da = 0;
} else if ($da == 1 && ($sI >= 10103105 && $sI <= 10119997)){
$da = 1;
} else if ($da == 1) {
$da = 0;
} else if ($da > 1) {
$da = 11 - $da;
}
$ret = ($aI[8] == $da);
}
return $ret;
}
//Maranhao
function isIeMA($sI){
$ret = false;
$aI = str_split($sI);
if (count($aI) == 9 && $aI[0] == '1' && $aI[1] == '2') {
$s = (9 * $aI[0]) + (8 * $aI[1]) + (7 * $aI[2]) + (6 * $aI[3]) + (5 * $aI[4]) + (4 * $aI[5]) + (3 * $aI[6]) + (2 * $aI[7]);
$da = $s % 11;
if ($da < 2) {
$da = 0;
} else {
$da = 11 - $da;
}
$ret = ($aI[8] == $da);
}
return $ret;
}
//Mato Grosso
function isIeMT($sI){
$ret = false;
$sI = str_pad($sI, 11, '0', STR_PAD_LEFT);
$aI = str_split($sI);
if (count($aI) == 11) {
$s = (3 * $aI[0]) + (2 * $aI[1]) + (9 * $aI[2]) + (8 * $aI[3]) + (7 * $aI[4]) + (6 * $aI[5]) + (5 * $aI[6]) + (4 * $aI[7]) + (3 * $aI[8]) + (2 * $aI[9]);
$da = $s % 11;
if ($da < 2) {
$da = 0;
} else {
$da = 11 - $da;
}
$ret = ($aI[10] == $da);
}
return $ret;
}
//Mato Grosso do Sul
function isIeMS($sI){
$ret = false;
$aI = str_split($sI);
if (count($aI) == 9 && $aI[0] == '2' && $aI[1] == '8') {
$s = (9 * $aI[0]) + (8 * $aI[1]) + (7 * $aI[2]) + (6 * $aI[3]) + (5 * $aI[4]) + (4 * $aI[5]) + (3 * $aI[6]) + (2 * $aI[7]);
$da = $s % 11;
if ($da > 0) {
$da = 11 - $da;
if ($da > 9) {
$da = 0;
}
}
$ret = ($aI[8] == $da);
}
return $ret;
}
//Minas Gerais
function isIeMG($sI){
$ret = false;
$aI = str_split($sI);
if (count($aI) == 13) {
$s = (1 * $aI[0]) . (2 * $aI[1]) . (1 * $aI[2]) . (1 * $aI[3]) . (2 * $aI[4]) . (1 * $aI[5]) . (2 * $aI[6]) . (1 * $aI[7]) . (2 * $aI[8]) . (1 * $aI[9]) . (2 * $aI[10]);
$s = str_split(array_sum(str_split($s)));
$da = (($s[0]+1).'0') - ($s[0].$s[1]);
if ($da > 9) $da = 0;
$s = (3 * $aI[0]) + (2 * $aI[1]) + (11 * $aI[2]) + (10 * $aI[3]) + (9 * $aI[4]) + (8 * $aI[5]) + (7 * $aI[6]) + (6 * $aI[7]) + (5 * $aI[8]) + (4 * $aI[9]) + (3 * $aI[10]) + (2 * $aI[11]);
$db = $s % 11;
if ($db < 2) {
$db = 0;
} else {
$db = 11 - $db;
}
$ret = (($aI[11] == $da) && ($aI[12] == $db));
}
return $ret;
}
//Para
function isIePA($sI){
$ret = false;
$aI = str_split($sI);
if (count($aI) == 9 && $aI[0] == '1' && $aI[1] == '5') {
$s = (9 * $aI[0]) + (8 * $aI[1]) + (7 * $aI[2]) + (6 * $aI[3]) + (5 * $aI[4]) + (4 * $aI[5]) + (3 * $aI[6]) + (2 * $aI[7]);
$da = 11 - ($s % 11);
if ($da > 9) $da = 0;
$ret = ($aI[8] == $da);
}
return $ret;
}
//Paraiba
function isIePB($sI){
$ret = false;
$aI = str_split($sI);
if (count($aI) == 9) {
$s = (9 * $aI[0]) + (8 * $aI[1]) + (7 * $aI[2]) + (6 * $aI[3]) + (5 * $aI[4]) + (4 * $aI[5]) + (3 * $aI[6]) + (2 * $aI[7]);
$da = 11 - ($s % 11);
if ($da > 9) $da = 0;
$ret = ($aI[8] == $da);
}
return $ret;
}
//Parana
function isIePR($sI){
$ret = false;
$aI = str_split($sI);
if (count($aI) == 10) {
$s = (3 * $aI[0]) + (2 * $aI[1]) + (7 * $aI[2]) + (6 * $aI[3]) + (5 * $aI[4]) + (4 * $aI[5]) + (3 * $aI[6]) + (2 * $aI[7]);
$da = 11 - ($s % 11);
if ($da > 9) $da = 0;
$s = (4 * $aI[0]) + (3 * $aI[1]) + (2 * $aI[2]) + (7 * $aI[3]) + (6 * $aI[4]) + (5 * $aI[5]) + (4 * $aI[6]) + (3 * $aI[7]) + (2 * $aI[8]);
$db = 11 - ($s % 11);
if ($db > 9) $db = 0;
$ret = (($aI[8] == $da) && ($aI[9] == $db));
}
return $ret;
}
//Pernambuco
function isIePE($sI){
$ret = false;
$c = strlen($sI);
if ($c < 9)
$sI = str_pad($sI, 9, '0', STR_PAD_LEFT);
else if ($c > 9 && $c < 14)
$sI = str_pad($sI, 14, '0', STR_PAD_LEFT);
$aI = str_split($sI);
if (count($aI) == 9) {
$s = (8 * $aI[0]) + (7 * $aI[1]) + (6 * $aI[2]) + (5 * $aI[3]) + (4 * $aI[4]) + (3 * $aI[5]) + (2 * $aI[6]);
$da = $s % 11;
if ($da < 2) {
$da = 0;
} else {
$da = 11 - $da;
}
$s = (9 * $aI[0]) + (8 * $aI[1]) + (7 * $aI[2]) + (6 * $aI[3]) + (5 * $aI[4]) + (4 * $aI[5]) + (3 * $aI[6]) + (2 * $aI[7]);
$db = $s % 11;
if ($db < 2) {
$db = 0;
} else {
$db = 11 - $db;
}
$ret = (($aI[7] == $da) && ($aI[8] == $db));
} else if (count($aI) == 14) {
$s = (5 * $aI[0]) + (4 * $aI[1]) + (3 * $aI[2]) + (2 * $aI[3]) + (1 * $aI[4]) + (9 * $aI[5]) + (8 * $aI[6]) + (7 * $aI[7]) + (6 * $aI[8]) + (5 * $aI[9]) + (4 * $aI[10]) + (3 * $aI[11]) + (2 * $aI[12]);
$da = 11 - ($s % 11);
if ($da > 9)
$da = $da - 10;
$ret = ($aI[13] == $da);
}
return $ret;
}
//Piaui
function isIePI($sI){
$ret = false;
$aI = str_split($sI);
if (count($aI) == 9) {
$s = (9 * $aI[0]) + (8 * $aI[1]) + (7 * $aI[2]) + (6 * $aI[3]) + (5 * $aI[4]) + (4 * $aI[5]) + (3 * $aI[6]) + (2 * $aI[7]);
$da = 11 - ($s % 11);
if ($da > 9) $da = 0;
$ret = ($aI[8] == $da);
}
return $ret;
}
//Rio de Janeiro
function isIeRJ($sI){
$ret = false;
$aI = str_split($sI);
if (count($aI) == 8) {
$s = (2 * $aI[0]) + (7 * $aI[1]) + (6 * $aI[2]) + (5 * $aI[3]) + (4 * $aI[4]) + (3 * $aI[5]) + (2 * $aI[6]);
$da = $s % 11;
if ($da < 2) {
$da = 0;
} else {
$da = 11 - $da;
}
$ret = ($aI[7] == $da);
}
return $ret;
}
//Rio Grande do Norte
function isIeRN($sI){
$ret = false;
$aI = str_split($sI);
$c = count($aI);
if ($c == 9 || $c == 10) {
if ($c == 9) {
$s = (9 * $aI[0]) + (8 * $aI[1]) + (7 * $aI[2]) + (6 * $aI[3]) + (5 * $aI[4]) + (4 * $aI[5]) + (3 * $aI[6]) + (2 * $aI[7]);
} else {
$s = (10 * $aI[0]) + (9 * $aI[1]) + (8 * $aI[2]) + (7 * $aI[3]) + (6 * $aI[4]) + (5 * $aI[5]) + (4 * $aI[6]) + (3 * $aI[7]) + (2 * $aI[8]);
}
$da = (10*$s) % 11;
if ($da > 9) $da = 0;
$ret = ($c == 9)? ($aI[8] == $da) : ($aI[9] == $da);
}
return $ret;
}
//Rio Grande do Sul
function isIeRS($sI){
$ret = false;
$sI = str_pad($sI, 10, '0', STR_PAD_LEFT);
$aI = str_split($sI);
if (count($aI) == 10) {
$s = (2 * $aI[0]) + (9 * $aI[1]) + (8 * $aI[2]) + (7 * $aI[3]) + (6 * $aI[4]) + (5 * $aI[5]) + (4 * $aI[6]) + (3 * $aI[7]) + (2 * $aI[8]);
$da = 11 - ($s % 11);
if ($da > 9) $da = 0;
$ret = ($aI[9] == $da);
}
return $ret;
}
//Rondonia
function isIeRO($sI){
$ret = false;
$aI = str_split($sI);
if (count($aI) == 14) {
$s = (6 * $aI[0]) + (5 * $aI[1]) + (4 * $aI[2]) + (3 * $aI[3]) + (2 * $aI[4]) + (9 * $aI[5]) + (8 * $aI[6]) + (7 * $aI[7]) + (6 * $aI[8]) + (5 * $aI[9]) + (4 * $aI[10]) + (3 * $aI[11]) + (2 * $aI[12]);
$da = 11 - ($s % 11);
if ($da > 9) $da = $da - 10;
$ret = ($aI[13] == $da);
}
return $ret;
}
//Roraima
function isIeRR($sI){
$ret = false;
$aI = str_split($sI);
if (count($aI) == 9 && $aI[0] == '2' && $aI[1] == '4') {
$s = (1 * $aI[0]) + (2 * $aI[1]) + (3 * $aI[2]) + (4 * $aI[3]) + (5 * $aI[4]) + (6 * $aI[5]) + (7 * $aI[6]) + (8 * $aI[7]);
$da = $s % 9;
$ret = ($aI[8] == $da);
}
return $ret;
}
//Santa Catarina
function isIeSC($sI){
$ret = false;
$aI = str_split($sI);
if (count($aI) == 9) {
$s = (9 * $aI[0]) + (8 * $aI[1]) + (7 * $aI[2]) + (6 * $aI[3]) + (5 * $aI[4]) + (4 * $aI[5]) + (3 * $aI[6]) + (2 * $aI[7]);
$da = $s % 11;
if ($da < 2) {
$da = 0;
} else {
$da = 11 - $da;
}
$ret = ($aI[8] == $da);
}
return $ret;
}
//Sao Paulo
function isIeSP($sI){
$ret = false;
$aI = str_split($sI);
if (count($aI) == 13 && $aI[0] == 'P') { // Produtor Rural
$s = ($aI[1] * 1) + ($aI[2] * 3) + ($aI[3] * 4) + ($aI[4] * 5) + ($aI[5] * 6) + ($aI[6] * 7) + ($aI[7] * 8) + ($aI[8] * 10);
$da = $s % 11;
if ($da > 9) $da = $da % 10;
$ret = (($aI[9] == $da));
} else if (count($aI) == 12){
$s = ($aI[0] * 1) + ($aI[1] * 3) + ($aI[2] * 4) + ($aI[3] * 5) + ($aI[4] * 6) + ($aI[5] * 7) + ($aI[6] * 8) + ($aI[7] * 10);
$da = $s % 11;
if ($da > 9) $da = $da % 10;
$s = ($aI[0] * 3) + ($aI[1] * 2) + ($aI[2] * 10) + ($aI[3] * 9) + ($aI[4] * 8) + ($aI[5] * 7) + ($aI[6] * 6) + ($aI[7] * 5) + ($aI[8] * 4) + ($aI[9] * 3) + ($aI[10] * 2);
$db = $s % 11;
if ($db > 9) $db = $db % 10;
$ret = (($aI[8] == $da) && ($aI[11] == $db));
}
return $ret;
}
//Sergipe
function isIeSE($sI){
$ret = false;
$aI = str_split($sI);
if (count($aI) == 9) {
$s = (9 * $aI[0]) + (8 * $aI[1]) + (7 * $aI[2]) + (6 * $aI[3]) + (5 * $aI[4]) + (4 * $aI[5]) + (3 * $aI[6]) + (2 * $aI[7]);
$da = 11 - ($s % 11);
if ($da > 9) $da = 0;
$ret = ($aI[8] == $da);
}
return $ret;
}
//Tocantins
function isIeTO($sI){
$ret = false;
if (strlen($sI) == 9)
$sI = substr($sI, 0, 2).'99'.substr($sI, -7);
$aI = str_split($sI);
if (count($aI) == 11 && ($aI[2] == 0 || $aI[2] == 9) && (($aI[3] > 0 && $aI[3] < 4) || ($aI[3] == 9))) {
$s = (9 * $aI[0]) + (8 * $aI[1]) + (7 * $aI[4]) + (6 * $aI[5]) + (5 * $aI[6]) + (4 * $aI[7]) + (3 * $aI[8]) + (2 * $aI[9]);
$da = $s % 11;
if ($da < 2) {
$da = 0;
} else {
$da = 11 - $da;
}
$ret = ($aI[10] == $da);
}
return $ret;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment