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
356
357
358
359
360
361
362
363
364
365
366
367
|
/***************************************************************************
objectinfotable.cpp
-------------------
begin : Sat 28 jun 2008
copyright : (C) 2004-2005 by Ace Jones
2008 by Alvaro Soliverez
email : acejones@users.sourceforge.net
asoliverez@gmail.com
***************************************************************************/
/***************************************************************************
* *
* 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 <tqvaluelist.h>
#include <tqfile.h>
#include <tqtextstream.h>
// ----------------------------------------------------------------------------
// 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.
#include <tdelocale.h>
#include <kdebug.h>
// ----------------------------------------------------------------------------
// Project Includes
#include "../mymoney/mymoneyfile.h"
#include "../mymoney/mymoneyreport.h"
#include "../mymoney/mymoneyexception.h"
#include "../kmymoneyutils.h"
#include "reportaccount.h"
#include "reportdebug.h"
#include "objectinfotable.h"
namespace reports {
// ****************************************************************************
//
// ObjectInfoTable implementation
//
// ****************************************************************************
/**
* TODO
*
* - Collapse 2- & 3- groups when they are identical
* - Way more test cases (especially splits & transfers)
* - Option to collapse splits
* - Option to exclude transfers
*
*/
ObjectInfoTable::ObjectInfoTable(const MyMoneyReport& _report): ListTable(_report)
{
// seperated into its own method to allow debugging (setting breakpoints
// directly in ctors somehow does not work for me (ipwizard))
// TODO: remove the init() method and move the code back to the ctor
init();
}
void ObjectInfoTable::init ( void )
{
switch ( m_config.rowType() )
{
case MyMoneyReport::eSchedule:
constructScheduleTable();
m_columns = "nextduedate,name";
break;
case MyMoneyReport::eAccountInfo:
constructAccountTable();
m_columns = "institution,type,name";
break;
case MyMoneyReport::eAccountLoanInfo:
constructAccountLoanTable();
m_columns = "institution,type,name";
break;
default:
break;
}
// Sort the data to match the report definition
m_subtotal="value";
switch ( m_config.rowType() )
{
case MyMoneyReport::eSchedule:
m_group = "type";
m_subtotal="value";
break;
case MyMoneyReport::eAccountInfo:
case MyMoneyReport::eAccountLoanInfo:
m_group = "topcategory,institution";
m_subtotal="currentbalance";
break;
default:
throw new MYMONEYEXCEPTION ( "ObjectInfoTable::ObjectInfoTable(): unhandled row type" );
}
TQString sort = m_group + "," + m_columns + ",id,rank";
switch ( m_config.rowType() ) {
case MyMoneyReport::eSchedule:
if ( m_config.detailLevel() == MyMoneyReport::eDetailAll ) {
m_columns="name,payee,paymenttype,occurence,nextduedate,category";
} else {
m_columns="name,payee,paymenttype,occurence,nextduedate";
}
break;
case MyMoneyReport::eAccountInfo:
m_columns="type,name,number,description,openingdate,currencyname,balancewarning,maxbalancelimit,creditwarning,maxcreditlimit,tax,favorite";
break;
case MyMoneyReport::eAccountLoanInfo:
m_columns="type,name,number,description,openingdate,currencyname,payee,loanamount,interestrate,nextinterestchange,periodicpayment,finalpayment,favorite";
break;
default:
m_columns = "";
}
TableRow::setSortCriteria ( sort );
qHeapSort ( m_rows );
}
void ObjectInfoTable::constructScheduleTable ( void )
{
MyMoneyFile* file = MyMoneyFile::instance();
TQValueList<MyMoneySchedule> schedules;
schedules = file->scheduleList ( "", MyMoneySchedule::TYPE_ANY, MyMoneySchedule::OCCUR_ANY, MyMoneySchedule::STYPE_ANY, m_config.fromDate(), m_config.toDate() );
TQValueList<MyMoneySchedule>::const_iterator it_schedule = schedules.begin();
while ( it_schedule != schedules.end() )
{
MyMoneySchedule schedule = *it_schedule;
ReportAccount account = schedule.account();
if ( m_config.includes ( account ) ) {
//get fraction for account
int fraction = account.fraction();
//use base currency fraction if not initialized
if ( fraction == -1 )
fraction = MyMoneyFile::instance()->baseCurrency().smallestAccountFraction();
TableRow scheduleRow;
//convert to base currency if needed
MyMoneyMoney xr = MyMoneyMoney(1,1);
if (m_config.isConvertCurrency() && account.isForeignCurrency()) {
xr = account.baseCurrencyPrice(TQDate::currentDate()).reduce();
}
// help for sort and render functions
scheduleRow["rank"] = "0";
//schedule data
scheduleRow["id"] = schedule.id();
scheduleRow["name"] = schedule.name();
scheduleRow["nextduedate"] = schedule.nextDueDate().toString ( Qt::ISODate );
scheduleRow["type"] = KMyMoneyUtils::scheduleTypeToString ( schedule.type() );
scheduleRow["occurence"] = i18n( schedule.occurenceToString() );
scheduleRow["paymenttype"] = KMyMoneyUtils::paymentMethodToString ( schedule.paymentType() );
//scheduleRow["category"] = account.name();
//to get the payee we must look into the splits of the transaction
MyMoneyTransaction transaction = schedule.transaction();
MyMoneySplit split = transaction.splitByAccount ( account.id(), true );
scheduleRow["value"] = (split.value() * xr).toString();
MyMoneyPayee payee = file->payee ( split.payeeId() );
scheduleRow["payee"] = payee.name();
m_rows += scheduleRow;
//the text matches the main split
bool transaction_text = m_config.match(&split);
if ( m_config.detailLevel() == MyMoneyReport::eDetailAll )
{
//get the information for all splits
TQValueList<MyMoneySplit> splits = transaction.splits();
TQValueList<MyMoneySplit>::const_iterator split_it = splits.begin();
for ( ;split_it != splits.end(); split_it++ )
{
TableRow splitRow;
ReportAccount splitAcc = ( *split_it ).accountId();
splitRow["rank"] = "1";
splitRow["id"] = schedule.id();
splitRow["name"] = schedule.name();
splitRow["type"] = KMyMoneyUtils::scheduleTypeToString ( schedule.type() );
splitRow["nextduedate"] = schedule.nextDueDate().toString ( Qt::ISODate );
if ( ( *split_it ).value() == MyMoneyMoney::autoCalc ) {
splitRow["split"] = MyMoneyMoney::autoCalc.toString();
} else if ( ! splitAcc.isIncomeExpense() ) {
splitRow["split"] = ( *split_it ).value().toString();
} else {
splitRow["split"] = ( - ( *split_it ).value() ).toString();
}
//if it is an assett account, mark it as a transfer
if ( ! splitAcc.isIncomeExpense() ) {
splitRow["category"] = ( ( * split_it ).value().isNegative() )
? i18n ( "Transfer from %1" ).arg ( splitAcc.fullName() )
: i18n ( "Transfer to %1" ).arg ( splitAcc.fullName() );
} else {
splitRow ["category"] = splitAcc.fullName();
}
//add the split only if it matches the text or it matches the main split
if(m_config.match( &(*split_it) )
|| transaction_text )
m_rows += splitRow;
}
}
}
++it_schedule;
}
}
void ObjectInfoTable::constructAccountTable ( void )
{
MyMoneyFile* file = MyMoneyFile::instance();
//make sure we have all subaccounts of investment accounts
includeInvestmentSubAccounts();
TQValueList<MyMoneyAccount> accounts;
file->accountList(accounts);
TQValueList<MyMoneyAccount>::const_iterator it_account = accounts.begin();
while ( it_account != accounts.end() )
{
TableRow accountRow;
ReportAccount account = *it_account;
if(m_config.includes(account)
&& account.accountType() != MyMoneyAccount::Stock
&& !account.isClosed())
{
MyMoneyMoney value;
accountRow["rank"] = "0";
accountRow["topcategory"] = KMyMoneyUtils::accountTypeToString(account.accountGroup());
accountRow["institution"] = (file->institution(account.institutionId())).name();
accountRow["type"] = KMyMoneyUtils::accountTypeToString(account.accountType());
accountRow["name"] = account.name();
accountRow["number"] = account.number();
accountRow["description"] = account.description();
accountRow["openingdate"] = account.openingDate().toString( Qt::ISODate );
//accountRow["currency"] = (file->currency(account.currencyId())).tradingSymbol();
accountRow["currencyname"] = (file->currency(account.currencyId())).name();
accountRow["balancewarning"] = account.value("minBalanceEarly");
accountRow["maxbalancelimit"] = account.value("minBalanceAbsolute");
accountRow["creditwarning"] = account.value("maxCreditEarly");
accountRow["maxcreditlimit"] = account.value("maxCreditAbsolute");
accountRow["tax"] = account.value("Tax");
accountRow["favorite"] = account.value("PreferredAccount");
//investment accounts show the balances of all its subaccounts
if(account.accountType() == MyMoneyAccount::Investment) {
value = investmentBalance(account);
} else {
value = file->balance(account.id());
}
//convert to base currency if needed
if (m_config.isConvertCurrency() && account.isForeignCurrency()) {
MyMoneyMoney xr = account.baseCurrencyPrice(TQDate::currentDate()).reduce();
value = value * xr;
}
accountRow["currentbalance"] = value.toString();
m_rows += accountRow;
}
++it_account;
}
}
void ObjectInfoTable::constructAccountLoanTable ( void )
{
MyMoneyFile* file = MyMoneyFile::instance();
TQValueList<MyMoneyAccount> accounts;
file->accountList(accounts);
TQValueList<MyMoneyAccount>::const_iterator it_account = accounts.begin();
while ( it_account != accounts.end() )
{
TableRow accountRow;
ReportAccount account = *it_account;
MyMoneyAccountLoan loan = *it_account;
if(m_config.includes(account) &&
( account.accountType() == MyMoneyAccount::Loan
|| account.accountType() == MyMoneyAccount::AssetLoan )
&& !account.isClosed())
{
//convert to base currency if needed
MyMoneyMoney xr = MyMoneyMoney(1,1);
if (m_config.isConvertCurrency() && account.isForeignCurrency()) {
xr = account.baseCurrencyPrice(TQDate::currentDate()).reduce();
}
accountRow["rank"] = "0";
accountRow["topcategory"] = KMyMoneyUtils::accountTypeToString(account.accountGroup());
accountRow["institution"] = (file->institution(account.institutionId())).name();
accountRow["type"] = KMyMoneyUtils::accountTypeToString(account.accountType());
accountRow["name"] = account.name();
accountRow["number"] = account.number();
accountRow["description"] = account.description();
accountRow["openingdate"] = account.openingDate().toString( Qt::ISODate );
//accountRow["currency"] = (file->currency(account.currencyId())).tradingSymbol();
accountRow["currencyname"] = (file->currency(account.currencyId())).name();
accountRow["payee"] = file->payee(loan.payee()).name();
accountRow["loanamount"] = (loan.loanAmount() * xr).toString();
accountRow["interestrate"] = (loan.interestRate(TQDate::currentDate())/MyMoneyMoney(100,1)*xr).toString();
accountRow["nextinterestchange"] = loan.nextInterestChange().toString( Qt::ISODate );
accountRow["periodicpayment"] = (loan.periodicPayment() * xr).toString();
accountRow["finalpayment"] = (loan.finalPayment() * xr).toString();
accountRow["favorite"] = account.value("PreferredAccount");
MyMoneyMoney value = file->balance(account.id());
value = value * xr;
accountRow["currentbalance"] = value.toString();
m_rows += accountRow;
}
++it_account;
}
}
MyMoneyMoney ObjectInfoTable::investmentBalance(const MyMoneyAccount& acc)
{
MyMoneyFile* file = MyMoneyFile::instance();
MyMoneyMoney value;
value = file->balance(acc.id());
TQValueList<TQString>::const_iterator it_a;
for(it_a = acc.accountList().begin(); it_a != acc.accountList().end(); ++it_a) {
MyMoneyAccount stock = file->account(*it_a);
try {
MyMoneyMoney val;
MyMoneyMoney balance = file->balance(stock.id());
MyMoneySecurity security = file->security(stock.currencyId());
MyMoneyPrice price = file->price(stock.currencyId(), security.tradingCurrency());
val = balance * price.rate(security.tradingCurrency());
// adjust value of security to the currency of the account
MyMoneySecurity accountCurrency = file->currency(acc.currencyId());
val = val * file->price(security.tradingCurrency(), accountCurrency.id()).rate(accountCurrency.id());
val = val.convert(acc.fraction());
value += val;
} catch(MyMoneyException* e) {
tqWarning("%s", (TQString("cannot convert stock balance of %1 to base currency: %2").arg(stock.name(), e->what())).data());
delete e;
}
}
return value;
}
}
|