|
| 1 | +import isDigits from "../helpers/isDigits"; |
| 2 | +import isCedula from "./isCedula"; |
| 3 | + |
| 4 | +export default function isRUC(ruc: string): boolean { |
| 5 | + // Verificamos que el RUC tenga 13 dígitos |
| 6 | + if (ruc.length !== 13) { |
| 7 | + return false; |
| 8 | + } |
| 9 | + |
| 10 | + // Verificamos que todos los caracteres sean dígitos |
| 11 | + if (!isDigits(ruc)) { |
| 12 | + return false; |
| 13 | + } |
| 14 | + |
| 15 | + // Extraemos los primeros 10 dígitos (cédula o código base) |
| 16 | + const baseCode = ruc.substring(0, 10); |
| 17 | + |
| 18 | + // Extraemos los últimos 3 dígitos (código de establecimiento) |
| 19 | + const establishmentCode = ruc.substring(10, 13); |
| 20 | + |
| 21 | + // Verificamos que el código de establecimiento sea 001 |
| 22 | + if (establishmentCode !== "001") { |
| 23 | + return false; |
| 24 | + } |
| 25 | + |
| 26 | + // Obtenemos el tercer dígito para determinar el tipo de entidad |
| 27 | + // Fuente: https://www.sri.gob.ec/ruc-personas-naturales |
| 28 | + const thirdDigit = parseInt(ruc[2], 10); |
| 29 | + |
| 30 | + // Validación para personas naturales (tercer dígito entre 0 y 5) |
| 31 | + if (thirdDigit >= 0 && thirdDigit <= 5) { |
| 32 | + // Validamos que los primeros 10 dígitos sean una cédula válida |
| 33 | + return isCedula(baseCode); |
| 34 | + } |
| 35 | + |
| 36 | + // Validación para personas jurídicas (tercer dígito es 9) |
| 37 | + if (thirdDigit === 9) { |
| 38 | + return validateLegalEntityRUC(ruc); |
| 39 | + } |
| 40 | + |
| 41 | + // Validación para entidades públicas (tercer dígito es 6) |
| 42 | + if (thirdDigit === 6) { |
| 43 | + return validatePublicEntityRUC(ruc); |
| 44 | + } |
| 45 | + |
| 46 | + // Si el tercer dígito no es válido, el RUC es inválido |
| 47 | + return false; |
| 48 | +} |
| 49 | + |
| 50 | +// Función para validar RUC de personas jurídicas |
| 51 | +function validateLegalEntityRUC(ruc: string): boolean { |
| 52 | + // Coeficientes para personas jurídicas |
| 53 | + const coefficients = [4, 3, 2, 7, 6, 5, 4, 3, 2]; |
| 54 | + const baseCode = ruc.substring(0, 9); |
| 55 | + const validatorDigit = parseInt(ruc[9], 10); |
| 56 | + |
| 57 | + let sum = 0; |
| 58 | + for (let i = 0; i < coefficients.length; i++) { |
| 59 | + const value = parseInt(baseCode[i], 10) * coefficients[i]; |
| 60 | + sum += value; |
| 61 | + } |
| 62 | + |
| 63 | + const remainder = sum % 11; |
| 64 | + const result = remainder === 0 ? 0 : 11 - remainder; |
| 65 | + |
| 66 | + return result === validatorDigit; |
| 67 | +} |
| 68 | + |
| 69 | +// Función para validar RUC de entidades públicas |
| 70 | +function validatePublicEntityRUC(ruc: string): boolean { |
| 71 | + // Coeficientes para entidades públicas |
| 72 | + const coefficients = [3, 2, 7, 6, 5, 4, 3, 2]; |
| 73 | + const baseCode = ruc.substring(0, 8); |
| 74 | + const validatorDigit = parseInt(ruc[8], 10); |
| 75 | + |
| 76 | + let sum = 0; |
| 77 | + for (let i = 0; i < coefficients.length; i++) { |
| 78 | + const value = parseInt(baseCode[i], 10) * coefficients[i]; |
| 79 | + sum += value; |
| 80 | + } |
| 81 | + |
| 82 | + const remainder = sum % 11; |
| 83 | + const result = remainder === 0 ? 0 : 11 - remainder; |
| 84 | + |
| 85 | + return result === validatorDigit; |
| 86 | +} |
0 commit comments