From 3b99114faaf39590f794117190e99b1dfa56f2ae Mon Sep 17 00:00:00 2001 From: Ngewi Fet Date: Mon, 26 May 2014 15:53:40 +0200 Subject: [PATCH] Undoes the computation of account balances to simple summation of the transactions. More specifically, reverts changes from commit 218e837 Closes #163 #164 --- CHANGELOG.md | 3 ++ app/AndroidManifest.xml | 2 +- app/res/values/strings.xml | 2 +- .../android/app/GnuCashApplication.java | 14 +++++++ .../gnucash/android/db/AccountsDbAdapter.java | 8 +++- .../android/db/TransactionsDbAdapter.java | 24 +++++++++--- .../org/gnucash/android/model/Account.java | 39 ++++++++++++------- .../android/ui/account/AccountsActivity.java | 2 + 8 files changed, 70 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 40122bae3..a75449171 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ Change Log =============================================================================== +Version 1.3.3 *(2014-05-26)* +* Reversed changes in the computation of balances, back to pre-v1.3.2 mode (will be re-instated in the future) + Version 1.3.2 *(2014-05-23)* ---------------------------- * Fixed: Editing account modifies the transaction type of transfer transactions diff --git a/app/AndroidManifest.xml b/app/AndroidManifest.xml index 45184fdb4..37fba2687 100644 --- a/app/AndroidManifest.xml +++ b/app/AndroidManifest.xml @@ -17,7 +17,7 @@ diff --git a/app/res/values/strings.xml b/app/res/values/strings.xml index 704ef1037..3d04f3c6a 100644 --- a/app/res/values/strings.xml +++ b/app/res/values/strings.xml @@ -17,7 +17,7 @@ GnuCash - 1.3.2 + 1.3.3 Create Account Edit Account Info diff --git a/app/src/org/gnucash/android/app/GnuCashApplication.java b/app/src/org/gnucash/android/app/GnuCashApplication.java index b18d4e1bf..8d01d9fa1 100644 --- a/app/src/org/gnucash/android/app/GnuCashApplication.java +++ b/app/src/org/gnucash/android/app/GnuCashApplication.java @@ -17,6 +17,9 @@ import android.app.Application; import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; +import org.gnucash.android.R; /** * An {@link Application} subclass for retrieving static context @@ -39,4 +42,15 @@ public void onCreate(){ public static Context getAppContext() { return GnuCashApplication.context; } + + /** + * Returns true if double entry is enabled in the app settings, false otherwise. + * If the value is not set, the default value can be specified in the parameters. + * @param defaultValue Default value to return if double entry is not explicitly set + * @return true if double entry is enabled, false otherwise + */ + public static boolean isDoubleEntryEnabled(boolean defaultValue){ + SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); + return sharedPrefs.getBoolean(context.getString(R.string.key_use_double_entry), defaultValue); + } } \ No newline at end of file diff --git a/app/src/org/gnucash/android/db/AccountsDbAdapter.java b/app/src/org/gnucash/android/db/AccountsDbAdapter.java index db144a071..4206f671b 100644 --- a/app/src/org/gnucash/android/db/AccountsDbAdapter.java +++ b/app/src/org/gnucash/android/db/AccountsDbAdapter.java @@ -546,7 +546,13 @@ public Money getAccountBalance(long accountId){ balance = balance.add(subBalance); } } - return balance.add(getAccount(accountId).getBalance()); + + return balance.add(mTransactionsAdapter.getTransactionsSum(accountId)); + +// properly compute the account balance taking double entry into account +// TODO: re-enable this when splits are added +// return balance.add(getAccount(accountId).getBalance()); + } /** diff --git a/app/src/org/gnucash/android/db/TransactionsDbAdapter.java b/app/src/org/gnucash/android/db/TransactionsDbAdapter.java index f6a845644..45d6dc3a1 100644 --- a/app/src/org/gnucash/android/db/TransactionsDbAdapter.java +++ b/app/src/org/gnucash/android/db/TransactionsDbAdapter.java @@ -21,6 +21,7 @@ import android.database.Cursor; import android.database.sqlite.SQLiteStatement; import android.util.Log; +import org.gnucash.android.app.GnuCashApplication; import org.gnucash.android.model.Account; import org.gnucash.android.model.Money; import org.gnucash.android.model.Transaction; @@ -186,14 +187,25 @@ public List getAllTransactionsForAccount(String accountUID){ while (c.moveToNext()) { Transaction transaction = buildTransactionInstance(c); String doubleEntryAccountUID = transaction.getDoubleEntryAccountUID(); - // Negate double entry transactions for the transfer account + + //one transaction in this case represents both sides of the split if (doubleEntryAccountUID != null && doubleEntryAccountUID.equals(accountUID)){ - if (transaction.getType() == TransactionType.DEBIT) { - transaction.setType(TransactionType.CREDIT); - } else { - transaction.setType(TransactionType.DEBIT); - } + transaction.setAmount(transaction.getAmount().negate()); +/* +//use this to properly compute the account balance + if (GnuCashApplication.isDoubleEntryEnabled(false)) { + if (transaction.getType() == TransactionType.DEBIT) { + transaction.setType(TransactionType.CREDIT); + } else { + transaction.setType(TransactionType.DEBIT); + } + } else { + // Negate double entry transactions for the transfer account + transaction.setAmount(transaction.getAmount().negate()); + } +*/ } + transactionsList.add(transaction); } c.close(); diff --git a/app/src/org/gnucash/android/model/Account.java b/app/src/org/gnucash/android/model/Account.java index 4a854cf42..833d99850 100644 --- a/app/src/org/gnucash/android/model/Account.java +++ b/app/src/org/gnucash/android/model/Account.java @@ -371,21 +371,30 @@ public boolean hasUnexportedTransactions(){ public Money getBalance(){ Money balance = new Money(new BigDecimal(0), this.mCurrency); for (Transaction transaction : mTransactionsList) { - boolean isDebitAccount = getAccountType().hasDebitNormalBalance(); - boolean isDebitTransaction = transaction.getType() == TransactionType.DEBIT; - if (isDebitAccount) { - if (isDebitTransaction) { - balance = balance.add(transaction.getAmount()); - } else { - balance = balance.subtract(transaction.getAmount()); - } - } else { - if (isDebitTransaction) { - balance = balance.subtract(transaction.getAmount()); - } else { - balance = balance.add(transaction.getAmount()); - } - } + balance = balance.add(transaction.getAmount()); + +/* + //TODO: Re-enable proper computation of balance for double-entries in the future + if (GnuCashApplication.isDoubleEntryEnabled(false)) { + boolean isDebitAccount = getAccountType().hasDebitNormalBalance(); + boolean isDebitTransaction = transaction.getType() == TransactionType.DEBIT; + if (isDebitAccount) { + if (isDebitTransaction) { + balance = balance.add(transaction.getAmount()); + } else { + balance = balance.subtract(transaction.getAmount()); + } + } else { + if (isDebitTransaction) { + balance = balance.subtract(transaction.getAmount()); + } else { + balance = balance.add(transaction.getAmount()); + } + } + } else { //not using double entry + balance = balance.add(transaction.getAmount()); + } +*/ } return balance; } diff --git a/app/src/org/gnucash/android/ui/account/AccountsActivity.java b/app/src/org/gnucash/android/ui/account/AccountsActivity.java index 471dcbb26..09cdf12c6 100644 --- a/app/src/org/gnucash/android/ui/account/AccountsActivity.java +++ b/app/src/org/gnucash/android/ui/account/AccountsActivity.java @@ -279,6 +279,8 @@ private void init() { boolean firstRun = prefs.getBoolean(getString(R.string.key_first_run), true); if (firstRun){ createDefaultAccounts(); + //default to using double entry and save the preference explicitly + prefs.edit().putBoolean(getString(R.string.key_use_double_entry), true).commit(); } if (hasNewFeatures()){