Created
February 5, 2020 08:04
-
-
Save steima/88ecf232697384f0647829706b64e55b to your computer and use it in GitHub Desktop.
Taxation.java
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 com.clesyclean.service.entities.base; | |
import java.math.BigDecimal; | |
import javax.persistence.Embeddable; | |
import lombok.Data; | |
@Data | |
@Embeddable | |
public class Taxation { | |
public static final int SCALE = 2; | |
public static final int ROUNDING_MODE = BigDecimal.ROUND_HALF_UP; | |
private BigDecimal net; | |
private BigDecimal taxRate; | |
private BigDecimal tax; | |
private BigDecimal gross; | |
private Taxation() { } | |
public static Taxation fromNet(BigDecimal net, BigDecimal taxRate) { | |
Taxation t = new Taxation(); | |
t.net = net.setScale(SCALE, ROUNDING_MODE); | |
t.taxRate = taxRate.setScale(SCALE, ROUNDING_MODE); | |
t.gross = t.net.multiply(Taxation.taxMultiplicator(taxRate)).setScale(SCALE, ROUNDING_MODE); | |
t.tax = t.gross.subtract(t.net); | |
return t; | |
} | |
public static Taxation fromNet(float net, float taxRate) { | |
return fromNet(new BigDecimal(net), new BigDecimal(taxRate)); | |
} | |
public static Taxation fromGross(BigDecimal gross, BigDecimal taxRate) { | |
Taxation t = new Taxation(); | |
t.gross = gross.setScale(SCALE, ROUNDING_MODE); | |
t.taxRate = taxRate.setScale(SCALE, ROUNDING_MODE); | |
BigDecimal m = Taxation.taxMultiplicator(taxRate); | |
t.net = t.gross.divide(m, SCALE, ROUNDING_MODE); | |
t.tax = t.gross.subtract(t.net); | |
return t; | |
} | |
public static Taxation fromGross(float gross, float taxRate) { | |
return fromGross(new BigDecimal(gross), new BigDecimal(taxRate)); | |
} | |
public static BigDecimal taxMultiplicator(BigDecimal taxRate) { | |
BigDecimal normalized = taxRate.divide(BigDecimal.valueOf(100f)); | |
return BigDecimal.ONE.add(normalized); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment