use of org.estatio.module.budget.dom.keyitem.KeyItem in project estatio by estatio.
the class KeyItemImportExportLineItem method apply.
// region > apply (action)
@Action(semantics = SemanticsOf.IDEMPOTENT, invokeOn = InvokeOn.OBJECT_AND_COLLECTION, publishing = Publishing.DISABLED)
public KeyItem apply() {
switch(getStatus()) {
case ADDED:
KeyItem keyItem = new KeyItem();
keyItem.setKeyTable(getKeyTable());
keyItem.setUnit(getUnit());
keyItem.setValue(getKeyValue().setScale(keyTable.getPrecision(), BigDecimal.ROUND_HALF_UP));
keyItem.setSourceValue(getSourceValue().setScale(2, BigDecimal.ROUND_HALF_UP));
container.persistIfNotAlready(keyItem);
break;
case UPDATED:
getKeyItem().changeValue(this.getKeyValue().setScale(keyTable.getPrecision(), BigDecimal.ROUND_HALF_UP));
getKeyItem().setSourceValue(this.getSourceValue().setScale(2, BigDecimal.ROUND_HALF_UP));
break;
case DELETED:
String message = "KeyItem for unit " + getKeyItem().getUnit().getReference() + " deleted";
getKeyItem().deleteBudgetKeyItem();
container.informUser(message);
return null;
case NOT_FOUND:
container.informUser("KeyItem not found");
return null;
default:
break;
}
return getKeyItem();
}
use of org.estatio.module.budget.dom.keyitem.KeyItem in project estatio by estatio.
the class DistributionService_Test method originalOrderPreserved.
@Test
public void originalOrderPreserved() {
// given
DistributionService distributionService = new DistributionService();
List<Distributable> input = new ArrayList<>();
for (int i = 0; i <= 25; i = i + 1) {
KeyItem item = new KeyItem();
BigDecimal sourceValue = new BigDecimal(i).multiply(new BigDecimal(100).setScale(2));
item.setSourceValue(sourceValue);
input.add(item);
}
// when
List<Distributable> output = distributionService.distribute(input, BigDecimal.ONE, 3);
// then
int i = 0;
for (Distributable outputItem : output) {
BigDecimal sourceValue = new BigDecimal(i).multiply(new BigDecimal(100).setScale(2));
assertThat(outputItem.getSourceValue()).isEqualTo(sourceValue);
i++;
}
}
use of org.estatio.module.budget.dom.keyitem.KeyItem in project estatio by estatio.
the class DistributionService_Test method generateKeyValuesRoundingBy6DecimalsPositiveDelta.
@Test
public void generateKeyValuesRoundingBy6DecimalsPositiveDelta() {
// given
DistributionService distributionService = new DistributionService();
List<Distributable> input = new ArrayList<>();
// 10000.99 1 item
KeyItem item1 = new KeyItem();
item1.setSourceValue(BigDecimal.valueOf(10000.99));
input.add(item1);
// 0.99 1 item
KeyItem item2 = new KeyItem();
item2.setSourceValue(BigDecimal.valueOf(0.99));
input.add(item2);
// 1.99 10 items
for (int i = 1; i < 11; i = i + 1) {
KeyItem item = new KeyItem();
item.setSourceValue(BigDecimal.valueOf(1.99));
input.add(item);
}
// 0 2 items
for (int i = 11; i < 13; i = i + 1) {
KeyItem item = new KeyItem();
item.setSourceValue(BigDecimal.ZERO);
input.add(item);
}
// theoretical max rounding error: 14*0.0000005 = +/- 0.000007
// in this example here we get to + 0.000005
// when
List<Distributable> output = distributionService.distribute(input, new BigDecimal(1000), 6);
BigDecimal sumRoundedValues = BigDecimal.ZERO;
for (Distributable object : output) {
sumRoundedValues = sumRoundedValues.add(object.getValue());
}
// then
assertThat(output).hasSize(14);
assertThat(output.get(0).getValue()).isEqualTo(BigDecimal.valueOf(997.915561).setScale(6, BigDecimal.ROUND_HALF_UP));
assertThat(output.get(1).getValue()).isEqualTo(BigDecimal.valueOf(0.098784).setScale(6, BigDecimal.ROUND_HALF_UP));
// 5 items not rounded
for (int i = 2; i < 7; i = i + 1) {
assertThat(output.get(i).getValue()).isEqualTo(BigDecimal.valueOf(0.198566).setScale(6, BigDecimal.ROUND_HALF_UP));
}
// 5 items rounded down
for (int i = 7; i < 12; i = i + 1) {
assertThat(output.get(i).getValue()).isEqualTo(BigDecimal.valueOf(0.198565).setScale(6, BigDecimal.ROUND_HALF_UP));
}
for (int i = 12; i < 13; i = i + 1) {
assertThat(output.get(i).getValue()).isEqualTo(BigDecimal.ZERO.setScale(6, BigDecimal.ROUND_HALF_UP));
}
assertThat(sumRoundedValues.setScale(6, BigDecimal.ROUND_HALF_UP)).isEqualTo(BigDecimal.valueOf(1000.000000).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 generateKeyValuesRoundingBy3DecimalsPositiveDelta.
@Test
public void generateKeyValuesRoundingBy3DecimalsPositiveDelta() {
// 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.01
KeyItem item2 = new KeyItem();
item2.setSourceValue(BigDecimal.valueOf(100.01));
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.0005 = +/- 0.007
// in this example here we get to + 0.006
// when
List<Distributable> output = distributionService.distribute(input, new BigDecimal(1000), 3);
BigDecimal sumRoundedValues = BigDecimal.ZERO;
for (Distributable object : output) {
sumRoundedValues = sumRoundedValues.add(object.getValue());
}
// then
assertThat(output).hasSize(14);
assertThat(output.get(0).getValue()).isEqualTo(BigDecimal.valueOf(987.764).setScale(3, BigDecimal.ROUND_HALF_UP));
assertThat(output.get(1).getValue()).isEqualTo(BigDecimal.valueOf(9.878).setScale(3, BigDecimal.ROUND_HALF_UP));
for (int i = 2; i < 8; i = i + 1) {
assertThat(output.get(i).getValue()).isEqualTo(BigDecimal.valueOf(0.197).setScale(3, BigDecimal.ROUND_HALF_UP));
}
for (int i = 8; i < 14; i = i + 1) {
assertThat(output.get(i).getValue()).isEqualTo(BigDecimal.valueOf(0.196).setScale(3, BigDecimal.ROUND_HALF_UP));
}
assertThat(sumRoundedValues.setScale(3, BigDecimal.ROUND_HALF_UP)).isEqualTo(BigDecimal.valueOf(1000.000).setScale(3, BigDecimal.ROUND_HALF_UP));
}
use of org.estatio.module.budget.dom.keyitem.KeyItem in project estatio by estatio.
the class DistributionService_Test method generateKeyValuesRoundingBy3DecimalsNegativeDeltaPercent.
@Test
public void generateKeyValuesRoundingBy3DecimalsNegativeDeltaPercent() {
// 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);
// 200.99
KeyItem item2 = new KeyItem();
item2.setSourceValue(BigDecimal.valueOf(200.99));
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.0005 = +/- 0.007
// in this example here we get to -0.005
// when
List<Distributable> output = distributionService.distribute(input, new BigDecimal(100), 3);
BigDecimal sumRoundedValues = BigDecimal.ZERO;
for (Distributable object : output) {
sumRoundedValues = sumRoundedValues.add(object.getValue(), MathContext.DECIMAL64);
}
// then
assertThat(output).hasSize(14);
assertThat(output.get(0).getValue()).isEqualTo(BigDecimal.valueOf(97.801).setScale(3, BigDecimal.ROUND_HALF_UP));
assertThat(output.get(1).getValue()).isEqualTo(BigDecimal.valueOf(1.966).setScale(3, BigDecimal.ROUND_HALF_UP));
for (int i = 2; i < 9; i = i + 1) {
assertThat(output.get(i).getValue()).isEqualTo(BigDecimal.valueOf(0.019).setScale(3, BigDecimal.ROUND_HALF_UP));
}
for (int i = 9; i < 14; i = i + 1) {
assertThat(output.get(i).getValue()).isEqualTo(BigDecimal.valueOf(0.020).setScale(3, BigDecimal.ROUND_HALF_UP));
}
// Corrected Rounding Error for 3 decimals
assertThat(sumRoundedValues.setScale(3, BigDecimal.ROUND_HALF_UP)).isEqualTo(BigDecimal.valueOf(100.000).setScale(3, BigDecimal.ROUND_HALF_UP));
}
Aggregations