]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/json/v1_23/ParsedLedgerTransaction.java
more pronounced day/month delimiters in the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / json / v1_23 / ParsedLedgerTransaction.java
1 /*
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.
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.json.v1_23;
19
20 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
21
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;
27
28 import java.text.ParseException;
29 import java.util.ArrayList;
30 import java.util.List;
31
32 @JsonIgnoreProperties(ignoreUnknown = true)
33 public class ParsedLedgerTransaction implements net.ktnx.mobileledger.json.ParsedLedgerTransaction {
34     private String tdate;
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 = "";
41     private int tindex;
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);
49
50         tsourcepos.add(startPos);
51         tsourcepos.add(endPos);
52     }
53     public static ParsedLedgerTransaction fromLedgerTransaction(LedgerTransaction tr) {
54         ParsedLedgerTransaction result = new ParsedLedgerTransaction();
55         result.setTcomment(Misc.nullIsEmpty(tr.getComment()));
56         result.setTprecedingcomment("");
57
58         ArrayList<ParsedPosting> postings = new ArrayList<>();
59         for (LedgerTransactionAccount acc : tr.getAccounts()) {
60             if (!acc.getAccountName()
61                     .isEmpty())
62                 postings.add(ParsedPosting.fromLedgerAccount(acc));
63         }
64
65         result.setTpostings(postings);
66         SimpleDate transactionDate = tr.getDateIfAny();
67         if (transactionDate == null) {
68             transactionDate = SimpleDate.today();
69         }
70         result.setTdate(Globals.formatIsoDate(transactionDate));
71         result.setTdate2(null);
72         result.setTindex(1);
73         result.setTdescription(tr.getDescription());
74         return result;
75     }
76     public String getTcode() {
77         return tcode;
78     }
79     public void setTcode(String tcode) {
80         this.tcode = tcode;
81     }
82     public String getTstatus() {
83         return tstatus;
84     }
85     public void setTstatus(String tstatus) {
86         this.tstatus = tstatus;
87     }
88     public List<List<String>> getTtags() {
89         return ttags;
90     }
91     public void setTtags(List<List<String>> ttags) {
92         this.ttags = ttags;
93     }
94     public List<ParsedSourcePos> getTsourcepos() {
95         return tsourcepos;
96     }
97     public void setTsourcepos(List<ParsedSourcePos> tsourcepos) {
98         this.tsourcepos = tsourcepos;
99     }
100     public String getTprecedingcomment() {
101         return tprecedingcomment;
102     }
103     public void setTprecedingcomment(String tprecedingcomment) {
104         this.tprecedingcomment = tprecedingcomment;
105     }
106     public String getTdate() {
107         return tdate;
108     }
109     public void setTdate(String tdate) {
110         this.tdate = tdate;
111     }
112     public String getTdate2() {
113         return tdate2;
114     }
115     public void setTdate2(String tdate2) {
116         this.tdate2 = tdate2;
117     }
118     public String getTdescription() {
119         return tdescription;
120     }
121     public void setTdescription(String tdescription) {
122         this.tdescription = tdescription;
123     }
124     public String getTcomment() {
125         return tcomment;
126     }
127     public void setTcomment(String tcomment) {
128         this.tcomment = tcomment;
129     }
130     public int getTindex() {
131         return tindex;
132     }
133     public void setTindex(int tindex) {
134         this.tindex = tindex;
135         if (tpostings != null)
136             for (ParsedPosting p : tpostings) {
137                 p.setPtransaction_(tindex);
138             }
139     }
140     public List<ParsedPosting> getTpostings() {
141         return tpostings;
142     }
143     public void setTpostings(List<ParsedPosting> tpostings) {
144         this.tpostings = tpostings;
145     }
146     public void addPosting(ParsedPosting posting) {
147         posting.setPtransaction_(tindex);
148         tpostings.add(posting);
149     }
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)));
154
155         List<ParsedPosting> postings = tpostings;
156
157         if (postings != null) {
158             for (ParsedPosting p : postings) {
159                 tr.addAccount(p.asLedgerAccount());
160             }
161         }
162
163         tr.markDataAsLoaded();
164         return tr;
165     }
166 }