summaryrefslogtreecommitdiffstats
path: root/kmymoney2/views/kinvestmentlistitem.cpp
blob: a26719a001c21a03e9d6077c3133651866cb9c80 (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/***************************************************************************
                          kinvestmentlistitem.cpp  -  description
                             -------------------
    begin                : Wed Feb 6 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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

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

#include <tqpainter.h>

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

#include <tdelistview.h>

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

#include <kmymoney/kmymoneyglobalsettings.h>

#include "kinvestmentlistitem.h"

#include <kmymoney/mymoneysecurity.h>
#include <kmymoney/mymoneyfile.h>

KInvestmentListItem::KInvestmentListItem(TDEListView* parent, const MyMoneyAccount& account)
  : TDEListViewItem(parent)
{
  bColumn5Negative = false;
  bColumn6Negative = false;
  bColumn7Negative = false;
  bColumn8Negative = false;
  bColumn9Negative = false;

  m_account = account;
  m_listView = parent;

  MyMoneySecurity security;
  MyMoneyFile* file = MyMoneyFile::instance();

  security = file->security(m_account.currencyId());
  m_tradingCurrency = file->security(security.tradingCurrency());

  int prec = MyMoneyMoney::denomToPrec(m_tradingCurrency.smallestAccountFraction());

  TQValueList<MyMoneyTransaction> transactionList;
  // FIXME PRICE
  // equity_price_history history = equity.priceHistory();

  //column 0 (COLUMN_NAME_INDEX) is the name of the stock
  setText(COLUMN_NAME_INDEX, m_account.name());

  //column 1 (COLUMN_SYMBOL_INDEX) is the ticker symbol
  setText(COLUMN_SYMBOL_INDEX, security.tradingSymbol());

  //column 2 is the net value (price * quantity owned)
  MyMoneyPrice price = file->price(m_account.currencyId(), m_tradingCurrency.id());
  if(price.isValid()) {
    setText(COLUMN_VALUE_INDEX, (file->balance(m_account.id()) * price.rate(m_tradingCurrency.id())).formatMoney(m_tradingCurrency.tradingSymbol(), prec));
  } else {
    setText(COLUMN_VALUE_INDEX, "---");
  }

  //column 3 (COLUMN_QUANTITY_INDEX) is the quantity of shares owned
  prec = MyMoneyMoney::denomToPrec(security.smallestAccountFraction());
  setText(COLUMN_QUANTITY_INDEX, file->balance(m_account.id()).formatMoney("", prec));

  //column 4 is the current price
  // Get the price precision from the configuration
  prec = KMyMoneyGlobalSettings::pricePrecision();

  // prec = MyMoneyMoney::denomToPrec(m_tradingCurrency.smallestAccountFraction());
  if(price.isValid()) {
    setText(COLUMN_PRICE_INDEX, price.rate(m_tradingCurrency.id()).formatMoney(m_tradingCurrency.tradingSymbol(), prec));
  } else {
    setText(COLUMN_PRICE_INDEX, "---");
  }
}

KInvestmentListItem::~KInvestmentListItem()
{
}

// FIXME PRICE
#if 0
const TQString KInvestmentListItem::calculate1WeekGain(const equity_price_history& history)
{
  return calculateGain(history, -7, 0, false, bColumn6Negative);
}

const TQString KInvestmentListItem::calculate4WeekGain(const equity_price_history& history)
{
  return calculateGain(history, -28, 0, false, bColumn7Negative);
}

const TQString KInvestmentListItem::calculate3MonthGain(const equity_price_history& history)
{
  return calculateGain(history, 0, -3, false, bColumn8Negative);
}

const TQString KInvestmentListItem::calculateYTDGain(const equity_price_history& history)
{
  return calculateGain(history, 0, 0, true, bColumn9Negative);
}

const TQString KInvestmentListItem::calculateGain(const equity_price_history& history, int dayDifference, int monthDifference, bool YTD, bool& bNegative)
{
  bNegative = false;
  if(history.isEmpty())
  {
    return TQString("0.0%");
  }
  else
  {
    bool bFoundCurrent = false, bFoundComparison = false;
    TQDate tempDate, comparisonDate = TQDate::currentDate();

    if(YTD)
    {
      //if it is YTD, set the date to 01/01/<current year>
      comparisonDate.setYMD(comparisonDate.year(), 1, 1);
    }
    else
    {
      comparisonDate = comparisonDate.addDays(dayDifference);
      comparisonDate = comparisonDate.addMonths(monthDifference);
    }

    MyMoneyMoney comparisonValue, currentValue;

    //find the current value, or closest to the current value.
    equity_price_history::ConstIterator itToday = history.end();
    for(tempDate = TQDate::currentDate(); tempDate >= comparisonDate; )
    {
      itToday = history.find(tempDate);
      if(itToday != history.end())
      {
        currentValue = itToday.data();
        bFoundCurrent = true;
        break;
      }

      tempDate = tempDate.addDays(-1);
    }

    if(!bFoundCurrent)
    {
      return TQString("0.0%");
    }

    //find a date that is closest to a week old, not older, and not today's date.  Because its a TQMap, this map
    //should already be sorted earliest to latest.
    for(equity_price_history::ConstIterator it = history.begin(); it != history.end(); ++it)
    {
      if(it.key() >= comparisonDate && it.key() < TQDate::currentDate())
      {
        comparisonDate = it.key();
        comparisonValue = it.data();
        bFoundComparison = true;
        break;
      }
    }

    if(!bFoundComparison)
    {
      return TQString("0.0%");
    }

    //tqDebug("Current date/value to use is %s/%s, Previous is %s/%s", tempDate.toString().data(), currentValue.toString().data(), comparisonDate.toString().data(), comparisonValue.toString().data());

    //compute the percentage difference
    if(comparisonValue != currentValue)
    {
      double result = (currentValue.toDouble() / comparisonValue.toDouble()) * 100.0;
      result -= 100.0;
      if(result < 0.0)
      {
        bNegative = true;
      }

      TQString ds = TQString("%1%").arg(result, 0, 'f', 3);
      return ds;

      /*MyMoneyMoney result = (currentValue / comparisonValue);
      result = result * 100;
      result = result - 100;
      tqDebug("final result = %s", result.toString().data());
      return TQString(result.formatMoney("", 3) + "%");*/
    }
  }
  return TQString("");
}
#endif

