Skip to content

Commit 6615458

Browse files
committed
codinguser#876 - hasDebitNormalBalance should not invert amount signum
1 parent b2ea8a0 commit 6615458

File tree

1 file changed

+72
-29
lines changed

1 file changed

+72
-29
lines changed

app/src/main/java/org/gnucash/android/db/adapter/SplitsDbAdapter.java

Lines changed: 72 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -179,20 +179,35 @@ public Money computeSplitBalance(List<String> accountUIDList, String currencyCod
179179
}
180180

181181

182-
private Money calculateSplitBalance(List<String> accountUIDList, String currencyCode, boolean hasDebitNormalBalance,
183-
long startTimestamp, long endTimestamp){
184-
if (accountUIDList.size() == 0){
185-
return new Money("0", currencyCode);
182+
private Money calculateSplitBalance(List<String> accountUIDList,
183+
String currencyCode,
184+
boolean hasDebitNormalBalance,
185+
long startTimestamp,
186+
long endTimestamp) {
187+
188+
if (accountUIDList.size() == 0) {
189+
return new Money("0",
190+
currencyCode);
186191
}
187192

188-
Cursor cursor;
193+
Cursor cursor;
189194
String[] selectionArgs = null;
190-
String selection = DatabaseSchema.AccountEntry.TABLE_NAME + "_" + DatabaseSchema.CommonColumns.COLUMN_UID + " in ( '" + TextUtils.join("' , '", accountUIDList) + "' ) AND " +
191-
TransactionEntry.TABLE_NAME + "_" + TransactionEntry.COLUMN_TEMPLATE + " = 0";
195+
String selection = DatabaseSchema.AccountEntry.TABLE_NAME
196+
+ "_"
197+
+ DatabaseSchema.CommonColumns.COLUMN_UID
198+
+ " in ( '"
199+
+ TextUtils.join("' , '",
200+
accountUIDList)
201+
+ "' ) AND "
202+
+ TransactionEntry.TABLE_NAME
203+
+ "_"
204+
+ TransactionEntry.COLUMN_TEMPLATE
205+
+ " = 0";
192206

193207
if (startTimestamp != -1 && endTimestamp != -1) {
194208
selection += " AND " + TransactionEntry.TABLE_NAME + "_" + TransactionEntry.COLUMN_TIMESTAMP + " BETWEEN ? AND ? ";
195-
selectionArgs = new String[]{String.valueOf(startTimestamp), String.valueOf(endTimestamp)};
209+
selectionArgs = new String[]{String.valueOf(startTimestamp),
210+
String.valueOf(endTimestamp)};
196211
} else if (startTimestamp == -1 && endTimestamp != -1) {
197212
selection += " AND " + TransactionEntry.TABLE_NAME + "_" + TransactionEntry.COLUMN_TIMESTAMP + " <= ?";
198213
selectionArgs = new String[]{String.valueOf(endTimestamp)};
@@ -202,34 +217,57 @@ private Money calculateSplitBalance(List<String> accountUIDList, String currency
202217
}
203218

204219
cursor = mDb.query("trans_split_acct",
205-
new String[]{"TOTAL ( CASE WHEN " + SplitEntry.TABLE_NAME + "_" + SplitEntry.COLUMN_TYPE + " = 'DEBIT' THEN " +
206-
SplitEntry.TABLE_NAME + "_" + SplitEntry.COLUMN_QUANTITY_NUM + " ELSE - " +
207-
SplitEntry.TABLE_NAME + "_" + SplitEntry.COLUMN_QUANTITY_NUM + " END )",
208-
SplitEntry.TABLE_NAME + "_" + SplitEntry.COLUMN_QUANTITY_DENOM,
209-
DatabaseSchema.AccountEntry.TABLE_NAME + "_" + DatabaseSchema.AccountEntry.COLUMN_CURRENCY},
210-
selection, selectionArgs, DatabaseSchema.AccountEntry.TABLE_NAME + "_" + DatabaseSchema.AccountEntry.COLUMN_CURRENCY, null, null);
220+
new String[]{"TOTAL ( CASE WHEN "
221+
+ SplitEntry.TABLE_NAME
222+
+ "_"
223+
+ SplitEntry.COLUMN_TYPE
224+
+ " = 'DEBIT' THEN "
225+
+ SplitEntry.TABLE_NAME
226+
+ "_"
227+
+ SplitEntry.COLUMN_QUANTITY_NUM
228+
+ " ELSE - "
229+
+ SplitEntry.TABLE_NAME
230+
+ "_"
231+
+ SplitEntry.COLUMN_QUANTITY_NUM
232+
+ " END )",
233+
SplitEntry.TABLE_NAME + "_" + SplitEntry.COLUMN_QUANTITY_DENOM,
234+
DatabaseSchema.AccountEntry.TABLE_NAME
235+
+ "_"
236+
+ DatabaseSchema.AccountEntry.COLUMN_CURRENCY},
237+
selection,
238+
selectionArgs,
239+
DatabaseSchema.AccountEntry.TABLE_NAME + "_" + DatabaseSchema.AccountEntry.COLUMN_CURRENCY,
240+
null,
241+
null);
211242

212243
try {
213-
Money total = Money.createZeroInstance(currencyCode);
244+
Money total = Money.createZeroInstance(currencyCode);
214245
CommoditiesDbAdapter commoditiesDbAdapter = null;
215-
PricesDbAdapter pricesDbAdapter = null;
216-
Commodity commodity = null;
217-
String currencyUID = null;
246+
PricesDbAdapter pricesDbAdapter = null;
247+
Commodity commodity = null;
248+
String currencyUID = null;
249+
218250
while (cursor.moveToNext()) {
219-
long amount_num = cursor.getLong(0);
220-
long amount_denom = cursor.getLong(1);
251+
long amount_num = cursor.getLong(0);
252+
long amount_denom = cursor.getLong(1);
221253
String commodityCode = cursor.getString(2);
254+
222255
//Log.d(getClass().getName(), commodity + " " + amount_num + "/" + amount_denom);
223256
if (commodityCode.equals("XXX") || amount_num == 0) {
224257
// ignore custom currency
225258
continue;
226259
}
227-
if (!hasDebitNormalBalance) {
228-
amount_num = -amount_num;
229-
}
260+
261+
// #876
262+
// if (!hasDebitNormalBalance) {
263+
// amount_num = -amount_num;
264+
// }
265+
230266
if (commodityCode.equals(currencyCode)) {
231267
// currency matches
232-
total = total.add(new Money(amount_num, amount_denom, currencyCode));
268+
total = total.add(new Money(amount_num,
269+
amount_denom,
270+
currencyCode));
233271
//Log.d(getClass().getName(), "currency " + commodity + " sub - total " + total);
234272
} else {
235273
// there is a second currency involved
@@ -241,18 +279,23 @@ private Money calculateSplitBalance(List<String> accountUIDList, String currency
241279
}
242280
// get price
243281
String commodityUID = commoditiesDbAdapter.getCommodityUID(commodityCode);
244-
Pair<Long, Long> price = pricesDbAdapter.getPrice(commodityUID, currencyUID);
282+
Pair<Long, Long> price = pricesDbAdapter.getPrice(commodityUID,
283+
currencyUID);
245284
if (price.first <= 0 || price.second <= 0) {
246285
// no price exists, just ignore it
247286
continue;
248287
}
249-
BigDecimal amount = Money.getBigDecimal(amount_num, amount_denom);
288+
BigDecimal amount = Money.getBigDecimal(amount_num,
289+
amount_denom);
250290
BigDecimal amountConverted = amount.multiply(new BigDecimal(price.first))
251-
.divide(new BigDecimal(price.second), commodity.getSmallestFractionDigits(), BigDecimal.ROUND_HALF_EVEN);
252-
total = total.add(new Money(amountConverted, commodity));
291+
.divide(new BigDecimal(price.second),
292+
commodity.getSmallestFractionDigits(),
293+
BigDecimal.ROUND_HALF_EVEN);
294+
total = total.add(new Money(amountConverted,
295+
commodity));
253296
//Log.d(getClass().getName(), "currency " + commodity + " sub - total " + total);
254297
}
255-
}
298+
} // while
256299
return total;
257300
} finally {
258301
cursor.close();

0 commit comments

Comments
 (0)