]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/async/TransactionAccumulator.java
more pronounced day/month delimiters in the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / async / TransactionAccumulator.java
1 /*
2  * Copyright © 2021 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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.async;
19
20 import androidx.annotation.Nullable;
21
22 import net.ktnx.mobileledger.model.Data;
23 import net.ktnx.mobileledger.model.LedgerAccount;
24 import net.ktnx.mobileledger.model.LedgerTransaction;
25 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
26 import net.ktnx.mobileledger.model.TransactionListItem;
27 import net.ktnx.mobileledger.ui.MainModel;
28 import net.ktnx.mobileledger.utils.Misc;
29 import net.ktnx.mobileledger.utils.SimpleDate;
30
31 import java.math.BigDecimal;
32 import java.math.RoundingMode;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35
36 public class TransactionAccumulator {
37     private final ArrayList<TransactionListItem> list = new ArrayList<>();
38     private final String boldAccountName;
39     private final String accumulateAccount;
40     private final HashMap<String, BigDecimal> runningTotal = new HashMap<>();
41     private SimpleDate earliestDate, latestDate;
42     private SimpleDate lastDate;
43     private int transactionCount = 0;
44     public TransactionAccumulator(@Nullable String boldAccountName,
45                                   @Nullable String accumulateAccount) {
46         this.boldAccountName = boldAccountName;
47         this.accumulateAccount = accumulateAccount;
48
49         list.add(new TransactionListItem());    // head item
50     }
51     public void put(LedgerTransaction transaction) {
52         put(transaction, transaction.getDate());
53     }
54     public void put(LedgerTransaction transaction, SimpleDate date) {
55         transactionCount++;
56
57         // first item
58         if (null == earliestDate)
59             earliestDate = date;
60         latestDate = date;
61
62         if (lastDate != null && !date.equals(lastDate)) {
63             boolean showMonth = date.month != lastDate.month || date.year != lastDate.year;
64             list.add(1, new TransactionListItem(lastDate, showMonth));
65         }
66
67         String currentTotal = null;
68         if (accumulateAccount != null) {
69             for (LedgerTransactionAccount acc : transaction.getAccounts()) {
70                 if (acc.getAccountName()
71                        .equals(accumulateAccount) ||
72                     LedgerAccount.isParentOf(accumulateAccount, acc.getAccountName()))
73                 {
74                     BigDecimal amt = runningTotal.get(acc.getCurrency());
75                     if (amt == null)
76                         amt = BigDecimal.ZERO;
77                     BigDecimal newAmount = BigDecimal.valueOf(acc.getAmount());
78                     newAmount = newAmount.setScale(2, RoundingMode.HALF_EVEN);
79                     amt = amt.add(newAmount);
80                     runningTotal.put(acc.getCurrency(), amt);
81                 }
82             }
83
84             currentTotal = summarizeRunningTotal(runningTotal);
85         }
86         list.add(1, new TransactionListItem(transaction, boldAccountName, currentTotal));
87
88         lastDate = date;
89     }
90     private String summarizeRunningTotal(HashMap<String, BigDecimal> runningTotal) {
91         StringBuilder b = new StringBuilder();
92         for (String currency : runningTotal.keySet()) {
93             if (b.length() != 0)
94                 b.append('\n');
95             if (Misc.emptyIsNull(currency) != null)
96                 b.append(currency)
97                  .append(' ');
98             BigDecimal val = runningTotal.get(currency);
99             b.append(Data.formatNumber((val == null) ? 0f : val.floatValue()));
100         }
101         return b.toString();
102     }
103     public void publishResults(MainModel model) {
104         if (lastDate != null) {
105             SimpleDate today = SimpleDate.today();
106             if (!lastDate.equals(today)) {
107                 boolean showMonth = today.month != lastDate.month || today.year != lastDate.year;
108                 list.add(1, new TransactionListItem(lastDate, showMonth));
109             }
110         }
111
112         model.setDisplayedTransactions(list, transactionCount);
113         model.setFirstTransactionDate(earliestDate);
114         model.setLastTransactionDate(latestDate);
115     }
116 }