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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
|
/***************************************************************************
knewaccountdlg.cpp
-------------------
copyright : (C) 2000 by Michael Edwardes
2004 by Thomas Baumgart
email : [email protected]
[email protected]
***************************************************************************/
/***************************************************************************
* *
* 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 <qpixmap.h>
#include <qpushbutton.h>
#include <qlabel.h>
#include <qheader.h>
#include <qtooltip.h>
#include <qcheckbox.h>
#include <qtimer.h>
#include <qtabwidget.h>
#include <qbuttongroup.h>
#include <qradiobutton.h>
#include <qtextedit.h>
#include <qlayout.h>
#include <qtabwidget.h>
// ----------------------------------------------------------------------------
// KDE Headers
#include <klocale.h>
#include <kglobal.h>
#include <kmessagebox.h>
#include <kcombobox.h>
#include <klistview.h>
#include <kstandarddirs.h>
#include <kiconloader.h>
#include <kpushbutton.h>
#include <kled.h>
#include <kdebug.h>
#include <kglobalsettings.h>
// ----------------------------------------------------------------------------
// Project Includes
#include "knewaccountdlg.h"
#include <kmymoney/kmymoneyedit.h>
#include <kmymoney/kmymoneydateinput.h>
#include <kmymoney/mymoneyexception.h>
#include <kmymoney/mymoneyfile.h>
#include <kmymoney/kmymoneyaccounttree.h>
#include <kmymoney/kmymoneyglobalsettings.h>
#include <kmymoney/mymoneyreport.h>
#include <kmymoney/kguiutils.h>
#include <kmymoney/kmymoneycombo.h>
#include "../widgets/kmymoneycurrencyselector.h"
#include "../widgets/kmymoneyaccountselector.h"
#include "../mymoney/mymoneyexception.h"
#include "../mymoney/mymoneykeyvaluecontainer.h"
#include "../dialogs/knewbankdlg.h"
#include "../views/kmymoneyfile.h"
#include "../kmymoneyutils.h"
#include "../reports/kreportchartview.h"
#include "../reports/pivottable.h"
// in KOffice version < 1.5 KDCHART_PROPSET_NORMAL_DATA was a static const
// but in 1.5 this has been changed into a #define'd value. So we have to
// make sure, we use the right one.
#ifndef KDCHART_PROPSET_NORMAL_DATA
#define KMM_KDCHART_PROPSET_NORMAL_DATA KDChartParams::KDCHART_PROPSET_NORMAL_DATA
#else
#define KMM_KDCHART_PROPSET_NORMAL_DATA KDCHART_PROPSET_NORMAL_DATA
#endif
KNewAccountDlg::KNewAccountDlg(const MyMoneyAccount& account, bool isEditing, bool categoryEditor, QWidget *parent, const char *name, const QString& title)
: KNewAccountDlgDecl(parent,name,true),
m_account(account),
m_bSelectedParentAccount(false),
m_categoryEditor(categoryEditor),
m_isEditing(isEditing)
{
QString columnName = ( (categoryEditor) ? i18n("Categories") : i18n("Accounts") );
m_qlistviewParentAccounts->setRootIsDecorated(true);
m_qlistviewParentAccounts->setAllColumnsShowFocus(true);
m_qlistviewParentAccounts->setSectionHeader(columnName);
m_qlistviewParentAccounts->setMultiSelection(false);
m_qlistviewParentAccounts->header()->setResizeEnabled(true);
m_qlistviewParentAccounts->setColumnWidthMode(0, QListView::Maximum);
m_qlistviewParentAccounts->setEnabled(false);
// never show the horizontal scroll bar
m_qlistviewParentAccounts->setHScrollBarMode(QScrollView::AlwaysOff);
m_subAccountLabel->setText(i18n("Is a sub account"));
m_qlistviewParentAccounts->header()->setFont(KMyMoneyGlobalSettings::listHeaderFont());
accountNameEdit->setText(account.name());
descriptionEdit->setText(account.description());
typeCombo->setEnabled(true);
MyMoneyFile *file = MyMoneyFile::instance();
// load the price mode combo
m_priceMode->insertItem(i18n("default price mode", "<default>"), 0);
m_priceMode->insertItem(i18n("Price per share"), 1);
m_priceMode->insertItem(i18n("Total for all shares"), 2);
int priceMode = 0;
if(m_account.accountType() == MyMoneyAccount::Investment) {
m_priceMode->setEnabled(true);
if(!m_account.value("priceMode").isEmpty())
priceMode = m_account.value("priceMode").toInt();
}
m_priceMode->setCurrentItem(priceMode);
bool haveMinBalance = false;
bool haveMaxCredit = false;
if (categoryEditor)
{
// get rid of the tabs that are not used for categories
QWidget* tab = m_tab->page(m_tab->indexOf(m_institutionTab));
if(tab)
m_tab->removePage(tab);
tab = m_tab->page(m_tab->indexOf(m_limitsTab));
if(tab)
m_tab->removePage(tab);
//m_qlistviewParentAccounts->setEnabled(true);
startDateEdit->setEnabled(false);
accountNoEdit->setEnabled(false);
m_institutionBox->hide();
m_qcheckboxNoVat->hide();
typeCombo->insertItem(KMyMoneyUtils::accountTypeToString(MyMoneyAccount::Income));
typeCombo->insertItem(KMyMoneyUtils::accountTypeToString(MyMoneyAccount::Expense));
// Hardcoded but acceptable
switch (account.accountType())
{
case MyMoneyAccount::Income:
typeCombo->setCurrentItem(0);
break;
case MyMoneyAccount::Expense:
default:
typeCombo->setCurrentItem(1);
break;
}
m_currency->setEnabled(true);
if (m_isEditing)
{
typeCombo->setEnabled(false);
m_currency->setDisabled(MyMoneyFile::instance()->isReferenced(m_account));
}
m_qcheckboxPreferred->hide();
m_qcheckboxTax->setChecked(account.value("Tax") == "Yes");
loadVatAccounts();
}
else
{
// get rid of the tabs that are not used for accounts
QWidget* taxtab = m_tab->page(m_tab->indexOf(m_taxTab));
if (taxtab) {
if(m_account.isAssetLiability()) {
m_vatCategory->setText(i18n( "VAT account"));
m_vatAssignmentFrame->hide();
m_qcheckboxTax->setChecked(account.value("Tax") == "Yes");
} else {
m_tab->removePage(taxtab);
}
}
switch(m_account.accountType()) {
case MyMoneyAccount::Savings:
case MyMoneyAccount::Cash:
haveMinBalance = true;
break;
case MyMoneyAccount::Checkings:
haveMinBalance = true;
haveMaxCredit = true;
break;
case MyMoneyAccount::CreditCard:
haveMaxCredit = true;
break;
default:
// no limit available, so we might get rid of the tab
QWidget* tab = m_tab->page(m_tab->indexOf(m_limitsTab));
if(tab)
m_tab->removePage(tab);
// don't try to hide the widgets we just wiped
// in the next step
haveMaxCredit = haveMinBalance = true;
break;
}
if(!haveMaxCredit) {
m_maxCreditLabel->setEnabled(false);
m_maxCreditLabel->hide();
m_maxCreditEarlyEdit->hide();
m_maxCreditAbsoluteEdit->hide();
}
if(!haveMinBalance) {
m_minBalanceLabel->setEnabled(false);
m_minBalanceLabel->hide();
m_minBalanceEarlyEdit->hide();
m_minBalanceAbsoluteEdit->hide();
}
typeCombo->insertItem(KMyMoneyUtils::accountTypeToString(MyMoneyAccount::Checkings));
typeCombo->insertItem(KMyMoneyUtils::accountTypeToString(MyMoneyAccount::Savings));
typeCombo->insertItem(KMyMoneyUtils::accountTypeToString(MyMoneyAccount::Cash));
typeCombo->insertItem(KMyMoneyUtils::accountTypeToString(MyMoneyAccount::CreditCard));
typeCombo->insertItem(KMyMoneyUtils::accountTypeToString(MyMoneyAccount::Loan));
typeCombo->insertItem(KMyMoneyUtils::accountTypeToString(MyMoneyAccount::Investment));
typeCombo->insertItem(KMyMoneyUtils::accountTypeToString(MyMoneyAccount::Asset));
typeCombo->insertItem(KMyMoneyUtils::accountTypeToString(MyMoneyAccount::Liability));
typeCombo->insertItem(KMyMoneyUtils::accountTypeToString(MyMoneyAccount::Stock));
/*
typeCombo->insertItem(KMyMoneyUtils::accountTypeToString(MyMoneyAccount::CertificateDep));
typeCombo->insertItem(KMyMoneyUtils::accountTypeToString(MyMoneyAccount::MoneyMarket));
typeCombo->insertItem(KMyMoneyUtils::accountTypeToString(MyMoneyAccount::Currency));
*/
// Hardcoded but acceptable
switch (account.accountType())
{
default:
case MyMoneyAccount::Checkings:
typeCombo->setCurrentItem(0);
break;
case MyMoneyAccount::Savings:
typeCombo->setCurrentItem(1);
break;
case MyMoneyAccount::Cash:
typeCombo->setCurrentItem(2);
break;
case MyMoneyAccount::CreditCard:
typeCombo->setCurrentItem(3);
break;
case MyMoneyAccount::Loan:
typeCombo->setCurrentItem(4);
break;
case MyMoneyAccount::Investment:
typeCombo->setCurrentItem(5);
break;
case MyMoneyAccount::Asset:
typeCombo->setCurrentItem(6);
break;
case MyMoneyAccount::Liability:
typeCombo->setCurrentItem(7);
break;
case MyMoneyAccount::Stock:
m_institutionBox->hide();
typeCombo->setCurrentItem(8);
break;
/*
case MyMoneyAccount::CertificateDep:
typeCombo->setCurrentItem(5);
break;
case MyMoneyAccount::MoneyMarket:
typeCombo->setCurrentItem(7);
break;
case MyMoneyAccount::Currency:
typeCombo->setCurrentItem(8);
break;
*/
}
if(!m_account.openingDate().isValid())
m_account.setOpeningDate(QDate::currentDate());
startDateEdit->setDate(m_account.openingDate());
accountNoEdit->setText(account.number());
m_qcheckboxPreferred->setChecked(account.value("PreferredAccount") == "Yes");
m_qcheckboxNoVat->setChecked(account.value("NoVat") == "Yes");
loadKVP("iban", ibanEdit);
loadKVP("minBalanceAbsolute", m_minBalanceAbsoluteEdit);
loadKVP("minBalanceEarly", m_minBalanceEarlyEdit);
loadKVP("maxCreditAbsolute", m_maxCreditAbsoluteEdit);
loadKVP("maxCreditEarly", m_maxCreditEarlyEdit);
// reverse the sign for display purposes
if(!m_maxCreditAbsoluteEdit->lineedit()->text().isEmpty())
m_maxCreditAbsoluteEdit->setValue(m_maxCreditAbsoluteEdit->value()*MyMoneyMoney(-1,1));
if(!m_maxCreditEarlyEdit->lineedit()->text().isEmpty())
m_maxCreditEarlyEdit->setValue(m_maxCreditEarlyEdit->value()*MyMoneyMoney(-1,1));
loadKVP("lastNumberUsed", m_lastCheckNumberUsed);
// we do not allow to change the account type once an account
// was created. Same applies to currency if it is referenced.
if (m_isEditing)
{
typeCombo->setEnabled(false);
m_currency->setDisabled(MyMoneyFile::instance()->isReferenced(m_account));
}
if(m_account.isInvest()) {
typeCombo->setEnabled(false);
m_qcheckboxPreferred->hide();
m_currencyText->hide();
m_currency->hide();
} else {
// use the old field and override a possible new value
if(!MyMoneyMoney(account.value("minimumBalance")).isZero()) {
m_minBalanceAbsoluteEdit->setValue(MyMoneyMoney(account.value("minimumBalance")));
}
}
// m_qcheckboxTax->hide(); TODO should only be visible for VAT category/account
}
m_currency->setSecurity(file->currency(account.currencyId()));
// Load the institutions
// then the accounts
QString institutionName;
try
{
if (m_isEditing && !account.institutionId().isEmpty())
institutionName = file->institution(account.institutionId()).name();
else
institutionName = QString();
}
catch (MyMoneyException *e)
{
qDebug("exception in init for account dialog: %s", e->what().latin1());
delete e;
}
initParentWidget(account.parentAccountId(), account.id());
if(m_account.isInvest())
m_qlistviewParentAccounts->setEnabled(false);
if (!categoryEditor)
slotLoadInstitutions(institutionName);
accountNameEdit->setFocus();
if (title)
setCaption(title);
// load button icons
KIconLoader* il = KGlobal::iconLoader();
cancelButton->setGuiItem(KStdGuiItem::cancel());
createButton->setGuiItem(KStdGuiItem::ok());
connect(cancelButton, SIGNAL(clicked()), SLOT(reject()));
connect(createButton, SIGNAL(clicked()), this, SLOT(okClicked()));
connect(m_qlistviewParentAccounts, SIGNAL(selectionChanged(QListViewItem*)),
this, SLOT(slotSelectionChanged(QListViewItem*)));
connect(m_qbuttonNew, SIGNAL(clicked()), this, SLOT(slotNewClicked()));
connect(typeCombo, SIGNAL(activated(const QString&)),
this, SLOT(slotAccountTypeChanged(const QString&)));
connect(accountNameEdit, SIGNAL(textChanged(const QString&)), this, SLOT(slotCheckFinished()));
connect(m_vatCategory, SIGNAL(toggled(bool)), this, SLOT(slotVatChanged(bool)));
connect(m_vatAssignment, SIGNAL(toggled(bool)), this, SLOT(slotVatAssignmentChanged(bool)));
connect(m_vatCategory, SIGNAL(toggled(bool)), this, SLOT(slotCheckFinished()));
connect(m_vatAssignment, SIGNAL(toggled(bool)), this, SLOT(slotCheckFinished()));
connect(m_vatRate, SIGNAL(textChanged(const QString&)), this, SLOT(slotCheckFinished()));
connect(m_vatAccount, SIGNAL(stateChanged()), this, SLOT(slotCheckFinished()));
connect(m_minBalanceEarlyEdit, SIGNAL(valueChanged(const QString&)), this, SLOT(slotAdjustMinBalanceAbsoluteEdit(const QString&)));
connect(m_minBalanceAbsoluteEdit, SIGNAL(valueChanged(const QString&)), this, SLOT(slotAdjustMinBalanceEarlyEdit(const QString&)));
connect(m_maxCreditEarlyEdit, SIGNAL(valueChanged(const QString&)), this, SLOT(slotAdjustMaxCreditAbsoluteEdit(const QString&)));
connect(m_maxCreditAbsoluteEdit, SIGNAL(valueChanged(const QString&)), this, SLOT(slotAdjustMaxCreditEarlyEdit(const QString&)));
connect(m_qcomboboxInstitutions, SIGNAL(activated(const QString&)), this, SLOT(slotLoadInstitutions(const QString&)));
m_vatCategory->setChecked(false);
m_vatAssignment->setChecked(false);
// make sure our account does not have an id and no parent assigned
// and certainly no children in case we create a new account
if(!m_isEditing) {
m_account.clearId();
m_account.setParentAccountId(QString());
QStringList::ConstIterator it;
while((it = m_account.accountList().begin()) != m_account.accountList().end())
m_account.removeAccountId(*it);
if(m_parentItem == 0) {
// force loading of initial parent
m_account.setAccountType(MyMoneyAccount::UnknownAccountType);
MyMoneyAccount::_accountTypeE type = account.accountType();
if(type == MyMoneyAccount::UnknownAccountType)
type = MyMoneyAccount::Checkings;
slotAccountTypeChanged(KMyMoneyUtils::accountTypeToString(type));
}
} else {
if(!m_account.value("VatRate").isEmpty()) {
m_vatCategory->setChecked(true);
m_vatRate->setValue(MyMoneyMoney(m_account.value("VatRate"))*MyMoneyMoney(100,1));
} else {
if(!m_account.value("VatAccount").isEmpty()) {
QString accId = m_account.value("VatAccount").latin1();
try {
// make sure account exists
MyMoneyFile::instance()->account(accId);
m_vatAssignment->setChecked(true);
m_vatAccount->setSelected(accId);
m_grossAmount->setChecked(true);
if(m_account.value("VatAmount") == "Net")
m_netAmount->setChecked(true);
} catch(MyMoneyException *e) {
delete e;
}
}
}
}
slotVatChanged(m_vatCategory->isChecked());
slotVatAssignmentChanged(m_vatAssignment->isChecked());
slotCheckFinished();
kMandatoryFieldGroup* requiredFields = new kMandatoryFieldGroup (this);
requiredFields->setOkButton(createButton); // button to be enabled when all fields present
requiredFields->add(accountNameEdit);
// using a timeout is the only way, I got the 'ensureItemVisible'
// working when creating the dialog. I assume, this
// has something to do with the delayed update of the display somehow.
QTimer::singleShot(50, this, SLOT(timerDone()));
}
void KNewAccountDlg::timerDone(void)
{
if(m_accountItem) m_qlistviewParentAccounts->ensureItemVisible(m_accountItem);
if(m_parentItem) m_qlistviewParentAccounts->ensureItemVisible(m_parentItem);
// KNewAccountDlgDecl::resizeEvent(0);
m_qlistviewParentAccounts->setColumnWidth(m_qlistviewParentAccounts->nameColumn(), m_qlistviewParentAccounts->visibleWidth());
m_qlistviewParentAccounts->repaintContents(false);
}
void KNewAccountDlg::setOpeningBalance(const MyMoneyMoney& balance)
{
m_openingBalanceEdit->setValue(balance);
}
void KNewAccountDlg::setOpeningBalanceShown(bool shown)
{
m_openingBalanceEdit->setShown(shown);
}
void KNewAccountDlg::okClicked()
{
MyMoneyFile* file = MyMoneyFile::instance();
QString accountNameText = accountNameEdit->text();
if (accountNameText.isEmpty())
{
KMessageBox::error(this, i18n("You have not specified a name.\nPlease fill in this field."));
accountNameEdit->setFocus();
return;
}
MyMoneyAccount parent = parentAccount();
if (parent.name().length() == 0)
{
KMessageBox::error(this, i18n("Please select a parent account."));
return;
}
if (!m_categoryEditor)
{
QString institutionNameText = m_qcomboboxInstitutions->currentText();
if (institutionNameText != i18n("<No Institution>"))
{
try
{
MyMoneyFile *file = MyMoneyFile::instance();
QValueList<MyMoneyInstitution> list = file->institutionList();
QValueList<MyMoneyInstitution>::ConstIterator institutionIterator;
for (institutionIterator = list.begin(); institutionIterator != list.end(); ++institutionIterator)
{
if ((*institutionIterator).name() == institutionNameText)
m_account.setInstitutionId((*institutionIterator).id());
}
}
catch (MyMoneyException *e)
{
qDebug("Exception in account institution set: %s", e->what().latin1());
delete e;
}
}
else
{
m_account.setInstitutionId(QString());
}
}
m_account.setName(accountNameText);
m_account.setNumber(accountNoEdit->text());
storeKVP("iban", ibanEdit);
storeKVP("minBalanceAbsolute", m_minBalanceAbsoluteEdit);
storeKVP("minBalanceEarly", m_minBalanceEarlyEdit);
// the figures for credit line with reversed sign
if(!m_maxCreditAbsoluteEdit->lineedit()->text().isEmpty())
m_maxCreditAbsoluteEdit->setValue(m_maxCreditAbsoluteEdit->value()*MyMoneyMoney(-1,1));
if(!m_maxCreditEarlyEdit->lineedit()->text().isEmpty())
m_maxCreditEarlyEdit->setValue(m_maxCreditEarlyEdit->value()*MyMoneyMoney(-1,1));
storeKVP("maxCreditAbsolute", m_maxCreditAbsoluteEdit);
storeKVP("maxCreditEarly", m_maxCreditEarlyEdit);
if(!m_maxCreditAbsoluteEdit->lineedit()->text().isEmpty())
m_maxCreditAbsoluteEdit->setValue(m_maxCreditAbsoluteEdit->value()*MyMoneyMoney(-1,1));
if(!m_maxCreditEarlyEdit->lineedit()->text().isEmpty())
m_maxCreditEarlyEdit->setValue(m_maxCreditEarlyEdit->value()*MyMoneyMoney(-1,1));
storeKVP("lastNumberUsed", m_lastCheckNumberUsed);
// delete a previous version of the minimumbalance information
storeKVP("minimumBalance", QString(), QString());
MyMoneyAccount::accountTypeE acctype;
if (!m_categoryEditor)
{
acctype = KMyMoneyUtils::stringToAccountType(typeCombo->currentText());
// If it's a loan, check if the parent is asset or liability. In
// case of asset, we change the account type to be AssetLoan
if(acctype == MyMoneyAccount::Loan
&& parent.accountGroup() == MyMoneyAccount::Asset)
acctype = MyMoneyAccount::AssetLoan;
#if 0
// we do allow the same name for different accounts, so
// we don't need this check anymore.
if(!file->nameToAccount(accountNameText).isEmpty()
&& (file->nameToAccount(accountNameText) != m_account.id())) {
KMessageBox::error(this, QString("<qt>")+i18n("An account named <b>%1</b> already exists. You cannot create a second account with the same name.").arg(accountNameText)+QString("</qt>"));
return;
}
#endif
}
else
{
acctype = parent.accountGroup();
QString newName;
if(!MyMoneyFile::instance()->isStandardAccount(parent.id())) {
newName = MyMoneyFile::instance()->accountToCategory(parent.id()) + MyMoneyFile::AccountSeperator;
}
newName += accountNameText;
if(!file->categoryToAccount(newName, acctype).isEmpty()
&& (file->categoryToAccount(newName, acctype) != m_account.id())) {
KMessageBox::error(this, QString("<qt>")+i18n("A category named <b>%1</b> already exists. You cannot create a second category with the same name.").arg(newName)+QString("</qt>"));
return;
}
}
m_account.setAccountType(acctype);
m_account.setDescription(descriptionEdit->text());
if (!m_categoryEditor)
{
m_account.setOpeningDate(startDateEdit->date());
m_account.setCurrencyId(m_currency->security().id());
if(m_qcheckboxPreferred->isChecked())
m_account.setValue("PreferredAccount", "Yes");
else
m_account.deletePair("PreferredAccount");
if(m_qcheckboxNoVat->isChecked())
m_account.setValue("NoVat", "Yes");
else
m_account.deletePair("NoVat");
if(m_minBalanceAbsoluteEdit->isVisible()) {
m_account.setValue("minimumBalance", m_minBalanceAbsoluteEdit->value().toString());
}
}
else
{
if(KMyMoneyGlobalSettings::hideUnusedCategory() && !m_isEditing) {
KMessageBox::information(this, i18n("You have selected to suppress the display of unused categories in the KMyMoney configuration dialog. The category you just created will therefore only be shown if it is used. Otherwise, it will be hidden in the accounts/categories view."), i18n("Hidden categories"), "NewHiddenCategory");
}
}
if ( m_qcheckboxTax->isChecked())
m_account.setValue("Tax", "Yes");
else
m_account.deletePair("Tax");
m_account.deletePair("VatAccount");
m_account.deletePair("VatAmount");
m_account.deletePair("VatRate");
if(m_vatCategory->isChecked()) {
m_account.setValue("VatRate", (m_vatRate->value().abs() / MyMoneyMoney(100,1)).toString());
} else {
if(m_vatAssignment->isChecked()) {
m_account.setValue("VatAccount", m_vatAccount->selectedItems().first());
if(m_netAmount->isChecked())
m_account.setValue("VatAmount", "Net");
}
}
accept();
}
void KNewAccountDlg::loadKVP(const QString& key, kMyMoneyEdit* widget)
{
if(!widget)
return;
if(m_account.value(key).isEmpty()) {
widget->clearText();
} else {
widget->setValue(MyMoneyMoney(m_account.value(key)));
}
}
void KNewAccountDlg::loadKVP(const QString& key, KLineEdit* widget)
{
if(!widget)
return;
widget->setText(m_account.value(key));
}
void KNewAccountDlg::storeKVP(const QString& key, const QString& text, const QString& value)
{
if(text.isEmpty())
m_account.deletePair(key);
else
m_account.setValue(key, value);
}
void KNewAccountDlg::storeKVP(const QString& key, kMyMoneyEdit* widget)
{
storeKVP(key, widget->lineedit()->text(), widget->text());
}
void KNewAccountDlg::storeKVP(const QString& key, KLineEdit* widget)
{
storeKVP(key, widget->text(), widget->text());
}
const MyMoneyAccount& KNewAccountDlg::account(void)
{
// assign the right currency to the account
m_account.setCurrencyId(m_currency->security().id());
// and the price mode
switch(m_priceMode->currentItem()) {
case 0:
m_account.deletePair("priceMode");
break;
case 1:
case 2:
m_account.setValue("priceMode", QString("%1").arg(m_priceMode->currentItem()));
break;
}
return m_account;
}
const MyMoneyAccount& KNewAccountDlg::parentAccount(void)
{
if (!m_bSelectedParentAccount)
{
MyMoneyFile *file = MyMoneyFile::instance();
switch (m_account.accountGroup())
{
case MyMoneyAccount::Asset:
m_parentAccount = file->asset();
break;
case MyMoneyAccount::Liability:
m_parentAccount = file->liability();
break;
case MyMoneyAccount::Income:
m_parentAccount = file->income();
break;
case MyMoneyAccount::Expense:
m_parentAccount = file->expense();
break;
case MyMoneyAccount::Equity:
m_parentAccount = file->equity();
break;
default:
qDebug("Seems we have an account that hasn't been mapped to the top five");
if(m_categoryEditor)
m_parentAccount = file->income();
else
m_parentAccount = file->asset();
}
}
return m_parentAccount;
}
void KNewAccountDlg::initParentWidget(QString parentId, const QString& accountId)
{
MyMoneyFile *file = MyMoneyFile::instance();
MyMoneyAccount liabilityAccount = file->liability();
MyMoneyAccount assetAccount = file->asset();
MyMoneyAccount expenseAccount = file->expense();
MyMoneyAccount incomeAccount = file->income();
MyMoneyAccount equityAccount = file->equity();
m_parentItem = 0;
m_accountItem = 0;
// Determine the parent account
try
{
m_parentAccount = file->account(parentId);
}
catch (MyMoneyException *e)
{
m_bSelectedParentAccount = false;
m_parentAccount = MyMoneyAccount();
if(m_account.accountType() != MyMoneyAccount::UnknownAccountType) {
parentAccount();
parentId = m_parentAccount.id();
}
delete e;
}
m_bSelectedParentAccount = true;
// extract the account type from the combo box
MyMoneyAccount::accountTypeE type;
MyMoneyAccount::accountTypeE groupType;
type = KMyMoneyUtils::stringToAccountType(typeCombo->currentText());
groupType = MyMoneyAccount::accountGroup(type);
m_qlistviewParentAccounts->clear();
// Now scan all 4 account roots to load the list and mark the parent
try
{
if (!m_categoryEditor)
{
if(groupType == MyMoneyAccount::Asset || type == MyMoneyAccount::Loan) {
// Asset
KMyMoneyAccountTreeBaseItem *assetTopLevelAccount = new KMyMoneyAccountTreeItem(m_qlistviewParentAccounts, assetAccount);
if(m_parentAccount.id().isEmpty()) {
m_parentAccount = assetAccount;
parentId = m_parentAccount.id();
}
if (parentId == assetAccount.id())
m_parentItem = assetTopLevelAccount;
assetTopLevelAccount->setOpen(true);
for ( QStringList::ConstIterator it = assetAccount.accountList().begin();
it != assetAccount.accountList().end();
++it )
{
MyMoneyAccount acc = file->account(*it);
if(acc.isClosed())
continue;
KMyMoneyAccountTreeBaseItem *accountItem = new KMyMoneyAccountTreeItem(assetTopLevelAccount, acc);
if(parentId == acc.id()) {
m_parentItem = accountItem;
} else if(accountId == acc.id()) {
if(m_isEditing)
accountItem->setSelectable(false);
m_accountItem = accountItem;
}
QStringList subAccounts = acc.accountList();
if (subAccounts.count() >= 1)
{
showSubAccounts(subAccounts, accountItem, parentId, acc.id());
}
}
}
if(groupType == MyMoneyAccount::Liability) {
// Liability
KMyMoneyAccountTreeBaseItem *liabilityTopLevelAccount = new KMyMoneyAccountTreeItem(m_qlistviewParentAccounts, liabilityAccount);
if(m_parentAccount.id().isEmpty()) {
m_parentAccount = liabilityAccount;
parentId = m_parentAccount.id();
}
if (parentId == liabilityAccount.id())
m_parentItem = liabilityTopLevelAccount;
liabilityTopLevelAccount->setOpen(true);
for ( QStringList::ConstIterator it = liabilityAccount.accountList().begin();
it != liabilityAccount.accountList().end();
++it )
{
MyMoneyAccount acc = file->account(*it);
if(acc.isClosed())
continue;
KMyMoneyAccountTreeBaseItem *accountItem = new KMyMoneyAccountTreeItem(liabilityTopLevelAccount, acc);
if(parentId == acc.id()) {
m_parentItem = accountItem;
} else if(accountId == acc.id()) {
if(m_isEditing)
accountItem->setSelectable(false);
m_accountItem = accountItem;
}
QStringList subAccounts = acc.accountList();
if (subAccounts.count() >= 1)
{
showSubAccounts(subAccounts, accountItem, parentId, acc.id());
}
}
}
}
else
{
if(groupType == MyMoneyAccount::Income) {
// Income
KMyMoneyAccountTreeBaseItem *incomeTopLevelAccount = new KMyMoneyAccountTreeItem(m_qlistviewParentAccounts,
incomeAccount);
if(m_parentAccount.id().isEmpty()) {
m_parentAccount = incomeAccount;
parentId = m_parentAccount.id();
}
if (parentId == incomeAccount.id())
m_parentItem = incomeTopLevelAccount;
incomeTopLevelAccount->setOpen(true);
for ( QStringList::ConstIterator it = incomeAccount.accountList().begin();
it != incomeAccount.accountList().end();
++it )
{
KMyMoneyAccountTreeBaseItem *accountItem = new KMyMoneyAccountTreeItem(incomeTopLevelAccount,
file->account(*it));
QString id = file->account(*it).id();
if(parentId == id) {
m_parentItem = accountItem;
} else if(accountId == id) {
if(m_isEditing)
accountItem->setSelectable(false);
m_accountItem = accountItem;
}
QStringList subAccounts = file->account(*it).accountList();
if (subAccounts.count() >= 1)
{
showSubAccounts(subAccounts, accountItem, parentId, accountId);
}
}
}
if(groupType == MyMoneyAccount::Expense) {
// Expense
KMyMoneyAccountTreeBaseItem *expenseTopLevelAccount = new KMyMoneyAccountTreeItem(m_qlistviewParentAccounts,
expenseAccount);
if(m_parentAccount.id().isEmpty()) {
m_parentAccount = expenseAccount;
parentId = m_parentAccount.id();
}
if (parentId == expenseAccount.id())
m_parentItem = expenseTopLevelAccount;
expenseTopLevelAccount->setOpen(true);
for ( QStringList::ConstIterator it = expenseAccount.accountList().begin();
it != expenseAccount.accountList().end();
++it )
{
KMyMoneyAccountTreeBaseItem *accountItem = new KMyMoneyAccountTreeItem(expenseTopLevelAccount,
file->account(*it));
QString id = file->account(*it).id();
if(parentId == id) {
m_parentItem = accountItem;
} else if(accountId == id) {
if(m_isEditing)
accountItem->setSelectable(false);
m_accountItem = accountItem;
}
QStringList subAccounts = file->account(*it).accountList();
if (subAccounts.count() >= 1)
{
showSubAccounts(subAccounts, accountItem, parentId, accountId);
}
}
}
}
}
catch (MyMoneyException *e)
{
qDebug("Exception in assets account refresh: %s", e->what().latin1());
delete e;
}
m_qlistviewParentAccounts->setColumnWidth(0, m_qlistviewParentAccounts->width());
if (m_parentItem)
{
m_subAccountLabel->setText(i18n("Is a sub account of %1").arg(m_parentAccount.name()));
m_parentItem->setOpen(true);
m_qlistviewParentAccounts->setSelected(m_parentItem, true);
}
m_qlistviewParentAccounts->setEnabled(true);
}
void KNewAccountDlg::showSubAccounts(QStringList accounts, KMyMoneyAccountTreeBaseItem *parentItem,
const QString& parentId, const QString& accountId)
{
MyMoneyFile *file = MyMoneyFile::instance();
for ( QStringList::ConstIterator it = accounts.begin(); it != accounts.end(); ++it )
{
KMyMoneyAccountTreeBaseItem *accountItem = new KMyMoneyAccountTreeItem(parentItem,
file->account(*it));
QString id = file->account(*it).id();
if(parentId == id) {
m_parentItem = accountItem;
} else if(accountId == id) {
if(m_isEditing)
accountItem->setSelectable(false);
m_accountItem = accountItem;
}
QStringList subAccounts = file->account(*it).accountList();
if (subAccounts.count() >= 1)
{
showSubAccounts(subAccounts, accountItem, parentId, accountId);
}
}
}
void KNewAccountDlg::resizeEvent(QResizeEvent* e)
{
m_qlistviewParentAccounts->setColumnWidth(0, m_qlistviewParentAccounts->width());
// call base class resizeEvent()
KNewAccountDlgDecl::resizeEvent(e);
}
void KNewAccountDlg::slotSelectionChanged(QListViewItem *item)
{
KMyMoneyAccountTreeBaseItem *accountItem = dynamic_cast<KMyMoneyAccountTreeBaseItem*>(item);
try
{
MyMoneyFile *file = MyMoneyFile::instance();
//qDebug("Selected account id: %s", accountItem->accountID().data());
m_parentAccount = file->account(accountItem->id());
m_subAccountLabel->setText(i18n("Is a sub account of %1").arg(m_parentAccount.name()));
if(m_qlistviewParentAccounts->isEnabled()) {
m_bSelectedParentAccount = true;
}
}
catch (MyMoneyException *e)
{
qDebug("This shouldn't happen! : %s", e->what().latin1());
delete e;
}
}
void KNewAccountDlg::loadVatAccounts(void)
{
QValueList<MyMoneyAccount> list;
MyMoneyFile::instance()->accountList(list);
QValueList<MyMoneyAccount>::Iterator it;
QStringList loadListExpense;
QStringList loadListIncome;
QStringList loadListAsset;
QStringList loadListLiability;
for(it = list.begin(); it != list.end(); ++it) {
if(!(*it).value("VatRate").isEmpty()) {
if((*it).accountType() == MyMoneyAccount::Expense)
loadListExpense += (*it).id();
else if((*it).accountType() == MyMoneyAccount::Income)
loadListIncome += (*it).id();
else if((*it).accountType() == MyMoneyAccount::Asset)
loadListAsset += (*it).id();
else if((*it).accountType() == MyMoneyAccount::Liability)
loadListLiability += (*it).id();
}
}
AccountSet vatSet;
if(!loadListAsset.isEmpty())
vatSet.load(m_vatAccount, i18n("Asset"), loadListAsset, true);
if(!loadListLiability.isEmpty())
vatSet.load(m_vatAccount, i18n("Liability"), loadListLiability, false);
if(!loadListIncome.isEmpty())
vatSet.load(m_vatAccount, i18n("Income"), loadListIncome, false);
if(!loadListExpense.isEmpty())
vatSet.load(m_vatAccount, i18n("Expense"), loadListExpense, false);
}
void KNewAccountDlg::slotLoadInstitutions(const QString& name)
{
int id=-1, counter=0;
m_qcomboboxInstitutions->clear();
QString bic;
// Are we forcing the user to use institutions?
m_qcomboboxInstitutions->insertItem(i18n("<No Institution>"));
m_bicValue->setText(" ");
ibanEdit->setEnabled(false);
accountNoEdit->setEnabled(false);
try
{
MyMoneyFile *file = MyMoneyFile::instance();
QValueList<MyMoneyInstitution> list = file->institutionList();
QValueList<MyMoneyInstitution>::ConstIterator institutionIterator;
for (institutionIterator = list.begin(), counter=1; institutionIterator != list.end(); ++institutionIterator, counter++)
{
if ((*institutionIterator).name() == name) {
id = counter;
ibanEdit->setEnabled(true);
accountNoEdit->setEnabled(true);
m_bicValue->setText((*institutionIterator).value("bic"));
}
m_qcomboboxInstitutions->insertItem((*institutionIterator).name());
}
if (id != -1)
{
m_qcomboboxInstitutions->setCurrentItem(id);
}
}
catch (MyMoneyException *e)
{
qDebug("Exception in institution load: %s", e->what().latin1());
delete e;
}
}
void KNewAccountDlg::slotNewClicked()
{
MyMoneyInstitution institution;
KNewBankDlg dlg(institution, this);
if (dlg.exec())
{
MyMoneyFileTransaction ft;
try
{
MyMoneyFile *file = MyMoneyFile::instance();
institution = dlg.institution();
file->addInstitution(institution);
ft.commit();
slotLoadInstitutions(institution.name());
}
catch (MyMoneyException *e)
{
delete e;
KMessageBox::information(this, i18n("Cannot add institution"));
}
}
}
void KNewAccountDlg::slotAccountTypeChanged(const QString& typeStr)
{
MyMoneyAccount::accountTypeE type;
MyMoneyAccount::accountTypeE oldType;
MyMoneyFile* file = MyMoneyFile::instance();
type = KMyMoneyUtils::stringToAccountType(typeStr);
try {
oldType = m_account.accountType();
if(oldType != type) {
QString parentId;
switch(MyMoneyAccount::accountGroup(type)) {
case MyMoneyAccount::Asset:
parentId = file->asset().id();
break;
case MyMoneyAccount::Liability:
parentId = file->liability().id();
break;
case MyMoneyAccount::Expense:
parentId = file->expense().id();
break;
case MyMoneyAccount::Income:
parentId = file->income().id();
break;
default:
qWarning("Unknown account group in KNewAccountDlg::slotAccountTypeChanged()");
break;
}
initParentWidget(parentId, QString());
m_account.setAccountType(type);
}
} catch(MyMoneyException *e) {
delete e;
qWarning("Unexpected exception in KNewAccountDlg::slotAccountTypeChanged()");
}
}
void KNewAccountDlg::slotCheckFinished(void)
{
bool showButton = true;
if(accountNameEdit->text().length() == 0) {
showButton = false;
}
if(m_vatCategory->isChecked() && m_vatRate->value() <= MyMoneyMoney(0)) {
showButton = false;
} else {
if(m_vatAssignment->isChecked() && m_vatAccount->selectedItems().isEmpty())
showButton = false;
}
createButton->setEnabled(showButton);
}
void KNewAccountDlg::slotVatChanged(bool state)
{
if(state) {
m_vatCategoryFrame->show();
m_vatAssignmentFrame->hide();
} else {
m_vatCategoryFrame->hide();
if(!m_account.isAssetLiability()) {
m_vatAssignmentFrame->show();
}
}
}
void KNewAccountDlg::slotVatAssignmentChanged(bool state)
{
m_vatAccount->setEnabled(state);
m_amountGroup->setEnabled(state);
}
void KNewAccountDlg::adjustEditWidgets(kMyMoneyEdit* dst, kMyMoneyEdit* src, char mode, int corr)
{
MyMoneyMoney factor(corr, 1);
if(m_account.accountGroup() == MyMoneyAccount::Asset)
factor = -factor;
switch(mode) {
case '<':
if(src->value()*factor < dst->value()*factor)
dst->setValue(src->value());
break;
case '>':
if(src->value()*factor > dst->value()*factor)
dst->setValue(src->value());
break;
}
}
void KNewAccountDlg::slotAdjustMinBalanceAbsoluteEdit(const QString&)
{
adjustEditWidgets(m_minBalanceAbsoluteEdit, m_minBalanceEarlyEdit, '<', -1);
}
void KNewAccountDlg::slotAdjustMinBalanceEarlyEdit(const QString&)
{
adjustEditWidgets(m_minBalanceEarlyEdit, m_minBalanceAbsoluteEdit, '>', -1);
}
void KNewAccountDlg::slotAdjustMaxCreditAbsoluteEdit(const QString&)
{
adjustEditWidgets(m_maxCreditAbsoluteEdit, m_maxCreditEarlyEdit, '>', 1);
}
void KNewAccountDlg::slotAdjustMaxCreditEarlyEdit(const QString&)
{
adjustEditWidgets(m_maxCreditEarlyEdit, m_maxCreditAbsoluteEdit, '<', 1);
}
void KNewAccountDlg::addTab(QWidget* w, const QString& name)
{
if(w) {
w->reparent(m_tab, QPoint(0,0));
m_tab->addTab(w, name);
}
}
#include "knewaccountdlg.moc"
// vim:cin:si:ai:et:ts=2:sw=2:
|