]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/LedgerTransactionAccount.java
801e0169845f8c2ff2daedb06177eec7d62ba3d6
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / LedgerTransactionAccount.java
1 /*
2  * Copyright © 2019 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
22 import net.ktnx.mobileledger.json.ParsedAmount;
23 import net.ktnx.mobileledger.json.ParsedPosting;
24 import net.ktnx.mobileledger.json.ParsedQuantity;
25 import net.ktnx.mobileledger.json.ParsedStyle;
26
27 import java.util.ArrayList;
28
29 public class LedgerTransactionAccount {
30     private String accountName;
31     private String shortAccountName;
32     private float amount;
33     private boolean amountSet = false;
34     private String currency;
35
36     public LedgerTransactionAccount(String accountName, float amount) {
37         this(accountName, amount, null);
38     }
39     public LedgerTransactionAccount(String accountName, float amount, String currency) {
40         this.setAccountName(accountName);
41         this.amount = amount;
42         this.amountSet = true;
43         this.currency = currency;
44     }
45
46     public LedgerTransactionAccount(String accountName) {
47         this.accountName = accountName;
48     }
49     public LedgerTransactionAccount(LedgerTransactionAccount origin) {
50         // copy constructor
51         setAccountName(origin.getAccountName());
52         if (origin.isAmountSet())
53             setAmount(origin.getAmount());
54         currency = origin.getCurrency();
55     }
56
57     public String getAccountName() {
58         return accountName;
59     }
60     public void setAccountName(String accountName) {
61         this.accountName = accountName;
62         shortAccountName = accountName.replaceAll("(?<=^|:)(.)[^:]+(?=:)", "$1");
63     }
64     public String getShortAccountName() {
65         return shortAccountName;
66     }
67     public float getAmount() {
68         if (!amountSet)
69             throw new IllegalStateException("Account amount is not set");
70
71         return amount;
72     }
73
74     public void setAmount(float account_amount) {
75         this.amount = account_amount;
76         this.amountSet = true;
77     }
78
79     public void resetAmount() {
80         this.amountSet = false;
81     }
82
83     public boolean isAmountSet() {
84         return amountSet;
85     }
86     public String getCurrency() {
87         return currency;
88     }
89     @NonNull
90     public String toString() {
91         if (!amountSet)
92             return "";
93
94         StringBuilder sb = new StringBuilder();
95         if (currency != null) {
96             sb.append(currency);
97             sb.append(' ');
98         }
99         sb.append(String.format("%,1.2f", amount));
100
101         return sb.toString();
102     }
103     public ParsedPosting asParsedPosting() {
104         ParsedPosting result = new ParsedPosting();
105         result.setPaccount(accountName);
106         ArrayList<ParsedAmount> amounts = new ArrayList<>();
107         ParsedAmount amt = new ParsedAmount();
108         amt.setAcommodity((currency == null) ? "" : currency);
109         amt.setAismultiplier(false);
110         ParsedQuantity qty = new ParsedQuantity();
111         qty.setDecimalPlaces(2);
112         qty.setDecimalMantissa(Math.round(amount * 100));
113         amt.setAquantity(qty);
114         ParsedStyle style = new ParsedStyle();
115         style.setAscommodityside('L');
116         style.setAscommodityspaced(false);
117         style.setAsprecision(2);
118         style.setAsdecimalpoint('.');
119         amt.setAstyle(style);
120         amounts.add(amt);
121         result.setPamount(amounts);
122         return result;
123     }
124 }