Skip to content

Commit

Permalink
Revert "codinguser#876 - hasDebitNormalBalance should not invert amou…
Browse files Browse the repository at this point in the history
…nt signum"

This reverts commit 6615458.

# Conflicts:
#	app/src/main/java/org/gnucash/android/db/adapter/SplitsDbAdapter.java
  • Loading branch information
slak44 committed Sep 6, 2020
1 parent 163b9d2 commit 04fb088
Showing 1 changed file with 34 additions and 106 deletions.
140 changes: 34 additions & 106 deletions app/src/main/java/org/gnucash/android/db/adapter/SplitsDbAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> accountUIDList,
String currencyCode,
boolean hasDebitNormalBalance) {

return computeSplitBalance(accountUIDList,
currencyCode,
hasDebitNormalBalance,
-1,
-1);
public Money computeSplitBalance(List<String> accountUIDList, String currencyCode, boolean hasDebitNormalBalance){
return calculateSplitBalance(accountUIDList, currencyCode, hasDebitNormalBalance, -1, -1);
}

/**
Expand All @@ -180,152 +173,87 @@ public Money computeSplitBalance(List<String> 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<String> accountUIDList,
String currencyCode,
boolean hasDebitNormalBalance,
long startTimestamp,
long endTimestamp) {

return calculateSplitBalance(accountUIDList,
currencyCode,
hasDebitNormalBalance,
startTimestamp,
endTimestamp);
public Money computeSplitBalance(List<String> accountUIDList, String currencyCode, boolean hasDebitNormalBalance,
long startTimestamp, long endTimestamp){
return calculateSplitBalance(accountUIDList, currencyCode, hasDebitNormalBalance, startTimestamp, endTimestamp);
}


private Money calculateSplitBalance(List<String> accountUIDList,
String currencyCode,
boolean hasDebitNormalBalance,
long startTimestamp,
long endTimestamp) {

if (accountUIDList.size() == 0) {
return new Money("0",
currencyCode);
private Money calculateSplitBalance(List<String> 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<Long, Long> price = pricesDbAdapter.getPrice(commodityUID,
currencyUID);
Pair<Long, Long> 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();
}
Expand Down

0 comments on commit 04fb088

Please sign in to comment.