summaryrefslogtreecommitdiffstats
path: root/kmymoney2/views/kscheduledlistitem.cpp
blob: d509521576c702596ad9a01905e83d52e3d4ef01 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/***************************************************************************
                          kscheduledlistitem.cpp  -  description
                             -------------------
    begin                : Sun Jan 27 2002
    copyright            : (C) 2000-2002 by Michael Edwardes
    email                : mte@users.sourceforge.net
                           Javier Campos Morales <javi_c@users.sourceforge.net>
                           Felix Rodriguez <frodriguez@users.sourceforge.net>
                           John C <thetacoturtle@users.sourceforge.net>
                           Thomas Baumgart <ipwizard@users.sourceforge.net>
                           Kevin Tambascio <ktambascio@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 <tqpainter.h>
#include <tqstyle.h>

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

#include <tdeconfig.h>
#include <klocale.h>
#include <kglobal.h>
#include <kglobalsettings.h>

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

#include "kscheduledlistitem.h"
#include "../mymoney/mymoneyfile.h"
#include "../kmymoneyglobalsettings.h"
#include "../kmymoneyutils.h"

KScheduledListItem::KScheduledListItem(TDEListView *parent, const TQString& name, const TQPixmap& pixmap, const TQString& sortKey) :
  TDEListViewItem(parent, name),
  m_sortKey(sortKey)
{
  setPixmap(0, pixmap);
  if(m_sortKey.isEmpty())
    m_sortKey = name;
}

KScheduledListItem::KScheduledListItem(KScheduledListItem *parent, const MyMoneySchedule& schedule/*, bool even*/)
 : TDEListViewItem(parent)
{
  m_schedule = schedule;
  m_sortKey = schedule.name();
  setPixmap(0, KMyMoneyUtils::scheduleIcon(TDEIcon::Small));

  try
  {
    MyMoneyTransaction transaction = schedule.transaction();
    MyMoneySplit s1 = transaction.splits()[0];
    MyMoneySplit s2 = transaction.splits()[1];
    TQValueList<MyMoneySplit>::ConstIterator it_s;
    MyMoneySplit split;
    MyMoneyAccount acc;

    switch(schedule.type()) {
      case MyMoneySchedule::TYPE_DEPOSIT:
        if (s1.value().isNegative())
          split = s2;
        else
          split = s1;
        break;

      case MyMoneySchedule::TYPE_LOANPAYMENT:
        for(it_s = transaction.splits().begin(); it_s != transaction.splits().end(); ++it_s) {
          acc = MyMoneyFile::instance()->account((*it_s).accountId());
          if(acc.accountGroup() == MyMoneyAccount::Asset
          || acc.accountGroup() == MyMoneyAccount::Liability) {
            if(acc.accountType() != MyMoneyAccount::Loan
            && acc.accountType() != MyMoneyAccount::AssetLoan) {
              split = *it_s;
              break;
            }
          }
        }
        if(it_s == transaction.splits().end()) {
          tqFatal("Split for payment account not found in %s:%d.", __FILE__, __LINE__);
        }
        break;

      default:
        if (!s1.value().isPositive())
          split = s1;
        else
          split = s2;
        break;
    }
    acc = MyMoneyFile::instance()->account(split.accountId());

/*
    if (schedule.type() == MyMoneySchedule::TYPE_DEPOSIT)
    {
      if (s1.value() >= 0)
        split = s1;
      else
        split = s2;
    }
    else if(schedule.type() == MyMoneySchedule::TYPE_LOANPAYMENT)
    {

    }
    else
    {
      if (s1.value() < 0)
        split = s1;
      else
        split = s2;
    }
*/
    setText(0, schedule.name());
    MyMoneySecurity currency = MyMoneyFile::instance()->currency(acc.currencyId());

    setText(1, acc.name());
    if(!s1.payeeId().isEmpty())
      setText(2, MyMoneyFile::instance()->payee(s1.payeeId()).name());
    else
      setText(2, "---");
    m_amount = split.shares().abs();
    setText(3, TQString("%1  ").arg(m_amount.formatMoney(acc, currency)));
    // Do the real next payment like ms-money etc
    if (schedule.isFinished())
    {
      setText(4, i18n("Finished"));
    }
    else
      setText(4, TDEGlobal::locale()->formatDate(schedule.adjustedNextDueDate(), true));

    setText(5, i18n(schedule.occurenceToString()));
    setText(6, KMyMoneyUtils::paymentMethodToString(schedule.paymentType()));
  }
  catch (MyMoneyException *e)
  {
    setText(0, "Error:");
    setText(1, e->what());
    delete e;
  }
}

KScheduledListItem::~KScheduledListItem()
{
}

void KScheduledListItem::paintCell(TQPainter* p, const TQColorGroup& cg, int column, int width, int align)
{
  TQColorGroup cg2(cg);

  TQColor textColour = TDEGlobalSettings::textColor();
  TQFont cellFont = KMyMoneyGlobalSettings::listCellFont();

  // avoid colorizing lines that do not contain a schedule
  if(!m_schedule.id().isEmpty()) {
    if (m_schedule.isFinished())
      textColour = TQt::darkGreen;
    else if (m_schedule.isOverdue())
      textColour = TQt::red;
  }

  cg2.setColor(TQColorGroup::Text, textColour);

  // display group items in bold
  if (!parent())
    cellFont.setBold(true);

  p->setFont(cellFont);

  if (isAlternate())
    cg2.setColor(TQColorGroup::Base, KMyMoneyGlobalSettings::listColor());
  else
    cg2.setColor(TQColorGroup::Base, KMyMoneyGlobalSettings::listBGColor());

  TQListViewItem::paintCell(p, cg2, column, width, align);
}

int KScheduledListItem::compare(TQListViewItem* i, int col, bool ascending) const
{
  KScheduledListItem* item = dynamic_cast<KScheduledListItem*>(i);
  int rc;
  // do special sorting only if
  // a) date
  // b) amount
  // c) name/group
  // d) occurence
  // in all other cases use the standard sorting
  MyMoneyMoney diff;
  switch(col) {
    case 0:   // type and name
      rc = m_sortKey.compare(item->m_sortKey);
      break;

    case 3:   // amount
      diff = m_amount - item->m_amount;
      if(diff.isZero())
        rc = 0;
      else if(diff.isPositive())
        rc = 1;
      else
        rc = -1;
      break;

    case 4:   // date
      rc = item->m_schedule.adjustedNextDueDate().daysTo(m_schedule.adjustedNextDueDate());
      break;

    case 5:   // occurence
      rc = (m_schedule.occurence() - item->m_schedule.occurence());
      break;

    default:
      rc = TDEListViewItem::compare(i, col, ascending);
      break;
  }
  // adjust to [-1..1]
  if(rc != 0) {
    rc = (rc > 0) ? 1 : -1;
  }
  return rc;
}