]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java
more pronounced day/month delimiters in the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / LedgerAccount.java
1 /*
2  * Copyright © 2024 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.model;
19
20 import androidx.annotation.NonNull;
21 import androidx.annotation.Nullable;
22
23 import net.ktnx.mobileledger.db.Account;
24 import net.ktnx.mobileledger.db.AccountValue;
25 import net.ktnx.mobileledger.db.AccountWithAmounts;
26
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.regex.Pattern;
30
31 public class LedgerAccount {
32     private static final char ACCOUNT_DELIMITER = ':';
33     static Pattern reHigherAccount = Pattern.compile("^[^:]+:");
34     private final LedgerAccount parent;
35     private long dbId;
36     private long profileId;
37     private String name;
38     private String shortName;
39     private int level;
40     private boolean expanded;
41     private List<LedgerAmount> amounts;
42     private boolean hasSubAccounts;
43     private boolean amountsExpanded;
44
45     public LedgerAccount(String name, @Nullable LedgerAccount parent) {
46         this.parent = parent;
47         if (parent != null && !name.startsWith(parent.getName() + ":"))
48             throw new IllegalStateException(
49                     String.format("Account name '%s' doesn't match parent account '%s'", name,
50                             parent.getName()));
51         this.setName(name);
52     }
53     @Nullable
54     public static String extractParentName(@NonNull String accName) {
55         int colonPos = accName.lastIndexOf(ACCOUNT_DELIMITER);
56         if (colonPos < 0)
57             return null;    // no parent account -- this is a top-level account
58         else
59             return accName.substring(0, colonPos);
60     }
61     public static boolean isParentOf(@NonNull String possibleParent, @NonNull String accountName) {
62         return accountName.startsWith(possibleParent + ':');
63     }
64     @NonNull
65     static public LedgerAccount fromDBO(AccountWithAmounts in, LedgerAccount parent) {
66         LedgerAccount res = new LedgerAccount(in.account.getName(), parent);
67         res.dbId = in.account.getId();
68         res.profileId = in.account.getProfileId();
69         res.setName(in.account.getName());
70         res.setExpanded(in.account.isExpanded());
71         res.setAmountsExpanded(in.account.isAmountsExpanded());
72
73         res.amounts = new ArrayList<>();
74         for (AccountValue val : in.amounts) {
75             res.amounts.add(new LedgerAmount(val.getValue(), val.getCurrency()));
76         }
77
78         return res;
79     }
80     public static int determineLevel(String accName) {
81         int level = 0;
82         int delimiterPosition = accName.indexOf(ACCOUNT_DELIMITER);
83         while (delimiterPosition >= 0) {
84             level++;
85             delimiterPosition = accName.indexOf(ACCOUNT_DELIMITER, delimiterPosition + 1);
86         }
87         return level;
88     }
89     @Override
90     public int hashCode() {
91         return name.hashCode();
92     }
93     @Override
94     public boolean equals(@Nullable Object obj) {
95         if (obj == null)
96             return false;
97
98         if (!(obj instanceof LedgerAccount))
99             return false;
100
101         LedgerAccount acc = (LedgerAccount) obj;
102         if (!name.equals(acc.name))
103             return false;
104
105         if (!getAmountsString().equals(acc.getAmountsString()))
106             return false;
107
108         return expanded == acc.expanded && amountsExpanded == acc.amountsExpanded;
109     }
110     // an account is visible if:
111     //  - it has an expanded visible parent or is a top account
112     public boolean isVisible() {
113         if (parent == null)
114             return true;
115
116         return (parent.isExpanded() && parent.isVisible());
117     }
118     public boolean isParentOf(LedgerAccount potentialChild) {
119         return potentialChild.getName()
120                              .startsWith(name + ":");
121     }
122     private void stripName() {
123         String[] split = name.split(":");
124         shortName = split[split.length - 1];
125         level = split.length - 1;
126     }
127     public String getName() {
128         return name;
129     }
130     public void setName(String name) {
131         this.name = name;
132         stripName();
133     }
134     public void addAmount(float amount, @NonNull String currency) {
135         if (amounts == null)
136             amounts = new ArrayList<>();
137         amounts.add(new LedgerAmount(amount, currency));
138     }
139     public void addAmount(float amount) {
140         this.addAmount(amount, "");
141     }
142     public int getAmountCount() { return (amounts != null) ? amounts.size() : 0; }
143     public String getAmountsString() {
144         if ((amounts == null) || amounts.isEmpty())
145             return "";
146
147         StringBuilder builder = new StringBuilder();
148         for (LedgerAmount amount : amounts) {
149             String amt = amount.toString();
150             if (builder.length() > 0)
151                 builder.append('\n');
152             builder.append(amt);
153         }
154
155         return builder.toString();
156     }
157     public String getAmountsString(int limit) {
158         if ((amounts == null) || amounts.isEmpty())
159             return "";
160
161         int included = 0;
162         StringBuilder builder = new StringBuilder();
163         for (LedgerAmount amount : amounts) {
164             String amt = amount.toString();
165             if (builder.length() > 0)
166                 builder.append('\n');
167             builder.append(amt);
168             included++;
169             if (included == limit)
170                 break;
171         }
172
173         return builder.toString();
174     }
175     public int getLevel() {
176         return level;
177     }
178     @NonNull
179     public String getShortName() {
180         return shortName;
181     }
182     public String getParentName() {
183         return (parent == null) ? null : parent.getName();
184     }
185     public boolean hasSubAccounts() {
186         return hasSubAccounts;
187     }
188     public void setHasSubAccounts(boolean hasSubAccounts) {
189         this.hasSubAccounts = hasSubAccounts;
190     }
191     public boolean isExpanded() {
192         return expanded;
193     }
194     public void setExpanded(boolean expanded) {
195         this.expanded = expanded;
196     }
197     public void toggleExpanded() {
198         expanded = !expanded;
199     }
200     public void removeAmounts() {
201         if (amounts != null)
202             amounts.clear();
203     }
204     public boolean amountsExpanded() {return amountsExpanded;}
205     public void setAmountsExpanded(boolean flag) {amountsExpanded = flag;}
206     public void toggleAmountsExpanded() {amountsExpanded = !amountsExpanded;}
207     public void propagateAmountsTo(LedgerAccount acc) {
208         for (LedgerAmount a : amounts)
209             a.propagateToAccount(acc);
210     }
211     public boolean allAmountsAreZero() {
212         for (LedgerAmount a : amounts) {
213             if (a.getAmount() != 0)
214                 return false;
215         }
216
217         return true;
218     }
219     public List<LedgerAmount> getAmounts() {
220         return amounts;
221     }
222     @NonNull
223     public Account toDBO() {
224         Account dbo = new Account();
225         dbo.setName(name);
226         dbo.setNameUpper(name.toUpperCase());
227         dbo.setParentName(extractParentName(name));
228         dbo.setLevel(level);
229         dbo.setId(dbId);
230         dbo.setProfileId(profileId);
231         dbo.setExpanded(expanded);
232         dbo.setAmountsExpanded(amountsExpanded);
233
234         return dbo;
235     }
236     @NonNull
237     public AccountWithAmounts toDBOWithAmounts() {
238         AccountWithAmounts dbo = new AccountWithAmounts();
239         dbo.account = toDBO();
240
241         dbo.amounts = new ArrayList<>();
242         for (LedgerAmount amt : getAmounts()) {
243             AccountValue val = new AccountValue();
244             val.setCurrency(amt.getCurrency());
245             val.setValue(amt.getAmount());
246             dbo.amounts.add(val);
247         }
248
249         return dbo;
250     }
251     public long getId() {
252         return dbId;
253     }
254 }