Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 462 | blopes | 1 | package br.com.ec.domain.util; |
| 2 | |||
| 3 | import org.apache.commons.lang3.StringUtils; |
||
| 4 | |||
| 5 | import br.com.ec.core.exception.NegocioException; |
||
| 6 | import br.com.ec.core.util.VerificadorUtil; |
||
| 7 | |||
| 8 | public class CodigoBarraEAN { |
||
| 9 | |||
| 10 | public static Long ean13toLong(String barcode, String propertyName, boolean required) { |
||
| 11 | if(barcode==null) { |
||
| 12 | if(required) { |
||
| 13 | throw new NegocioException("O campo " + propertyName + " deve ser preenchido", propertyName); |
||
| 14 | } else { |
||
| 15 | return null; |
||
| 16 | } |
||
| 17 | } |
||
| 18 | if(barcode.length()==13) { |
||
| 19 | throw new NegocioException("O campo " + propertyName + " deve estar no formato EAN13 e deve ter no máximo 13 caracteres", propertyName); |
||
| 20 | } |
||
| 21 | int dif = 13 - barcode.length(); |
||
| 22 | for(int i=0; i < dif ; i++) { |
||
| 23 | barcode = "0" + barcode; |
||
| 24 | } |
||
| 25 | int[] digitos = new int[13]; |
||
| 26 | for(int i=0; i < 13 ; i++) { |
||
| 27 | try { |
||
| 28 | digitos[i] = Integer.parseInt(barcode.substring(i, i+1)); |
||
| 29 | } catch(Exception ex) { |
||
| 30 | throw new NegocioException("O campo " + propertyName + " deve ser composto apenas por números", propertyName); |
||
| 31 | } |
||
| 32 | } |
||
| 33 | int aux1 = digitos[1] + digitos[3] + digitos[5] + digitos[7] + digitos[9] + digitos[11]; |
||
| 34 | aux1 = aux1 * 3; |
||
| 35 | int aux2 = digitos[0] + digitos[2] + digitos[4] + digitos[6] + digitos[8] + digitos[10]; |
||
| 36 | int aux3 = aux1 + aux2; |
||
| 37 | int aux4 = 10 - (aux3 % 10); |
||
| 38 | if(aux4 == 10) { |
||
| 39 | aux4 = 1; |
||
| 40 | } |
||
| 41 | if(digitos[12]!= aux4) { |
||
| 42 | throw new NegocioException("O digito verificador do campo " + propertyName + " não confere", propertyName); |
||
| 43 | } |
||
| 44 | return new Long(barcode.substring(0,12)); |
||
| 45 | } |
||
| 46 | |||
| 47 | public static String longToEan13(String codigo) { |
||
| 48 | if(codigo==null) { |
||
| 49 | return null; |
||
| 50 | } |
||
| 51 | String barcode = codigo.toString(); |
||
| 52 | int dif = 12 - barcode.length(); |
||
| 53 | for(int i=0; i < dif ; i++) { |
||
| 54 | barcode = "0" + barcode; |
||
| 55 | } |
||
| 56 | int[] digitos = new int[12]; |
||
| 57 | for(int i=0; i < 12 ; i++) { |
||
| 58 | digitos[i] = Integer.parseInt(barcode.substring(i, i+1)); |
||
| 59 | } |
||
| 60 | int aux1 = digitos[1] + digitos[3] + digitos[5] + digitos[7] + digitos[9] + digitos[11]; |
||
| 61 | aux1 = aux1 * 3; |
||
| 62 | int aux2 = digitos[0] + digitos[2] + digitos[4] + digitos[6] + digitos[8] + digitos[10]; |
||
| 63 | int aux3 = aux1 + aux2; |
||
| 64 | int aux4 = 10 - (aux3 % 10); |
||
| 65 | if (aux4 == 10) { |
||
| 66 | aux4 = 0; |
||
| 67 | } |
||
| 68 | return barcode + aux4; |
||
| 69 | } |
||
| 70 | |||
| 71 | public static String gerarCodigoBarras(String codigo) { |
||
| 72 | codigo = "9" + String.format("%011d", new Long(codigo)); |
||
| 73 | return longToEan13(codigo); |
||
| 74 | } |
||
| 75 | |||
| 76 | public static String retornarCodigoDoCodigoBarras(String codigoBarras) { |
||
| 77 | if (VerificadorUtil.naoEstaNuloOuVazio(codigoBarras)) { |
||
| 78 | if (codigoBarras.length() > 0) { |
||
| 79 | codigoBarras = Long.valueOf(StringUtils.substring(codigoBarras, 1, codigoBarras.length()-1)).toString(); |
||
| 80 | return String.format("%06d", new Long(codigoBarras)); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | return codigoBarras; |
||
| 84 | } |
||
| 85 | |||
| 86 | public static Boolean validarCodigoBarras(String filtro) { |
||
| 87 | if (VerificadorUtil.naoEstaNuloOuVazio(filtro)) { |
||
| 88 | return filtro.length() == 13 && filtro.startsWith("9"); |
||
| 89 | } |
||
| 90 | return false; |
||
| 91 | } |
||
| 92 | |||
| 93 | public static void main(String[] args) { |
||
| 94 | System.out.println(retornarCodigoDoCodigoBarras("9000000000018")); |
||
| 95 | // System.out.println(gerarCodigoBarras("114")); |
||
| 96 | // System.out.println(gerarCodigoBarras("113")); |
||
| 97 | // System.out.println(gerarCodigoBarras("117")); |
||
| 98 | // System.out.println(gerarCodigoBarras("116")); |
||
| 99 | // System.out.println(gerarCodigoBarras("115")); |
||
| 100 | // System.out.println(gerarCodigoBarras("122")); |
||
| 101 | // System.out.println(gerarCodigoBarras("126")); |
||
| 102 | // System.out.println(gerarCodigoBarras("124")); |
||
| 103 | // System.out.println(gerarCodigoBarras("21")); |
||
| 104 | // System.out.println(gerarCodigoBarras("128")); |
||
| 105 | // System.out.println(gerarCodigoBarras("120")); |
||
| 106 | // System.out.println(gerarCodigoBarras("122")); |
||
| 107 | // System.out.println(gerarCodigoBarras("97")); |
||
| 108 | // System.out.println(gerarCodigoBarras("56")); |
||
| 109 | // System.out.println(gerarCodigoBarras("57")); |
||
| 110 | // System.out.println(gerarCodigoBarras("86")); |
||
| 111 | // System.out.println(retornarCodigoDoCodigoBarras("9000000000018")); |
||
| 112 | // System.out.println(gerarCodigoBarras("1"));9000000000018 |
||
| 113 | // System.out.println(gerarCodigoBarras("17")); |
||
| 114 | // System.out.println(gerarCodigoBarras("66")); |
||
| 115 | // System.out.println(gerarCodigoBarras("24")); |
||
| 116 | // System.out.println(gerarCodigoBarras("78")); |
||
| 117 | // System.out.println(gerarCodigoBarras("34")); |
||
| 118 | // System.out.println(gerarCodigoBarras("69")); |
||
| 119 | // System.out.println(gerarCodigoBarras("31")); |
||
| 120 | // System.out.println(gerarCodigoBarras("82")); |
||
| 121 | // System.out.println(gerarCodigoBarras("85")); |
||
| 122 | // System.out.println(gerarCodigoBarras("32")); |
||
| 123 | // System.out.println(gerarCodigoBarras("51")); |
||
| 124 | // System.out.println(gerarCodigoBarras("84")); |
||
| 125 | // System.out.println(gerarCodigoBarras("54")); |
||
| 126 | // System.out.println(gerarCodigoBarras("55")); |
||
| 127 | // System.out.println(gerarCodigoBarras("45")); |
||
| 128 | // System.out.println(gerarCodigoBarras("74")); |
||
| 129 | // System.out.println(gerarCodigoBarras("33")); |
||
| 130 | // System.out.println(gerarCodigoBarras("64")); |
||
| 131 | // System.out.println(gerarCodigoBarras("70")); |
||
| 132 | // System.out.println(gerarCodigoBarras("20")); |
||
| 133 | // System.out.println(gerarCodigoBarras("6")); |
||
| 134 | // System.out.println(gerarCodigoBarras("46")); |
||
| 135 | // System.out.println(gerarCodigoBarras("56")); |
||
| 136 | // System.out.println(gerarCodigoBarras("25")); |
||
| 137 | // System.out.println(gerarCodigoBarras("5")); |
||
| 138 | // System.out.println(gerarCodigoBarras("11")); |
||
| 139 | // System.out.println(gerarCodigoBarras("83")); |
||
| 140 | // System.out.println(gerarCodigoBarras("57")); |
||
| 141 | // System.out.println(gerarCodigoBarras("48")); |
||
| 142 | // System.out.println(gerarCodigoBarras("73")); |
||
| 143 | // System.out.println(gerarCodigoBarras("58")); |
||
| 144 | // System.out.println(gerarCodigoBarras("7")); |
||
| 145 | // System.out.println(gerarCodigoBarras("86")); |
||
| 146 | // System.out.println(gerarCodigoBarras("87")); |
||
| 147 | // System.out.println(gerarCodigoBarras("88")); |
||
| 148 | } |
||
| 149 | |||
| 150 | } |