Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added build/jrimum-bopepo-0.2.4-dep-sources.jar
Binary file not shown.
Binary file added build/jrimum-bopepo-0.2.4-dep.jar
Binary file not shown.
Binary file added build/jrimum-bopepo-0.2.4-examples.zip
Binary file not shown.
Binary file added build/jrimum-bopepo-0.2.4-sources.jar
Binary file not shown.
Binary file added build/jrimum-bopepo-0.2.4.jar
Binary file not shown.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<artifactId>jrimum-bopepo</artifactId>
<packaging>jar</packaging>
<name>Projeto Bopepo</name>
<version>0.2.3-DEV-SNAPSHOT</version>
<version>0.2.4</version>
<description>Projeto open source de componentes de software para o domínio de negócios do Brasil.</description>
<url>http://www.jrimum.org/bopepo</url>

Expand Down Expand Up @@ -176,12 +176,12 @@
<dependency>
<groupId>org.jrimum</groupId>
<artifactId>jrimum-domkee</artifactId>
<version>0.2.3-DEV-SNAPSHOT</version>
<version>0.2.4</version>
</dependency>
<dependency>
<groupId>org.jrimum</groupId>
<artifactId>jrimum-texgit</artifactId>
<version>0.2.3-DEV-SNAPSHOT</version>
<version>0.2.4</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
Expand Down
105 changes: 67 additions & 38 deletions src/main/java/org/jrimum/bopepo/FatorDeVencimento.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@

package org.jrimum.bopepo;

import static java.lang.String.format;
import static org.jrimum.utilix.Objects.isNull;
import static org.jrimum.utilix.text.DateFormat.DDMMYYYY_B;

