From 02c179e4ce41f55cf179ea6d2aabe4bb79f81a72 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Fri, 29 Mar 2019 13:31:23 +0200 Subject: [PATCH] transaction auto-fill: honor the preferred account filter --- .../ui/activity/NewTransactionActivity.java | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java index f075929d..6f0ea1a8 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java @@ -487,10 +487,28 @@ public class NewTransactionActivity extends ProfileThemedActivity Log.d("descr selected", description); if (!inputStateIsInitial()) return; - try (Cursor c = MLDB.getDatabase().rawQuery( - "select profile, id from transactions where description=? order by date desc " + - "limit 1", new String[]{description})) - { + MobileLedgerProfile currentProfile = Data.profile.get(); + String accFilter = currentProfile.getPreferredAccountsFilter(); + + ArrayList params = new ArrayList<>(); + StringBuilder sb = new StringBuilder( + "select t.profile, t.id from transactions t where t.description=?"); + params.add(description); + + if (accFilter != null) { + sb.append(" AND EXISTS (").append("SELECT 1 FROM transaction_accounts ta ") + .append("WHERE ta.profile = t.profile").append(" AND ta.transaction_id = t.id") + .append(" AND UPPER(ta.account_name) LIKE '%'||?||'%')"); + params.add(accFilter.toUpperCase()); + } + + sb.append(" ORDER BY date desc limit 1"); + + final String sql = sb.toString(); + Log.d("descr", sql); + Log.d("descr", params.toString()); + + try (Cursor c = MLDB.getDatabase().rawQuery(sql, params.toArray(new String[]{}))) { if (!c.moveToNext()) return; String profileUUID = c.getString(0); -- 2.39.5