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.json.v1_15;
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;
27 import java.text.ParseException;
28 import java.util.ArrayList;
29 import java.util.Date;
30 import java.util.GregorianCalendar;
31 import java.util.List;
33 @JsonIgnoreProperties(ignoreUnknown = true)
34 public class ParsedLedgerTransaction implements net.ktnx.mobileledger.json.ParsedLedgerTransaction {
36 private String tdate2 = null;
37 private String tdescription;
38 private String tcomment;
39 private String tcode = "";
40 private String tstatus = "Unmarked";
41 private String tprecedingcomment = "";
43 private List<ParsedPosting> tpostings;
44 private List<List<String>> ttags = new ArrayList<>();
45 private ParsedSourcePos tsourcepos = new ParsedSourcePos();
46 public ParsedLedgerTransaction() {
48 public static ParsedLedgerTransaction fromLedgerTransaction(LedgerTransaction tr) {
49 ParsedLedgerTransaction result = new ParsedLedgerTransaction();
50 result.setTcomment(tr.getComment());
51 result.setTprecedingcomment("");
53 ArrayList<ParsedPosting> postings = new ArrayList<>();
54 for (LedgerTransactionAccount acc : tr.getAccounts()) {
55 if (!acc.getAccountName()
57 postings.add(ParsedPosting.fromLedgerAccount(acc));
60 result.setTpostings(postings);
61 Date transactionDate = tr.getDate();
62 if (transactionDate == null) {
63 transactionDate = new GregorianCalendar().getTime();
65 result.setTdate(Globals.formatIsoDate(transactionDate));
66 result.setTdate2(null);
68 result.setTdescription(tr.getDescription());
71 public String getTcode() {
74 public void setTcode(String tcode) {
77 public String getTstatus() {
80 public void setTstatus(String tstatus) {
81 this.tstatus = tstatus;
83 public List<List<String>> getTtags() {
86 public void setTtags(List<List<String>> ttags) {
89 public ParsedSourcePos getTsourcepos() {
92 public void setTsourcepos(ParsedSourcePos tsourcepos) {
93 this.tsourcepos = tsourcepos;
95 public String getTprecedingcomment() {
96 return tprecedingcomment;
98 public void setTprecedingcomment(String tprecedingcomment) {
99 this.tprecedingcomment = tprecedingcomment;
101 public String getTdate() {
104 public void setTdate(String tdate) {
107 public String getTdate2() {
110 public void setTdate2(String tdate2) {
111 this.tdate2 = tdate2;
113 public String getTdescription() {
116 public void setTdescription(String tdescription) {
117 this.tdescription = tdescription;
119 public String getTcomment() {
122 public void setTcomment(String tcomment) {
123 this.tcomment = tcomment;
125 public int getTindex() {
128 public void setTindex(int tindex) {
129 this.tindex = tindex;
130 if (tpostings != null)
131 for (ParsedPosting p : tpostings) {
132 p.setPtransaction_(tindex);
135 public List<ParsedPosting> getTpostings() {
138 public void setTpostings(List<ParsedPosting> tpostings) {
139 this.tpostings = tpostings;
141 public void addPosting(ParsedPosting posting) {
142 posting.setPtransaction_(tindex);
143 tpostings.add(posting);
145 public LedgerTransaction asLedgerTransaction() throws ParseException {
146 Date date = Globals.parseIsoDate(tdate);
147 LedgerTransaction tr = new LedgerTransaction(tindex, date, tdescription);
148 tr.setComment(Misc.trim(Misc.emptyIsNull(tcomment)));
150 List<ParsedPosting> postings = tpostings;
152 if (postings != null) {
153 for (ParsedPosting p : postings) {
154 tr.addAccount(p.asLedgerAccount());