summaryrefslogtreecommitdiffstats
path: root/kmymoney2/reports/reportaccount.cpp
blob: 5f091e2f5b59d83da05d39d6ad1cd0c4fe539487 (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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/***************************************************************************
                          reportaccount.cpp
                             -------------------
    begin                : Mon May 17 2004
    copyright            : (C) 2004-2005 by Ace Jones
    email                : <ace.j@hotpop.com>
                           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

// ----------------------------------------------------------------------------
// KDE Includes
// This is just needed for i18n().  Once I figure out how to handle i18n
// without using this macro directly, I'll be freed of KDE dependency.  This
// is a minor problem because we use these terms when rendering to HTML,
// and a more major problem because we need it to translate account types
// (e.g. MyMoneyAccount::Checkings) into their text representation.  We also
// use that text representation in the core data structure of the report. (Ace)

#include <klocale.h>

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

#include "../mymoney/mymoneyfile.h"
#include "../mymoney/mymoneysecurity.h"
#include "reportdebug.h"
#include "reportaccount.h"

namespace reports {

ReportAccount::ReportAccount( void )
{
}

ReportAccount::ReportAccount( const ReportAccount& copy ):
  MyMoneyAccount( copy ), m_nameHierarchy( copy.m_nameHierarchy )
{
  // NOTE: I implemented the copy constructor solely for debugging reasons

  DEBUG_ENTER(__PRETTY_FUNCTION__);
}

ReportAccount::ReportAccount( const TQString& accountid ):
  MyMoneyAccount( MyMoneyFile::instance()->account(accountid) )
{
  DEBUG_ENTER(__PRETTY_FUNCTION__);
  DEBUG_OUTPUT(TQString("Account %1").tqarg(accountid));
  calculateAccountHierarchy();
}

ReportAccount::ReportAccount( const MyMoneyAccount& account ):
  MyMoneyAccount( account )
{
  DEBUG_ENTER(__PRETTY_FUNCTION__);
  DEBUG_OUTPUT(TQString("Account %1").tqarg(account.id()));
  calculateAccountHierarchy();
}

void ReportAccount::calculateAccountHierarchy( void )
{
  DEBUG_ENTER(__PRETTY_FUNCTION__);

  MyMoneyFile* file = MyMoneyFile::instance();
  TQString resultid = id();
  TQString parentid = parentAccountId();

#ifdef DEBUG_HIDE_SENSITIVE
  m_nameHierarchy.prepend(file->account(resultid).id());
#else
  m_nameHierarchy.prepend(file->account(resultid).name());
#endif
  while (!file->isStandardAccount(parentid))
  {
    // take on the identity of our tqparent
    resultid = parentid;

    // and try again
    parentid = file->account(resultid).parentAccountId();
#ifdef DEBUG_HIDE_SENSITIVE
    m_nameHierarchy.prepend(file->account(resultid).id());
#else
    m_nameHierarchy.prepend(file->account(resultid).name());
#endif
  }
}

MyMoneyMoney ReportAccount::deepCurrencyPrice( const TQDate& date ) const
{
  DEBUG_ENTER(__PRETTY_FUNCTION__);

  MyMoneyMoney result(1, 1);
  MyMoneyFile* file = MyMoneyFile::instance();

  MyMoneySecurity undersecurity = file->security( currencyId() );
  if ( ! undersecurity.isCurrency() )
  {
    MyMoneyPrice price = file->price(undersecurity.id(),undersecurity.tradingCurrency(),date);
    if ( price.isValid() )
    {
      result = price.rate(undersecurity.tradingCurrency());

      DEBUG_OUTPUT(TQString("Converting under %1 to deep %2, price on %3 is %4")
        .tqarg(undersecurity.name())
        .tqarg(file->security(undersecurity.tradingCurrency()).name())
        .tqarg(date.toString())
        .tqarg(result.toDouble()));
    }
    else
    {
      DEBUG_OUTPUT(TQString("No price to convert under %1 to deep %2 on %3")
        .tqarg(undersecurity.name())
        .tqarg(file->security(undersecurity.tradingCurrency()).name())
        .tqarg(date.toString()));
    }
  }

  return result;
}

MyMoneyMoney ReportAccount::baseCurrencyPrice( const TQDate& date ) const
{
  // Note that whether or not the user chooses to convert to base currency, all the values
  // for a given account/category are converted to the currency for THAT account/category
  // The "Convert to base currency" tells the report to convert from the account/category
  // currency to the file's base currency.
  //
  // An example where this matters is if Category 'C' and account 'U' are in USD, but
  // Account 'J' is in JPY.  Say there are two transactions, one is US$100 from U to C,
  // the other is JPY10,000 from J to C.  Given a JPY price of USD$0.01, this means
  // C will show a balance of $200 NO MATTER WHAT the user chooses for 'convert to base
  // currency.  This confused me for a while, which is why I wrote this comment.
  //    --acejones

  DEBUG_ENTER(__PRETTY_FUNCTION__);

  MyMoneyMoney result(1, 1);
  MyMoneyFile* file = MyMoneyFile::instance();

  if(isForeignCurrency())
  {
    result = foreignCurrencyPrice(file->baseCurrency().id(), date);
  }

  return result;
}

MyMoneyMoney ReportAccount::foreignCurrencyPrice( const TQString foreignCurrency, const TQDate& date ) const
{
  DEBUG_ENTER(__PRETTY_FUNCTION__);

  MyMoneyPrice price;
  MyMoneyMoney result(1, 1);
  MyMoneyFile* file = MyMoneyFile::instance();
  MyMoneySecurity security = file->security(foreignCurrency);

  //check whether it is a currency or a commodity. In the latter case case, get the trading currency
  TQString tradingCurrency;
  if(security.isCurrency()) {
    tradingCurrency = foreignCurrency;
  } else {
    tradingCurrency = security.tradingCurrency();
  }

  //It makes no sense to get the price if both currencies are the same
  if(currency().id() != tradingCurrency) {
    price = file->price(currency().id(), tradingCurrency, date);

    if(price.isValid())
    {
      result = price.rate(tradingCurrency);
      DEBUG_OUTPUT(TQString("Converting deep %1 to currency %2, price on %3 is %4")
          .tqarg(file->currency(currency().id()).name())
          .tqarg(file->currency(foreignCurrency).name())
          .tqarg(date.toString())
          .tqarg(result.toDouble()));
    }
    else
    {
      DEBUG_OUTPUT(TQString("No price to convert deep %1 to currency %2 on %3")
          .tqarg(file->currency(currency().id()).name())
          .tqarg(file->currency(foreignCurrency).name())
          .tqarg(date.toString()));
    }
  }
  return result;
}

/**
  * Fetch the trading currency of this account's currency
  *
  * @return The account's currency trading currency
  */
MyMoneySecurity ReportAccount::currency( void ) const
{
  MyMoneyFile* file = MyMoneyFile::instance();

  // First, get the deep currency
  MyMoneySecurity deepcurrency = file->security( currencyId() );
  if ( ! deepcurrency.isCurrency() )
    deepcurrency = file->security( deepcurrency.tradingCurrency() );

  // Return the deep currency's ID
  return deepcurrency;
}

/**
  * Determine if this account's deep currency is different from the file's
  * base currency
  *
  * @return bool True if this account is in a foreign currency
  */
bool ReportAccount::isForeignCurrency( void ) const
{
  return ( currency().id() != MyMoneyFile::instance()->baseCurrency().id() );
}

bool ReportAccount::operator<(const ReportAccount& second) const
{
//   DEBUG_ENTER(__PRETTY_FUNCTION__);

  bool result = false;
  bool haveresult = false;
  TQStringList::const_iterator it_first = m_nameHierarchy.begin();
  TQStringList::const_iterator it_second = second.m_nameHierarchy.begin();
  while ( it_first != m_nameHierarchy.end() )
  {
    // The first string is longer than the second, but otherwise identical
    if ( it_second == second.m_nameHierarchy.end() )
    {
      result = false;
      haveresult = true;
      break;
    }

    if ( (*it_first) < (*it_second) )
    {
      result = true;
      haveresult = true;
      break;
    }
    else if ( (*it_first) > (*it_second) )
    {
      result = false;
      haveresult = true;
      break;
    }

    ++it_first;
    ++it_second;
  }

  // The second string is longer than the first, but otherwise identical
  if ( !haveresult && ( it_second != second.m_nameHierarchy.end() ) )
    result = true;

//   DEBUG_OUTPUT(TQString("%1 < %2 is %3").tqarg(debugName(),second.debugName()).tqarg(result));
  return result;
}

/**
  * The name of only this account.  No matter how deep the hierarchy, this
  * method only returns the last name in the list, which is the engine name]
  * of this account.
  *
  * @return TQString The account's name
  */
TQString ReportAccount::name( void ) const
{
  return m_nameHierarchy.back();
}

// MyMoneyAccount:fullHierarchyDebug()
TQString ReportAccount::debugName( void ) const
{
  return m_nameHierarchy.join("|");
}

// MyMoneyAccount:fullHierarchy()
TQString ReportAccount::fullName( void ) const
{
  return m_nameHierarchy.join(": ");
}

// MyMoneyAccount:isTopCategory()
bool ReportAccount::isTopLevel( void ) const
{
  return ( m_nameHierarchy.size() == 1 );
}

// MyMoneyAccount:hierarchyDepth()
unsigned ReportAccount::hierarchyDepth( void ) const
{
  return ( m_nameHierarchy.size() );
}

ReportAccount ReportAccount::tqparent( void ) const
{
  return ReportAccount( parentAccountId() );
}

ReportAccount ReportAccount::topParent( void ) const
{
  DEBUG_ENTER(__PRETTY_FUNCTION__);

  MyMoneyFile* file = MyMoneyFile::instance();
  TQString resultid = id();
  TQString parentid = parentAccountId();

  while (!file->isStandardAccount(parentid))
  {
    // take on the identity of our tqparent
    resultid = parentid;

    // and try again
    parentid = file->account(resultid).parentAccountId();
  }

  return ReportAccount( resultid );
}

TQString ReportAccount::topParentName( void ) const
{
  return m_nameHierarchy.first();
}

bool ReportAccount::isLiquidAsset( void ) const
{
  return accountType() == MyMoneyAccount::Cash ||
      accountType() == MyMoneyAccount::Checkings ||
      accountType() == MyMoneyAccount::Savings;
}


bool ReportAccount::isLiquidLiability( void ) const
{
  return accountType() == MyMoneyAccount::CreditCard;

}




}  // end namespace reports