File indexing completed on 2024-05-12 16:42:33

0001 /*
0002     SPDX-FileCopyrightText: 2001-2002 Michael Edwardes <mte@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2001 Felix Rodriguez <frodriguez@users.sourceforge.net>
0004     SPDX-FileCopyrightText: 2002-2003 Kevin Tambascio <ktambascio@users.sourceforge.net>
0005     SPDX-FileCopyrightText: 2006-2017 Thomas Baumgart <tbaumgart@kde.org>
0006     SPDX-FileCopyrightText: 2006 Ace Jones <acejones@users.sourceforge.net>
0007     SPDX-FileCopyrightText: 2017-2018 Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com>
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include "mymoneyaccountloan.h"
0012 
0013 // ----------------------------------------------------------------------------
0014 // QT Includes
0015 
0016 #include <QRegExp>
0017 #include <QDate>
0018 #include <QMap>
0019 
0020 // ----------------------------------------------------------------------------
0021 // KDE Includes
0022 
0023 // ----------------------------------------------------------------------------
0024 // Project Includes
0025 
0026 #include "mymoneymoney.h"
0027 
0028 MyMoneyAccountLoan::MyMoneyAccountLoan(const MyMoneyAccount& acc)
0029     : MyMoneyAccount(acc)
0030 {
0031 }
0032 
0033 const MyMoneyMoney MyMoneyAccountLoan::loanAmount() const
0034 {
0035     return MyMoneyMoney(value("loan-amount"));
0036 }
0037 
0038 void MyMoneyAccountLoan::setLoanAmount(const MyMoneyMoney& amount)
0039 {
0040     setValue("loan-amount", amount.toString());
0041 }
0042 
0043 const MyMoneyMoney MyMoneyAccountLoan::interestRate(const QDate& date) const
0044 {
0045     MyMoneyMoney rate;
0046     QString key;
0047     QString val;
0048 
0049     if (!date.isValid())
0050         return rate;
0051 
0052     key.sprintf("ir-%04d-%02d-%02d", date.year(), date.month(), date.day());
0053 
0054     QRegExp regExp("ir-(\\d{4})-(\\d{2})-(\\d{2})");
0055 
0056     QMap<QString, QString>::ConstIterator it;
0057     for (it = pairs().constBegin(); it != pairs().constEnd(); ++it) {
0058         if (regExp.indexIn(it.key()) > -1) {
0059             if (qstrcmp(it.key().toLatin1(), key.toLatin1()) <= 0)
0060                 val = *it;
0061             else
0062                 break;
0063 
0064         } else if (!val.isEmpty())
0065             break;
0066     }
0067     if (!val.isEmpty()) {
0068         rate = MyMoneyMoney(val);
0069     }
0070 
0071     return rate;
0072 }
0073 
0074 void MyMoneyAccountLoan::setInterestRate(const QDate& date, const MyMoneyMoney& value)
0075 {
0076     if (!date.isValid())
0077         return;
0078 
0079     QString key;
0080     key.sprintf("ir-%04d-%02d-%02d", date.year(), date.month(), date.day());
0081     setValue(key, value.toString());
0082 }
0083 
0084 MyMoneyAccountLoan::interestDueE MyMoneyAccountLoan::interestCalculation() const
0085 {
0086     QString payTime(value("interest-calculation"));
0087     if (payTime == "paymentDue")
0088         return paymentDue;
0089     return paymentReceived;
0090 }
0091 
0092 void MyMoneyAccountLoan::setInterestCalculation(const MyMoneyAccountLoan::interestDueE onReception)
0093 {
0094     if (onReception == paymentDue)
0095         setValue("interest-calculation", "paymentDue");
0096     else
0097         setValue("interest-calculation", "paymentReceived");
0098 }
0099 
0100 const QDate MyMoneyAccountLoan::nextInterestChange() const
0101 {
0102     QDate rc;
0103 
0104     QRegExp regExp("(\\d{4})-(\\d{2})-(\\d{2})");
0105     if (regExp.indexIn(value("interest-nextchange")) != -1) {
0106         rc.setDate(regExp.cap(1).toInt(), regExp.cap(2).toInt(), regExp.cap(3).toInt());
0107     }
0108     return rc;
0109 }
0110 
0111 void MyMoneyAccountLoan::setNextInterestChange(const QDate& date)
0112 {
0113     setValue("interest-nextchange", date.toString(Qt::ISODate));
0114 }
0115 
0116 int MyMoneyAccountLoan::interestChangeFrequency(int* unit) const
0117 {
0118     int rc = -1;
0119 
0120     if (unit)
0121         *unit = 1;
0122 
0123     QRegExp regExp("(\\d+)/(\\d{1})");
0124     if (regExp.indexIn(value("interest-changefrequency")) != -1) {
0125         rc = regExp.cap(1).toInt();
0126         if (unit != 0) {
0127             *unit = regExp.cap(2).toInt();
0128         }
0129     }
0130     return rc;
0131 }
0132 
0133 void MyMoneyAccountLoan::setInterestChangeFrequency(const int amount, const int unit)
0134 {
0135     QString val;
0136     val.sprintf("%d/%d", amount, unit);
0137     setValue("interest-changeFrequency", val);
0138 }
0139 
0140 const QString MyMoneyAccountLoan::schedule() const
0141 {
0142     return QString(value("schedule").toLatin1());
0143 }
0144 
0145 void MyMoneyAccountLoan::setSchedule(const QString& sched)
0146 {
0147     setValue("schedule", sched);
0148 }
0149 
0150 bool MyMoneyAccountLoan::fixedInterestRate() const
0151 {
0152     // make sure, that an empty kvp element returns true
0153     return !(value("fixed-interest") == "no");
0154 }
0155 
0156 void MyMoneyAccountLoan::setFixedInterestRate(const bool fixed)
0157 {
0158     setValue("fixed-interest", fixed ? "yes" : "no");
0159     if (fixed) {
0160         deletePair("interest-nextchange");
0161         deletePair("interest-changeFrequency");
0162     }
0163 }
0164 
0165 const MyMoneyMoney MyMoneyAccountLoan::finalPayment() const
0166 {
0167     return MyMoneyMoney(value("final-payment"));
0168 }
0169 
0170 void MyMoneyAccountLoan::setFinalPayment(const MyMoneyMoney& finalPayment)
0171 {
0172     setValue("final-payment", finalPayment.toString());
0173 }
0174 
0175 unsigned int MyMoneyAccountLoan::term() const
0176 {
0177     return value("term").toUInt();
0178 }
0179 
0180 void MyMoneyAccountLoan::setTerm(const unsigned int payments)
0181 {
0182     setValue("term", QString::number(payments));
0183 }
0184 
0185 const MyMoneyMoney MyMoneyAccountLoan::periodicPayment() const
0186 {
0187     return MyMoneyMoney(value("periodic-payment"));
0188 }
0189 
0190 void MyMoneyAccountLoan::setPeriodicPayment(const MyMoneyMoney& payment)
0191 {
0192     setValue("periodic-payment", payment.toString());
0193 }
0194 
0195 const QString MyMoneyAccountLoan::payee() const
0196 {
0197     return value("payee");
0198 }
0199 
0200 void MyMoneyAccountLoan::setPayee(const QString& payee)
0201 {
0202     setValue("payee", payee);
0203 }
0204 
0205 const QString MyMoneyAccountLoan::interestAccountId() const
0206 {
0207     return QString();
0208 }
0209 
0210 void MyMoneyAccountLoan::setInterestAccountId(const QString& /* id */)
0211 {
0212 
0213 }
0214 
0215 bool MyMoneyAccountLoan::hasReferenceTo(const QString& id) const
0216 {
0217     return MyMoneyAccount::hasReferenceTo(id)
0218            || (id == payee())
0219            || (id == schedule());
0220 }
0221 
0222 void MyMoneyAccountLoan::setInterestCompounding(int frequency)
0223 {
0224     setValue("compoundingFrequency", QString("%1").arg(frequency));
0225 }
0226 
0227 int MyMoneyAccountLoan::interestCompounding() const
0228 {
0229     return value("compoundingFrequency").toInt();
0230 }