int KInvestmentListItem::compare(TQListViewItem* i, int col, bool ascending) const
{
  KInvestmentListItem* item = dynamic_cast<KInvestmentListItem*>(i);
  // do special sorting only for numeric columns
  // in all other cases use the standard sorting
  if(item) {
    switch(col) {
      case COLUMN_VALUE_INDEX:
      case COLUMN_QUANTITY_INDEX:
      case COLUMN_PRICE_INDEX:
      {
        bool inv1 = text(col) == "---";
        bool inv2 = item->text(col) == "---";
        if(!inv1 && !inv2) {
          MyMoneyMoney result = MyMoneyMoney(text(col)) - MyMoneyMoney(item->text(col));
          if(result.isNegative())
            return -1;
          if(result.isZero())
            return 0;
          return 1;
        } else if(inv1 && inv2) {
          return 0;
        } else if(inv1) {
          return -1;
        }
        return 1;
      }
      break;

      default:
        break;
    }
  }

  // do standard sorting here
  return TDEListViewItem::compare(i, col, ascending);
}

void KInvestmentListItem::paintCell(TQPainter * p, const TQColorGroup & cg, int column, int width, int align)
{
  bool bPaintRed = false;
  if((column == COLUMN_RAWGAIN_INDEX && bColumn5Negative) ||
     (column == COLUMN_1WEEKGAIN_INDEX && bColumn6Negative) ||
     (column == COLUMN_4WEEKGAIN_INDEX && bColumn7Negative) ||
     (column == COLUMN_3MONGAIN_INDEX && bColumn8Negative) ||
     (column == COLUMN_YTDGAIN_INDEX && bColumn9Negative))
  {
    bPaintRed = true;
  }

  p->save();

  TQColorGroup cg2(cg);

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

#ifndef KMM_DESIGNER
  TQFont font = KMyMoneyGlobalSettings::listCellFont();
  // strike out closed accounts
  if(m_account.isClosed())
    font.setStrikeOut(true);

  p->setFont(font);
#endif

  if(bPaintRed)
  {
    TQColorGroup _cg( cg2);
    TQColor c = _cg.text();
    _cg.setColor(TQColorGroup::Text, TQt::red);
    TQListViewItem::paintCell(p, _cg, column, width, align);
    _cg.setColor(TQColorGroup::Text, c);
  }
  else
  {
    TQListViewItem::paintCell(p, cg2, column, width, align);
  }

  p->restore();
}