1 /***************************************************************************
2                              kgeneralloaninfopage.cpp
3                              -------------------
4     begin                : Tue Sep 25 2006
5     copyright            : (C) 2007 Thomas Baumgart
6     email                : Thomas Baumgart <ipwizard@users.sourceforge.net>
7                            (C) 2017 by Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com>
8  ***************************************************************************/
9 
10 /***************************************************************************
11  *                                                                         *
12  *   This program is free software; you can redistribute it and/or modify  *
13  *   it under the terms of the GNU General Public License as published by  *
14  *   the Free Software Foundation; either version 2 of the License, or     *
15  *   (at your option) any later version.                                   *
16  *                                                                         *
17  ***************************************************************************/
18 
19 #include "kgeneralloaninfopage.h"
20 #include "kgeneralloaninfopage_p.h"
21 
22 // ----------------------------------------------------------------------------
23 // QT Includes
24 
25 #include <QDate>
26 #include <QPushButton>
27 #include <QSpinBox>
28 
29 // ----------------------------------------------------------------------------
30 // KDE Includes
31 
32 #include <KComboBox>
33 #include <KLineEdit>
34 #include <KLocalizedString>
35 
36 // ----------------------------------------------------------------------------
37 // Project Includes
38 
39 #include "ui_kgeneralloaninfopage.h"
40 
41 #include "kmymoneydateinput.h"
42 #include "kmymoneyfrequencycombo.h"
43 #include "kmymoneypayeecombo.h"
44 #include "kmymoneywizardpage.h"
45 #include "knewaccountwizard.h"
46 #include "knewaccountwizard_p.h"
47 #include "kloandetailspage.h"
48 #include "mymoneyenums.h"
49 #include "mymoneyfile.h"
50 #include "mymoneymoney.h"
51 #include "mymoneypayee.h"
52 #include "kguiutils.h"
53 #include "wizardpage.h"
54 
55 using namespace eMyMoney;
56 
57 namespace NewAccountWizard
58 {
GeneralLoanInfoPage(Wizard * wizard)59   GeneralLoanInfoPage::GeneralLoanInfoPage(Wizard* wizard) :
60     QWidget(wizard),
61     WizardPage<Wizard>(*new GeneralLoanInfoPagePrivate(wizard), StepDetails, this, wizard)
62   {
63     Q_D(GeneralLoanInfoPage);
64     d->ui->setupUi(this);
65 
66     // reduce the amount of characters shown for a payee
67     d->ui->m_payee->setMinimumContentsLength(40);
68     d->ui->m_payee->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength);
69 
70     d->m_firstTime = true;
71     d->m_mandatoryGroup->add(d->ui->m_payee);
72 
73     // remove the unsupported payment and compounding frequencies and setup default
74     d->ui->m_paymentFrequency->removeItem((int)Schedule::Occurrence::Once);
75     d->ui->m_paymentFrequency->removeItem((int)Schedule::Occurrence::EveryOtherYear);
76     d->ui->m_paymentFrequency->setCurrentItem((int)Schedule::Occurrence::Monthly);
77     d->ui->m_compoundFrequency->removeItem((int)Schedule::Occurrence::Once);
78     d->ui->m_compoundFrequency->removeItem((int)Schedule::Occurrence::EveryOtherYear);
79     d->ui->m_compoundFrequency->setCurrentItem((int)Schedule::Occurrence::Monthly);
80 
81     slotLoadWidgets();
82 
83     connect(d->ui->m_payee, &KMyMoneyMVCCombo::createItem, wizard, &Wizard::slotPayeeNew);
84     connect(d->ui->m_anyPayments, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), object(),  &KMyMoneyWizardPagePrivate::completeStateChanged);
85     connect(d->ui->m_recordings, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), object(), &KMyMoneyWizardPagePrivate::completeStateChanged);
86 
87     connect(d->ui->m_interestType, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), object(),  &KMyMoneyWizardPagePrivate::completeStateChanged);
88     connect(d->ui->m_interestChangeDateEdit, &KMyMoneyDateInput::dateChanged, object(),  &KMyMoneyWizardPagePrivate::completeStateChanged);
89     connect(d->ui->m_openingBalance, &AmountEdit::textChanged, object(),  &KMyMoneyWizardPagePrivate::completeStateChanged);
90 
91     connect(MyMoneyFile::instance(), &MyMoneyFile::dataChanged, this, &GeneralLoanInfoPage::slotLoadWidgets);
92   }
93 
~GeneralLoanInfoPage()94   GeneralLoanInfoPage::~GeneralLoanInfoPage()
95   {
96   }
97 
nextPage() const98   KMyMoneyWizardPage* GeneralLoanInfoPage::nextPage() const
99   {
100     Q_D(const GeneralLoanInfoPage);
101     return d->m_wizard->d_func()->m_loanDetailsPage;
102   }
103 
recordAllPayments() const104   bool GeneralLoanInfoPage::recordAllPayments() const
105   {
106     Q_D(const GeneralLoanInfoPage);
107     bool rc = true;     // all payments
108     if (d->ui->m_recordings->isEnabled()) {
109         if (d->ui->m_recordings->currentIndex() != 0)
110           rc = false;
111       }
112     return rc;
113   }
114 
enterPage()115   void GeneralLoanInfoPage::enterPage()
116   {
117     Q_D(GeneralLoanInfoPage);
118     if (d->m_firstTime) {
119         // setup default dates to last of this month and one year on top
120         QDate firstDay(QDate::currentDate().year(), QDate::currentDate().month(), 1);
121         d->ui->m_firstPaymentDate->setDate(firstDay.addMonths(1).addDays(-1));
122         d->ui->m_interestChangeDateEdit->setDate(d->ui->m_firstPaymentDate->date().addYears(1));;
123         d->m_firstTime = false;
124       }
125   }
126 
isComplete() const127   bool GeneralLoanInfoPage::isComplete() const
128   {
129     Q_D(const GeneralLoanInfoPage);
130     d->m_wizard->d_func()->setStepHidden(StepPayout, !d->m_wizard->openingBalance().isZero());
131     bool rc = KMyMoneyWizardPage::isComplete();
132     if (!rc) {
133         d->m_wizard->d_func()->m_nextButton->setToolTip(i18n("No payee supplied"));
134       }
135 
136     // fixup availability of items on this page
137     d->ui->m_recordings->setDisabled(d->ui->m_anyPayments->currentIndex() == 0);
138 
139     d->ui->m_interestFrequencyAmountEdit->setDisabled(d->ui->m_interestType->currentIndex() == 0);
140     d->ui->m_interestFrequencyUnitEdit->setDisabled(d->ui->m_interestType->currentIndex() == 0);
141     d->ui->m_interestChangeDateEdit->setDisabled(d->ui->m_interestType->currentIndex() == 0);
142 
143     d->ui->m_openingBalance->setDisabled(recordAllPayments());
144 
145     if (d->ui->m_openingBalance->isEnabled() && d->ui->m_openingBalance->text().length() == 0) {
146         rc = false;
147         d->m_wizard->d_func()->m_nextButton->setToolTip(i18n("No opening balance supplied"));
148       }
149 
150     if (rc
151         && (d->ui->m_interestType->currentIndex() != 0)
152         && (d->ui->m_interestChangeDateEdit->date() <= d->ui->m_firstPaymentDate->date())) {
153         rc = false;
154         d->m_wizard->d_func()->m_nextButton->setToolTip(i18n("An interest change can only happen after the first payment"));
155       }
156     return rc;
157   }
158 
parentAccount()159   MyMoneyAccount GeneralLoanInfoPage::parentAccount()
160   {
161     Q_D(GeneralLoanInfoPage);
162     return (d->ui->m_loanDirection->currentIndex() == 0)
163         ? MyMoneyFile::instance()->liability()
164         : MyMoneyFile::instance()->asset();
165   }
166 
initialFocusWidget() const167   QWidget* GeneralLoanInfoPage::initialFocusWidget() const
168   {
169     Q_D(const GeneralLoanInfoPage);
170     return d->ui->m_loanDirection;
171   }
172 
slotLoadWidgets()173   void GeneralLoanInfoPage::slotLoadWidgets()
174   {
175     Q_D(GeneralLoanInfoPage);
176     d->ui->m_payee->loadPayees(MyMoneyFile::instance()->payeeList());
177   }
178 
179 }
180