Hi,
I am trying to commit data through data manager I receive no errors but there is no changes occurring in the data I always receive the same values.
The idea is I have a customer with multiple accounts each account has a property debt this value is position if he owes me and negative if i owe him. So what I am trying to implement is settleAccounts which checks all his accounts and settles all the debt of the customer accounts and places any extra debt in main account.
for example:
Customer A: has 2 accounts A, B
Account A: has 2000 Debt - main account
Account B: has -500 Debt
result should be: A.Debt = 1500, B.Debt = 0
public void settleAccount(Customer customer) {
BigDecimal total = BigDecimal.ZERO;
for (Account acc: customer.getAccounts()) {
if (acc.getDebt().doubleValue() < 0) {
total = total.add(acc.getDebt().abs());
}
}
for (Account acc: customer.getAccounts()) {
if(!acc.getName().equals("عام")) {
if(acc.getDebt().doubleValue() > 0) {
if (acc.getDebt().subtract(total).doubleValue() <= 0) {
total = total.subtract(acc.getDebt());
acc.setDebt(BigDecimal.ZERO);
dataManager.commit(acc);
} else {
total = BigDecimal.ZERO;
acc.setDebt(acc.getDebt().subtract(total));
dataManager.commit(acc);
}
}else{
acc.setDebt(BigDecimal.ZERO);
dataManager.commit(acc);
}
}
}
if(total.doubleValue() != 0){
for (Account acc: customer.getAccounts()) {
if(acc.getName().equals("عام")) {
if(acc.getDebt().doubleValue() < 0)
acc.setDebt(BigDecimal.ZERO);
acc.setDebt(acc.getDebt().subtract(total));
dataManager.commit(acc);
}
}
}
}