Last active
May 23, 2018 12:11
-
-
Save rkleine/046aaf78847515300f6d to your computer and use it in GitHub Desktop.
CUIT - CUIL
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
function generateCUIL(dni, type) { | |
if (dni.toString().length < 7) return false; | |
let xy = 30; | |
if (type === 'f') { | |
xy = 27; | |
} else if(type === 'm') { | |
xy = 20; | |
} | |
const parts = `${dni}`.split(''); | |
const diff = 8 - parts.length; | |
for (let i = 0; i < diff; i++) { | |
parts.unshift('0'); | |
} | |
parts.unshift(...`${xy}`.split('')) | |
const numbers = '5432765432'.split(''); | |
let sum = 0; | |
for (let i = 0; i <= 9; i++) { | |
sum += +parts[i] * +numbers[i]; | |
} | |
const remainder = sum % 11; | |
let z = 11 - remainder; | |
if (remainder === 0) { | |
z = 0; | |
} else if (remainder === 1 && type === 'm') { | |
z = 9; | |
xy = 23; | |
} else if (remainder === 1 && type === 'f') { | |
z = 4; | |
xy = 23; | |
} | |
return `${xy}-${dni}-${z}`; | |
} | |
function validateCUIT(number, type) { | |
const parts = `${number}`.split(''); | |
if (parts.length !== 11) return false; | |
const numbers = '5432765432'.split(''); | |
let sum = 0; | |
for (let i = 0; i <= 9; i++) { | |
sum += +parts[i] * +numbers[i]; | |
} | |
const remainder = sum % 11; | |
let z = 11 - remainder; | |
if (remainder === 0) { | |
z = 0; | |
} else if (remainder === 1 && type === 'm') { | |
z = 9; | |
xy = 23; | |
} else if (remainder === 1 && type === 'f') { | |
z = 4; | |
xy = 23; | |
} else if (remainder === 1) { | |
z = 9; | |
} | |
return z === +parts[10]; | |
} |
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
class Object | |
def valid_cuit? | |
number = self.to_s.gsub(/\D/, '').split('') | |
return if number.size != 11 | |
numbers = %w(5 4 3 2 7 6 5 4 3 2) | |
result = 0 | |
10.times.each { |i| result += number[i].to_i * numbers[i].to_i } | |
result = result % 11 | |
result = 11 - result | |
result = 0 if result == 11 | |
result = 9 if result == 10 | |
result == number[10].to_i | |
end | |
end | |
20311251407.valid_cuit? # => true | |
"20-31125140-7".valid_cuit? # => true | |
20311251405.valid_cuit? # => false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment