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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
|
/***************************************************************************
kmymoneyutils.cpp - description
-------------------
begin : Wed Feb 5 2003
copyright : (C) 2000-2003 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
// ----------------------------------------------------------------------------
// TQt Includes
// ----------------------------------------------------------------------------
// TDE Headers
#include <tdelocale.h>
#include <tdeglobal.h>
#include <kiconloader.h>
#include <tdemessagebox.h>
#include <tdeconfig.h>
#include <kstandarddirs.h>
// ----------------------------------------------------------------------------
// Project Includes
#include <kmymoney/mymoneyfile.h>
#include <kmymoney/mymoneyforecast.h>
#include <kmymoney/kmymoneyglobalsettings.h>
#include <kmymoney/investtransactioneditor.h>
#include "kmymoneyutils.h"
KMyMoneyUtils::KMyMoneyUtils()
{
}
KMyMoneyUtils::~KMyMoneyUtils()
{
}
const TQString KMyMoneyUtils::accountTypeToString(const MyMoneyAccount::accountTypeE accountType)
{
return MyMoneyAccount::accountTypeToString(accountType);
}
MyMoneyAccount::accountTypeE KMyMoneyUtils::stringToAccountType(const TQString& type)
{
MyMoneyAccount::accountTypeE rc = MyMoneyAccount::UnknownAccountType;
TQString tmp = type.lower();
if(tmp == i18n("Checking").lower())
rc = MyMoneyAccount::Checkings;
else if(tmp == i18n("Savings").lower())
rc = MyMoneyAccount::Savings;
else if(tmp == i18n("Credit Card").lower())
rc = MyMoneyAccount::CreditCard;
else if(tmp == i18n("Cash").lower())
rc = MyMoneyAccount::Cash;
else if(tmp == i18n("Loan").lower())
rc = MyMoneyAccount::Loan;
else if(tmp == i18n("Certificate of Deposit").lower())
rc = MyMoneyAccount::CertificateDep;
else if(tmp == i18n("Investment").lower())
rc = MyMoneyAccount::Investment;
else if(tmp == i18n("Money Market").lower())
rc = MyMoneyAccount::MoneyMarket;
else if(tmp == i18n("Asset").lower())
rc = MyMoneyAccount::Asset;
else if(tmp == i18n("Liability").lower())
rc = MyMoneyAccount::Liability;
else if(tmp == i18n("Currency").lower())
rc = MyMoneyAccount::Currency;
else if(tmp == i18n("Income").lower())
rc = MyMoneyAccount::Income;
else if(tmp == i18n("Expense").lower())
rc = MyMoneyAccount::Expense;
else if(tmp == i18n("Investment Loan").lower())
rc = MyMoneyAccount::AssetLoan;
else if(tmp == i18n("Stock").lower())
rc = MyMoneyAccount::Stock;
else if(tmp == i18n("Equity").lower())
rc = MyMoneyAccount::Equity;
return rc;
}
MyMoneySecurity::eSECURITYTYPE KMyMoneyUtils::stringToSecurity(const TQString& txt)
{
MyMoneySecurity::eSECURITYTYPE rc = MyMoneySecurity::SECURITY_NONE;
TQString tmp = txt.lower();
if(tmp == i18n("Stock").lower())
rc = MyMoneySecurity::SECURITY_STOCK;
else if(tmp == i18n("Mutual Fund").lower())
rc = MyMoneySecurity::SECURITY_MUTUALFUND;
else if(tmp == i18n("Bond").lower())
rc = MyMoneySecurity::SECURITY_BOND;
else if(tmp == i18n("Currency").lower())
rc = MyMoneySecurity::SECURITY_CURRENCY;
return rc;
}
const TQString KMyMoneyUtils::securityTypeToString(const MyMoneySecurity::eSECURITYTYPE securityType)
{
return i18n(MyMoneySecurity::securityTypeToString(securityType).utf8());
}
const TQString KMyMoneyUtils::occurenceToString(const MyMoneySchedule::occurenceE occurence)
{
return i18n(MyMoneySchedule::occurenceToString(occurence).utf8());
}
const TQString KMyMoneyUtils::paymentMethodToString(MyMoneySchedule::paymentTypeE paymentType)
{
return i18n(MyMoneySchedule::paymentMethodToString(paymentType).utf8());
}
const TQString KMyMoneyUtils::weekendOptionToString(MyMoneySchedule::weekendOptionE weekendOption)
{
return i18n(MyMoneySchedule::weekendOptionToString(weekendOption).utf8());
}
const TQString KMyMoneyUtils::scheduleTypeToString(MyMoneySchedule::typeE type)
{
return i18n(MyMoneySchedule::scheduleTypeToString(type).utf8());
}
KGuiItem KMyMoneyUtils::scheduleNewGuiItem(void)
{
TDEIconLoader *ic = TDEGlobal::iconLoader();
KGuiItem splitGuiItem( i18n("&New Schedule..."),
TQIconSet(ic->loadIcon("document-new", TDEIcon::Small, TDEIcon::SizeSmall)),
i18n("Create a new schedule."),
i18n("Use this to create a new schedule."));
return splitGuiItem;
}
KGuiItem KMyMoneyUtils::accountsFilterGuiItem(void)
{
TDEIconLoader *ic = TDEGlobal::iconLoader();
KGuiItem splitGuiItem( i18n("&Filter"),
TQIconSet(ic->loadIcon("filter", TDEIcon::Small, TDEIcon::SizeSmall)),
i18n("Filter out accounts"),
i18n("Use this to filter out accounts"));
return splitGuiItem;
}
TQPixmap KMyMoneyUtils::billScheduleIcon(int size)
{
TDEIconLoader *ic = TDEGlobal::iconLoader();
return ic->loadIcon("billschedule", TDEIcon::User, size);
}
TQPixmap KMyMoneyUtils::depositScheduleIcon(int size)
{
TDEIconLoader *ic = TDEGlobal::iconLoader();
return ic->loadIcon("depositschedule", TDEIcon::User, size);
}
TQPixmap KMyMoneyUtils::transferScheduleIcon(int size)
{
TDEIconLoader *ic = TDEGlobal::iconLoader();
return ic->loadIcon("transferschedule", TDEIcon::User, size);
}
TQPixmap KMyMoneyUtils::scheduleIcon(int size)
{
TDEIconLoader *ic = TDEGlobal::iconLoader();
return ic->loadIcon("schedule", TDEIcon::User, size);
}
const char* homePageItems[] = {
I18N_NOOP("Payments"),
I18N_NOOP("Preferred accounts"),
I18N_NOOP("Payment accounts"),
I18N_NOOP("Favorite reports"),
I18N_NOOP("Forecast (schedule)"),
I18N_NOOP("Networth forecast"),
I18N_NOOP("Forecast (history)"),
I18N_NOOP("Assets and Liabilities"),
I18N_NOOP("Budget"),
I18N_NOOP("CashFlow"),
// insert new items above this comment
0
};
const TQString KMyMoneyUtils::homePageItemToString(const int idx)
{
TQString rc;
if(abs(idx) > 0 && abs(idx) < static_cast<int>(sizeof(homePageItems)/sizeof(homePageItems[0]))) {
rc = i18n(homePageItems[abs(idx-1)]);
}
return rc;
}
int KMyMoneyUtils::stringToHomePageItem(const TQString& txt)
{
int idx = 0;
for(idx = 0; homePageItems[idx] != 0; ++idx) {
if(txt == i18n(homePageItems[idx]))
return idx+1;
}
return 0;
}
bool KMyMoneyUtils::appendCorrectFileExt(TQString& str, const TQString& strExtToUse)
{
bool rc = false;
if(!str.isEmpty()) {
//find last . delminator
int nLoc = str.findRev('.');
if(nLoc != -1) {
TQString strExt, strTemp;
strTemp = str.left(nLoc + 1);
strExt = str.right(str.length() - (nLoc + 1));
if(strExt.find(strExtToUse, 0, FALSE) == -1) {
// if the extension given contains a period, we remove our's
if(strExtToUse.find('.') != -1)
strTemp = strTemp.left(strTemp.length()-1);
//append extension to make complete file name
strTemp.append(strExtToUse);
str = strTemp;
rc = true;
}
} else {
str.append(".");
str.append(strExtToUse);
rc = true;
}
}
return rc;
}
void KMyMoneyUtils::checkConstants(void)
{
Q_ASSERT(static_cast<int>(TDELocale::ParensAround) == static_cast<int>(MyMoneyMoney::ParensAround));
Q_ASSERT(static_cast<int>(TDELocale::BeforeQuantityMoney) == static_cast<int>(MyMoneyMoney::BeforeQuantityMoney));
Q_ASSERT(static_cast<int>(TDELocale::AfterQuantityMoney) == static_cast<int>(MyMoneyMoney::AfterQuantityMoney));
Q_ASSERT(static_cast<int>(TDELocale::BeforeMoney) == static_cast<int>(MyMoneyMoney::BeforeMoney));
Q_ASSERT(static_cast<int>(TDELocale::AfterMoney) == static_cast<int>(MyMoneyMoney::AfterMoney));
}
TQString KMyMoneyUtils::variableCSS(void)
{
TQColor tcolor = TDEGlobalSettings::textColor();
TQString css;
css += "<style type=\"text/css\">\n<!--\n";
css += TQString(".row-even, .item0 { background-color: %1; color: %2 }\n")
.arg((KMyMoneyGlobalSettings::listBGColor()).name()).arg(tcolor.name());
css += TQString(".row-odd, .item1 { background-color: %1; color: %2 }\n")
.arg((KMyMoneyGlobalSettings::listColor()).name()).arg(tcolor.name());
css += "-->\n</style>\n";
return css;
}
TQString KMyMoneyUtils::findResource(const char* type, const TQString& filename)
{
TQString language = TDEGlobal::locale()->language();
TQString country = TDEGlobal::locale()->country();
TQString rc, mask;
// check that the placeholder is present
if(!filename.find("%1")) {
tqWarning(TQString("%%1 not found in '%1'").arg(filename));
return filename;
}
// search the given resource
mask = filename.arg("_%1.%2");
rc = TDEGlobal::dirs()->findResource(type, mask.arg(country).arg(language));
if(rc.isEmpty()) {
mask = filename.arg("_%1");
rc = TDEGlobal::dirs()->findResource(type, mask.arg(language));
}
if(rc.isEmpty()) {
// tqDebug(TQString("html/home_%1.html not found").arg(country));
rc = TDEGlobal::dirs()->findResource(type, mask.arg(country));
}
if(rc.isEmpty()) {
rc = TDEGlobal::dirs()->findResource(type, filename.arg(""));
}
if(rc.isEmpty()) {
tqWarning(TQString("No resource found for (%1,%2)").arg(type).arg(filename));
}
return rc;
}
const MyMoneySplit KMyMoneyUtils::stockSplit(const MyMoneyTransaction& t)
{
TQValueList<MyMoneySplit>::ConstIterator it_s;
MyMoneySplit investmentAccountSplit;
for(it_s = t.splits().begin(); it_s != t.splits().end(); ++it_s) {
if(!(*it_s).accountId().isEmpty()) {
MyMoneyAccount acc = MyMoneyFile::instance()->account((*it_s).accountId());
if(acc.isInvest()) {
return *it_s;
}
// if we have a reference to an investment account, we remember it here
if(acc.accountType() == MyMoneyAccount::Investment)
investmentAccountSplit = *it_s;
}
}
// if we haven't found a stock split, we see if we've seen
// an investment account on the way. If so, we return it.
if(!investmentAccountSplit.id().isEmpty())
return investmentAccountSplit;
// if none was found, we return an empty split.
return MyMoneySplit();
}
KMyMoneyUtils::transactionTypeE KMyMoneyUtils::transactionType(const MyMoneyTransaction& t)
{
if(!stockSplit(t).id().isEmpty())
return InvestmentTransaction;
if(t.splitCount() < 2) {
return Unknown;
} else if(t.splitCount() > 2) {
// FIXME check for loan transaction here
return SplitTransaction;
}
TQString ida, idb;
ida = t.splits()[0].accountId();
idb = t.splits()[1].accountId();
if(ida.isEmpty() || idb.isEmpty())
return Unknown;
MyMoneyAccount a, b;
a = MyMoneyFile::instance()->account(ida);
b = MyMoneyFile::instance()->account(idb);
if((a.accountGroup() == MyMoneyAccount::Asset
|| a.accountGroup() == MyMoneyAccount::Liability)
&& (b.accountGroup() == MyMoneyAccount::Asset
|| b.accountGroup() == MyMoneyAccount::Liability))
return Transfer;
return Normal;
}
void KMyMoneyUtils::calculateAutoLoan(const MyMoneySchedule& schedule, MyMoneyTransaction& transaction, const TQMap<TQString, MyMoneyMoney>& balances)
{
try {
MyMoneyForecast::calculateAutoLoan(schedule, transaction, balances);
} catch (MyMoneyException* e) {
KMessageBox::detailedError(0, i18n("Unable to load schedule details"), e->what());
delete e;
}
}
TQString KMyMoneyUtils::nextCheckNumber(const MyMoneyAccount& acc)
{
// determine next check number
TQString number;
TQRegExp exp(TQString("(.*\\D)?(\\d+)(\\D.*)?"));
if(exp.search(acc.value("lastNumberUsed")) != -1) {
number = TQString("%1%2%3").arg(exp.cap(1)).arg(exp.cap(2).toULongLong() + 1).arg(exp.cap(3));
} else {
number = "1";
}
return number;
}
TQString KMyMoneyUtils::reconcileStateToString(MyMoneySplit::reconcileFlagE flag, bool text)
{
TQString txt;
if(text) {
switch(flag) {
case MyMoneySplit::NotReconciled:
txt = i18n("Reconcile state 'Not reconciled'", "Not reconciled");
break;
case MyMoneySplit::Cleared:
txt = i18n("Reconcile state 'Cleared'", "Cleared");
break;
case MyMoneySplit::Reconciled:
txt = i18n("Reconcile state 'Reconciled'", "Reconciled");
break;
case MyMoneySplit::Frozen:
txt = i18n("Reconcile state 'Frozen'", "Frozen");
break;
default:
txt = i18n("Unknown");
break;
}
} else {
switch(flag) {
case MyMoneySplit::NotReconciled:
break;
case MyMoneySplit::Cleared:
txt = i18n("Reconcile flag C", "C");
break;
case MyMoneySplit::Reconciled:
txt = i18n("Reconcile flag R", "R");
break;
case MyMoneySplit::Frozen:
txt = i18n("Reconcile flag F", "F");
break;
default:
txt = i18n("Flag for unknown reconciliation state", "?");
break;
}
}
return txt;
}
MyMoneyTransaction KMyMoneyUtils::scheduledTransaction(const MyMoneySchedule& schedule)
{
MyMoneyTransaction t = schedule.transaction();
try {
if (schedule.type() == MyMoneySchedule::TYPE_LOANPAYMENT) {
calculateAutoLoan(schedule, t, TQMap<TQString, MyMoneyMoney>());
}
} catch (MyMoneyException* e) {
tqDebug(TQString("Unable to load schedule details for '%1' during transaction match: %1").arg(schedule.name()).arg(e->what()));
delete e;
}
t.clearId();
t.setEntryDate(TQDate());
return t;
}
void KMyMoneyUtils::previouslyUsedCategories(const TQString& investmentAccount, TQString& feesId, TQString& interestId)
{
feesId = interestId = TQString();
MyMoneyFile* file = MyMoneyFile::instance();
try {
MyMoneyAccount acc = file->account(investmentAccount);
MyMoneyTransactionFilter filter(investmentAccount);
filter.setReportAllSplits(false);
// since we assume an investment account here, we need to collect the stock accounts as well
filter.addAccount(acc.accountList());
TQValueList< TQPair<MyMoneyTransaction, MyMoneySplit> > list;
file->transactionList(list, filter);
TQValueList< TQPair<MyMoneyTransaction, MyMoneySplit> >::const_iterator it_t;
for(it_t = list.begin(); it_t != list.end(); ++it_t) {
const MyMoneyTransaction& t = (*it_t).first;
const MyMoneySplit&s = (*it_t).second;
MyMoneySplit assetAccountSplit;
TQValueList<MyMoneySplit> feeSplits;
TQValueList<MyMoneySplit> interestSplits;
MyMoneySecurity security;
MyMoneySecurity currency;
MyMoneySplit::investTransactionTypeE transactionType;
InvestTransactionEditor::dissectTransaction(t, s, assetAccountSplit, feeSplits, interestSplits, security, currency, transactionType);
if(feeSplits.count() == 1) {
feesId = feeSplits.first().accountId();
}
if(interestSplits.count() == 1) {
interestId = interestSplits.first().accountId();
}
}
} catch(MyMoneyException *e) {
delete e;
}
}
|