]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java
separate packages for the different aspects of the application
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / LedgerTransaction.java
1 /*
2  * Copyright © 2018 Damyan Ivanov.
3  * This file is part of Mobile-Ledger.
4  * Mobile-Ledger 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  * Mobile-Ledger 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 Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.model;
19
20 import android.database.sqlite.SQLiteDatabase;
21
22 import java.util.ArrayList;
23 import java.util.Iterator;
24 import java.util.List;
25
26 public class LedgerTransaction {
27     private String id;
28     private String date;
29     private String description;
30     private List<LedgerTransactionItem> items;
31
32     public LedgerTransaction(String id, String date, String description) {
33         this.id = id;
34         this.date = date;
35         this.description = description;
36         this.items = new ArrayList<>();
37     }
38     public LedgerTransaction(String date, String description) {
39         this(null, date, description);
40     }
41     public void add_item(LedgerTransactionItem item) {
42         items.add(item);
43     }
44
45     public String getDate() {
46         return date;
47     }
48
49     public void setDate(String date) {
50         this.date = date;
51     }
52
53     public String getDescription() {
54         return description;
55     }
56
57     public void setDescription(String description) {
58         this.description = description;
59     }
60
61     public Iterator<LedgerTransactionItem> getItemsIterator() {
62         return new Iterator<LedgerTransactionItem>() {
63             private int pointer = 0;
64             @Override
65             public boolean hasNext() {
66                 return pointer < items.size();
67             }
68
69             @Override
70             public LedgerTransactionItem next() {
71                 return hasNext() ? items.get(pointer++) : null;
72             }
73         };
74     }
75     public String getId() {
76         return id;
77     }
78
79     public void insertInto(SQLiteDatabase db) {
80         db.execSQL("INSERT INTO transactions(id, date, " + "description) values(?, ?, ?)",
81                 new String[]{id, date, description});
82
83         for(LedgerTransactionItem item : items) {
84             db.execSQL("INSERT INTO transaction_accounts(transaction_id, account_name, amount, "
85                     + "currency) values(?, ?, ?, ?)", new Object[]{id, item.getAccountName(),
86                                                                    item.getAmount(), item.getCurrency()});
87         }
88     }
89 }