2 * Copyright © 2020 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.async;
20 import android.database.Cursor;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.os.AsyncTask;
24 import net.ktnx.mobileledger.App;
25 import net.ktnx.mobileledger.model.Data;
26 import net.ktnx.mobileledger.model.LedgerTransaction;
27 import net.ktnx.mobileledger.model.MobileLedgerProfile;
28 import net.ktnx.mobileledger.model.TransactionListItem;
29 import net.ktnx.mobileledger.utils.SimpleDate;
31 import java.util.ArrayList;
33 import static net.ktnx.mobileledger.utils.Logger.debug;
35 public class UpdateTransactionsTask extends AsyncTask<String, Void, String> {
36 protected String doInBackground(String[] filterAccName) {
37 final MobileLedgerProfile profile = Data.profile.getValue();
39 return "Profile not configured";
41 String profile_uuid = profile.getUuid();
42 Data.backgroundTaskStarted();
44 ArrayList<TransactionListItem> newList = new ArrayList<>();
49 if (filterAccName[0] == null) {
50 sql = "SELECT id, year, month, day FROM transactions WHERE profile=? ORDER BY " +
51 "year desc, month desc, day desc, id desc";
52 params = new String[]{profile_uuid};
56 sql = "SELECT distinct tr.id, tr.year, tr.month, tr.day from transactions tr " +
57 "JOIN " + "transaction_accounts ta " +
58 "ON ta.transaction_id=tr.id AND ta.profile=tr.profile WHERE tr.profile=? " +
59 "and ta.account_name LIKE ?||'%' AND ta" +
60 ".amount <> 0 ORDER BY tr.year desc, tr.month desc, tr.day desc, tr.id " +
62 params = new String[]{profile_uuid, filterAccName[0]};
66 SimpleDate latestDate = null, earliestDate = null;
67 SQLiteDatabase db = App.getDatabase();
69 SimpleDate lastDate = SimpleDate.today();
70 try (Cursor cursor = db.rawQuery(sql, params)) {
71 while (cursor.moveToNext()) {
75 int transaction_id = cursor.getInt(0);
77 new SimpleDate(cursor.getInt(1), cursor.getInt(2), cursor.getInt(3));
79 if (null == latestDate)
83 if (!date.equals(lastDate)) {
85 (date.month != lastDate.month) || (date.year != lastDate.year);
86 newList.add(new TransactionListItem(date, showMonth));
89 new TransactionListItem(new LedgerTransaction(transaction_id), odd));
90 // debug("UTT", String.format("got transaction %d", transaction_id));
95 Data.transactions.setList(newList);
96 Data.latestTransactionDate.postValue(latestDate);
97 Data.earliestTransactionDate.postValue(earliestDate);
98 debug("UTT", "transaction list value updated");
104 Data.backgroundTaskFinished();