use of org.estatio.module.budget.dom.keyitem.KeyItem in project estatio by estatio.
the class BudgetCalculationService method calculateForTotalAndType.
private List<BudgetCalculationViewmodel> calculateForTotalAndType(final PartitionItem partitionItem, final BigDecimal total, final BudgetCalculationType calculationType) {
List<BudgetCalculationViewmodel> results = new ArrayList<>();
BigDecimal divider = partitionItem.getKeyTable().getKeyValueMethod().divider(partitionItem.getKeyTable());
for (KeyItem keyItem : partitionItem.getKeyTable().getItems()) {
BudgetCalculationViewmodel calculationResult;
calculationResult = new BudgetCalculationViewmodel(partitionItem, keyItem, total.multiply(keyItem.getValue()).divide(divider, MathContext.DECIMAL64).setScale(keyItem.getKeyTable().getPrecision(), BigDecimal.ROUND_HALF_UP), calculationType);
results.add(calculationResult);
}
return results;
}
use of org.estatio.module.budget.dom.keyitem.KeyItem in project estatio by estatio.
the class KeyTable method generateItems.
@Action(semantics = SemanticsOf.NON_IDEMPOTENT_ARE_YOU_SURE)
public KeyTable generateItems() {
// delete old items
for (Iterator<KeyItem> it = this.getItems().iterator(); it.hasNext(); ) {
it.next().deleteBudgetKeyItem();
}
/*
create list of input pairs: identifier - sourcevalue
sourcevalue is determined by FoundationValueType
*/
List<Distributable> input = new ArrayList<>();
for (Unit unit : unitRepository.findByProperty(this.getBudget().getProperty())) {
if (unitIntervalValidForThisKeyTable(unit)) {
BigDecimal sourceValue;
if (getFoundationValueType().valueOf(unit) != null) {
sourceValue = getFoundationValueType().valueOf(unit);
} else {
sourceValue = BigDecimal.ZERO;
}
KeyItem newItem = new KeyItem();
newItem.setSourceValue(sourceValue);
newItem.setValue(BigDecimal.ZERO);
newItem.setUnit(unit);
newItem.setKeyTable(this);
persistIfNotAlready(newItem);
input.add(newItem);
}
}
/*
call distribute method
*/
DistributionService distributionService = new DistributionService();
distributionService.distribute(input, getKeyValueMethod().divider(this), getPrecision());
return this;
}
use of org.estatio.module.budget.dom.keyitem.KeyItem in project estatio by estatio.
the class DistributionService_Test method generateKeyValuesRoundingBy6DecimalsPositiveDeltaPercent.
@Test
public void generateKeyValuesRoundingBy6DecimalsPositiveDeltaPercent() {
// given
DistributionService distributionService = new DistributionService();
List<Distributable> input = new ArrayList<>();
for (int i = 2; i <= 25; i = i + 1) {
KeyItem item = new KeyItem();
item.setSourceValue(new BigDecimal(i).multiply(new BigDecimal(100).setScale(2)));
input.add(item);
}
// when
List<Distributable> output = distributionService.distribute(input, new BigDecimal(1000), 6);
BigDecimal sumRoundedValues = BigDecimal.ZERO;
for (Distributable object : output) {
sumRoundedValues = sumRoundedValues.add(object.getValue(), MathContext.DECIMAL64);
}
assertThat(output).hasSize(24);
assertThat(sumRoundedValues.setScale(6, BigDecimal.ROUND_HALF_UP)).isEqualTo(new BigDecimal(1000).setScale(6, BigDecimal.ROUND_HALF_UP));
}
use of org.estatio.module.budget.dom.keyitem.KeyItem in project estatio by estatio.
the class DistributionService_Test method generateKeyValuesRoundingBy2DecimalsPositiveDelta.
@Test
public void generateKeyValuesRoundingBy2DecimalsPositiveDelta() {
// given
DistributionService distributionService = new DistributionService();
List<Distributable> input = new ArrayList<>();
// 10000.99
KeyItem item1 = new KeyItem();
item1.setSourceValue(BigDecimal.valueOf(10000.99));
input.add(item1);
// 100.09
KeyItem item2 = new KeyItem();
item2.setSourceValue(BigDecimal.valueOf(100.09));
input.add(item2);
// 1.99
for (int i = 2; i < 14; i = i + 1) {
KeyItem item = new KeyItem();
item.setSourceValue(BigDecimal.valueOf(1.99));
input.add(item);
}
// theoretical max rounding error: 14*0.005 = +/- 0.07
// in this example here we get to + 0.05
// when
List<Distributable> output = distributionService.distribute(input, BigDecimal.valueOf(1000), 2);
BigDecimal sumRoundedValues = BigDecimal.ZERO;
for (Distributable object : output) {
sumRoundedValues = sumRoundedValues.add(object.getValue(), MathContext.DECIMAL32);
}
// then
assertThat(output).hasSize(14);
assertThat(output.get(0).getValue()).isEqualTo(BigDecimal.valueOf(987.76).setScale(2, BigDecimal.ROUND_HALF_UP));
assertThat(output.get(1).getValue()).isEqualTo(BigDecimal.valueOf(9.89).setScale(2, BigDecimal.ROUND_HALF_UP));
for (int i = 2; i < 9; i = i + 1) {
assertThat(output.get(i).getValue()).isEqualTo(BigDecimal.valueOf(0.20).setScale(2, BigDecimal.ROUND_HALF_UP));
}
for (int i = 9; i < 14; i = i + 1) {
assertThat(output.get(i).getValue()).isEqualTo(BigDecimal.valueOf(0.19).setScale(2, BigDecimal.ROUND_HALF_UP));
}
assertThat(sumRoundedValues.setScale(2, BigDecimal.ROUND_HALF_UP)).isEqualTo(BigDecimal.valueOf(1000.00).setScale(2, BigDecimal.ROUND_HALF_UP));
}
use of org.estatio.module.budget.dom.keyitem.KeyItem in project estatio by estatio.
the class DistributionService_Test method generateKeyValuesRoundingBy6DecimalsPositiveDeltaPercent2.
@Test
public void generateKeyValuesRoundingBy6DecimalsPositiveDeltaPercent2() {
// given
DistributionService distributionService = new DistributionService();
List<Distributable> input = new ArrayList<>();
for (int i = 1; i <= 25; i = i + 1) {
KeyItem item = new KeyItem();
item.setSourceValue(new BigDecimal(i).multiply(new BigDecimal(100).setScale(2)));
input.add(item);
}
// when
List<Distributable> output = distributionService.distribute(input, new BigDecimal(1000, MathContext.DECIMAL64), 5);
BigDecimal sumRoundedValues = BigDecimal.ZERO;
for (Distributable object : output) {
sumRoundedValues = sumRoundedValues.add(object.getValue(), MathContext.DECIMAL64);
}
assertThat(output).hasSize(25);
assertThat(sumRoundedValues.setScale(5, BigDecimal.ROUND_HALF_UP)).isEqualTo(new BigDecimal(1000).setScale(5, BigDecimal.ROUND_HALF_UP));
}
Aggregations