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.
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.model;
20 import androidx.annotation.NonNull;
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;
27 import java.util.ArrayList;
29 public class LedgerTransactionAccount {
30 private String accountName;
31 private String shortAccountName;
33 private boolean amountSet = false;
34 private String currency;
36 public LedgerTransactionAccount(String accountName, float amount) {
37 this(accountName, amount, null);
39 public LedgerTransactionAccount(String accountName, float amount, String currency) {
40 this.setAccountName(accountName);
42 this.amountSet = true;
43 this.currency = currency;
46 public LedgerTransactionAccount(String accountName) {
47 this.accountName = accountName;
49 public LedgerTransactionAccount(LedgerTransactionAccount origin) {
51 setAccountName(origin.getAccountName());
52 if (origin.isAmountSet())
53 setAmount(origin.getAmount());
54 currency = origin.getCurrency();
57 public String getAccountName() {
60 public void setAccountName(String accountName) {
61 this.accountName = accountName;
62 shortAccountName = accountName.replaceAll("(?<=^|:)(.)[^:]+(?=:)", "$1");
64 public String getShortAccountName() {
65 return shortAccountName;
67 public float getAmount() {
69 throw new IllegalStateException("Account amount is not set");
74 public void setAmount(float account_amount) {
75 this.amount = account_amount;
76 this.amountSet = true;
79 public void resetAmount() {
80 this.amountSet = false;
83 public boolean isAmountSet() {
86 public String getCurrency() {
90 public String toString() {
94 StringBuilder sb = new StringBuilder();
95 if (currency != null) {
99 sb.append(String.format("%,1.2f", amount));
101 return sb.toString();
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);
121 result.setPamount(amounts);