File indexing completed on 2024-06-16 04:47:09

0001 /*
0002     SPDX-FileCopyrightText: 2000-2003 Michael Edwardes <mte@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2000-2003 Javier Campos Morales <javi_c@users.sourceforge.net>
0004     SPDX-FileCopyrightText: 2000-2003 Felix Rodriguez <frodriguez@users.sourceforge.net>
0005     SPDX-FileCopyrightText: 2000-2003 John C <thetacoturtle@users.sourceforge.net>
0006     SPDX-FileCopyrightText: 2000-2003 Thomas Baumgart <ipwizard@users.sourceforge.net>
0007     SPDX-FileCopyrightText: 2000-2003 Kevin Tambascio <ktambascio@users.sourceforge.net>
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include "knewloanwizard.h"
0012 #include "knewloanwizard_p.h"
0013 
0014 // ----------------------------------------------------------------------------
0015 // QT Includes
0016 
0017 #include <QList>
0018 
0019 // ----------------------------------------------------------------------------
0020 // KDE Includes
0021 
0022 // ----------------------------------------------------------------------------
0023 // Project Includes
0024 
0025 #include "knewaccountdlg.h"
0026 #include "mymoneypayee.h"
0027 
0028 KNewLoanWizard::KNewLoanWizard(QWidget *parent) :
0029     QWizard(parent),
0030     d_ptr(new KNewLoanWizardPrivate(this))
0031 {
0032     Q_D(KNewLoanWizard);
0033     d->init();
0034 }
0035 
0036 KNewLoanWizard::KNewLoanWizard(KNewLoanWizardPrivate &dd, QWidget *parent) :
0037     QWizard(parent),
0038     d_ptr(&dd)
0039 {
0040     Q_D(KNewLoanWizard);
0041     d->init();
0042 }
0043 
0044 KNewLoanWizard::~KNewLoanWizard()
0045 {
0046 }
0047 
0048 const MyMoneyAccountLoan KNewLoanWizard::account() const
0049 {
0050     Q_D(const KNewLoanWizard);
0051     return d->m_account;
0052 }
0053 
0054 int KNewLoanWizard::nextId() const
0055 {
0056     Q_D(const KNewLoanWizard);
0057     // Starting from the current page, look for the first enabled page
0058     // and return that value
0059     // If the end of the list is encountered first, then return -1.
0060     for (int i = currentId() + 1; i < d->m_pages.size() && i < pageIds().size(); ++i) {
0061         if (d->m_pages.testBit(i))
0062             return pageIds()[i];
0063     }
0064     return -1;
0065 }
0066 
0067 bool KNewLoanWizard::validateCurrentPage()
0068 {
0069     Q_D(KNewLoanWizard);
0070     auto dontLeavePage = false;
0071     KLocalizedString ks = ki18n(
0072                               "The loan wizard is unable to calculate two different values for your loan "
0073                               "at the same time. "
0074                               "Please enter a value for the %1 on this page or backup to the page where the "
0075                               "current value to be calculated is defined and fill in a value.");
0076 
0077     if (currentPage() == d->ui->m_lendBorrowPage) {
0078         // load the appropriate categories into the list
0079         d->loadAccountList();
0080 
0081     } else if (currentPage() == d->ui->m_interestTypePage) {
0082         if (field("fixedInterestButton").toBool()) {
0083             d->m_pages.setBit(Page_PreviousPayments);
0084             if (field("previousPaymentButton").toBool())
0085                 d->m_pages.setBit(Page_RecordPayment);
0086             else
0087                 d->m_pages.clearBit(Page_RecordPayment);
0088             d->m_pages.clearBit(Page_VariableInterestDate);
0089 
0090         } else {
0091             d->m_pages.clearBit(Page_PreviousPayments);
0092             d->m_pages.clearBit(Page_RecordPayment);
0093             d->m_pages.setBit(Page_VariableInterestDate);
0094         }
0095 
0096     } else if (currentPage() == d->ui->m_previousPaymentsPage) {
0097         if (field("previousPaymentButton").toBool()) {
0098             d->m_pages.setBit(Page_RecordPayment);
0099         } else if (field("noPreviousPaymentButton").toBool()) {
0100             d->m_pages.clearBit(Page_RecordPayment);
0101         }
0102     } else if (currentPage() == d->ui->m_loanAmountPage) {
0103         if (field("thisYearPaymentButton").toBool()
0104                 && !field("loanAmountEditValid").toBool()) {
0105             dontLeavePage = true;
0106             KMessageBox::error(0, i18n("You selected, that payments have already been made towards this loan. "
0107                                        "This requires you to enter the loan amount exactly as found on your "
0108                                        "last statement."), i18n("Calculation error"));
0109         } else
0110             d->updateLoanAmount();
0111 
0112     } else if (currentPage() == d->ui->m_interestPage) {
0113 
0114         if (!field("loanAmountEditValid").toBool()
0115                 && !field("interestRateEditValid").toBool()) {
0116             dontLeavePage = true;
0117             KMessageBox::error(0, ks.subs(i18n("interest rate")).toString(), i18n("Calculation error"));
0118         } else
0119             d->updateInterestRate();
0120 
0121     } else if (currentPage() == d->ui->m_durationPage) {
0122         if ((!field("loanAmountEditValid").toBool()
0123                 || !field("interestRateEditValid").toBool())
0124                 && field("durationValueEdit").toInt() == 0) {
0125             dontLeavePage = true;
0126             KMessageBox::error(0, ks.subs(i18n("term")).toString(), i18n("Calculation error"));
0127         } else
0128             d->updateDuration();
0129 
0130     } else if (currentPage() == d->ui->m_paymentPage) {
0131         if ((!field("loanAmountEditValid").toBool()
0132                 || !field("interestRateEditValid").toBool()
0133                 || field("durationValueEdit").toInt() == 0)
0134                 && !field("paymentEditValid").toBool()) {
0135             dontLeavePage = true;
0136             KMessageBox::error(0, ks.subs(i18n("principal and interest")).toString(), i18n("Calculation error"));
0137         } else
0138             d->updatePayment();
0139 
0140     } else if (currentPage() == d->ui->m_finalPaymentPage) {
0141         if ((!field("loanAmountEditValid").toBool()
0142                 || !field("interestRateEditValid").toBool()
0143                 || field("durationValueEdit").toInt() == 0
0144                 || !field("paymentEditValid").toBool())
0145                 && !field("finalPaymentEditValid").toBool()) {
0146             // if two fields are empty and one of them is the final payment
0147             // we assume the final payment to be 0 instead of presenting a dialog
0148             setField("finalPaymentEdit", QVariant::fromValue<MyMoneyMoney>((MyMoneyMoney())));
0149         }
0150         d->updateFinalPayment();
0151         if (!d->calculateLoan()) {
0152             dontLeavePage = true;
0153         } else
0154             d->updateLoanInfo();
0155     } else if (currentPage() == d->ui->m_schedulePage) {
0156         if (field("allPaymentsButton").toBool() || field("noPreviousPaymentButton").toBool()) {
0157             if (d->ui->m_assetAccountPage)
0158                 d->m_pages.setBit(Page_AssetAccount);
0159         } else {
0160             if (d->ui->m_assetAccountPage) {
0161                 d->m_pages.clearBit(Page_AssetAccount);
0162                 if (d->ui->m_assetAccountPage->ui && d->ui->m_assetAccountPage->ui->m_assetAccountEdit) {
0163                     d->ui->m_assetAccountPage->ui->m_assetAccountEdit->slotDeselectAllAccounts();
0164                 }
0165             }
0166         }
0167     }
0168 
0169     if (!dontLeavePage)
0170         return true;
0171     else
0172         return false;
0173 }
0174 
0175 MyMoneySchedule KNewLoanWizard::schedule() const
0176 {
0177     MyMoneySchedule sched(field("nameEdit").toString(),
0178                           eMyMoney::Schedule::Type::LoanPayment,
0179                           eMyMoney::Schedule::Occurrence(field("paymentFrequencyUnitEdit").toInt()), 1,
0180                           eMyMoney::Schedule::PaymentType::Other,
0181                           QDate(),
0182                           QDate(),
0183                           false,
0184                           false);
0185 
0186     Q_D(const KNewLoanWizard);
0187     MyMoneyTransaction t = d->transaction();
0188     t.setPostDate(field("nextDueDateEdit").toDate());
0189     sched.setTransaction(t);
0190 
0191     return sched;
0192 }
0193 
0194 void KNewLoanWizard::slotReloadEditWidgets()
0195 {
0196     Q_D(KNewLoanWizard);
0197     // load the various account widgets
0198     d->loadAccountList();
0199 
0200     // reload payee widget
0201     auto payeeId = field("payeeEdit").toString();
0202 
0203     //FIXME: port
0204     d->ui->m_namePage->ui->m_payeeEdit->loadPayees(MyMoneyFile::instance()->payeeList());
0205 
0206     if (!payeeId.isEmpty()) {
0207         setField("payeeEdit", payeeId);
0208     }
0209 }
0210 
0211 QString KNewLoanWizard::initialPaymentAccount() const
0212 {
0213     if (field("dontCreatePayoutCheckBox").toBool()) {
0214         return QString();
0215     }
0216     return field("assetAccountEdit").toStringList().first();
0217 }
0218 
0219 QDate KNewLoanWizard::initialPaymentDate() const
0220 {
0221     if (field("dontCreatePayoutCheckBox").toBool()) {
0222         return QDate();
0223     }
0224     return field("paymentDate").toDate();
0225 }
0226 
0227 void KNewLoanWizard::slotNewCategory(MyMoneyAccount& acc)
0228 {
0229     KNewAccountDlg::newCategory(acc, MyMoneyAccount());
0230 }
0231 
0232 void KNewLoanWizard::slotNewPayee(const QString& newnameBase, QString& id)
0233 {
0234     bool ok;
0235     std::tie(ok, id) = KMyMoneyUtils::newPayee(newnameBase);
0236 }