diff --git a/src/com/rjconsultores/ventaboletos/service/impl/PuntoVentaServiceImpl.java b/src/com/rjconsultores/ventaboletos/service/impl/PuntoVentaServiceImpl.java index 1eb0b531f..c9c967b18 100644 --- a/src/com/rjconsultores/ventaboletos/service/impl/PuntoVentaServiceImpl.java +++ b/src/com/rjconsultores/ventaboletos/service/impl/PuntoVentaServiceImpl.java @@ -30,6 +30,7 @@ import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado; import com.rjconsultores.ws.totvs.service.TotvsService; import com.rjconsultores.ws.utileria.Atributos.CodPaisBanco; import com.rjconsultores.ws.utileria.Atributos.ContribuinteICMS; +import com.rjconsultores.ws.utileria.Atributos.ContribuinteSimplesNacional; import com.rjconsultores.ws.utileria.Atributos.TipoCliente; import com.rjconsultores.ws.utileria.Atributos.TipoEntidade; import com.rjconsultores.ws.utileria.Atributos.TipoFornecedor; @@ -164,9 +165,43 @@ public class PuntoVentaServiceImpl implements PuntoVentaService { return puntosVenta; } + private boolean validaCanalVenta(Short tipoptovtaId) { + + boolean resp = false; + + switch (tipoptovtaId) { + // PROPRIAS + case 11: + resp = true; + break; + // FRANQUEADAS + case 12: + resp = true; + break; + // COBRADOR + case 15: + resp = true; + break; + // MOTORISTA + case 14: + resp = true; + break; + + default: + break; + } + return resp; + } + private void integracionTotvs(PuntoVenta puntoVenta) throws IntegracionException { if (ApplicationProperties.getInstance().integracionTotvs()) { + boolean clienteValido = validaCanalVenta(puntoVenta.getTipoPuntoVenta().getTipoptovtaId()); + boolean fornecedorValido = (puntoVenta.getTipoPuntoVenta().getTipoptovtaId() == 13); + + if (fornecedorValido) + clienteValido = true; + String endpointFornecedor = getEndpointFornecedor(); String endpointCliente = getEndpointCliente(); @@ -189,8 +224,6 @@ public class PuntoVentaServiceImpl implements PuntoVentaService { String cep = puntoVenta.getColonia() == null ? null : puntoVenta.getColonia().getCodpostal(); - String pais = puntoVenta.getColonia() == null ? null : puntoVenta.getColonia().getCiudad().getEstado().getPais().getNombpais(); - String tel = puntoVenta.getNumtelefonouno(); String ddd = null; try { @@ -207,6 +240,8 @@ public class PuntoVentaServiceImpl implements PuntoVentaService { String nreduz = puntoVenta.getRazonSocial() != null ? puntoVenta.getRazonSocial() : puntoVenta.getNombpuntoventa(); + String simpnac = ContribuinteSimplesNacional.NAO.getValor(); + String tppessoa = TipoFornecedor.OS.name(); String tppessoaCliente = TipoCliente.F.name(); @@ -219,18 +254,14 @@ public class PuntoVentaServiceImpl implements PuntoVentaService { String obs = puntoVenta.getComentarios(); obs = StringUtils.isBlank(obs) ? "sem obs." : obs; - String conta = "1110101001001";// DEVE SER REVISADO COM A GUANABARA + String conta = "1110101001001"; // DEVE SER REVISADO COM A GUANABARA String cnae = ""; - String inscrm = "ISENTO";// DEVE SER REVISADO COM A GUANABARA + String inscrm = "ISENTO"; // DEVE SER REVISADO COM A GUANABARA String naturez = ""; - - // 1 = SIM - // 2 = Não - String simpnac = "1";// deve ser revisado com a guanabara String fator = ""; - + String agencia = puntoVenta.getAgenciaId() == null ? "" : puntoVenta.getAgenciaId().getNumagencia(); String dgverag = ""; try { @@ -247,33 +278,40 @@ public class PuntoVentaServiceImpl implements PuntoVentaService { Exception respEx = null; - try { - log.info(" ------------- INICIO INTEGRACION FORNECEDOR TOTVS ------------- "); + if (clienteValido) { + try { + log.info(" ------------- INICIO INTEGRACION FORNECEDOR TOTVS ------------- "); - TotvsService.cadastrarFornecedor(endpointFornecedor, bairro, cep, cgc, cnae, codmun, codpais, complemen, conta, ddd, email, endereco, - est, inscr, inscrm, naturez, nome, nreduz, simpnac, tel, tipoPessoa, tppessoa, cnpjEmpresaProtheus, xidsvp, agencia, banco, - dgverag, ctacorr, dgverct); - } catch (Exception e) { - log.error("", e); - respEx = e; + TotvsService.cadastrarFornecedor(endpointFornecedor, bairro, cep, cgc, cnae, codmun, codpais, complemen, conta, ddd, email, endereco, + est, inscr, inscrm, naturez, nome, nreduz, simpnac, tel, tipoPessoa, tppessoa, cnpjEmpresaProtheus, xidsvp, agencia, banco, + dgverag, ctacorr, dgverct); + } catch (Exception e) { + log.error("", e); + respEx = e; + } } - String contrib = ContribuinteICMS.NAO.getValor();// deve ser revisado com a guanabara + String contrib = ContribuinteICMS.NAO.getValor(); String entid = TipoEntidade.TRANSPORTE_PASSAGEIRO.getValor(); - try { - log.info(" ------------- INICIO INTEGRACION CLIENTE TOTVS ------------- "); - pais = "";// segundo mateus, passar em branco, segundo orientação do mateus da totvs - TotvsService.cadastrarCliente(endpointCliente, bairro, cep, cgc, codmun, codpais, complemen, conta, contrib, ddd, email, cnpjEmpresaProtheus, - endereco, entid, fator, fax, inscr, inscrm, naturez, nome, nreduz, obs, pais, tppessoaCliente, tel, tipoPessoa, est, xidsvp); - } catch (Exception e) { - log.error("", e); - respEx = e; - } - if (respEx != null) { - throw new IntegracionException("integracion.totvs"); - } + String pais = puntoVenta.getColonia() == null ? null : puntoVenta.getColonia().getCiudad().getEstado().getPais().getNombpais(); + pais = "";// segundo mateus, passar em branco, segundo orientação do mateus da totvs + + if (fornecedorValido) { + try { + log.info(" ------------- INICIO INTEGRACION CLIENTE TOTVS ------------- "); + TotvsService.cadastrarCliente(endpointCliente, bairro, cep, cgc, codmun, codpais, complemen, conta, contrib, ddd, email, cnpjEmpresaProtheus, + endereco, entid, fator, fax, inscr, inscrm, naturez, nome, nreduz, obs, pais, tppessoaCliente, tel, tipoPessoa, est, xidsvp); + } catch (Exception e) { + log.error("", e); + respEx = e; + } + + if (respEx != null) { + throw new IntegracionException("integracion.totvs"); + } + } } } diff --git a/src/com/rjconsultores/ventaboletos/utilerias/InscricaoEstadual.java b/src/com/rjconsultores/ventaboletos/utilerias/InscricaoEstadual.java new file mode 100644 index 000000000..1b223fab5 --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/utilerias/InscricaoEstadual.java @@ -0,0 +1,1145 @@ +package com.rjconsultores.ventaboletos.utilerias; + +public class InscricaoEstadual { + + public enum Estado { + AC, AL, AM, AP, BA, CE, DF, ES, GO, MA, MG, MS, MT, PA, PB, PE, PI, PR, RJ, RN, RO, RR, RS, SC, SE, SP, TO + } + + private static String removeMascara(String ie) { + String strIE = ""; + for (int i = 0; i < ie.length(); i++) { + if (Character.isDigit(ie.charAt(i))) { + strIE += ie.charAt(i); + } + } + return strIE; + } + + public static void valida(String inscricaoEstadual, String siglaUf) throws Exception { + String strIE = removeMascara(inscricaoEstadual); + siglaUf = siglaUf.toUpperCase(); + + if (siglaUf.equals(Estado.AC.toString())) { + validaIEAcre(strIE); + } else if (siglaUf.equals(Estado.AL.toString())) { + validaIEAlagoas(strIE); + } else if (siglaUf.equals(Estado.AP.toString())) { + validaIEAmapa(strIE); + } else if (siglaUf.equals(Estado.AM.toString())) { + validaIEAmazonas(strIE); + } else if (siglaUf.equals(Estado.BA.toString())) { + validaIEBahia(strIE); + } else if (siglaUf.equals(Estado.CE.toString())) { + validaIECeara(strIE); + } else if (siglaUf.equals(Estado.ES.toString())) { + validaIEEspiritoSanto(strIE); + } else if (siglaUf.equals(Estado.GO.toString())) { + validaIEGoias(strIE); + } else if (siglaUf.equals(Estado.MA.toString())) { + validaIEMaranhao(strIE); + } else if (siglaUf.equals(Estado.MT.toString())) { + validaIEMatoGrosso(strIE); + } else if (siglaUf.equals(Estado.MS.toString())) { + validaIEMatoGrossoSul(strIE); + } else if (siglaUf.equals(Estado.MG.toString())) { + validaIEMinasGerais(strIE); + } else if (siglaUf.equals(Estado.PA.toString())) { + validaIEPara(strIE); + } else if (siglaUf.equals(Estado.PB.toString())) { + validaIEParaiba(strIE); + } else if (siglaUf.equals(Estado.PR.toString())) { + validaIEParana(strIE); + } else if (siglaUf.equals(Estado.PE.toString())) { + validaIEPernambuco(strIE); + } else if (siglaUf.equals(Estado.PI.toString())) { + validaIEPiaui(strIE); + } else if (siglaUf.equals(Estado.RJ.toString())) { + validaIERioJaneiro(strIE); + } else if (siglaUf.equals(Estado.RN.toString())) { + validaIERioGrandeNorte(strIE); + } else if (siglaUf.equals(Estado.RS.toString())) { + validaIERioGrandeSul(strIE); + } else if (siglaUf.equals(Estado.RO.toString())) { + validaIERondonia(strIE); + } else if (siglaUf.equals(Estado.RR.toString())) { + validaIERoraima(strIE); + } else if (siglaUf.equals(Estado.SC.toString())) { + validaIESantaCatarina(strIE); + } else if (siglaUf.equals(Estado.SP.toString())) { + if (inscricaoEstadual.charAt(0) == 'P') { + strIE = "P" + strIE; + } + validaIESaoPaulo(strIE); + } else if (siglaUf.equals(Estado.SE.toString())) { + validaIESergipe(strIE); + } else if (siglaUf.equals(Estado.TO.toString())) { + validaIETocantins(strIE); + } else if (siglaUf.equals(Estado.DF.toString())) { + validaIEDistritoFederal(strIE); + } else { + throw new Exception("Estado não encontrado : " + siglaUf); + } + } + + private static void validaIEAcre(String ie) throws Exception { + if (ie.length() != 13) { + throw new Exception("Quantidade de digitos inválida."); + } + + // valida os dois primeiros digitos - devem ser iguais a 01 + for (int i = 0; i < 2; i++) { + if (Integer.parseInt(String.valueOf(ie.charAt(i))) != i) { + throw new Exception("Inscrição Estadual inválida"); + } + } + + int soma = 0; + int pesoInicial = 4; + int pesoFinal = 9; + int d1 = 0; // primeiro digito verificador + int d2 = 0; // segundo digito verificador + + // calcula o primeiro digito + for (int i = 0; i < ie.length() - 2; i++) { + if (i < 3) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * pesoInicial; + pesoInicial--; + } + else { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * pesoFinal; + pesoFinal--; + } + } + d1 = 11 - (soma % 11); + if (d1 == 10 || d1 == 11) { + d1 = 0; + } + + // calcula o segundo digito + soma = d1 * 2; + pesoInicial = 5; + pesoFinal = 9; + for (int i = 0; i < ie.length() - 2; i++) { + if (i < 4) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * pesoInicial; + pesoInicial--; + } + else { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * pesoFinal; + pesoFinal--; + } + } + + d2 = 11 - (soma % 11); + if (d2 == 10 || d2 == 11) { + d2 = 0; + } + + // valida os digitos verificadores + String dv = d1 + "" + d2; + if (!dv.equals(ie.substring(ie.length() - 2, ie.length()))) { + throw new Exception("Digito verificador inválido."); + } + } // fim do validaIEAcre() + + private static void validaIEAlagoas(String ie) throws Exception { + + if (ie.length() != 9) { + throw new Exception("Quantidade de d�gitos inv�lida."); + } + + if (!ie.substring(0, 2).equals("24")) { + throw new Exception("Inscrição estadual inválida."); + } + + int[] digits = { 0, 3, 5, 7, 8 }; + boolean check = false; + for (int i = 0; i < digits.length; i++) { + if (Integer.parseInt(String.valueOf(ie.charAt(2))) == digits[i]) { + check = true; + break; + } + } + if (!check) { + throw new Exception("Inscrição estadual inválida."); + } + + int soma = 0; + int peso = 9; + int d = 0; + for (int i = 0; i < ie.length() - 1; i++) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * peso; + peso--; + } + d = ((soma * 10) % 11); + if (d == 10) { + d = 0; + } + + // valida o digito verificador + String dv = d + ""; + if (!ie.substring(ie.length() - 1, ie.length()).equals(dv)) { + throw new Exception("Digito verificador inválido."); + } + } + + private static void validaIEAmapa(String ie) throws Exception { + + if (ie.length() != 9) { + throw new Exception("Quantidade de digitos inválidas."); + } + + if (!ie.substring(0, 2).equals("03")) { + throw new Exception("Inscrição estadual inválida."); + } + + int d1 = -1; + int soma = -1; + int peso = 9; + + long x = Long.parseLong(ie.substring(0, ie.length() - 1)); + if (x >= 3017001L && x <= 3019022L) { + d1 = 1; + soma = 9; + } else if (x >= 3000001L && x <= 3017000L) { + d1 = 0; + soma = 5; + } else if (x >= 3019023L) { + d1 = 0; + soma = 0; + } + + for (int i = 0; i < ie.length() - 1; i++) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * peso; + peso--; + } + + int d = 11 - ((soma % 11)); + if (d == 10) { + d = 0; + } else if (d == 11) { + d = d1; + } + + String dv = d + ""; + if (!ie.substring(ie.length() - 1, ie.length()).equals(dv)) { + throw new Exception("Digito verificador inválido."); + } + } + + private static void validaIEAmazonas(String ie) throws Exception { + + if (ie.length() != 9) { + throw new Exception("Quantidade de digitos inválidas."); + } + + int soma = 0; + int peso = 9; + int d = -1; + for (int i = 0; i < ie.length() - 1; i++) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * peso; + peso--; + } + + if (soma < 11) { + d = 11 - soma; + } else if ((soma % 11) <= 1) { + d = 0; + } else { + d = 11 - (soma % 11); + } + + // valida o digito verificador + String dv = d + ""; + if (!ie.substring(ie.length() - 1, ie.length()).equals(dv)) { + throw new Exception("Digito verificador inválido."); + } + } + + /** + * Valida inscri��o estadual do estado do Bahia + * + * @param ie + * (Inscri��o estadual) + * @throws Exception + */ + private static void validaIEBahia(String ie) throws Exception { + + if (ie.length() != 8 && ie.length() != 9) { + throw new Exception("Quantidade de digitos inválidas." + ie); + } + + int modulo = 10; + int firstDigit = Integer.parseInt(String.valueOf(ie.charAt(ie.length() == 8 ? 0 : 1))); + if (firstDigit == 6 || firstDigit == 7 || firstDigit == 9) + modulo = 11; + + int d2 = -1; + int soma = 0; + int peso = ie.length() == 8 ? 7 : 8; + for (int i = 0; i < ie.length() - 2; i++) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * peso; + peso--; + } + + int resto = soma % modulo; + + if (resto == 0 || (modulo == 11 && resto == 1)) { + d2 = 0; + } else { + d2 = modulo - resto; + } + + int d1 = -1; + soma = d2 * 2; + peso = ie.length() == 8 ? 8 : 9; + for (int i = 0; i < ie.length() - 2; i++) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * peso; + peso--; + } + + resto = soma % modulo; + + if (resto == 0 || (modulo == 11 && resto == 1)) { + d1 = 0; + } else { + d1 = modulo - resto; + } + + // valida os digitos verificadores + String dv = d1 + "" + d2; + if (!dv.equals(ie.substring(ie.length() - 2, ie.length()))) { + throw new Exception("Digito verificador inválido." + ie); + } + } + + private static void validaIECeara(String ie) throws Exception { + + if (ie.length() != 9) { + throw new Exception("Quantidade de digitos inválidas."); + } + + int soma = 0; + int peso = 9; + int d = -1; + for (int i = 0; i < ie.length() - 1; i++) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * peso; + peso--; + } + + d = 11 - (soma % 11); + if (d == 10 || d == 11) { + d = 0; + } + // valida o digito verificador + String dv = d + ""; + if (!ie.substring(ie.length() - 1, ie.length()).equals(dv)) { + throw new Exception("Digito verificador inválido."); + } + } + + private static void validaIEEspiritoSanto(String ie) throws Exception { + + if (ie.length() != 9) { + throw new Exception("Quantidade de digitos inválidas."); + } + + int soma = 0; + int peso = 9; + int d = -1; + for (int i = 0; i < ie.length() - 1; i++) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * peso; + peso--; + } + + int resto = soma % 11; + if (resto < 2) { + d = 0; + } else if (resto > 1) { + d = 11 - resto; + } + + // valida o digito verificador + String dv = d + ""; + if (!ie.substring(ie.length() - 1, ie.length()).equals(dv)) { + throw new Exception("Digito verificador inválido."); + } + } + + private static void validaIEGoias(String ie) throws Exception { + + if (ie.length() != 9) { + throw new Exception("Quantidade de digitos inválidas."); + } + + if (!"10".equals(ie.substring(0, 2))) { + if (!"11".equals(ie.substring(0, 2))) { + if (!"15".equals(ie.substring(0, 2))) { + throw new Exception("Inscrição estadual inválida"); + } + } + } + + if (ie.substring(0, ie.length() - 1).equals("11094402")) { + if (!ie.substring(ie.length() - 1, ie.length()).equals("0")) { + if (!ie.substring(ie.length() - 1, ie.length()).equals("1")) { + throw new Exception("Inscrição estadual inválida."); + } + } + } else { + + int soma = 0; + int peso = 9; + int d = -1; + for (int i = 0; i < ie.length() - 1; i++) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * peso; + peso--; + } + + int resto = soma % 11; + long faixaInicio = 10103105; + long faixaFim = 10119997; + long insc = Long.parseLong(ie.substring(0, ie.length() - 1)); + if (resto == 0) { + d = 0; + } else if (resto == 1) { + if (insc >= faixaInicio && insc <= faixaFim) { + d = 1; + } else { + d = 0; + } + } else if (resto != 0 && resto != 1) { + d = 11 - resto; + } + + // valida o digito verificador + String dv = d + ""; + if (!ie.substring(ie.length() - 1, ie.length()).equals(dv)) { + throw new Exception("Digito verificador inválido."); + } + } + } + + private static void validaIEMaranhao(String ie) throws Exception { + + if (ie.length() != 9) { + throw new Exception("Quantidade de digitos inválidas."); + } + + if (!ie.substring(0, 2).equals("12")) { + throw new Exception("Inscrição estadual inválida."); + } + + int soma = 0; + int peso = 9; + int d = -1; + for (int i = 0; i < ie.length() - 1; i++) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * peso; + peso--; + } + + d = 11 - (soma % 11); + if ((soma % 11) == 0 || (soma % 11) == 1) { + d = 0; + } + + // valida o digito verificador + String dv = d + ""; + if (!ie.substring(ie.length() - 1, ie.length()).equals(dv)) { + throw new Exception("Digito verificador inválido."); + } + } + + private static void validaIEMatoGrosso(String ie) throws Exception { + + if (ie.length() != 11) { + throw new Exception("Quantidade de digitos inválidas."); + } + + int soma = 0; + int pesoInicial = 3; + int pesoFinal = 9; + int d = -1; + + for (int i = 0; i < ie.length() - 1; i++) { + if (i < 2) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * pesoInicial; + pesoInicial--; + } + else { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * pesoFinal; + pesoFinal--; + } + } + + d = 11 - (soma % 11); + if ((soma % 11) == 0 || (soma % 11) == 1) { + d = 0; + } + + // valida o digito verificador + String dv = d + ""; + if (!ie.substring(ie.length() - 1, ie.length()).equals(dv)) { + throw new Exception("Digito verificador inválido."); + } + } + + private static void validaIEMatoGrossoSul(String ie) throws Exception { + + if (ie.length() != 9) { + throw new Exception("Quantidade de digitos inválidas."); + } + + if (!ie.substring(0, 2).equals("28")) { + throw new Exception("Inscrição estadual inválida."); + } + + int soma = 0; + int peso = 9; + int d = -1; + for (int i = 0; i < ie.length() - 1; i++) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * peso; + peso--; + } + + int resto = soma % 11; + int result = 11 - resto; + if (resto == 0) { + d = 0; + } + else if (resto > 0) { + if (result > 9) { + d = 0; + } else if (result < 10) { + d = result; + } + } + + // valida o digito verificador + String dv = d + ""; + if (!ie.substring(ie.length() - 1, ie.length()).equals(dv)) { + throw new Exception("Digito verificador inválido."); + } + } + + private static void validaIEMinasGerais(String ie) throws Exception { + + if (ie.length() != 13) { + throw new Exception("Quantidade de digitos inválidas."); + } + + String str = ""; + for (int i = 0; i < ie.length() - 2; i++) { + if (Character.isDigit(ie.charAt(i))) { + if (i == 3) { + str += "0"; + str += ie.charAt(i); + } else { + str += ie.charAt(i); + } + } + } + + int soma = 0; + int pesoInicio = 1; + int pesoFim = 2; + int d1 = -1; + for (int i = 0; i < str.length(); i++) { + if (i % 2 == 0) { + int x = Integer.parseInt(String.valueOf(str.charAt(i))) * pesoInicio; + String strX = Integer.toString(x); + for (int j = 0; j < strX.length(); j++) { + soma += Integer.parseInt(String.valueOf(strX.charAt(j))); + } + } else { + int y = Integer.parseInt(String.valueOf(str.charAt(i))) * pesoFim; + String strY = Integer.toString(y); + for (int j = 0; j < strY.length(); j++) { + soma += Integer.parseInt(String.valueOf(strY.charAt(j))); + } + } + } + + int dezenaExata = soma; + while (dezenaExata % 10 != 0) { + dezenaExata++; + } + d1 = dezenaExata - soma; + + soma = d1 * 2; + pesoInicio = 3; + pesoFim = 11; + int d2 = -1; + for (int i = 0; i < ie.length() - 2; i++) { + if (i < 2) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * pesoInicio; + pesoInicio--; + } else { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * pesoFim; + pesoFim--; + } + } + + d2 = 11 - (soma % 11); + if ((soma % 11 == 0) || (soma % 11 == 1)) { + d2 = 0; + } + + // valida os digitos verificadores + String dv = d1 + "" + d2; + if (!dv.equals(ie.substring(ie.length() - 2, ie.length()))) { + throw new Exception("Digito verificador inválido."); + } + } + + private static void validaIEPara(String ie) throws Exception { + + if (ie.length() != 9) { + throw new Exception("Quantidade de digitos inválidas."); + } + + if (!ie.substring(0, 2).equals("15")) { + throw new Exception("Inscrição estadual inválida."); + } + + int soma = 0; + int peso = 9; + int d = -1; + for (int i = 0; i < ie.length() - 1; i++) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * peso; + peso--; + } + + d = 11 - (soma % 11); + if ((soma % 11) == 0 || (soma % 11) == 1) { + d = 0; + } + + // valida o digito verificador + String dv = d + ""; + if (!ie.substring(ie.length() - 1, ie.length()).equals(dv)) { + throw new Exception("Digito verificador inválido."); + } + } + + private static void validaIEParaiba(String ie) throws Exception { + + if (ie.length() != 9) { + throw new Exception("Quantidade de digitos inválidas."); + } + + int soma = 0; + int peso = 9; + int d = -1; + for (int i = 0; i < ie.length() - 1; i++) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * peso; + peso--; + } + + d = 11 - (soma % 11); + if (d == 10 || d == 11) { + d = 0; + } + + // valida o digito verificador + String dv = d + ""; + if (!ie.substring(ie.length() - 1, ie.length()).equals(dv)) { + throw new Exception("Digito verificador inválido."); + } + } + + private static void validaIEParana(String ie) throws Exception { + + if (ie.length() != 10) { + throw new Exception("Quantidade de digitos inválidas."); + } + + int soma = 0; + int pesoInicio = 3; + int pesoFim = 7; + int d1 = -1; + for (int i = 0; i < ie.length() - 2; i++) { + if (i < 2) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * pesoInicio; + pesoInicio--; + } else { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * pesoFim; + pesoFim--; + } + } + + d1 = 11 - (soma % 11); + if ((soma % 11) == 0 || (soma % 11) == 1) { + d1 = 0; + } + + soma = d1 * 2; + pesoInicio = 4; + pesoFim = 7; + int d2 = -1; + for (int i = 0; i < ie.length() - 2; i++) { + if (i < 3) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * pesoInicio; + pesoInicio--; + } else { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * pesoFim; + pesoFim--; + } + } + + d2 = 11 - (soma % 11); + if ((soma % 11) == 0 || (soma % 11) == 1) { + d2 = 0; + } + + // valida os digitos verificadores + String dv = d1 + "" + d2; + if (!dv.equals(ie.substring(ie.length() - 2, ie.length()))) { + throw new Exception("Digito verificador inválido."); + } + } + + private static void validaIEPernambuco(String ie) throws Exception { + + if (ie.length() != 14) { + throw new Exception("Quantidade de digitos inválidas."); + } + + int soma = 0; + int pesoInicio = 5; + int pesoFim = 9; + int d = -1; + + for (int i = 0; i < ie.length() - 1; i++) { + if (i < 5) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * pesoInicio; + pesoInicio--; + } else { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * pesoFim; + pesoFim--; + } + } + + d = 11 - (soma % 11); + if (d > 9) { + d -= 10; + } + + System.out.println(soma); + System.out.println(11 - (soma % 11)); + System.out.println(d); + + // valida o digito verificador + String dv = d + ""; + if (!ie.substring(ie.length() - 1, ie.length()).equals(dv)) { + throw new Exception("Digito verificador inválido."); + } + } + + private static void validaIEPiaui(String ie) throws Exception { + + if (ie.length() != 9) { + throw new Exception("Quantidade de digitos inválidas."); + } + + int soma = 0; + int peso = 9; + int d = -1; + for (int i = 0; i < ie.length() - 1; i++) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * peso; + peso--; + } + + d = 11 - (soma % 11); + if (d == 11 || d == 10) { + d = 0; + } + + // valida o digito verificador + String dv = d + ""; + if (!ie.substring(ie.length() - 1, ie.length()).equals(dv)) { + throw new Exception("Digito verificador inválido."); + } + } + + private static void validaIERioJaneiro(String ie) throws Exception { + + if (ie.length() != 8) { + throw new Exception("Quantidade de digitos inválidas."); + } + + int soma = 0; + int peso = 7; + int d = -1; + for (int i = 0; i < ie.length() - 1; i++) { + if (i == 0) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * 2; + } else { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * peso; + peso--; + } + } + + d = 11 - (soma % 11); + if ((soma % 11) <= 1) { + d = 0; + } + + // valida o digito verificador + String dv = d + ""; + if (!ie.substring(ie.length() - 1, ie.length()).equals(dv)) { + throw new Exception("Digito verificador inválido."); + } + } + + private static void validaIERioGrandeNorte(String ie) throws Exception { + + if (ie.length() != 10 && ie.length() != 9) { + throw new Exception("Quantidade de digitos inválidas."); + } + + if (!ie.substring(0, 2).equals("20")) { + throw new Exception("Inscrição estadual inválida."); + } + + if (ie.length() == 9) { + int soma = 0; + int peso = 9; + int d = -1; + for (int i = 0; i < ie.length() - 1; i++) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * peso; + peso--; + } + + d = ((soma * 10) % 11); + if (d == 10) { + d = 0; + } + + // valida o digito verificador + String dv = d + ""; + if (!ie.substring(ie.length() - 1, ie.length()).equals(dv)) { + throw new Exception("Digito verificador inválido."); + } + } else { + int soma = 0; + int peso = 10; + int d = -1; + for (int i = 0; i < ie.length() - 1; i++) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * peso; + peso--; + } + d = ((soma * 10) % 11); + if (d == 10) { + d = 0; + } + + // valida o digito verificador + String dv = d + ""; + if (!ie.substring(ie.length() - 1, ie.length()).equals(dv)) { + throw new Exception("Digito verificador inválido."); + } + } + + } + + private static void validaIERioGrandeSul(String ie) throws Exception { + + if (ie.length() != 10) { + throw new Exception("Quantidade de digitos inválidas."); + } + + int soma = Integer.parseInt(String.valueOf(ie.charAt(0))) * 2; + int peso = 9; + int d = -1; + for (int i = 1; i < ie.length() - 1; i++) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * peso; + peso--; + } + + d = 11 - (soma % 11); + if (d == 10 || d == 11) { + d = 0; + } + + // valida o digito verificador + String dv = d + ""; + if (!ie.substring(ie.length() - 1, ie.length()).equals(dv)) { + throw new Exception("Digito verificador inválido."); + } + } + + private static void validaIERondonia(String ie) throws Exception { + + if (ie.length() != 14) { + throw new Exception("Quantidade de digitos inválidas."); + } + + int soma = 0; + int pesoInicio = 6; + int pesoFim = 9; + int d = -1; + for (int i = 0; i < ie.length() - 1; i++) { + if (i < 5) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * pesoInicio; + pesoInicio--; + } + else { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * pesoFim; + pesoFim--; + } + } + + d = 11 - (soma % 11); + if (d == 11 || d == 10) { + d -= 10; + } + + // valida o digito verificador + String dv = d + ""; + if (!ie.substring(ie.length() - 1, ie.length()).equals(dv)) { + throw new Exception("Digito verificador inválido."); + } + } + + private static void validaIERoraima(String ie) throws Exception { + + if (ie.length() != 9) { + throw new Exception("Quantidade de digitos inválidas."); + } + + if (!ie.substring(0, 2).equals("24")) { + throw new Exception("Inscrição estadual inválida."); + } + + int soma = 0; + int peso = 1; + int d = -1; + for (int i = 0; i < ie.length() - 1; i++) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * peso; + peso++; + } + + d = soma % 9; + + // valida o digito verificador + String dv = d + ""; + if (!ie.substring(ie.length() - 1, ie.length()).equals(dv)) { + throw new Exception("Digito verificador inválido."); + } + } + + private static void validaIESantaCatarina(String ie) throws Exception { + + if (ie.length() != 9) { + throw new Exception("Quantidade de digitos inválidas."); + } + + int soma = 0; + int peso = 9; + int d = -1; + for (int i = 0; i < ie.length() - 1; i++) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * peso; + peso--; + } + + d = 11 - (soma % 11); + if ((soma % 11) == 0 || (soma % 11) == 1) { + d = 0; + } + + // valida o digito verificador + String dv = d + ""; + if (!ie.substring(ie.length() - 1, ie.length()).equals(dv)) { + throw new Exception("Digito verificador inválido."); + } + } + + private static void validaIESaoPaulo(String ie) throws Exception { + + if (ie.length() != 12 && ie.length() != 13) { + throw new Exception("Quantidade de digitos inválidas."); + } + + if (ie.length() == 12) { + int soma = 0; + int peso = 1; + int d1 = -1; + + for (int i = 0; i < ie.length() - 4; i++) { + if (i == 1 || i == 7) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * ++peso; + peso++; + } else { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * peso; + peso++; + } + } + + d1 = soma % 11; + String strD1 = Integer.toString(d1); + d1 = Integer.parseInt(String.valueOf(strD1.charAt(strD1.length() - 1))); + + soma = 0; + int pesoInicio = 3; + int pesoFim = 10; + int d2 = -1; + for (int i = 0; i < ie.length() - 1; i++) { + if (i < 2) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * pesoInicio; + pesoInicio--; + } else { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * pesoFim; + pesoFim--; + } + } + + d2 = soma % 11; + String strD2 = Integer.toString(d2); + d2 = Integer.parseInt(String.valueOf(strD2.charAt(strD2.length() - 1))); + + if (!ie.substring(8, 9).equals(d1 + "")) { + throw new Exception("Inscrição estadual inválida."); + } + if (!ie.substring(11, 12).equals(d2 + "")) { + throw new Exception("Inscrição estadual inválida."); + } + + } else { + // valida o primeiro caracter + if (ie.charAt(0) != 'P') { + throw new Exception("Inscrição estadual inválida."); + } + + String strIE = ie.substring(1, 10); + int soma = 0; + int peso = 1; + int d1 = -1; + + for (int i = 0; i < strIE.length() - 1; i++) { + if (i == 1 || i == 7) { + soma += Integer.parseInt(String.valueOf(strIE.charAt(i))) * ++peso; + peso++; + } else { + soma += Integer.parseInt(String.valueOf(strIE.charAt(i))) * peso; + peso++; + } + } + + d1 = soma % 11; + String strD1 = Integer.toString(d1); + d1 = Integer.parseInt(String.valueOf(strD1.charAt(strD1.length() - 1))); + + if (!ie.substring(9, 10).equals(d1 + "")) { + throw new Exception("Inscrição estadual inválida."); + } + } + } + + private static void validaIESergipe(String ie) throws Exception { + + if (ie.length() != 9) { + throw new Exception("Quantidade de digitos inválidas."); + } + + int soma = 0; + int peso = 9; + int d = -1; + for (int i = 0; i < ie.length() - 1; i++) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * peso; + peso--; + } + + d = 11 - (soma % 11); + if (d == 11 || d == 11 || d == 10) { + d = 0; + } + + // valida o digito verificador + String dv = d + ""; + if (!ie.substring(ie.length() - 1, ie.length()).equals(dv)) { + throw new Exception("Digito verificador inválido."); + } + } + + private static void validaIETocantins(String ie) throws Exception { + + if (ie.length() != 9 && ie.length() != 11) { + throw new Exception("Quantidade de d�gitos inv�lida."); + } else if (ie.length() == 9) { + ie = ie.substring(0, 2) + "02" + ie.substring(2); + } + + int soma = 0; + int peso = 9; + int d = -1; + for (int i = 0; i < ie.length() - 1; i++) { + if (i != 2 && i != 3) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * peso; + peso--; + } + } + d = 11 - (soma % 11); + if ((soma % 11) < 2) { + d = 0; + } + + // valida o digito verificador + String dv = d + ""; + if (!ie.substring(ie.length() - 1, ie.length()).equals(dv)) { + throw new Exception("Digito verificador inválido."); + } + } + + private static void validaIEDistritoFederal(String ie) throws Exception { + + if (ie.length() != 13) { + throw new Exception("Quantidade de digitos inválidas."); + } + + int soma = 0; + int pesoInicio = 4; + int pesoFim = 9; + int d1 = -1; + for (int i = 0; i < ie.length() - 2; i++) { + if (i < 3) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * pesoInicio; + pesoInicio--; + } else { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * pesoFim; + pesoFim--; + } + } + + d1 = 11 - (soma % 11); + if (d1 == 11 || d1 == 10) { + d1 = 0; + } + + soma = d1 * 2; + pesoInicio = 5; + pesoFim = 9; + int d2 = -1; + for (int i = 0; i < ie.length() - 2; i++) { + if (i < 4) { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * pesoInicio; + pesoInicio--; + } else { + soma += Integer.parseInt(String.valueOf(ie.charAt(i))) * pesoFim; + pesoFim--; + } + } + + d2 = 11 - (soma % 11); + if (d2 == 11 || d2 == 10) { + d2 = 0; + } + + // valida os digitos verificadores + String dv = d1 + "" + d2; + if (!dv.equals(ie.substring(ie.length() - 2, ie.length()))) { + throw new Exception("Digito verificador inválido."); + } + } +}