Created
February 13, 2017 15:26
-
-
Save jacricelli/663f7b1558a78e8871e0cb06917a3809 to your computer and use it in GitHub Desktop.
Generar / Validar CUILT
This file contains 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
<?php | |
public function generarCuilt($dni, $sexo) | |
{ | |
if (!empty($dni)) { | |
$dni = (string)$dni; | |
$prefix = $sexo === 'F' ? '27' : '20'; | |
$sum = ($prefix[0] * 5) + ($prefix[1] * 4); | |
foreach ([3, 2, 7, 6, 5, 4, 3, 2] as $key => $value) { | |
$sum += (int)$dni[$key] * $value; | |
} | |
$rest = $sum % 11; | |
if ($rest <= 1) { | |
if ($rest === 0) { | |
$suffix = 0; | |
} else { | |
if ($sexo === 'M') { | |
$suffix = 9; | |
} else { | |
$suffix = 4; | |
} | |
} | |
$prefix = 23; | |
} else { | |
$suffix = 11 - $rest; | |
} | |
return sprintf('%s-%s-%s', $prefix, $dni, $suffix); | |
} | |
} | |
public function validarCuilt($cuilt) | |
{ | |
$cuit = preg_replace('/[^\d]/', '', current($check)); | |
if (strlen($cuit) != 11) { | |
return false; | |
} | |
$sum = 0; | |
$digits = str_split($cuit); | |
$digit = array_pop($digits); | |
$total = count($digits); | |
for ($i = 0; $i < $total; $i++) { | |
$sum += $digits[9 - $i] * (2 + ($i % 6)); | |
} | |
$result = 11 - ($sum % 11); | |
if ($result == 11) { | |
$result = 0; | |
} elseif ($result == 10) { | |
$result = 9; | |
} | |
return $result == $digit; | |
} | |
$dni = "16826495"; | |
$sexo = 0; | |
if ($sexo == 1) { | |
$Primero = '20'; | |
} elseif ($sexo == 0) { | |
$Primero = '27'; | |
} else { | |
$Primero = '30'; | |
} | |
$calculo = ($Primero[0] * 5) + ($Primero[1] * 4); | |
foreach ([3, 2, 7, 6, 5, 4, 3, 2] as $index => $i) { | |
$calculo += (int)$dni[$index] * $i; | |
} | |
$resto = $calculo % 11; | |
debug($resto); | |
if (($sexo != '3') && ($resto <= 1)) { | |
if ($resto == 0) { | |
$C = '0'; | |
} else { | |
if ($sexo == 1) { | |
$C = '9'; | |
} else { | |
$C = '4'; | |
} | |
} | |
$Primero = '23'; | |
} else { | |
$C = 11 - $resto; | |
} | |
debug($Primero . "-" . $dni . "-" . $C); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment