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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
/***************************************************************************
kmymoneyaccounttreeforecast.cpp
-------------------
begin : Fri Aug 01 2008
copyright : (C) 2008 by Alvaro Soliverez
email : 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. *
* *
***************************************************************************/
// ----------------------------------------------------------------------------
// KDE Includes
#include <klocale.h>
// ----------------------------------------------------------------------------
// Project Includes
#include <kmymoney/mymoneyfile.h>
#include <kmymoneyaccounttreeforecast.h>
#include "../kmymoney2.h"
#include "../kmymoneyglobalsettings.h"
#include "../mymoney/mymoneyforecast.h"
#include "../reports/reportaccount.h"
using namespace reports;
KMyMoneyAccountTreeForecast::KMyMoneyAccountTreeForecast(TQWidget* parent, const char* name) :
KMyMoneyAccountTreeBase::KMyMoneyAccountTreeBase(parent, name)
{
setResizeMode(TQListView::NoColumn);
}
void KMyMoneyAccountTreeForecast::showAccount( void )
{
addColumn(i18n("Account"));
}
void KMyMoneyAccountTreeForecast::clearColumns( void )
{
clear();
while(columns() > 0) {
removeColumn(0);
}
}
void KMyMoneyAccountTreeForecast::showSummary(MyMoneyForecast& forecast)
{
int daysToBeginDay;
//add cycle interval columns
addColumn(i18n("Current"), -1);
//if beginning of forecast is today, set the begin day to next cycle to avoid repeating the first cycle
if(TQDate::currentDate() < forecast.beginForecastDate()) {
daysToBeginDay = TQDate::currentDate().daysTo(forecast.beginForecastDate());
} else {
daysToBeginDay = forecast.accountsCycle();
}
for(int i = 0; ((i*forecast.accountsCycle())+daysToBeginDay) <= forecast.forecastDays(); ++i) {
int intervalDays = ((i*forecast.accountsCycle())+daysToBeginDay);
TQString columnName = i18n("%1 days").arg(intervalDays, 0, 10);
addColumn(columnName, -1);
}
//add variation columns
addColumn(i18n("Total variation"), -1);
//align columns
for(int i = 1; i < columns(); ++i) {
setColumnAlignment(i, TQt::AlignRight);
}
}
void KMyMoneyAccountTreeForecast::showDetailed(MyMoneyForecast& forecast)
{
//add cycle interval columns
addColumn(i18n("Current"), -1);
for(int i = 1; i <= forecast.forecastDays(); ++i) {
TQDate forecastDate = TQDate::currentDate().addDays(i);
TQString columnName = TDEGlobal::locale()->formatDate(forecastDate, true);
addColumn(columnName, -1);
}
//add variation columns
addColumn(i18n("Total variation"), -1);
//align columns
for(int i = 1; i < columns(); ++i) {
setColumnAlignment(i, TQt::AlignRight);
}
}
void KMyMoneyAccountTreeForecast::showAdvanced(MyMoneyForecast& forecast)
{
int daysToBeginDay;
//if beginning of forecast is today, set the begin day to next cycle to avoid repeating the first cycle
if(TQDate::currentDate() < forecast.beginForecastDate()) {
daysToBeginDay = TQDate::currentDate().daysTo(forecast.beginForecastDate());
} else {
daysToBeginDay = forecast.accountsCycle();
}
//add columns
for(int i = 1; ((i * forecast.accountsCycle()) + daysToBeginDay) <= forecast.forecastDays(); ++i) {
int col = addColumn(i18n("Min Bal %1").arg(i), -1);
setColumnAlignment(col, TQt::AlignRight);
addColumn(i18n("Min Date %1").arg(i), -1);
}
for(int i = 1; ((i * forecast.accountsCycle()) + daysToBeginDay) <= forecast.forecastDays(); ++i) {
int col = addColumn(i18n("Max Bal %1").arg(i), -1);
setColumnAlignment(col, TQt::AlignRight);
addColumn(i18n("Max Date %1").arg(i), -1);
}
int col = addColumn(i18n("Average"), -1);
setColumnAlignment(col, TQt::AlignRight);
}
void KMyMoneyAccountTreeForecast::showBudget(MyMoneyForecast& forecast)
{
TQDate forecastStartDate = forecast.forecastStartDate();
TQDate forecastEndDate = forecast.forecastEndDate();
//add cycle interval columns
TQDate f_date = forecastStartDate;
for(; f_date <= forecastEndDate; f_date = f_date.addMonths(1)) {
TQString columnName = TQDate::longMonthName(f_date.month());
addColumn(columnName, -1);
}
//add total column
addColumn(i18n("Total"), -1);
//align columns
for(int i = 1; i < columns(); ++i) {
setColumnAlignment(i, TQt::AlignRight);
}
}
void KMyMoneyAccountTreeForecast::slotSelectObject(const TQListViewItem* i)
{
emit selectObject(MyMoneyInstitution());
emit selectObject(MyMoneyAccount());
const KMyMoneyAccountTreeBaseItem* item = dynamic_cast<const KMyMoneyAccountTreeBaseItem*>(i);
if(item) {
emit openObject(item->itemObject());
}
}
KMyMoneyAccountTreeForecastItem::KMyMoneyAccountTreeForecastItem(KListView *parent, const MyMoneyAccount& account, const MyMoneyForecast &forecast, const MyMoneySecurity& security, const TQString& name) :
KMyMoneyAccountTreeBaseItem(parent, account, security, name),
m_forecast(forecast)
{
updateAccount(true);
}
KMyMoneyAccountTreeForecastItem::KMyMoneyAccountTreeForecastItem(KMyMoneyAccountTreeForecastItem *parent, const MyMoneyAccount& account, const MyMoneyForecast& forecast, const TQValueList<MyMoneyPrice>& price, const MyMoneySecurity& security, const EForecastViewType forecastType) :
KMyMoneyAccountTreeBaseItem(parent, account, price, security),
m_forecast(forecast),
m_forecastType(forecastType)
{
//setForecastViewType(forecastViewType);
updateAccount(true);
switch(forecastViewType())
{
case eSummary:
updateSummary();
break;
case eDetailed:
updateDetailed();
break;
case eBudget:
updateBudget();
break;
default:
break;
}
}
KMyMoneyAccountTreeForecastItem::~KMyMoneyAccountTreeForecastItem()
{
}
void KMyMoneyAccountTreeForecastItem::setForecast(const MyMoneyForecast& forecast)
{
m_forecast = forecast;
updateAccount();
}
void KMyMoneyAccountTreeForecastItem::updateSummary()
{
MyMoneyMoney amountMM;
int it_c = 1; // iterator for the columns of the listview
MyMoneyFile* file = MyMoneyFile::instance();
int daysToBeginDay;
if(TQDate::currentDate() < m_forecast.beginForecastDate()) {
daysToBeginDay = TQDate::currentDate().daysTo(m_forecast.beginForecastDate());
} else {
daysToBeginDay = m_forecast.accountsCycle();
}
MyMoneySecurity currency;
if(m_account.isInvest()) {
MyMoneySecurity underSecurity = file->security(m_account.currencyId());
currency = file->security(underSecurity.tradingCurrency());
} else {
currency = file->security(m_account.currencyId());
}
//add current balance column
TQDate summaryDate = TQDate::currentDate();
amountMM = m_forecast.forecastBalance(m_account, summaryDate);
//calculate the balance in base currency for the total row
setAmount(it_c, amountMM);
setValue(it_c, amountMM, summaryDate);
showAmount(it_c, amountMM, currency);
it_c++;
//iterate through all other columns
for(TQDate summaryDate = TQDate::currentDate().addDays(daysToBeginDay); summaryDate <= m_forecast.forecastEndDate();summaryDate = summaryDate.addDays(m_forecast.accountsCycle()), ++it_c) {
amountMM = m_forecast.forecastBalance(m_account, summaryDate);
//calculate the balance in base currency for the total row
setAmount(it_c, amountMM);
setValue(it_c, amountMM, summaryDate);
showAmount(it_c, amountMM, currency);
}
//calculate and add variation per cycle
setNegative(m_forecast.accountTotalVariation(m_account).isNegative());
setAmount(it_c, m_forecast.accountTotalVariation(m_account));
setValue(it_c, m_forecast.accountTotalVariation(m_account), m_forecast.forecastEndDate());
showAmount(it_c, m_forecast.accountTotalVariation(m_account), currency);
}
void KMyMoneyAccountTreeForecastItem::updateDetailed()
{
TQString amount;
TQString vAmount;
MyMoneyMoney vAmountMM;
MyMoneyFile* file = MyMoneyFile::instance();
MyMoneySecurity currency;
if(m_account.isInvest()) {
MyMoneySecurity underSecurity = file->security(m_account.currencyId());
currency = file->security(underSecurity.tradingCurrency());
} else {
currency = file->security(m_account.currencyId());
}
int it_c = 1; // iterator for the columns of the listview
for(TQDate forecastDate = TQDate::currentDate(); forecastDate <= m_forecast.forecastEndDate(); ++it_c, forecastDate = forecastDate.addDays(1)) {
MyMoneyMoney amountMM = m_forecast.forecastBalance(m_account, forecastDate);
//calculate the balance in base currency for the total row
setAmount(it_c, amountMM);
setValue(it_c, amountMM, forecastDate);
showAmount(it_c, amountMM, currency);
}
//calculate and add variation per cycle
vAmountMM = m_forecast.accountTotalVariation(m_account);
setAmount(it_c, vAmountMM);
setValue(it_c, vAmountMM, m_forecast.forecastEndDate());
showAmount(it_c, vAmountMM, currency);
}
void KMyMoneyAccountTreeForecastItem::updateBudget()
{
MyMoneySecurity currency;
MyMoneyMoney tAmountMM;
MyMoneyFile* file = MyMoneyFile::instance();
int it_c = 1; // iterator for the columns of the listview
TQDate forecastDate = m_forecast.forecastStartDate();
if(m_account.isInvest()) {
MyMoneySecurity underSecurity = file->security(m_account.currencyId());
currency = file->security(underSecurity.tradingCurrency());
} else {
currency = file->security(m_account.currencyId());
}
//iterate columns
for(; forecastDate <= m_forecast.forecastEndDate(); forecastDate = forecastDate.addMonths(1), ++it_c) {
MyMoneyMoney amountMM;
amountMM = m_forecast.forecastBalance(m_account,forecastDate);
if(m_account.accountType() == MyMoneyAccount::Expense)
amountMM = -amountMM;
tAmountMM += amountMM;
setAmount(it_c, amountMM);
setValue(it_c, amountMM, forecastDate);
showAmount(it_c, amountMM, currency);
}
//set total column
setAmount(it_c, tAmountMM);
setValue(it_c, tAmountMM, m_forecast.forecastEndDate());
showAmount(it_c, tAmountMM, currency);
}
MyMoneyMoney KMyMoneyAccountTreeForecastItem::balance() const
{
return MyMoneyMoney();
}
void KMyMoneyAccountTreeForecastItem::showAmount(int column, const MyMoneyMoney amount, const MyMoneySecurity security)
{
setText(column, amount.formatMoney(m_account, security), amount.isNegative() );
}
void KMyMoneyAccountTreeForecastItem::adjustParentValue(int column, const MyMoneyMoney& value)
{
m_values[column] += value;
m_values[column] = m_values[column].convert(listView()->baseCurrency().smallestAccountFraction());
// if the entry has no children,
// or it is the top entry
// or it is currently not open
// we need to display the value of it
KMyMoneyAccountTreeForecast* lv = dynamic_cast<KMyMoneyAccountTreeForecast*>(listView());
if(!lv)
return;
if(!firstChild() || !parent() || (!isOpen() && firstChild())
|| depth() == 1 ) {
if(firstChild())
setText(column, " ");
showAmount(column, m_values[column], listView()->baseCurrency());
}
// now make sure, the upstream accounts also get notified about the value change
KMyMoneyAccountTreeForecastItem* p = dynamic_cast<KMyMoneyAccountTreeForecastItem*>(parent());
if(p != 0) {
p->adjustParentValue(column, value);
}
}
void KMyMoneyAccountTreeForecastItem::setValue(int column, MyMoneyMoney amount, TQDate forecastDate)
{
KMyMoneyAccountTreeForecastItem* p = dynamic_cast<KMyMoneyAccountTreeForecastItem*>(parent());
//calculate the balance in base currency for the total row
if(m_account.currencyId() != listView()->baseCurrency().id()) {
ReportAccount repAcc = ReportAccount(m_account.id());
MyMoneyMoney curPrice = repAcc.baseCurrencyPrice(forecastDate);
MyMoneyMoney baseAmountMM = amount * curPrice;
m_values[column] = baseAmountMM.convert(listView()->baseCurrency().smallestAccountFraction());
if(p != 0) {
p->adjustParentValue(column, m_values[column]);
}
} else {
m_values[column] += amount;
if(p != 0) {
p->adjustParentValue(column, amount);
}
}
}
void KMyMoneyAccountTreeForecastItem::setAmount(int column, MyMoneyMoney amount)
{
m_amounts[column] = amount;
}
void KMyMoneyAccountTreeForecastItem::setOpen(bool open)
{
if (open == isOpen())
return;
KMyMoneyAccountTreeBaseItem::setOpen(open);
if(!open)
{
for(int i = 1; i < listView()->columns(); ++i)
{
showAmount(i, m_values[i], listView()->baseCurrency());
}
} else if (depth() > 1) {
for(int i = 1; i < listView()->columns(); ++i)
{
showAmount(i, m_amounts[i], m_security);
}
}
}
#include "kmymoneyaccounttreeforecast.moc"
|