Created
September 16, 2010 17:56
-
-
Save luizkowalski/582851 to your computer and use it in GitHub Desktop.
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
package br.com.athus.util; | |
import java.awt.Desktop; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
import java.math.BigDecimal; | |
import java.util.Arrays; | |
import java.util.Calendar; | |
import java.util.Date; | |
import br.com.athus.controladores.ControladorEmpresa; | |
import br.com.athus.exceptions.EmpresaNullException; | |
import br.com.athus.modelos.Aluno; | |
import br.com.athus.modelos.ContaCorrente; | |
import br.com.athus.modelos.Empresa; | |
import br.com.nordestefomento.jrimum.JRimumException; | |
import br.com.nordestefomento.jrimum.bopepo.Boleto; | |
import br.com.nordestefomento.jrimum.bopepo.BoletoException; | |
import br.com.nordestefomento.jrimum.bopepo.campolivre.CampoLivreException; | |
import br.com.nordestefomento.jrimum.bopepo.campolivre.CampoLivreFactory; | |
import br.com.nordestefomento.jrimum.bopepo.view.BoletoViewer; | |
import br.com.nordestefomento.jrimum.domkee.comum.pessoa.endereco.CEP; | |
import br.com.nordestefomento.jrimum.domkee.comum.pessoa.endereco.Endereco; | |
import br.com.nordestefomento.jrimum.domkee.comum.pessoa.endereco.UnidadeFederativa; | |
import br.com.nordestefomento.jrimum.domkee.financeiro.banco.Banco; | |
import br.com.nordestefomento.jrimum.domkee.financeiro.banco.Pessoa; | |
import br.com.nordestefomento.jrimum.domkee.financeiro.banco.febraban.Agencia; | |
import br.com.nordestefomento.jrimum.domkee.financeiro.banco.febraban.Carteira; | |
import br.com.nordestefomento.jrimum.domkee.financeiro.banco.febraban.ContaBancaria; | |
import br.com.nordestefomento.jrimum.domkee.financeiro.banco.febraban.Modalidade; | |
import br.com.nordestefomento.jrimum.domkee.financeiro.banco.febraban.NumeroDaConta; | |
import br.com.nordestefomento.jrimum.domkee.financeiro.banco.febraban.TipoDeCobranca; | |
import br.com.nordestefomento.jrimum.domkee.financeiro.banco.febraban.TipoDeTitulo; | |
import br.com.nordestefomento.jrimum.domkee.financeiro.banco.febraban.Titulo; | |
import br.com.nordestefomento.jrimum.domkee.financeiro.banco.febraban.Titulo.EnumAceite; | |
public class BoletoUtils { | |
private static String compensacao; | |
public static Boleto gerarBoleto(Date dataProcessamento, Date dataVencimento, ContaCorrente cc, Aluno aluno, | |
String valorBoleto, String instrucoes, String locaisPagamento, String instrucao1, String instrucao2) | |
throws EmpresaNullException, NumberFormatException, FileNotFoundException, IOException { | |
Empresa emp = new ControladorEmpresa().getEmpresa(); | |
if (emp == null) | |
throw new EmpresaNullException("Nenhuma empresa cadastrada"); | |
Pessoa cedente = new Pessoa(emp.getRazaoSocial(), emp.getCnpj()); | |
// Informando dados sobre a conta banc�ria do cendente. | |
Banco banco = cc.getBancoSuportado(); | |
compensacao = banco.getCodigoDeCompensacaoBACEN().getCodigoFormatado(); | |
ContaBancaria contaBancariaCed = new ContaBancaria(banco); | |
contaBancariaCed.setBanco(banco); | |
contaBancariaCed.setNumeroDaConta( | |
new NumeroDaConta(Integer.valueOf(cc.getContaCorrente().trim()), ""+cc.getDvContaCorrente())); | |
contaBancariaCed.setCarteira(new Carteira(Integer.valueOf(cc.getCarteira().trim()), TipoDeCobranca.SEM_REGISTRO)); | |
contaBancariaCed.setModalidade(new Modalidade(4)); | |
contaBancariaCed.setAgencia(new Agencia(Integer.valueOf(cc.getAgencia().trim()), ""+cc.getDvAgencia())); | |
cedente.addContaBancaria(contaBancariaCed); | |
Pessoa sacado = new Pessoa(aluno.getNome(), aluno.getCpf()); | |
// Informando o endereço do sacado. | |
Endereco enderecoSac = new Endereco(); | |
enderecoSac.setUF(UnidadeFederativa.valueOf(aluno.getUf())); | |
enderecoSac.setLocalidade(aluno.getCidade()); | |
enderecoSac.setCep(new CEP(aluno.getCep())); | |
enderecoSac.setBairro(aluno.getBairro()); | |
enderecoSac.setLogradouro(aluno.getEndereco()); | |
enderecoSac.setNumero(aluno.getNumero()+""); | |
sacado.addEndereco(enderecoSac); | |
Pessoa sacadorAvalista = | |
new Pessoa(emp.getRazaoSocial(), emp.getCnpj()); | |
// Informando o endere�o do sacador avalista. | |
Endereco enderecoSacAval = new Endereco(); | |
enderecoSacAval.setUF(UnidadeFederativa.DESCONHECIDO); | |
enderecoSacAval.setLocalidade(emp.getCidade()); | |
enderecoSacAval.setCep(new CEP(emp.getCep())); | |
enderecoSacAval.setBairro(emp.getBairro()); | |
enderecoSacAval.setLogradouro(emp.getEndereco()); | |
enderecoSacAval.setNumero(emp.getNumero()); | |
sacadorAvalista.addEndereco(enderecoSacAval); | |
Titulo titulo = new Titulo(contaBancariaCed, sacado, cedente,sacadorAvalista); | |
titulo.setNossoNumero(getNossoNumero(true)); | |
titulo.setNumeroDoDocumento(getNossoNumero(false)); | |
titulo.setDigitoDoNossoNumero(NossoNumeroUtils.getDigitioVerificador(getNossoNumero(false))); | |
titulo.setValor(new BigDecimal(valorBoleto)); | |
titulo.setDataDoDocumento(dataProcessamento); | |
titulo.setDataDoVencimento(dataVencimento); | |
titulo.setTipoDeDocumento(TipoDeTitulo.DM_DUPLICATA_MERCANTIL); | |
titulo.setAceite(EnumAceite.A); | |
// boolean caixa = compensacao.equals("104"); | |
Boleto boleto = new Boleto(titulo, CampoLivreFactory.create(titulo)); | |
boleto.setLocalPagamento(locaisPagamento); | |
boleto.setInstrucao1(instrucao1); | |
boleto.setInstrucao2(instrucao2); | |
boleto.setInstrucaoAoSacado(instrucoes); | |
return boleto; | |
} | |
public static void showBoletos(Boleto... boletos) throws IOException, JRimumException, BoletoException, CampoLivreException | |
{ | |
String nomeArquivo = | |
CaminhoUtils.CAMINHO_PASTA_ATHUS + "boleto"+NossoNumeroUtils.getNossoNumeroAtual().toString() | |
+".pdf"; | |
//Se gerar mais de um boleto, cria um nome aleat�rio para o arquivo | |
if (boletos.length > 1) | |
nomeArquivo = CaminhoUtils.CAMINHO_PASTA_ATHUS + Calendar.getInstance().getTimeInMillis() + ".pdf"; | |
File boletoViewer = null; | |
if(boletos.length > 1) | |
boletoViewer = BoletoViewer.groupInOnePDF(nomeArquivo, Arrays.asList(boletos)); | |
else | |
boletoViewer = new BoletoViewer(boletos[0]).getPdfAsFile(nomeArquivo); | |
Desktop desktop = Desktop.getDesktop(); | |
desktop.open(boletoViewer); | |
} | |
public static String getNossoNumero(boolean novo) throws FileNotFoundException, IOException | |
{ | |
String var = "99"; | |
String nossoString = novo ? NossoNumeroUtils.getNovoNossoNumero().toString() : | |
NossoNumeroUtils.getNossoNumeroAtual().toString(); | |
int size = compensacao.equals("001") ? 10 : 8; | |
int left = size - var.length() - nossoString.length(); | |
String rand = ""; | |
for(int i = 0; i < left; i++){ | |
rand = rand.concat("0"); | |
} | |
return var+rand+nossoString; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment