summaryrefslogtreecommitdiffstats
path: root/kmymoney2/widgets/stdtransactionmatched.cpp
blob: 9bc191bb4fd96d8f917da7c535328bda37942da8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/***************************************************************************
                          stdtransactionmatched.cpp
                             -------------------
    begin                : Sat May 11 2008
    copyright            : (C) 2008 by Thomas Baumgart
    email                : Thomas Baumgart <ipwizard@users.sourceforge.net>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

// ----------------------------------------------------------------------------
// QT Includes

#include <qregion.h>

// ----------------------------------------------------------------------------
// KDE Includes

#include <klocale.h>
#include <kdebug.h>

// ----------------------------------------------------------------------------
// Project Includes

#include <stdtransactionmatched.h>
#include <kmymoney/kmymoneyglobalsettings.h>
#include <kmymoney/register.h>

using namespace KMyMoneyRegister;
using namespace KMyMoneyTransactionForm;

StdTransactionMatched::StdTransactionMatched(Register *parent, const MyMoneyTransaction& transaction, const MyMoneySplit& split, int uniqueId) :
  StdTransaction(parent, transaction, split, uniqueId),
  m_drawCounter(parent->drawCounter()-1)
{
  // setup initial size
  setNumRowsRegister(numRowsRegister(KMyMoneyGlobalSettings::showRegisterDetailed()));
}

bool StdTransactionMatched::paintRegisterCellSetup(QPainter* painter, int& row, int& col, QRect& cellRect, QRect& textRect, QColorGroup& cg, QBrush& brush)
{
  QRect r(cellRect);

  bool rc = Transaction::paintRegisterCellSetup(painter, row, col, cellRect, textRect, cg, brush);

  // if not selected paint in matched background color
  if(!isSelected()) {
    cg.setColor(QColorGroup::Base, KMyMoneyGlobalSettings::matchedTransactionColor());
    brush = QBrush(cg.base());
  }

  // the first line needs to be painted across all columns
  if(row + m_additionalRows - m_rowsRegister == 0) {
    // avoid painting the text over multiple columns twice for the same update round
    unsigned int drawCounter = m_parent->drawCounter();
    if(m_drawCounter == drawCounter) {
      return false;
    }


    // the fixed text always uses all cols
    col = m_parent->columnAt(1);
    painter->translate(-r.x() + m_parent->columnPos(col), 0);
#if 0
    r.setX(m_parent->columnPos(col));
    r.setWidth(m_parent->visibleWidth());
    painter->translate(r.x(), r.y());
#endif
    cellRect.setX(0);
    cellRect.setY(0);
    cellRect.setWidth(m_parent->visibleWidth());
    cellRect.setHeight(m_parent->rowHeight(row + m_startRow));

    textRect = cellRect;
    textRect.setX(2);
    textRect.setWidth(textRect.width()-4);
  }
  return rc;
}

void StdTransactionMatched::registerCellText(QString& txt, int& align, int row, int col, QPainter* painter)
{
  // run through the standard
  StdTransaction::registerCellText(txt, align, row, col, painter);

  // we only cover the additional rows
  if(row >= m_rowsRegister - m_additionalRows) {
    // make row relative to the last three rows
    row += m_additionalRows - m_rowsRegister;

    // remove anything that had been added by the standard method
    txt = "";

    // and we draw this information in italics
    if(painter) {
      QFont font = painter->font();
      font.setItalic(true);
      painter->setFont(font);
    }

    MyMoneyTransaction matchedTransaction = m_split.matchedTransaction();
    MyMoneySplit matchedSplit;
    try {
      matchedSplit = matchedTransaction.splitById(m_split.value("kmm-match-split"));
    } catch(MyMoneyException *e) {
      delete e;
    }

    QValueList<MyMoneySplit>::const_iterator it_s;
    const QValueList<MyMoneySplit>& list = matchedTransaction.splits();
    MyMoneyMoney importedValue;
    for(it_s = list.begin(); it_s != list.end(); ++it_s) {
      if((*it_s).accountId() == m_account.id()) {
        importedValue += (*it_s).shares();
      }
    }

    QDate postDate;
    QString memo;
    switch(row) {
      case 0:
        if(painter)
          txt = QString(" ")+i18n("KMyMoney has matched a downloaded transaction with a manually entered one (result above)");
        // return true for the first visible column only
        break;

      case 1:
        switch(col) {
          case DateColumn:
            align |= Qt::AlignLeft;
            txt = i18n("Bank entry:");
            break;

          case DetailColumn:
            align |= Qt::AlignLeft;
            txt = QString("%1 %2").arg(matchedTransaction.postDate().toString(Qt::ISODate)).arg(matchedTransaction.memo());
            break;

          case PaymentColumn:
            align |= Qt::AlignRight;
            if(importedValue.isNegative()) {
              txt = (-importedValue).formatMoney(m_account.fraction());
            }
            break;

          case DepositColumn:
            align |= Qt::AlignRight;
            if(!importedValue.isNegative()) {
              txt = importedValue.formatMoney(m_account.fraction());
            }
            break;
        }
        break;

      case 2:
        switch(col) {
          case DateColumn:
            align |= Qt::AlignLeft;
            txt = i18n("Your entry:");
            break;

          case DetailColumn:
            align |= Qt::AlignLeft;
            postDate = m_transaction.postDate();
            if(!m_split.value("kmm-orig-postdate").isEmpty()) {
              postDate = QDate::fromString(m_split.value("kmm-orig-postdate"), Qt::ISODate);
            }
            memo = m_split.memo();
            if(!matchedSplit.memo().isEmpty() && memo != matchedSplit.memo()) {
              int pos = memo.findRev(matchedSplit.memo());
              if(pos != -1) {
                memo = memo.left(pos);
                if(memo.endsWith("\n"))
                  memo = memo.left(pos-1);
              }
            }
            txt = QString("%1 %2").arg(postDate.toString(Qt::ISODate)).arg(memo);
            break;

          case PaymentColumn:
            align |= Qt::AlignRight;
            if(m_split.value().isNegative()) {
              txt = (-m_split.value(m_transaction.commodity(), m_splitCurrencyId)).formatMoney(m_account.fraction());
            }
            break;

          case DepositColumn:
            align |= Qt::AlignRight;
            if(!m_split.value().isNegative()) {
              txt = m_split.value(m_transaction.commodity(), m_splitCurrencyId).formatMoney(m_account.fraction());
            }
            break;

        }
        break;
    }
  }
}

void StdTransactionMatched::paintRegisterGrid(QPainter* painter, int row, int col, const QRect& r, const QColorGroup& _cg) const
{
  // the last 3 rows should not show a grid
  if(row < m_rowsRegister - m_additionalRows) {
    Transaction::paintRegisterGrid(painter, row, col, r, _cg);

  } else if(row == m_rowsRegister-1) {
    painter->setPen(KMyMoneyGlobalSettings::listGridColor());
    painter->drawLine(r.x(), r.height()-1, r.width(), r.height()-1);
  }
}