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