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_14;
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
46 tsourcepos = new ParsedSourcePos();
47 public ParsedLedgerTransaction() {
49 public static ParsedLedgerTransaction fromLedgerTransaction(LedgerTransaction tr) {
50 ParsedLedgerTransaction
51 result = new ParsedLedgerTransaction();
52 result.setTcomment(tr.getComment());
53 result.setTprecedingcomment("");
55 ArrayList<ParsedPosting> postings = new ArrayList<>();
56 for (LedgerTransactionAccount acc : tr.getAccounts()) {
57 if (!acc.getAccountName()
59 postings.add(ParsedPosting.fromLedgerAccount(acc));
62 result.setTpostings(postings);
63 Date transactionDate = tr.getDate();
64 if (transactionDate == null) {
65 transactionDate = new GregorianCalendar().getTime();
67 result.setTdate(Globals.formatIsoDate(transactionDate));
68 result.setTdate2(null);
70 result.setTdescription(tr.getDescription());
73 public String getTcode() {
76 public void setTcode(String tcode) {
79 public String getTstatus() {
82 public void setTstatus(String tstatus) {
83 this.tstatus = tstatus;
85 public List<List<String>> getTtags() {
88 public void setTtags(List<List<String>> ttags) {
91 public ParsedSourcePos getTsourcepos() {
94 public void setTsourcepos(ParsedSourcePos tsourcepos) {
95 this.tsourcepos = tsourcepos;
97 public String getTprecedingcomment() {
98 return tprecedingcomment;
100 public void setTprecedingcomment(String tprecedingcomment) {
101 this.tprecedingcomment = tprecedingcomment;
103 public String getTdate() {
106 public void setTdate(String tdate) {
109 public String getTdate2() {
112 public void setTdate2(String tdate2) {
113 this.tdate2 = tdate2;
115 public String getTdescription() {
118 public void setTdescription(String tdescription) {
119 this.tdescription = tdescription;
121 public String getTcomment() {
124 public void setTcomment(String tcomment) {
125 this.tcomment = tcomment;
127 public int getTindex() {
130 public void setTindex(int tindex) {
131 this.tindex = tindex;
132 if (tpostings != null)
133 for (ParsedPosting p : tpostings) {
134 p.setPtransaction_(tindex);
137 public List<ParsedPosting> getTpostings() {
140 public void setTpostings(List<ParsedPosting> tpostings) {
141 this.tpostings = tpostings;
143 public void addPosting(ParsedPosting posting) {
144 posting.setPtransaction_(tindex);
145 tpostings.add(posting);
147 public LedgerTransaction asLedgerTransaction() throws ParseException {
148 Date date = Globals.parseIsoDate(tdate);
149 LedgerTransaction tr = new LedgerTransaction(tindex, date, tdescription);
150 tr.setComment(Misc.trim(Misc.emptyIsNull(tcomment)));
152 List<ParsedPosting> postings = tpostings;
154 if (postings != null) {
155 for (ParsedPosting p : postings) {
156 tr.addAccount(p.asLedgerAccount());