Skip to content

Commit

Permalink
Merge pull request #17 from jaimedantas/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
jaimedantas authored Jan 5, 2020
2 parents 3da3366 + 7f9dcef commit fd68cbe
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 32 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ This application is able to perform calculations with regards to taxes and
profit from FII and Stocks transactions in the [B3](http://www.b3.com.br/en_us/)
.

This system is up and running in [here](http://jaimedantas.com/b3).
This system is up and running [here](http://jaimedantas.com/b3).


## Index
* [FII Tax Calculator microservice](fii-tax-calculator/README.md)
* Stock Tax Calculator microservice (in progress..)
* [Frontend](fii-tax-calculator/README.md)

#### Tecnologies:
#### Tecnologies
###### Backend:
* Spring Boot 2.2.2
* Swagger 2
Expand All @@ -31,17 +31,17 @@ This system is up and running in [here](http://jaimedantas.com/b3).
* HTML 5
* JavaScript

## Documentation:
## Documentation
You can browser through the Swagger documentation for this API in the link below.

`http://jaimedantas.com/fii`

## Versioning
This project uses [GitHub](https://github.com/jaimedantas/fii-tax-calculator) for versioning.
For the versions available, see the [tags on this repository]().
For the versions available, see the [tags on this repository](https://github.com/jaimedantas/b3-tax-calculator/tags).

## Author
* [Jaime Dantas](https://jaimedantas.com/) - _Initial work, Development, Test, Documentation_
* [Jaime Dantas](https://jaimedantas.com/) - _Initial work, development, test, documentation_

See also the list of [contributors](CONTRIBUTORS.txt) who participated in this project.

Expand Down
12 changes: 6 additions & 6 deletions fii-tax-calculator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,31 @@ This application is able to perform calculations with regards to taxes and
profit from a FII transaction in the [B3](http://www.b3.com.br/en_us/)
.

This microservice is up and running in [here](http://jaimedantas.com/b3).
This microservice is up and running [here](http://jaimedantas.com/b3).

###### Tecnologies:
###### Tecnologies
* Spring Boot 2.2.2
* Swagger 2
* JUnit

## Documentation:
## Documentation
You can browser through the Swagger documentation for this API in the link below.

`http://jaimedantas.com/fii`


## Build:
## Build
```
mvn clean install
```

## Run:
## Run
```
java -jar fii-tax-calculator-0.0.1-SNAPSHOT.jar
```
## Usege

This microservice will run on port 8081 by default.
This microservice will run in the port 8081 by default.
To access the swagger interface, go to:
```
http://localhost:8081/swagger-ui.html
Expand Down
8 changes: 7 additions & 1 deletion fii-tax-calculator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>com.jaimedantas</groupId>
<artifactId>fii-tax-calculator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>0.0.2-SNAPSHOT</version>
<name>fii-tax-calculator</name>
<description>Tax calculator for FII in Brazil</description>

Expand Down Expand Up @@ -60,6 +60,12 @@
</exclusions>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,26 @@ public FiiTax calculeFiiTaxes(FiiData fiiData){
FiiTax result = new FiiTax();

BigDecimal netProfitValue = CalculationEngine.subtract(fiiData.getTotalValueSold(), fiiData.getTotalValueBought());
BigDecimal emolumentosTaxes = CalculationEngine.multiply(fiiData.getTotalValueBought(),B3Taxes.EMOLUMENTOS.atomicValue);
BigDecimal liquidacaoTaxes = CalculationEngine.multiply(fiiData.getTotalValueBought(),B3Taxes.LIQUIDACAO.atomicValue);
BigDecimal IRRFTaxes = CalculationEngine.multiply(fiiData.getTotalValueBought(),B3Taxes.IRRF.atomicValue);
BigDecimal totalTaxes = CalculationEngine.add(emolumentosTaxes,liquidacaoTaxes, IRRFTaxes);

BigDecimal emolumentosTaxesBuy = CalculationEngine.multiply(fiiData.getTotalValueBought(),B3Taxes.EMOLUMENTOS.atomicValue);
BigDecimal liquidacaoTaxesBuy = CalculationEngine.multiply(fiiData.getTotalValueBought(),B3Taxes.LIQUIDACAO.atomicValue);
BigDecimal emolumentosTaxesSell = CalculationEngine.multiply(fiiData.getTotalValueSold(),B3Taxes.EMOLUMENTOS.atomicValue);
BigDecimal liquidacaoTaxesSell = CalculationEngine.multiply(fiiData.getTotalValueSold(),B3Taxes.LIQUIDACAO.atomicValue);
BigDecimal IRRFTaxes = CalculationEngine.multiply(fiiData.getTotalValueSold(),B3Taxes.IRRF.atomicValue);

BigDecimal totalTaxes = CalculationEngine.add(emolumentosTaxesBuy,liquidacaoTaxesBuy, IRRFTaxes,
emolumentosTaxesSell, liquidacaoTaxesSell);
BigDecimal realProfitAfterTaxes = CalculationEngine.subtract(netProfitValue, totalTaxes);
BigDecimal fixTax = CalculationEngine.multiply(realProfitAfterTaxes,B3Taxes.FIX_TAX.atomicValue);

result.setEmolumentosFee(emolumentosTaxes);
result.setLiquidacaoFee(liquidacaoTaxes);
result.setEmolumentosFee(CalculationEngine.add(emolumentosTaxesBuy,emolumentosTaxesSell));
result.setLiquidacaoFee(CalculationEngine.add(emolumentosTaxesBuy,liquidacaoTaxesSell));
result.setIRRFFee(IRRFTaxes);
result.setFixedTax(fixTax);

BigDecimal totalProfit = CalculationEngine.subtract(netProfitValue, totalTaxes, fixTax);
BigDecimal totalProfitPercentage = CalculationEngine.divide(totalProfit, fiiData.getTotalValueBought());
BigDecimal totalProfitPercentage = CalculationEngine.divide(totalProfit, CalculationEngine.add(fiiData.getTotalValueBought(),
liquidacaoTaxesBuy, emolumentosTaxesBuy));

result.setTotalTaxes(totalTaxes);
result.setTotalProfitValue(totalProfit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import com.jaimedantas.fiitaxcalculator.model.Error;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import javax.servlet.http.HttpServletRequest;

@Order(Ordered.HIGHEST_PRECEDENCE)
@ControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public static BigDecimal add(BigDecimal arg1, BigDecimal arg2, BigDecimal arg3,
return add(add(arg1, arg2, arg3), arg4);
}

public static BigDecimal add(BigDecimal arg1, BigDecimal arg2, BigDecimal arg3, BigDecimal arg4, BigDecimal arg5){
return add(add(arg1, arg2, arg3, arg4), arg4, arg5);
}

public static BigDecimal subtract(BigDecimal arg1, BigDecimal arg2){
return arg1.subtract(arg2).setScale(2, RoundingMode.HALF_UP);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ void shouldCalculateTaxForFiiXPLG11() {
fiiData.setTotalValueSold(new BigDecimal("3013.71"));

BigDecimal expectedTotalProfitPercentage = new BigDecimal("0.34");
BigDecimal expectedTotalProfitValue = new BigDecimal("713.55");
BigDecimal expectedTDarf = new BigDecimal("178.39");
BigDecimal expectedTotalProfitValue = new BigDecimal("712.66");
BigDecimal expectedTDarf = new BigDecimal("178.17");


TaxCalculator taxCalculator = new TaxCalculator();
Expand All @@ -44,12 +44,13 @@ void shouldCalculateTaxForFiiXPML11() {
fiiData.setQuantityBought(47);
fiiData.setQuantitySold(47);
fiiData.setSoldPriceUnit(new BigDecimal("138.99"));
fiiData.setTotalValueBought(new BigDecimal("5168.59"));
fiiData.setTotalValueBought(new BigDecimal("5295.93"));
fiiData.setTotalValueSold(new BigDecimal("6532.53"));

BigDecimal expectedTotalProfitPercentage = new BigDecimal("0.21");
BigDecimal expectedTotalProfitValue = new BigDecimal("1089.64");
BigDecimal expectedTDarf = new BigDecimal("272.41");
BigDecimal expectedTotalProfitPercentage = new BigDecimal("0.19");
BigDecimal expectedTotalProfitValue = new BigDecimal("985.82");
//darf xpml: 246.93
BigDecimal expectedTDarf = new BigDecimal("246.46");


TaxCalculator taxCalculator = new TaxCalculator();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.jaimedantas.fiitaxcalculator.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.jaimedantas.fiitaxcalculator.model.FiiData;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import java.math.BigDecimal;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@WebMvcTest(RestController.class)
public class RestControllerTest {

@Autowired
private MockMvc mvc;

@Test
public void shoudCallApiAndReturnSucess() throws Exception {

FiiData imput = new FiiData();
imput.setTotalValueSold(new BigDecimal("9029"));
imput.setTotalValueBought(new BigDecimal("1029"));

ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(imput);

this.mvc.perform(post("/tax/fii")
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().is2xxSuccessful());
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,68 @@ void shouldDivideTwoBigDecimals() {
assertEquals(expected, result);
}

@Test
void shouldAddThreeBigDecimals() {
BigDecimal num1 = new BigDecimal("10.00");
BigDecimal num2 = new BigDecimal("20.00");
BigDecimal num3 = new BigDecimal("21943.00");
BigDecimal expected = new BigDecimal("21973.00");

BigDecimal result = CalculationEngine.add(num1, num2, num3);

assertEquals(expected, result);
}

@Test
void shouldAddFourBigDecimals() {
BigDecimal num1 = new BigDecimal("10.00");
BigDecimal num2 = new BigDecimal("20.00");
BigDecimal num3 = new BigDecimal("2199.00");
BigDecimal num4 = new BigDecimal("329.00");
BigDecimal expected = new BigDecimal("2558.00");

BigDecimal result = CalculationEngine.add(num1, num2, num3, num4);

assertEquals(expected, result);
}

@Test
void shouldAddFiveBigDecimals() {
BigDecimal num1 = new BigDecimal("10.00");
BigDecimal num2 = new BigDecimal("20.00");
BigDecimal num3 = new BigDecimal("2199.00");
BigDecimal num4 = new BigDecimal("329.00");
BigDecimal num5 = new BigDecimal("323400");
BigDecimal expected = new BigDecimal("326287.00");

BigDecimal result = CalculationEngine.add(num1, num2, num3, num4, num5);

assertEquals(expected, result);
}

@Test
void shouldSubtractTwoBigDecimals() {
BigDecimal num1 = new BigDecimal("10.00");
BigDecimal num2 = new BigDecimal("20.00");
BigDecimal expected = new BigDecimal("-10.00");

BigDecimal result = CalculationEngine.subtract(num1, num2);

assertEquals(expected, result);
}

@Test
void shouldSubtractThreeBigDecimals() {
BigDecimal num1 = new BigDecimal("450.00");
BigDecimal num2 = new BigDecimal("20.00");
BigDecimal num3 = new BigDecimal("10.00");
BigDecimal expected = new BigDecimal("420.00");

BigDecimal result = CalculationEngine.subtract(num1, num2, num3);

assertEquals(expected, result);
}

@Test
void shouldDivideBigdecimalWithATooBigBigdecimal() {
BigDecimal num1 = new BigDecimal("10.00");
Expand Down
4 changes: 2 additions & 2 deletions frontend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

Frontend of the [B3 Tax Calculator](../README.md).

This service is up and running in [here](http://jaimedantas.com/b3).
This service is up and running [here](http://jaimedantas.com/b3).

###### Tecnologies:
###### Tecnologies
* HTML 5
* JavaScript

Expand Down

0 comments on commit fd68cbe

Please sign in to comment.