Skip to content

Instantly share code, notes, and snippets.

@damiancipolat
Created September 10, 2020 18:50
Show Gist options
  • Save damiancipolat/1fb66d0ed8aade13a151233838814a83 to your computer and use it in GitHub Desktop.
Save damiancipolat/1fb66d0ed8aade13a151233838814a83 to your computer and use it in GitHub Desktop.
Codigo JS para calcular el CUIL en base al DNI y sexo, pensado para ser usado en NODE.JS
const assert = require('assert');
const createError = require('http-errors');
/*
Receive a dni and gender 'M' or 'F' and return his cuil.
Params
document : number -> dni
gender : string
Returns
string -> cuil
*/
const calculateCUIL = (document, gender)=>{
const HOMBRE = ['HOMBRE', 'M','1', 'MALE', 'Masculino'];
const MUJER = ['MUJER', 'F','2', 'FEMALE', 'Femenino'];
const SOCIEDAD = ['SOCIEDAD', 'S', 'SS', 'SOCIETY'];
assert(document,createError.BadRequest());
document = document.toString();
assert(gender,createError.BadRequest());
gender = gender.toUpperCase();
assert(!isNaN(document),createError.BadRequest());
assert([7,8].includes(document.length),createError.BadRequest());
assert(HOMBRE.includes(gender) || MUJER.includes(gender) || SOCIEDAD.includes(gender),createError.BadRequest());
let AB,C;
document = `${document.length===7 ? '0' : ''}${document}`;
if(HOMBRE.includes(gender)) {
AB = '20';
} else if(MUJER.includes(gender)) {
AB = '27';
} else {
AB = '30';
}
const multiplicadores = [3, 2, 7, 6, 5, 4, 3, 2];
let calculo = ((parseInt(AB.charAt(0)) * 5) + (parseInt(AB.charAt(1)) * 4));
for(let i=0;i<8;i++) {
calculo += (parseInt(document.charAt(i)) * multiplicadores[i]);
}
let resto = (parseInt(calculo)) % 11;
if( !SOCIEDAD.includes(gender) && resto==1){
if(HOMBRE.includes(gender)){
C = 9;
} else {
C = 4;
}
AB = '23';
} else if(resto === 0){
C = 0;
} else {
C = 11 - resto;
}
return `${AB}${document}${C}`;
}
module.exports = {
calculateCUIL
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment