package com.fp.frontend.utility; import java.math.BigDecimal; /** * Clase para validar la cédula y el RUC de una persona * @author BPTWPA * @version 2.1 */ public class ValidateIdentification { /** * Método para validar la cédula * * @param id Número de la cédula * @return result Retorna V/F según la validez de la cédula */ public static boolean cedula(String id) { boolean result = true; int[] modulo9 = { 2, 1, 2, 1, 2, 1, 2, 1, 2 }; BigDecimal verif = new BigDecimal(0); if (id.length() != 10) result = false; else { for (int i = 0; i < 9; i++) { BigDecimal temp = new BigDecimal(new BigDecimal(id.substring(i, (i + 1))).multiply(new BigDecimal(modulo9[i])).toString()); if (temp.doubleValue() > 9){ temp = temp.subtract(new BigDecimal("9")); } verif = verif.add(temp); } if (verif.doubleValue() % 10 == 0){ if (Integer.parseInt(id.substring(9, 10)) == 0){ result = true; } else{ result = false; } } else if ((10 - (verif.doubleValue() % 10)) == Integer.parseInt(id.substring(9, 10))){ result = true; } else { result = false; } } return result; } /** * Método para validar el Ruc * * @param ruc Número del Ruc * @return Retorna V/F según la validez del Ruc */ public static boolean ruc(String ruc) { boolean result = true; int[] modulo11 = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; BigDecimal verif = new BigDecimal(0); if (ruc.length() < 13) result = false; else if (Integer.parseInt(ruc.substring(0, 2)) < 1 || Integer.parseInt(ruc.substring(0, 2)) > 25) { result = false; } else { if (Integer.parseInt(ruc.substring(2, 3)) < 0 || (Integer.parseInt(ruc.substring(2, 3)) > 6 && Integer .parseInt(ruc.substring(2, 3)) < 9)) { result = false; } else { if (Integer.parseInt(ruc.substring(2, 3)) == 9) { // sociedad privada o extranjeros if (!ruc.substring(10, 13).equals("001")) result = false; else { modulo11[0] = 4; modulo11[1] = 3; modulo11[2] = 2; modulo11[3] = 7; modulo11[4] = 6; modulo11[5] = 5; modulo11[6] = 4; modulo11[7] = 3; modulo11[8] = 2; for (int i = 0; i < 9; i++) { verif = verif.add(new BigDecimal(ruc.substring(i, (i + 1))).multiply(new BigDecimal( modulo11[i]))); } if (verif.doubleValue() % 11 == 0) if (Integer.parseInt(ruc.substring(9, 10)) == 0) result = true; else result = false; else if ((11 - (verif.doubleValue() % 11)) == Integer .parseInt(ruc.substring(9, 10))) result = true; else result = false; } } else if (Integer.parseInt(ruc.substring(2, 3)) == 6) { // sociedad publicas if (!ruc.substring(10, 13).equals("001")) result = false; else { modulo11[0] = 3; modulo11[1] = 2; modulo11[2] = 7; modulo11[3] = 6; modulo11[4] = 5; modulo11[5] = 4; modulo11[6] = 3; modulo11[7] = 2; for (int i = 0; i < 8; i++) { verif = verif.add(new BigDecimal(ruc.substring(i, (i + 1))).multiply(new BigDecimal( modulo11[i]))); } if (verif.doubleValue() % 11 == 0) if (Integer.parseInt(ruc.substring(8, 9)) == 0) result = true; else result = false; else if ((11 - (verif.doubleValue() % 11)) == Integer .parseInt(ruc.substring(8, 9))) result = true; else result = false; } } else if (Integer.parseInt(ruc.substring(2, 3)) < 6 && Integer.parseInt(ruc.substring(2, 3)) >= 0) { // personas naturales if (!ruc.substring(10, 13).equals("001")) result = false; else { result = cedula(ruc.substring(0, 10)); } } } } return result; } }