1 /*
2 * Copyright 2001-2002 Michael Edwardes <mte@users.sourceforge.net>
3 * Copyright 2001 Felix Rodriguez <frodriguez@users.sourceforge.net>
4 * Copyright 2002-2003 Kevin Tambascio <ktambascio@users.sourceforge.net>
5 * Copyright 2006-2017 Thomas Baumgart <tbaumgart@kde.org>
6 * Copyright 2006 Ace Jones <acejones@users.sourceforge.net>
7 * Copyright 2017-2018 Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "mymoneyaccountloan.h"
24
25 // ----------------------------------------------------------------------------
26 // QT Includes
27
28 #include <QRegExp>
29 #include <QDate>
30 #include <QMap>
31
32 // ----------------------------------------------------------------------------
33 // KDE Includes
34
35 // ----------------------------------------------------------------------------
36 // Project Includes
37
38 #include "mymoneymoney.h"
39
MyMoneyAccountLoan(const MyMoneyAccount & acc)40 MyMoneyAccountLoan::MyMoneyAccountLoan(const MyMoneyAccount& acc)
41 : MyMoneyAccount(acc)
42 {
43 }
44
loanAmount() const45 const MyMoneyMoney MyMoneyAccountLoan::loanAmount() const
46 {
47 return MyMoneyMoney(value("loan-amount"));
48 }
49
setLoanAmount(const MyMoneyMoney & amount)50 void MyMoneyAccountLoan::setLoanAmount(const MyMoneyMoney& amount)
51 {
52 setValue("loan-amount", amount.toString());
53 }
54
interestRate(const QDate & date) const55 const MyMoneyMoney MyMoneyAccountLoan::interestRate(const QDate& date) const
56 {
57 MyMoneyMoney rate;
58 QString key;
59 QString val;
60
61 if (!date.isValid())
62 return rate;
63
64 key.sprintf("ir-%04d-%02d-%02d", date.year(), date.month(), date.day());
65
66 QRegExp regExp("ir-(\\d{4})-(\\d{2})-(\\d{2})");
67
68 QMap<QString, QString>::ConstIterator it;
69 for (it = pairs().constBegin(); it != pairs().constEnd(); ++it) {
70 if (regExp.indexIn(it.key()) > -1) {
71 if (qstrcmp(it.key().toLatin1(), key.toLatin1()) <= 0)
72 val = *it;
73 else
74 break;
75
76 } else if (!val.isEmpty())
77 break;
78 }
79 if (!val.isEmpty()) {
80 rate = MyMoneyMoney(val);
81 }
82
83 return rate;
84 }
85
setInterestRate(const QDate & date,const MyMoneyMoney & value)86 void MyMoneyAccountLoan::setInterestRate(const QDate& date, const MyMoneyMoney& value)
87 {
88 if (!date.isValid())
89 return;
90
91 QString key;
92 key.sprintf("ir-%04d-%02d-%02d", date.year(), date.month(), date.day());
93 setValue(key, value.toString());
94 }
95
interestCalculation() const96 MyMoneyAccountLoan::interestDueE MyMoneyAccountLoan::interestCalculation() const
97 {
98 QString payTime(value("interest-calculation"));
99 if (payTime == "paymentDue")
100 return paymentDue;
101 return paymentReceived;
102 }
103
setInterestCalculation(const MyMoneyAccountLoan::interestDueE onReception)104 void MyMoneyAccountLoan::setInterestCalculation(const MyMoneyAccountLoan::interestDueE onReception)
105 {
106 if (onReception == paymentDue)
107 setValue("interest-calculation", "paymentDue");
108 else
109 setValue("interest-calculation", "paymentReceived");
110 }
111
nextInterestChange() const112 const QDate MyMoneyAccountLoan::nextInterestChange() const
113 {
114 QDate rc;
115
116 QRegExp regExp("(\\d{4})-(\\d{2})-(\\d{2})");
117 if (regExp.indexIn(value("interest-nextchange")) != -1) {
118 rc.setDate(regExp.cap(1).toInt(), regExp.cap(2).toInt(), regExp.cap(3).toInt());
119 }
120 return rc;
121 }
122
setNextInterestChange(const QDate & date)123 void MyMoneyAccountLoan::setNextInterestChange(const QDate& date)
124 {
125 setValue("interest-nextchange", date.toString(Qt::ISODate));
126 }
127
interestChangeFrequency(int * unit) const128 int MyMoneyAccountLoan::interestChangeFrequency(int* unit) const
129 {
130 int rc = -1;
131
132 if (unit)
133 *unit = 1;
134
135 QRegExp regExp("(\\d+)/(\\d{1})");
136 if (regExp.indexIn(value("interest-changefrequency")) != -1) {
137 rc = regExp.cap(1).toInt();
138 if (unit != 0) {
139 *unit = regExp.cap(2).toInt();
140 }
141 }
142 return rc;
143 }
144
setInterestChangeFrequency(const int amount,const int unit)145 void MyMoneyAccountLoan::setInterestChangeFrequency(const int amount, const int unit)
146 {
147 QString val;
148 val.sprintf("%d/%d", amount, unit);
149 setValue("interest-changeFrequency", val);
150 }
151
schedule() const152 const QString MyMoneyAccountLoan::schedule() const
153 {
154 return QString(value("schedule").toLatin1());
155 }
156
setSchedule(const QString & sched)157 void MyMoneyAccountLoan::setSchedule(const QString& sched)
158 {
159 setValue("schedule", sched);
160 }
161
fixedInterestRate() const162 bool MyMoneyAccountLoan::fixedInterestRate() const
163 {
164 // make sure, that an empty kvp element returns true
165 return !(value("fixed-interest") == "no");
166 }
167
setFixedInterestRate(const bool fixed)168 void MyMoneyAccountLoan::setFixedInterestRate(const bool fixed)
169 {
170 setValue("fixed-interest", fixed ? "yes" : "no");
171 if (fixed) {
172 deletePair("interest-nextchange");
173 deletePair("interest-changeFrequency");
174 }
175 }
176
finalPayment() const177 const MyMoneyMoney MyMoneyAccountLoan::finalPayment() const
178 {
179 return MyMoneyMoney(value("final-payment"));
180 }
181
setFinalPayment(const MyMoneyMoney & finalPayment)182 void MyMoneyAccountLoan::setFinalPayment(const MyMoneyMoney& finalPayment)
183 {
184 setValue("final-payment", finalPayment.toString());
185 }
186
term() const187 unsigned int MyMoneyAccountLoan::term() const
188 {
189 return value("term").toUInt();
190 }
191
setTerm(const unsigned int payments)192 void MyMoneyAccountLoan::setTerm(const unsigned int payments)
193 {
194 setValue("term", QString::number(payments));
195 }
196
periodicPayment() const197 const MyMoneyMoney MyMoneyAccountLoan::periodicPayment() const
198 {
199 return MyMoneyMoney(value("periodic-payment"));
200 }
201
setPeriodicPayment(const MyMoneyMoney & payment)202 void MyMoneyAccountLoan::setPeriodicPayment(const MyMoneyMoney& payment)
203 {
204 setValue("periodic-payment", payment.toString());
205 }
206
payee() const207 const QString MyMoneyAccountLoan::payee() const
208 {
209 return value("payee");
210 }
211
setPayee(const QString & payee)212 void MyMoneyAccountLoan::setPayee(const QString& payee)
213 {
214 setValue("payee", payee);
215 }
216
interestAccountId() const217 const QString MyMoneyAccountLoan::interestAccountId() const
218 {
219 return QString();
220 }
221
setInterestAccountId(const QString &)222 void MyMoneyAccountLoan::setInterestAccountId(const QString& /* id */)
223 {
224
225 }
226
hasReferenceTo(const QString & id) const227 bool MyMoneyAccountLoan::hasReferenceTo(const QString& id) const
228 {
229 return MyMoneyAccount::hasReferenceTo(id)
230 || (id == payee())
231 || (id == schedule());
232 }
233
setInterestCompounding(int frequency)234 void MyMoneyAccountLoan::setInterestCompounding(int frequency)
235 {
236 setValue("compoundingFrequency", QString("%1").arg(frequency));
237 }
238
interestCompounding() const239 int MyMoneyAccountLoan::interestCompounding() const
240 {
241 return value("compoundingFrequency").toInt();
242 }
243