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