]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/json/v1_15/ParsedLedgerTransaction.java
305a2adef949287f4283fd785edfcfaa9229148f
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / json / v1_15 / ParsedLedgerTransaction.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.json.v1_15;
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
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;
32
33 @JsonIgnoreProperties(ignoreUnknown = true)
34 public class ParsedLedgerTransaction implements net.ktnx.mobileledger.json.ParsedLedgerTransaction {
35     private String tdate;
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 = "";
42     private int tindex;
43     private List<ParsedPosting> tpostings;
44     private List<List<String>> ttags = new ArrayList<>();
45     private ParsedSourcePos tsourcepos = new ParsedSourcePos();
46     public ParsedLedgerTransaction() {
47     }
48     public static ParsedLedgerTransaction fromLedgerTransaction(LedgerTransaction tr) {
49         ParsedLedgerTransaction result = new ParsedLedgerTransaction();
50         result.setTcomment(tr.getComment());
51         result.setTprecedingcomment("");
52
53         ArrayList<ParsedPosting> postings = new ArrayList<>();
54         for (LedgerTransactionAccount acc : tr.getAccounts()) {
55             if (!acc.getAccountName()
56                     .isEmpty())
57                 postings.add(ParsedPosting.fromLedgerAccount(acc));
58         }
59
60         result.setTpostings(postings);
61         Date transactionDate = tr.getDate();
62         if (transactionDate == null) {
63             transactionDate = new GregorianCalendar().getTime();
64         }
65         result.setTdate(Globals.formatIsoDate(transactionDate));
66         result.setTdate2(null);
67         result.setTindex(1);
68         result.setTdescription(tr.getDescription());
69         return result;
70     }
71     public String getTcode() {
72         return tcode;
73     }
74     public void setTcode(String tcode) {
75         this.tcode = tcode;
76     }
77     public String getTstatus() {
78         return tstatus;
79     }
80     public void setTstatus(String tstatus) {
81         this.tstatus = tstatus;
82     }
83     public List<List<String>> getTtags() {
84         return ttags;
85     }
86     public void setTtags(List<List<String>> ttags) {
87         this.ttags = ttags;
88     }
89     public ParsedSourcePos getTsourcepos() {
90         return tsourcepos;
91     }
92     public void setTsourcepos(ParsedSourcePos tsourcepos) {
93         this.tsourcepos = tsourcepos;
94     }
95     public String getTprecedingcomment() {
96         return tprecedingcomment;
97     }
98     public void setTprecedingcomment(String tprecedingcomment) {
99         this.tprecedingcomment = tprecedingcomment;
100     }
101     public String getTdate() {
102         return tdate;
103     }
104     public void setTdate(String tdate) {
105         this.tdate = tdate;
106     }
107     public String getTdate2() {
108         return tdate2;
109     }
110     public void setTdate2(String tdate2) {
111         this.tdate2 = tdate2;
112     }
113     public String getTdescription() {
114         return tdescription;
115     }
116     public void setTdescription(String tdescription) {
117         this.tdescription = tdescription;
118     }
119     public String getTcomment() {
120         return tcomment;
121     }
122     public void setTcomment(String tcomment) {
123         this.tcomment = tcomment;
124     }
125     public int getTindex() {
126         return tindex;
127     }
128     public void setTindex(int tindex) {
129         this.tindex = tindex;
130         if (tpostings != null)
131             for (ParsedPosting p : tpostings) {
132                 p.setPtransaction_(tindex);
133             }
134     }
135     public List<ParsedPosting> getTpostings() {
136         return tpostings;
137     }
138     public void setTpostings(List<ParsedPosting> tpostings) {
139         this.tpostings = tpostings;
140     }
141     public void addPosting(ParsedPosting posting) {
142         posting.setPtransaction_(tindex);
143         tpostings.add(posting);
144     }
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)));
149
150         List<ParsedPosting> postings = tpostings;
151
152         if (postings != null) {
153             for (ParsedPosting p : postings) {
154                 tr.addAccount(p.asLedgerAccount());
155             }
156         }
157         return tr;
158     }
159 }