+ public void storeLast(TransactionWithAccounts rec) {
+ AsyncTask.execute(() -> appendSync(rec));
+ }
+ @androidx.room.Transaction
+ public void appendSync(TransactionWithAccounts rec) {
+ TransactionAccountDAO trAccDao = DB.get()
+ .getTransactionAccountDAO();
+ AccountDAO accDao = DB.get()
+ .getAccountDAO();
+ AccountValueDAO accValDao = DB.get()
+ .getAccountValueDAO();
+
+ Transaction transaction = rec.transaction;
+ final long profileId = transaction.getProfileId();
+ transaction.setGeneration(getGenerationSync(profileId));
+ transaction.setLedgerId(getMaxLedgerIdSync(profileId) + 1);
+ transaction.setId(insertSync(transaction));
+
+ for (TransactionAccount trAcc : rec.accounts) {
+ trAcc.setTransactionId(transaction.getId());
+ 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);
+ }
+ }
+ }