+ public long getMaxLedgerIdSync(long profileId) {
+ LedgerIdContainer result = getMaxLedgerIdPOJOSync(profileId);
+
+ if (result == null)
+ return 0;
+ return result.ledgerId;
+ }
+ @androidx.room.Transaction
+ public void storeTransactionsSync(List<TransactionWithAccounts> list, long profileId) {
+ long generation = getGenerationSync(profileId) + 1;
+
+ for (TransactionWithAccounts tr : list) {
+ tr.transaction.setGeneration(generation);
+ tr.transaction.setProfileId(profileId);
+
+ storeSync(tr);
+ }
+
+ Logger.debug("Transaction", "Purging old transactions");
+ int removed = purgeOldTransactionsSync(profileId, generation);
+ Logger.debug("Transaction", String.format(Locale.ROOT, "Purged %d transactions", removed));
+
+ removed = purgeOldTransactionAccountsSync(profileId, generation);
+ Logger.debug("Transaction",
+ String.format(Locale.ROOT, "Purged %d transaction accounts", removed));
+ }
+ @androidx.room.Transaction
+ void storeSync(TransactionWithAccounts rec) {
+ TransactionAccountDAO trAccDao = DB.get()
+ .getTransactionAccountDAO();
+
+ Transaction transaction = rec.transaction;
+ Transaction existing = getByLedgerId(transaction.getProfileId(), transaction.getLedgerId());
+ if (existing != null) {
+ if (Misc.equalStrings(transaction.getDataHash(), existing.getDataHash())) {
+ updateGenerationWithAccounts(existing.getId(), rec.transaction.getGeneration());
+ return;
+ }
+
+ existing.copyDataFrom(transaction);
+ updateSync(existing);
+
+ transaction = existing;
+ }
+ else
+ transaction.setId(insertSync(transaction));
+
+ for (TransactionAccount trAcc : rec.accounts) {
+ trAcc.setTransactionId(transaction.getId());
+ trAcc.setGeneration(transaction.getGeneration());
+ TransactionAccount existingAcc =
+ trAccDao.getByOrderNoSync(trAcc.getTransactionId(), trAcc.getOrderNo());
+ if (existingAcc != null) {
+ existingAcc.copyDataFrom(trAcc);
+ trAccDao.updateSync(existingAcc);
+ }
+ else
+ trAcc.setId(trAccDao.insertSync(trAcc));
+ }
+ }
+ public void storeLast(TransactionWithAccounts rec) {
+ BaseDAO.runAsync(() -> 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));
+
+ 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);
+ }
+ }
+ }