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.
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 androidx.annotation.Nullable;
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;
31 import java.math.BigDecimal;
32 import java.math.RoundingMode;
33 import java.util.ArrayList;
34 import java.util.HashMap;
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;
49 list.add(new TransactionListItem()); // head item
51 public void put(LedgerTransaction transaction) {
52 put(transaction, transaction.getDate());
54 public void put(LedgerTransaction transaction, SimpleDate date) {
58 if (null == earliestDate)
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));
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()))
74 BigDecimal amt = runningTotal.get(acc.getCurrency());
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);
84 currentTotal = summarizeRunningTotal(runningTotal);
86 list.add(1, new TransactionListItem(transaction, boldAccountName, currentTotal));
90 private String summarizeRunningTotal(HashMap<String, BigDecimal> runningTotal) {
91 StringBuilder b = new StringBuilder();
92 for (String currency : runningTotal.keySet()) {
95 if (Misc.emptyIsNull(currency) != null)
98 BigDecimal val = runningTotal.get(currency);
99 b.append(Data.formatNumber((val == null) ? 0f : val.floatValue()));
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));
112 model.setDisplayedTransactions(list, transactionCount);
113 model.setFirstTransactionDate(earliestDate);
114 model.setLastTransactionDate(latestDate);