@@ -179,20 +179,35 @@ public Money computeSplitBalance(List<String> accountUIDList, String currencyCod
179
179
}
180
180
181
181
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 );
186
191
}
187
192
188
- Cursor cursor ;
193
+ Cursor cursor ;
189
194
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" ;
192
206
193
207
if (startTimestamp != -1 && endTimestamp != -1 ) {
194
208
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 )};
196
211
} else if (startTimestamp == -1 && endTimestamp != -1 ) {
197
212
selection += " AND " + TransactionEntry .TABLE_NAME + "_" + TransactionEntry .COLUMN_TIMESTAMP + " <= ?" ;
198
213
selectionArgs = new String []{String .valueOf (endTimestamp )};
@@ -202,34 +217,57 @@ private Money calculateSplitBalance(List<String> accountUIDList, String currency
202
217
}
203
218
204
219
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 );
211
242
212
243
try {
213
- Money total = Money .createZeroInstance (currencyCode );
244
+ Money total = Money .createZeroInstance (currencyCode );
214
245
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
+
218
250
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 );
221
253
String commodityCode = cursor .getString (2 );
254
+
222
255
//Log.d(getClass().getName(), commodity + " " + amount_num + "/" + amount_denom);
223
256
if (commodityCode .equals ("XXX" ) || amount_num == 0 ) {
224
257
// ignore custom currency
225
258
continue ;
226
259
}
227
- if (!hasDebitNormalBalance ) {
228
- amount_num = -amount_num ;
229
- }
260
+
261
+ // #876
262
+ // if (!hasDebitNormalBalance) {
263
+ // amount_num = -amount_num;
264
+ // }
265
+
230
266
if (commodityCode .equals (currencyCode )) {
231
267
// 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 ));
233
271
//Log.d(getClass().getName(), "currency " + commodity + " sub - total " + total);
234
272
} else {
235
273
// there is a second currency involved
@@ -241,18 +279,23 @@ private Money calculateSplitBalance(List<String> accountUIDList, String currency
241
279
}
242
280
// get price
243
281
String commodityUID = commoditiesDbAdapter .getCommodityUID (commodityCode );
244
- Pair <Long , Long > price = pricesDbAdapter .getPrice (commodityUID , currencyUID );
282
+ Pair <Long , Long > price = pricesDbAdapter .getPrice (commodityUID ,
283
+ currencyUID );
245
284
if (price .first <= 0 || price .second <= 0 ) {
246
285
// no price exists, just ignore it
247
286
continue ;
248
287
}
249
- BigDecimal amount = Money .getBigDecimal (amount_num , amount_denom );
288
+ BigDecimal amount = Money .getBigDecimal (amount_num ,
289
+ amount_denom );
250
290
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 ));
253
296
//Log.d(getClass().getName(), "currency " + commodity + " sub - total " + total);
254
297
}
255
- }
298
+ } // while
256
299
return total ;
257
300
} finally {
258
301
cursor .close ();
0 commit comments