]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/db/TemplateAccount.java
fix crash when auto-balancing multi currency transaction
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / db / TemplateAccount.java
1 /*
2  * Copyright © 2021 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.db;
19
20 import androidx.annotation.NonNull;
21 import androidx.room.ColumnInfo;
22 import androidx.room.Entity;
23 import androidx.room.ForeignKey;
24 import androidx.room.Index;
25 import androidx.room.PrimaryKey;
26
27 import org.jetbrains.annotations.NotNull;
28
29 @Entity(tableName = "template_accounts",
30         indices = {@Index(name = "fk_template_accounts_template", value = "template_id"),
31                    @Index(name = "fk_template_accounts_currency", value = "currency")
32         }, foreignKeys = {@ForeignKey(childColumns = "template_id", parentColumns = "id",
33                                       entity = TemplateHeader.class, onDelete = ForeignKey.CASCADE,
34                                       onUpdate = ForeignKey.RESTRICT),
35                           @ForeignKey(childColumns = "currency", parentColumns = "id",
36                                       entity = Currency.class, onDelete = ForeignKey.RESTRICT,
37                                       onUpdate = ForeignKey.RESTRICT)
38 })
39 public class TemplateAccount extends TemplateBase {
40     @PrimaryKey(autoGenerate = true)
41     private long id;
42     @ColumnInfo(name = "template_id")
43     private long templateId;
44     @ColumnInfo(name = "acc")
45     private String accountName;
46     @ColumnInfo(name = "position")
47     @NonNull
48     private Long position;
49     @ColumnInfo(name = "acc_match_group")
50     private Integer accountNameMatchGroup;
51     @ColumnInfo
52     private Long currency;
53     @ColumnInfo(name = "currency_match_group")
54     private Integer currencyMatchGroup;
55     @ColumnInfo(name = "amount")
56     private Float amount;
57     @ColumnInfo(name = "amount_match_group")
58     private Integer amountMatchGroup;
59     @ColumnInfo(name = "comment")
60     private String accountComment;
61     @ColumnInfo(name = "comment_match_group")
62     private Integer accountCommentMatchGroup;
63     @ColumnInfo(name = "negate_amount")
64     private Boolean negateAmount;
65     public TemplateAccount(@NotNull Long id, @NonNull Long templateId, @NonNull Long position) {
66         this.id = id;
67         this.templateId = templateId;
68         this.position = position;
69     }
70     public TemplateAccount(TemplateAccount o) {
71         id = o.id;
72         templateId = o.templateId;
73         accountName = o.accountName;
74         position = o.position;
75         accountNameMatchGroup = o.accountNameMatchGroup;
76         currency = o.currency;
77         currencyMatchGroup = o.currencyMatchGroup;
78         amount = o.amount;
79         amountMatchGroup = o.amountMatchGroup;
80         accountComment = o.accountComment;
81         accountCommentMatchGroup = o.accountCommentMatchGroup;
82         negateAmount = o.negateAmount;
83     }
84     public long getId() {
85         return id;
86     }
87     public void setId(long id) {
88         this.id = id;
89     }
90     public Boolean getNegateAmount() {
91         return negateAmount;
92     }
93     public void setNegateAmount(Boolean negateAmount) {
94         this.negateAmount = negateAmount;
95     }
96     public long getTemplateId() {
97         return templateId;
98     }
99     public void setTemplateId(long templateId) {
100         this.templateId = templateId;
101     }
102     @NonNull
103     public String getAccountName() {
104         return accountName;
105     }
106     public void setAccountName(@NonNull String accountName) {
107         this.accountName = accountName;
108     }
109     @NonNull
110     public Long getPosition() {
111         return position;
112     }
113     public void setPosition(@NonNull Long position) {
114         this.position = position;
115     }
116     public void setPosition(int position) {
117         this.position = (long) position;
118     }
119     public Integer getAccountNameMatchGroup() {
120         return accountNameMatchGroup;
121     }
122     public void setAccountNameMatchGroup(Integer accountNameMatchGroup) {
123         this.accountNameMatchGroup = accountNameMatchGroup;
124     }
125     public Long getCurrency() {
126         return currency;
127     }
128     public void setCurrency(Long currency) {
129         this.currency = currency;
130     }
131     public Currency getCurrencyObject() {
132         if (currency == null || currency <= 0)
133             return null;
134         return DB.get()
135                  .getCurrencyDAO()
136                  .getByIdSync(currency);
137     }
138     public Integer getCurrencyMatchGroup() {
139         return currencyMatchGroup;
140     }
141     public void setCurrencyMatchGroup(Integer currencyMatchGroup) {
142         this.currencyMatchGroup = currencyMatchGroup;
143     }
144     public Float getAmount() {
145         return amount;
146     }
147     public void setAmount(Float amount) {
148         this.amount = amount;
149     }
150     public Integer getAmountMatchGroup() {
151         return amountMatchGroup;
152     }
153     public void setAmountMatchGroup(Integer amountMatchGroup) {
154         this.amountMatchGroup = amountMatchGroup;
155     }
156     public String getAccountComment() {
157         return accountComment;
158     }
159     public void setAccountComment(String accountComment) {
160         this.accountComment = accountComment;
161     }
162     public Integer getAccountCommentMatchGroup() {
163         return accountCommentMatchGroup;
164     }
165     public void setAccountCommentMatchGroup(Integer accountCommentMatchGroup) {
166         this.accountCommentMatchGroup = accountCommentMatchGroup;
167     }
168     public TemplateAccount createDuplicate(TemplateHeader header) {
169         TemplateAccount dup = new TemplateAccount(this);
170         dup.id = 0;
171         dup.templateId = header.getId();
172
173         return dup;
174     }
175 }