From dc425305f5d33fe37cd51ee0990f3dccfe5ece9b Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Wed, 5 May 2021 18:42:58 +0300 Subject: [PATCH] when speculatively updating account amounts, update parent accounts too --- .../ktnx/mobileledger/dao/TransactionDAO.java | 56 ++++++++++--------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/dao/TransactionDAO.java b/app/src/main/java/net/ktnx/mobileledger/dao/TransactionDAO.java index 869e88c2..78be12ab 100644 --- a/app/src/main/java/net/ktnx/mobileledger/dao/TransactionDAO.java +++ b/app/src/main/java/net/ktnx/mobileledger/dao/TransactionDAO.java @@ -253,32 +253,36 @@ public abstract class TransactionDAO extends BaseDAO { trAcc.setGeneration(transaction.getGeneration()); trAcc.setId(trAccDao.insertSync(trAcc)); - Account acc = accDao.getByNameSync(profileId, trAcc.getAccountName()); - if (acc == null) { - acc = new Account(); - acc.setProfileId(profileId); - acc.setName(trAcc.getAccountName()); - acc.setNameUpper(acc.getName() - .toUpperCase()); - acc.setParentName(LedgerAccount.extractParentName(acc.getName())); - acc.setLevel(LedgerAccount.determineLevel(acc.getName())); - acc.setGeneration(trAcc.getGeneration()); - - acc.setId(accDao.insertSync(acc)); - } - - AccountValue accVal = accValDao.getByCurrencySync(acc.getId(), trAcc.getCurrency()); - if (accVal == null) { - accVal = new AccountValue(); - accVal.setAccountId(acc.getId()); - accVal.setGeneration(trAcc.getGeneration()); - accVal.setCurrency(trAcc.getCurrency()); - accVal.setValue(trAcc.getAmount()); - accVal.setId(accValDao.insertSync(accVal)); - } - else { - accVal.setValue(accVal.getValue() + trAcc.getAmount()); - accValDao.updateSync(accVal); + String accName = trAcc.getAccountName(); + while (accName != null) { + Account acc = accDao.getByNameSync(profileId, accName); + if (acc == null) { + acc = new Account(); + acc.setProfileId(profileId); + acc.setName(accName); + acc.setNameUpper(accName.toUpperCase()); + acc.setParentName(LedgerAccount.extractParentName(accName)); + acc.setLevel(LedgerAccount.determineLevel(acc.getName())); + acc.setGeneration(trAcc.getGeneration()); + + acc.setId(accDao.insertSync(acc)); + } + + AccountValue accVal = accValDao.getByCurrencySync(acc.getId(), trAcc.getCurrency()); + if (accVal == null) { + accVal = new AccountValue(); + accVal.setAccountId(acc.getId()); + accVal.setGeneration(trAcc.getGeneration()); + accVal.setCurrency(trAcc.getCurrency()); + accVal.setValue(trAcc.getAmount()); + accVal.setId(accValDao.insertSync(accVal)); + } + else { + accVal.setValue(accVal.getValue() + trAcc.getAmount()); + accValDao.updateSync(accVal); + } + + accName = LedgerAccount.extractParentName(accName); } } } -- 2.39.5