File indexing completed on 2024-05-19 05:08:32

0001 /*
0002     SPDX-FileCopyrightText: 2000-2003 Michael Edwardes <mte@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2003-2012 Thomas Baumgart <tbaumgart@kde.org>
0004     SPDX-FileCopyrightText: 2017-2018 Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com>
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "kmymoneybriefschedule.h"
0009 
0010 // ----------------------------------------------------------------------------
0011 // QT Includes
0012 
0013 #include <QDate>
0014 #include <QLabel>
0015 #include <QToolButton>
0016 #include <QList>
0017 #include <QPushButton>
0018 #include <QIcon>
0019 
0020 // ----------------------------------------------------------------------------
0021 // KDE Includes
0022 
0023 #include <KLocalizedString>
0024 
0025 // ----------------------------------------------------------------------------
0026 // Project Includes
0027 
0028 #include "ui_kmymoneybriefschedule.h"
0029 
0030 #include "icons.h"
0031 #include "kmymoneyutils.h"
0032 #include "mymoneyaccount.h"
0033 #include "mymoneyenums.h"
0034 #include "mymoneyexception.h"
0035 #include "mymoneymoney.h"
0036 #include "mymoneyschedule.h"
0037 #include "mymoneysplit.h"
0038 #include "mymoneytransaction.h"
0039 #include "mymoneyutils.h"
0040 
0041 using namespace Icons;
0042 
0043 class KMyMoneyBriefSchedulePrivate
0044 {
0045     Q_DISABLE_COPY(KMyMoneyBriefSchedulePrivate)
0046 
0047 public:
0048     KMyMoneyBriefSchedulePrivate() :
0049         ui(new Ui::KMyMoneyBriefSchedule),
0050         m_index(0)
0051     {
0052     }
0053 
0054     ~KMyMoneyBriefSchedulePrivate()
0055     {
0056         delete ui;
0057     }
0058 
0059     void loadSchedule()
0060     {
0061         try {
0062             if (m_index < m_scheduleList.count()) {
0063                 MyMoneySchedule sched = m_scheduleList[m_index];
0064 
0065                 ui->m_indexLabel->setText(i18n("%1 of %2", m_index + 1, m_scheduleList.count()));
0066                 ui->m_name->setText(sched.name());
0067                 ui->m_type->setText(KMyMoneyUtils::scheduleTypeToString(sched.type()));
0068                 ui->m_account->setText(sched.account().name());
0069                 QString text;
0070                 MyMoneyMoney amount = sched.transaction().splitByAccount(sched.account().id()).value();
0071                 amount = amount.abs();
0072 
0073                 if (sched.willEnd()) {
0074                     int transactions = sched.paymentDates(m_date, sched.endDate()).count() - 1;
0075                     text = i18np("Payment on %2 for %3 with %1 transaction remaining occurring %4.",
0076                                  "Payment on %2 for %3 with %1 transactions remaining occurring %4.",
0077                                  transactions,
0078                                  MyMoneyUtils::formatDate(m_date),
0079                                  amount.formatMoney(sched.account().fraction()),
0080                                  sched.occurrenceToString());
0081                 } else {
0082                     text = i18n("Payment on %1 for %2 occurring %3.",
0083                                 MyMoneyUtils::formatDate(m_date),
0084                                 amount.formatMoney(sched.account().fraction()),
0085                                 sched.occurrenceToString());
0086                 }
0087 
0088                 if (m_date < QDate::currentDate()) {
0089                     if (sched.isOverdue()) {
0090                         QDate startD = (sched.lastPayment().isValid()) ?
0091                                        sched.lastPayment() :
0092                                        sched.startDate();
0093 
0094                         if (m_date.isValid())
0095                             startD = m_date;
0096 
0097                         auto days = startD.daysTo(QDate::currentDate());
0098                         int transactions = sched.paymentDates(startD, QDate::currentDate()).count();
0099 
0100                         text += "<br><font color=red>";
0101                         text += i18np("%1 day overdue", "%1 days overdue", days);
0102                         text += QString(" ");
0103                         text += i18np("(%1 occurrence.)", "(%1 occurrences.)", transactions);
0104                         text += "</color>";
0105                     }
0106                 }
0107 
0108                 ui->m_details->setText(text);
0109 
0110                 ui->m_prevButton->setEnabled(true);
0111                 ui->m_nextButton->setEnabled(true);
0112                 ui->m_skipButton->setEnabled(sched.occurrence() != eMyMoney::Schedule::Occurrence::Once);
0113 
0114                 if (m_index == 0)
0115                     ui->m_prevButton->setEnabled(false);
0116                 if (m_index == (m_scheduleList.count() - 1))
0117                     ui->m_nextButton->setEnabled(false);
0118             }
0119         } catch (const MyMoneyException &) {
0120         }
0121     }
0122 
0123 
0124     Ui::KMyMoneyBriefSchedule *ui;
0125     QList<MyMoneySchedule> m_scheduleList;
0126     int m_index;
0127     QDate m_date;
0128 
0129 };
0130 
0131 KMyMoneyBriefSchedule::KMyMoneyBriefSchedule(QWidget *parent) :
0132     QWidget(parent),
0133     d_ptr(new KMyMoneyBriefSchedulePrivate)
0134 {
0135     Q_D(KMyMoneyBriefSchedule);
0136     d->ui->setupUi(this);
0137     d->ui->m_nextButton->setIcon(Icons::get(Icon::ArrowRight));
0138     d->ui->m_prevButton->setIcon(Icons::get(Icon::ArrowLeft));
0139     d->ui->m_skipButton->setIcon(Icons::get(Icon::SeekForward));
0140     d->ui->m_buttonEnter->setIcon(Icons::get(Icon::KeyEnter));
0141 
0142     connect(d->ui->m_prevButton, &QAbstractButton::clicked, this, &KMyMoneyBriefSchedule::slotPrevClicked);
0143     connect(d->ui->m_nextButton, &QAbstractButton::clicked, this, &KMyMoneyBriefSchedule::slotNextClicked);
0144     connect(d->ui->m_closeButton, &QAbstractButton::clicked, this, &QWidget::hide);
0145     connect(d->ui->m_skipButton, &QAbstractButton::clicked, this, &KMyMoneyBriefSchedule::slotSkipClicked);
0146     connect(d->ui->m_buttonEnter, &QAbstractButton::clicked, this, &KMyMoneyBriefSchedule::slotEnterClicked);
0147 }
0148 
0149 KMyMoneyBriefSchedule::~KMyMoneyBriefSchedule()
0150 {
0151     Q_D(KMyMoneyBriefSchedule);
0152     delete d;
0153 }
0154 
0155 void KMyMoneyBriefSchedule::setSchedules(QList<MyMoneySchedule> list, const QDate& date)
0156 {
0157     Q_D(KMyMoneyBriefSchedule);
0158     d->m_scheduleList = list;
0159     d->m_date = date;
0160 
0161     d->m_index = 0;
0162     if (list.count() >= 1) {
0163         d->loadSchedule();
0164     }
0165 }
0166 
0167 void KMyMoneyBriefSchedule::slotPrevClicked()
0168 {
0169     Q_D(KMyMoneyBriefSchedule);
0170     if (d->m_index >= 1) {
0171         --d->m_index;
0172         d->loadSchedule();
0173     }
0174 }
0175 
0176 void KMyMoneyBriefSchedule::slotNextClicked()
0177 {
0178     Q_D(KMyMoneyBriefSchedule);
0179     if (d->m_index < (d->m_scheduleList.count() - 1)) {
0180         d->m_index++;
0181         d->loadSchedule();
0182     }
0183 }
0184 
0185 void KMyMoneyBriefSchedule::slotEnterClicked()
0186 {
0187     Q_D(KMyMoneyBriefSchedule);
0188     hide();
0189     Q_EMIT enterClicked(d->m_scheduleList[d->m_index], d->m_date);
0190 }
0191 
0192 void KMyMoneyBriefSchedule::slotSkipClicked()
0193 {
0194     Q_D(KMyMoneyBriefSchedule);
0195     hide();
0196     Q_EMIT skipClicked(d->m_scheduleList[d->m_index], d->m_date);
0197 }
0198