From 04fb088de4772786a5ce4cf759401c2c9ade2d2e Mon Sep 17 00:00:00 2001 From: Stefan Silviu Date: Sun, 6 Sep 2020 09:52:31 +0300 Subject: [PATCH] Revert "#876 - hasDebitNormalBalance should not invert amount signum" This reverts commit 66154580f08c89e36e6b6734b0c260b6c35ac6d0. # Conflicts: # app/src/main/java/org/gnucash/android/db/adapter/SplitsDbAdapter.java --- .../android/db/adapter/SplitsDbAdapter.java | 140 +++++------------- 1 file changed, 34 insertions(+), 106 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/db/adapter/SplitsDbAdapter.java b/app/src/main/java/org/gnucash/android/db/adapter/SplitsDbAdapter.java index c74a32c51..9750504de 100644 --- a/app/src/main/java/org/gnucash/android/db/adapter/SplitsDbAdapter.java +++ b/app/src/main/java/org/gnucash/android/db/adapter/SplitsDbAdapter.java @@ -158,15 +158,8 @@ public Split buildModelInstance(@NonNull final Cursor cursor){ * @param hasDebitNormalBalance Does the final balance has normal debit credit meaning * @return Balance of the splits for this account */ - public Money computeSplitBalance(List accountUIDList, - String currencyCode, - boolean hasDebitNormalBalance) { - - return computeSplitBalance(accountUIDList, - currencyCode, - hasDebitNormalBalance, - -1, - -1); + public Money computeSplitBalance(List accountUIDList, String currencyCode, boolean hasDebitNormalBalance){ + return calculateSplitBalance(accountUIDList, currencyCode, hasDebitNormalBalance, -1, -1); } /** @@ -180,152 +173,87 @@ public Money computeSplitBalance(List accountUIDList, * @param endTimestamp the end timestamp of the time range * @return Balance of the splits for this account within the specified time range */ - public Money computeSplitBalance(List accountUIDList, - String currencyCode, - boolean hasDebitNormalBalance, - long startTimestamp, - long endTimestamp) { - - return calculateSplitBalance(accountUIDList, - currencyCode, - hasDebitNormalBalance, - startTimestamp, - endTimestamp); + public Money computeSplitBalance(List accountUIDList, String currencyCode, boolean hasDebitNormalBalance, + long startTimestamp, long endTimestamp){ + return calculateSplitBalance(accountUIDList, currencyCode, hasDebitNormalBalance, startTimestamp, endTimestamp); } - private Money calculateSplitBalance(List accountUIDList, - String currencyCode, - boolean hasDebitNormalBalance, - long startTimestamp, - long endTimestamp) { - - if (accountUIDList.size() == 0) { - return new Money("0", - currencyCode); + private Money calculateSplitBalance(List accountUIDList, String currencyCode, boolean hasDebitNormalBalance, + long startTimestamp, long endTimestamp){ + if (accountUIDList.size() == 0){ + return new Money("0", currencyCode); } - Cursor cursor; + Cursor cursor; String[] selectionArgs = null; - String selection = DatabaseSchema.AccountEntry.TABLE_NAME - + "_" - + DatabaseSchema.CommonColumns.COLUMN_UID - + " in ( '" - + TextUtils.join("' , '", - accountUIDList) - + "' ) AND " - + TransactionEntry.TABLE_NAME - + "_" - + TransactionEntry.COLUMN_TEMPLATE - + " = 0"; + String selection = DatabaseSchema.AccountEntry.TABLE_NAME + "_" + DatabaseSchema.CommonColumns.COLUMN_UID + " in ( '" + TextUtils.join("' , '", accountUIDList) + "' ) AND " + + TransactionEntry.TABLE_NAME + "_" + TransactionEntry.COLUMN_TEMPLATE + " = 0"; if (startTimestamp != -1 && endTimestamp != -1) { - selection += " AND " + TransactionEntry.TABLE_NAME + "_" + TransactionEntry.COLUMN_TIMESTAMP + " BETWEEN ? AND ? "; - selectionArgs = new String[]{String.valueOf(startTimestamp), - String.valueOf(endTimestamp)}; - + selectionArgs = new String[]{String.valueOf(startTimestamp), String.valueOf(endTimestamp)}; } else if (startTimestamp == -1 && endTimestamp != -1) { - selection += " AND " + TransactionEntry.TABLE_NAME + "_" + TransactionEntry.COLUMN_TIMESTAMP + " <= ?"; selectionArgs = new String[]{String.valueOf(endTimestamp)}; - } else if (startTimestamp != -1/* && endTimestamp == -1*/) { - selection += " AND " + TransactionEntry.TABLE_NAME + "_" + TransactionEntry.COLUMN_TIMESTAMP + " >= ?"; selectionArgs = new String[]{String.valueOf(startTimestamp)}; } cursor = mDb.query("trans_split_acct", - new String[]{"TOTAL ( CASE WHEN " - + SplitEntry.TABLE_NAME - + "_" - + SplitEntry.COLUMN_TYPE - + " = 'DEBIT' THEN " - + SplitEntry.TABLE_NAME - + "_" - + SplitEntry.COLUMN_QUANTITY_NUM - + " ELSE - " - + SplitEntry.TABLE_NAME - + "_" - + SplitEntry.COLUMN_QUANTITY_NUM - + " END )", - SplitEntry.TABLE_NAME + "_" + SplitEntry.COLUMN_QUANTITY_DENOM, - DatabaseSchema.AccountEntry.TABLE_NAME - + "_" - + DatabaseSchema.AccountEntry.COLUMN_CURRENCY}, - selection, - selectionArgs, - DatabaseSchema.AccountEntry.TABLE_NAME + "_" + DatabaseSchema.AccountEntry.COLUMN_CURRENCY, - null, - null); + new String[]{"TOTAL ( CASE WHEN " + SplitEntry.TABLE_NAME + "_" + SplitEntry.COLUMN_TYPE + " = 'DEBIT' THEN " + + SplitEntry.TABLE_NAME + "_" + SplitEntry.COLUMN_QUANTITY_NUM + " ELSE - " + + SplitEntry.TABLE_NAME + "_" + SplitEntry.COLUMN_QUANTITY_NUM + " END )", + SplitEntry.TABLE_NAME + "_" + SplitEntry.COLUMN_QUANTITY_DENOM, + DatabaseSchema.AccountEntry.TABLE_NAME + "_" + DatabaseSchema.AccountEntry.COLUMN_CURRENCY}, + selection, selectionArgs, DatabaseSchema.AccountEntry.TABLE_NAME + "_" + DatabaseSchema.AccountEntry.COLUMN_CURRENCY, null, null); try { - Money total = Money.createZeroInstance(currencyCode); + Money total = Money.createZeroInstance(currencyCode); CommoditiesDbAdapter commoditiesDbAdapter = null; - PricesDbAdapter pricesDbAdapter = null; - Commodity commodity = null; - String currencyUID = null; - + PricesDbAdapter pricesDbAdapter = null; + Commodity commodity = null; + String currencyUID = null; while (cursor.moveToNext()) { - long amount_num = cursor.getLong(0); - long amount_denom = cursor.getLong(1); + long amount_num = cursor.getLong(0); + long amount_denom = cursor.getLong(1); String commodityCode = cursor.getString(2); - //Log.d(getClass().getName(), commodity + " " + amount_num + "/" + amount_denom); if (commodityCode.equals("XXX") || amount_num == 0) { // ignore custom currency continue; } - - // #876 -// if (!hasDebitNormalBalance) { -// // The account has usually DEBIT < CREDIT -// -// // Invert signum to get a positive amount -// amount_num = -amount_num; -// } - + if (!hasDebitNormalBalance) { + amount_num = -amount_num; + } if (commodityCode.equals(currencyCode)) { // currency matches - - total = total.add(new Money(amount_num, - amount_denom, - currencyCode)); + total = total.add(new Money(amount_num, amount_denom, currencyCode)); //Log.d(getClass().getName(), "currency " + commodity + " sub - total " + total); - } else { // there is a second currency involved - if (commoditiesDbAdapter == null) { commoditiesDbAdapter = new CommoditiesDbAdapter(mDb); pricesDbAdapter = new PricesDbAdapter(mDb); commodity = commoditiesDbAdapter.getCommodity(currencyCode); currencyUID = commoditiesDbAdapter.getCommodityUID(currencyCode); } - // get price String commodityUID = commoditiesDbAdapter.getCommodityUID(commodityCode); - Pair price = pricesDbAdapter.getPrice(commodityUID, - currencyUID); + Pair price = pricesDbAdapter.getPrice(commodityUID, currencyUID); if (price.first <= 0 || price.second <= 0) { // no price exists, just ignore it continue; } - BigDecimal amount = Money.getBigDecimal(amount_num, - amount_denom); + BigDecimal amount = Money.getBigDecimal(amount_num, amount_denom); BigDecimal amountConverted = amount.multiply(new BigDecimal(price.first)) - .divide(new BigDecimal(price.second), - commodity.getSmallestFractionDigits(), - BigDecimal.ROUND_HALF_EVEN); - total = total.add(new Money(amountConverted, - commodity)); + .divide(new BigDecimal(price.second), commodity.getSmallestFractionDigits(), BigDecimal.ROUND_HALF_EVEN); + total = total.add(new Money(amountConverted, commodity)); //Log.d(getClass().getName(), "currency " + commodity + " sub - total " + total); } - } // while - + } return total; - } finally { cursor.close(); }