]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/dao/TransactionDAO.java
when speculatively updating account amounts, update parent accounts too
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / dao / TransactionDAO.java
index 8e0156b77cf1993e29abbedabe2c9f79e14a1c94..78be12abf0fc4912916426930cfbf10332b2f397 100644 (file)
@@ -79,12 +79,11 @@ public abstract class TransactionDAO extends BaseDAO<Transaction> {
     @Query("SELECT * FROM transactions WHERE id = :transactionId")
     public abstract TransactionWithAccounts getByIdWithAccountsSync(long transactionId);
 
-    @Query("SELECT DISTINCT description, CASE WHEN description_upper LIKE :term||'%%' THEN 1 " +
-           "               WHEN description_upper LIKE '%%:'||:term||'%%' THEN 2 " +
-           "               WHEN description_upper LIKE '%% '||:term||'%%' THEN 3 " +
-           "               ELSE 9 END AS ordering " + "FROM description_history " +
-           "WHERE description_upper LIKE '%%'||:term||'%%' " +
-           "ORDER BY ordering, description_upper, rowid ")
+    @Query("SELECT DISTINCT description, CASE WHEN description LIKE :term||'%%' THEN 1 " +
+           "               WHEN description LIKE '%%:'||:term||'%%' THEN 2 " +
+           "               WHEN description LIKE '%% '||:term||'%%' THEN 3 " +
+           "               ELSE 9 END AS ordering FROM transactions " +
+           "WHERE description LIKE '%%'||:term||'%%' " + "ORDER BY ordering, description, rowid ")
     public abstract List<DescriptionContainer> lookupDescriptionSync(@NonNull String term);
 
     @androidx.room.Transaction
@@ -254,32 +253,36 @@ public abstract class TransactionDAO extends BaseDAO<Transaction> {
             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);
             }
         }
     }