import java.util.Calendar;
import java.util.Date;
Expand Down Expand Up @@ -74,14 +72,22 @@ public class FatorDeVencimento{
* </p>
*/
private static final Date DATA_BASE_DO_FATOR_DE_VENCIMENTO = BASE_DO_FATOR_DE_VENCIMENTO.getTime();

/**
*<p>
* Data máxima alcançada pelo fator de vencimento com base fixada em
* 07/10/1997.
* <p>
* Bruno Augusto da Silva <brunoaugustosilva8@gmail.com>
* Range de controle de fatores vencidos.
* </p>
*/
private static final Date DATA_LIMITE_DO_FATOR_DE_VENCIMENTO = new GregorianCalendar(2025, Calendar.FEBRUARY, 21).getTime();
private static final int RANGE_FATORES_VENCIDOS = 3001;

/**
* <p>
* Bruno Augusto da Silva <brunoaugustosilva8@gmail.com>
* Range de controle de fatores a vencer.
* </p>
*/
private static final int RANGE_FATORES_A_VENCER = 5500;

/**
* <p>
Expand Down Expand Up @@ -140,12 +146,40 @@ public static int toFator(Date data) throws IllegalArgumentException {

} else {

Date dataHojeTruncada = DateUtils.truncate(new Date(), Calendar.DATE);

Date dataTruncada = DateUtils.truncate(data, Calendar.DATE);

checkIntervalo(dataTruncada);

return (int) Dates.calculeDiferencaEmDias(DATA_BASE_DO_FATOR_DE_VENCIMENTO, dataTruncada);
return geraFator(dataTruncada, dataHojeTruncada);
}
}


/**
* <p>Gera um fator de vencimento com base na data de hoje</p>
*
* @param dataVencimento - Data de vencimento truncada de um titulo
* @param dataHoje - Data de execucao truncada
* @return fator de vencimento calculado e validado
* @throws IllegalArgumentException Caso o {@code fatorDeVencimento} < {@code fatorDeHoje} - {@linkplain #RANGE_FATORES_VENCIDOS}
* ou {@code fatorDeVencimento} > {@code fatorDeHoje} + {@linkplain #RANGE_FATORES_A_VENCER}
*/
private static int geraFator(Date dataVencimento, Date dataHoje) throws IllegalArgumentException {

int fatorDeVencimento = (int) Dates.calculeDiferencaEmDias(DATA_BASE_DO_FATOR_DE_VENCIMENTO, dataVencimento);
int fatorDeHoje = (int) Dates.calculeDiferencaEmDias(DATA_BASE_DO_FATOR_DE_VENCIMENTO, dataHoje);

checkIntervaloHoje(fatorDeVencimento, fatorDeHoje);

while(fatorDeVencimento > 9999) {
if(fatorDeVencimento - 9000 > 9999) {
fatorDeVencimento -= 10000;
}else {
fatorDeVencimento -= 9000;
}
}

return fatorDeVencimento;
}

/**
Expand All @@ -170,34 +204,6 @@ public static Date toDate(int fator) throws IllegalArgumentException {

return DateUtils.truncate(date.getTime(), Calendar.DATE);
}

/**
* <p>
* Lança exceção caso a {@code dataVencimentoTruncada} esteja fora do
* intervalo entre a {@linkplain #DATA_BASE_DO_FATOR_DE_VENCIMENTO} e a
* {@linkplain #DATA_LIMITE_DO_FATOR_DE_VENCIMENTO}.
* </p>
*
* @param dataVencimentoTruncada
* data de vencimento truncada com {@code
* DateUtils.truncate(date, Calendar.DATE)}
* @throws IllegalArgumentException
* Caso a data esteja {@code dataVencimentoTruncada} esteja fora
* do intervalo entre a
* {@linkplain #DATA_BASE_DO_FATOR_DE_VENCIMENTO} e a
* {@linkplain #DATA_LIMITE_DO_FATOR_DE_VENCIMENTO}
*/
private static void checkIntervalo(Date dataVencimentoTruncada) throws IllegalArgumentException {

if(dataVencimentoTruncada.before(DATA_BASE_DO_FATOR_DE_VENCIMENTO)
|| dataVencimentoTruncada.after(DATA_LIMITE_DO_FATOR_DE_VENCIMENTO)) {

Exceptions.throwIllegalArgumentException(
format("Para o cálculo do fator de vencimento se faz necessário informar uma data entre %s e %s.",
DDMMYYYY_B.format(DATA_BASE_DO_FATOR_DE_VENCIMENTO), DDMMYYYY_B.format(DATA_LIMITE_DO_FATOR_DE_VENCIMENTO)));

}
}

/**
* <p>Lança exceção caso o {@code fator} estja fora do intervalo.</p>
Expand All @@ -213,4 +219,27 @@ private static void checkIntervalo(int fatorDeVencimento) throws IllegalArgument
"Impossível transformar em data um fator menor que zero! O fator de vencimento deve ser um número entre 0 e 9999.");
}
}

/**
* <p>
* Lança exceção caso o {@code fatorDeVencimento} esteja fora do intervalo movel
* definido pela data de execucao.
* </p>
*
* @param fatorDeVencimento - Número entre o a data base e o vencimento atual
* @param fatorDeHoje - Número entre o a data base e a data de execucao
* @throws IllegalArgumentException Caso o {@code fatorDeVencimento} < {@code fatorDeHoje} - {@linkplain #RANGE_FATORES_VENCIDOS}
* ou {@code fatorDeVencimento} > {@code fatorDeHoje} + {@linkplain #RANGE_FATORES_A_VENCER}
*/
private static void checkIntervaloHoje(int fatorDeVencimento, int fatorDeHoje) throws IllegalArgumentException {

if (fatorDeHoje - RANGE_FATORES_VENCIDOS > fatorDeVencimento) {
Exceptions.throwIllegalArgumentException(
"Data de vencimento menor do que o limite permitido de notas vencidas.");
}
else if(fatorDeHoje + RANGE_FATORES_A_VENCER < fatorDeVencimento) {
Exceptions.throwIllegalArgumentException(
"Data de vencimento maior do que o limite permitido de notas a vencer.");
}
}
}
2 changes: 1 addition & 1 deletion src/test/java/org/jrimum/bopepo/TestBoleto.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class TestBoleto{

private Titulo titulo;

private Date VENCIMENTO = new GregorianCalendar(2000, Calendar.JULY, 3).getTime();
private Date VENCIMENTO = new GregorianCalendar().getTime();

private Boleto boleto;

Expand Down
40 changes: 36 additions & 4 deletions src/test/java/org/jrimum/bopepo/TestCodigoDeBarras.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.jrimum.domkee.financeiro.banco.febraban.Titulo;
import org.junit.Before;
import org.junit.Test;
import org.jrimum.bopepo.FatorDeVencimento;

/**
*
Expand All @@ -73,8 +74,9 @@ public class TestCodigoDeBarras{
private Titulo titulo;

private CodigoDeBarras codigoDeBarras;
private CodigoDeBarras codigoDeBarras2;

private Date VENCIMENTO = new GregorianCalendar(2000, Calendar.JULY, 3).getTime();
private Date VENCIMENTO = new GregorianCalendar().getTime();

@Before
public void setUp() throws Exception {
Expand Down Expand Up @@ -105,14 +107,44 @@ public void setUp() throws Exception {
codigoDeBarras = new CodigoDeBarras(titulo, clBradesco);

}

@Before
public void setUpTwo() throws Exception {

Sacado sacado = new Sacado("Sacado");
Cedente cedente = new Cedente("Cedente");

ContaBancaria contaBancaria = new ContaBancaria();
contaBancaria.setBanco(BancosSuportados.BANCO_BRADESCO.create());

Agencia agencia = new Agencia(1234, "1");
contaBancaria.setAgencia(agencia);

contaBancaria.setCarteira(new Carteira(5));

NumeroDaConta numeroDaConta = new NumeroDaConta();
numeroDaConta.setCodigoDaConta(6789);
contaBancaria.setNumeroDaConta(numeroDaConta);

titulo = new Titulo(contaBancaria, sacado, cedente);
titulo.setNossoNumero("12345678901");
titulo.setTipoDeMoeda(TipoDeMoeda.REAL);
titulo.setValor(BigDecimal.valueOf(100.23));
titulo.setDataDoVencimento(VENCIMENTO);

clBradesco = CampoLivreFactory.create(titulo);

codigoDeBarras2 = new CodigoDeBarras(titulo, clBradesco);

}

/**
* Test method for
* {@link org.jrimum.bopepo.CodigoDeBarras#getDigitoVerificadorGeral()}.
*/
@Test
public void testGetDigitoVerificadorGeral() {
assertTrue(2 == codigoDeBarras.getDigitoVerificadorGeral().getValue());
assertTrue(codigoDeBarras2.getDigitoVerificadorGeral().getValue() == codigoDeBarras.getDigitoVerificadorGeral().getValue());
}

/**
Expand All @@ -122,7 +154,7 @@ public void testGetDigitoVerificadorGeral() {
@Test
public void testWrite() {

assertEquals("23792100000000100231234051234567890100067890", codigoDeBarras.write());
assertEquals(codigoDeBarras2.write(), codigoDeBarras.write());

}

Expand All @@ -133,7 +165,7 @@ public void testWrite() {
@Test
public void testGetFatorDeVencimento() {

assertTrue(1000 == codigoDeBarras.getFatorDeVencimento().getValue());
assertTrue(FatorDeVencimento.toFator(new Date()) == codigoDeBarras.getFatorDeVencimento().getValue());

}

Expand Down
37 changes: 26 additions & 11 deletions src/test/java/org/jrimum/bopepo/TestFatorDeVencimento.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,38 @@ public void testToFatorComDataMenorQueDataBase() {

@Test(expected = IllegalArgumentException.class)
public void testToFatorComDataMaiorQueDataLimite() {

data.set(2025, Calendar.FEBRUARY, 22);

data = new GregorianCalendar();

data.add(Calendar.DAY_OF_YEAR, 5501);

FatorDeVencimento.toFator(data.getTime());
}

@Test
public final void testToFator() {

data.set(2000, Calendar.JULY, 3);
assertEquals(1000, FatorDeVencimento.toFator(data.getTime()));

data.set(2000, Calendar.JULY, 5);
assertEquals(1002, FatorDeVencimento.toFator(data.getTime()));

data.set(2025, Calendar.FEBRUARY, 21);
assertEquals(9999, FatorDeVencimento.toFator(data.getTime()));

data = new GregorianCalendar();

GregorianCalendar dataHoje = new GregorianCalendar();

data.add(Calendar.DAY_OF_YEAR, -1000);
dataHoje.add(Calendar.DAY_OF_YEAR, -1000);
assertEquals(FatorDeVencimento.toFator(dataHoje.getTime()), FatorDeVencimento.toFator(data.getTime()));

data = new GregorianCalendar();
dataHoje = new GregorianCalendar();

data.add(Calendar.DAY_OF_YEAR, 1000);
dataHoje.add(Calendar.DAY_OF_YEAR, 1000);
assertEquals(FatorDeVencimento.toFator(dataHoje.getTime()), FatorDeVencimento.toFator(data.getTime()));

data = new GregorianCalendar();
dataHoje = new GregorianCalendar();

data.add(Calendar.DAY_OF_YEAR, 5500);
dataHoje.add(Calendar.DAY_OF_YEAR, 5500);
assertEquals(FatorDeVencimento.toFator(dataHoje.getTime()), FatorDeVencimento.toFator(data.getTime()));
}

@Test(expected = IllegalArgumentException.class)
Expand Down
37 changes: 35 additions & 2 deletions src/test/java/org/jrimum/bopepo/TestLinhaDigitavel.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ public class TestLinhaDigitavel{
private CodigoDeBarras codigoDeBarras;

private LinhaDigitavel linhaDigitavel;
private LinhaDigitavel linhaDigitavel2;

private Date VENCIMENTO = DateFormat.DDMMYYYY_B.parse("03/07/2000");
private Date VENCIMENTO = new Date();

@Before
public void setUp() throws Exception {
Expand Down Expand Up @@ -97,13 +98,45 @@ public void setUp() throws Exception {

}

@Before
public void setUpTwo() throws Exception {

Sacado sacado = new Sacado("Sacado");
Cedente cedente = new Cedente("Cedente");

ContaBancaria contaBancaria = new ContaBancaria();
contaBancaria.setBanco(BancosSuportados.BANCO_BRADESCO.create());

Agencia agencia = new Agencia(1234, "1");
contaBancaria.setAgencia(agencia);

contaBancaria.setCarteira(new Carteira(5));

NumeroDaConta numeroDaConta = new NumeroDaConta();
numeroDaConta.setCodigoDaConta(6789);
contaBancaria.setNumeroDaConta(numeroDaConta);

titulo = new Titulo(contaBancaria, sacado, cedente);
titulo.setNossoNumero("12345678901");
titulo.setTipoDeMoeda(TipoDeMoeda.REAL);
titulo.setValor(BigDecimal.valueOf(100.23));
titulo.setDataDoVencimento(VENCIMENTO);

clBradesco = CampoLivreFactory.create(titulo);

codigoDeBarras = new CodigoDeBarras(titulo, clBradesco);

linhaDigitavel2 = new LinhaDigitavel(codigoDeBarras);

}

/**
* Test method for {@link org.jrimum.bopepo.LinhaDigitavel#toString()}.
*/
@Test
public void testWrite() {

assertEquals("23791.23405 51234.567892 01000.678902 2 10000000010023", linhaDigitavel.write());
assertEquals(linhaDigitavel2.write(), linhaDigitavel.write());

}

Expand Down