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.json.v1_23;
20 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
22 import net.ktnx.mobileledger.model.LedgerTransaction;
23 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
24 import net.ktnx.mobileledger.utils.Globals;
25 import net.ktnx.mobileledger.utils.Misc;
26 import net.ktnx.mobileledger.utils.SimpleDate;
28 import java.text.ParseException;
29 import java.util.ArrayList;
30 import java.util.List;
32 @JsonIgnoreProperties(ignoreUnknown = true)
33 public class ParsedLedgerTransaction implements net.ktnx.mobileledger.json.ParsedLedgerTransaction {
35 private String tdate2 = null;
36 private String tdescription;
37 private String tcomment;
38 private String tcode = "";
39 private String tstatus = "Unmarked";
40 private String tprecedingcomment = "";
42 private List<ParsedPosting> tpostings;
43 private List<List<String>> ttags = new ArrayList<>();
44 private List<ParsedSourcePos> tsourcepos = new ArrayList<>();
45 public ParsedLedgerTransaction() {
46 ParsedSourcePos startPos = new ParsedSourcePos();
47 ParsedSourcePos endPos = new ParsedSourcePos();
48 endPos.setSourceLine(2);
50 tsourcepos.add(startPos);
51 tsourcepos.add(endPos);
53 public static ParsedLedgerTransaction fromLedgerTransaction(LedgerTransaction tr) {
54 ParsedLedgerTransaction result = new ParsedLedgerTransaction();
55 result.setTcomment(Misc.nullIsEmpty(tr.getComment()));
56 result.setTprecedingcomment("");
58 ArrayList<ParsedPosting> postings = new ArrayList<>();
59 for (LedgerTransactionAccount acc : tr.getAccounts()) {
60 if (!acc.getAccountName()
62 postings.add(ParsedPosting.fromLedgerAccount(acc));
65 result.setTpostings(postings);
66 SimpleDate transactionDate = tr.getDateIfAny();
67 if (transactionDate == null) {
68 transactionDate = SimpleDate.today();
70 result.setTdate(Globals.formatIsoDate(transactionDate));
71 result.setTdate2(null);
73 result.setTdescription(tr.getDescription());
76 public String getTcode() {
79 public void setTcode(String tcode) {
82 public String getTstatus() {
85 public void setTstatus(String tstatus) {
86 this.tstatus = tstatus;
88 public List<List<String>> getTtags() {
91 public void setTtags(List<List<String>> ttags) {
94 public List<ParsedSourcePos> getTsourcepos() {
97 public void setTsourcepos(List<ParsedSourcePos> tsourcepos) {
98 this.tsourcepos = tsourcepos;
100 public String getTprecedingcomment() {
101 return tprecedingcomment;
103 public void setTprecedingcomment(String tprecedingcomment) {
104 this.tprecedingcomment = tprecedingcomment;
106 public String getTdate() {
109 public void setTdate(String tdate) {
112 public String getTdate2() {
115 public void setTdate2(String tdate2) {
116 this.tdate2 = tdate2;
118 public String getTdescription() {
121 public void setTdescription(String tdescription) {
122 this.tdescription = tdescription;
124 public String getTcomment() {
127 public void setTcomment(String tcomment) {
128 this.tcomment = tcomment;
130 public int getTindex() {
133 public void setTindex(int tindex) {
134 this.tindex = tindex;
135 if (tpostings != null)
136 for (ParsedPosting p : tpostings) {
137 p.setPtransaction_(tindex);
140 public List<ParsedPosting> getTpostings() {
143 public void setTpostings(List<ParsedPosting> tpostings) {
144 this.tpostings = tpostings;
146 public void addPosting(ParsedPosting posting) {
147 posting.setPtransaction_(tindex);
148 tpostings.add(posting);
150 public LedgerTransaction asLedgerTransaction() throws ParseException {
151 SimpleDate date = Globals.parseIsoDate(tdate);
152 LedgerTransaction tr = new LedgerTransaction(tindex, date, tdescription);
153 tr.setComment(Misc.trim(Misc.emptyIsNull(tcomment)));
155 List<ParsedPosting> postings = tpostings;
157 if (postings != null) {
158 for (ParsedPosting p : postings) {
159 tr.addAccount(p.asLedgerAccount());
163 tr.markDataAsLoaded();