File indexing completed on 2024-05-12 16:44:08

0001 /*
0002     SPDX-FileCopyrightText: 2008-2018 Thomas Baumgart <tbaumgart@kde.org>
0003     SPDX-FileCopyrightText: 2017 Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com>
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "stdtransactionmatched.h"
0008 #include "stdtransaction_p.h"
0009 
0010 // ----------------------------------------------------------------------------
0011 // QT Includes
0012 
0013 #include <QList>
0014 #include <QPainter>
0015 #include <QFont>
0016 
0017 // ----------------------------------------------------------------------------
0018 // KDE Includes
0019 
0020 #include <KLocalizedString>
0021 
0022 // ----------------------------------------------------------------------------
0023 // Project Includes
0024 
0025 #include "kmymoneysettings.h"
0026 #include "mymoneyaccount.h"
0027 #include "mymoneymoney.h"
0028 #include "mymoneysplit.h"
0029 #include "mymoneytransaction.h"
0030 #include "mymoneyexception.h"
0031 #include "widgetenums.h"
0032 
0033 using namespace KMyMoneyRegister;
0034 using namespace KMyMoneyTransactionForm;
0035 
0036 StdTransactionMatched::StdTransactionMatched(Register *parent, const MyMoneyTransaction& transaction, const MyMoneySplit& split, int uniqueId) :
0037     StdTransaction(parent, transaction, split, uniqueId)
0038 {
0039     // setup initial size
0040     setNumRowsRegister(numRowsRegister(KMyMoneySettings::showRegisterDetailed()));
0041 }
0042 
0043 StdTransactionMatched::~StdTransactionMatched()
0044 {
0045 }
0046 
0047 const char* StdTransactionMatched::className()
0048 {
0049     return "StdTransactionMatched";
0050 }
0051 
0052 bool StdTransactionMatched::paintRegisterCellSetup(QPainter *painter, QStyleOptionViewItem &option, const QModelIndex &index)
0053 {
0054     auto rc = Transaction::paintRegisterCellSetup(painter, option, index);
0055 
0056     // if not selected paint in matched background color
0057     if (!isSelected()) {
0058         option.palette.setColor(QPalette::Base, KMyMoneySettings::schemeColor(SchemeColor::TransactionMatched));
0059         option.palette.setColor(QPalette::AlternateBase, KMyMoneySettings::schemeColor(SchemeColor::TransactionMatched));
0060     }
0061     QFont font = painter->font();
0062     font.setBold(true);
0063     painter->setFont(font);
0064 
0065     //TODO: the first line needs to be painted across all columns
0066     return rc;
0067 }
0068 
0069 void StdTransactionMatched::registerCellText(QString& txt, Qt::Alignment& align, int row, int col, QPainter* painter)
0070 {
0071     Q_D(StdTransaction);
0072     // run through the standard
0073     StdTransaction::registerCellText(txt, align, row, col, painter);
0074 
0075     // we only cover the additional rows
0076     if (row >= RegisterItem::numRowsRegister() - m_additionalRows) {
0077         // make row relative to the last three rows
0078         row += m_additionalRows - RegisterItem::numRowsRegister();
0079 
0080         // remove anything that had been added by the standard method
0081         txt = QString();
0082 
0083         // and we draw this information in italics
0084         if (painter) {
0085             QFont font = painter->font();
0086             font.setItalic(true);
0087             painter->setFont(font);
0088         }
0089 
0090         MyMoneyTransaction matchedTransaction = d->m_split.matchedTransaction();
0091         MyMoneySplit matchedSplit;
0092         try {
0093             matchedSplit = matchedTransaction.splitById(d->m_split.value("kmm-match-split"));
0094         } catch (const MyMoneyException &) {
0095         }
0096 
0097         MyMoneyMoney importedValue;
0098         foreach (const auto split, matchedTransaction.splits()) {
0099             if (split.accountId() == d->m_account.id()) {
0100                 importedValue += split.shares();
0101             }
0102         }
0103 
0104         QDate postDate;
0105         QString memo;
0106         switch (row) {
0107         case 0:
0108             if (painter && col == (int)eWidgets::eTransaction::Column::Detail)
0109                 txt = QString(" ") + i18n("KMyMoney has matched the two selected transactions (result above)");
0110             // return true for the first visible column only
0111             break;
0112 
0113         case 1:
0114             switch (col) {
0115             case (int)eWidgets::eTransaction::Column::Date:
0116                 align |= Qt::AlignLeft;
0117                 txt = i18n("Bank entry:");
0118                 break;
0119 
0120             case (int)eWidgets::eTransaction::Column::Detail:
0121                 align |= Qt::AlignLeft;
0122                 memo = matchedTransaction.memo();
0123                 memo.replace("\n\n", "\n");
0124                 memo.replace('\n', ", ");
0125                 txt = QString("%1 %2").arg(matchedTransaction.postDate().toString(Qt::ISODate)).arg(memo);
0126                 break;
0127 
0128             case (int)eWidgets::eTransaction::Column::Payment:
0129                 align |= Qt::AlignRight;
0130                 if (importedValue.isNegative()) {
0131                     txt = (-importedValue).formatMoney(d->m_account.fraction());
0132                 }
0133                 break;
0134 
0135             case (int)eWidgets::eTransaction::Column::Deposit:
0136                 align |= Qt::AlignRight;
0137                 if (!importedValue.isNegative()) {
0138                     txt = importedValue.formatMoney(d->m_account.fraction());
0139                 }
0140                 break;
0141             }
0142             break;
0143 
0144         case 2:
0145             switch (col) {
0146             case (int)eWidgets::eTransaction::Column::Date:
0147                 align |= Qt::AlignLeft;
0148                 txt = i18n("Your entry:");
0149                 break;
0150 
0151             case (int)eWidgets::eTransaction::Column::Detail:
0152                 align |= Qt::AlignLeft;
0153                 postDate = d->m_transaction.postDate();
0154                 if (!d->m_split.value("kmm-orig-postdate").isEmpty()) {
0155                     postDate = QDate::fromString(d->m_split.value("kmm-orig-postdate"), Qt::ISODate);
0156                 }
0157                 memo = d->m_split.memo();
0158                 if (!matchedSplit.memo().isEmpty() && memo != matchedSplit.memo()) {
0159                     int pos = memo.lastIndexOf(matchedSplit.memo());
0160                     if (pos != -1) {
0161                         memo = memo.left(pos);
0162                         // replace all new line characters because we only have one line available for the displayed data
0163                     }
0164                 }
0165                 memo.replace("\n\n", "\n");
0166                 memo.replace('\n', ", ");
0167                 txt = QString("%1 %2").arg(postDate.toString(Qt::ISODate)).arg(memo);
0168                 break;
0169 
0170             case (int)eWidgets::eTransaction::Column::Payment:
0171                 align |= Qt::AlignRight;
0172                 if (d->m_split.value().isNegative()) {
0173                     txt = (-d->m_split.value(d->m_transaction.commodity(), d->m_splitCurrencyId)).formatMoney(d->m_account.fraction());
0174                 }
0175                 break;
0176 
0177             case (int)eWidgets::eTransaction::Column::Deposit:
0178                 align |= Qt::AlignRight;
0179                 if (!d->m_split.value().isNegative()) {
0180                     txt = d->m_split.value(d->m_transaction.commodity(), d->m_splitCurrencyId).formatMoney(d->m_account.fraction());
0181                 }
0182                 break;
0183 
0184             }
0185             break;
0186         }
0187     }
0188 }
0189 
0190 int StdTransactionMatched::numRowsRegister(bool expanded) const
0191 {
0192     return StdTransaction::numRowsRegister(expanded) + m_additionalRows;
0193 }
0194 
0195 int StdTransactionMatched::numRowsRegister() const
0196 {
0197     return StdTransaction::numRowsRegister();
0198 }