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
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
|
/***************************************************************************
transactioneditor.cpp
----------
begin : Wed Jun 07 2006
copyright : (C) 2006 by Thomas Baumgart
email : Thomas Baumgart <[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. *
* *
***************************************************************************/
// ----------------------------------------------------------------------------
// QT Includes
#include <tqlabel.h>
#include <tqapplication.h>
#include <tqeventloop.h>
#include <tqradiobutton.h>
#include <tqbuttongroup.h>
#include <tqtooltip.h>
// ----------------------------------------------------------------------------
// KDE Includes
#include <kglobal.h>
#include <ktextedit.h>
#include <klocale.h>
#include <kcombobox.h>
#include <kpushbutton.h>
#include <kmessagebox.h>
#include <kstdguiitem.h>
#include <kiconloader.h>
#include <kguiitem.h>
// ----------------------------------------------------------------------------
// Project Includes
#include <kmymoney/transactioneditor.h>
#include <kmymoney/kmymoneycategory.h>
#include <kmymoney/kmymoneydateinput.h>
#include <kmymoney/kmymoneyedit.h>
#include <kmymoney/kmymoneylineedit.h>
#include <kmymoney/kmymoneyaccountcompletion.h>
#include <kmymoney/kmymoneyaccountselector.h>
#include <kmymoney/mymoneyfile.h>
#include <kmymoney/kmymoneyutils.h>
#include <kmymoney/transactionform.h>
#include <kmymoney/kmymoneyglobalsettings.h>
#include "../dialogs/ksplittransactiondlg.h"
#include "../dialogs/kcurrencycalculator.h"
#include "../dialogs/kselecttransactionsdlg.h"
using namespace KMyMoneyRegister;
using namespace KMyMoneyTransactionForm;
TransactionEditor::TransactionEditor(TransactionEditorContainer* regForm, KMyMoneyRegister::Transaction* item, const KMyMoneyRegister::SelectedTransactions& list, const TQDate& lastPostDate) :
m_transactions(list),
m_regForm(regForm),
m_item(item),
m_transaction(item->transaction()),
m_split(item->split()),
m_lastPostDate(lastPostDate),
m_openEditSplits(false)
{
m_item->startEditMode();
connect(MyMoneyFile::instance(), TQT_SIGNAL(dataChanged()), this, TQT_SLOT(slotUpdateAccount()));
}
TransactionEditor::~TransactionEditor()
{
// Make sure the widgets do not send out signals to the editor anymore
// After all, the editor is about to die
TQMap<TQString, TQWidget*>::iterator it_w;
for(it_w = m_editWidgets.begin(); it_w != m_editWidgets.end(); ++it_w) {
(*it_w)->disconnect(this);
}
m_regForm->removeEditWidgets(m_editWidgets);
m_item->leaveEditMode();
emit finishEdit(m_transactions);
}
void TransactionEditor::slotUpdateAccount(const TQString& id)
{
m_account = MyMoneyFile::instance()->account(id);
setupPrecision();
}
void TransactionEditor::slotUpdateAccount(void)
{
// reload m_account as it might have been changed
m_account = MyMoneyFile::instance()->account(m_account.id());
setupPrecision();
}
void TransactionEditor::setupPrecision(void)
{
const int prec = (m_account.id().isEmpty()) ? 2 : MyMoneyMoney::denomToPrec(m_account.fraction());
TQStringList widgets = TQStringList::split(",", "amount,deposit,payment");
TQStringList::const_iterator it_w;
for(it_w = widgets.begin(); it_w != widgets.end(); ++it_w) {
TQWidget * w;
if((w = haveWidget(*it_w)) != 0) {
dynamic_cast<kMyMoneyEdit*>(w)->setPrecision(prec);
}
}
}
void TransactionEditor::setup(TQWidgetList& tabOrderWidgets, const MyMoneyAccount& account, KMyMoneyRegister::Action action)
{
m_account = account;
m_initialAction = action;
createEditWidgets();
m_regForm->arrangeEditWidgets(m_editWidgets, m_item);
m_regForm->tabOrder(tabOrderWidgets, m_item);
TQWidget* w = haveWidget("tabbar");
if(w)
tabOrderWidgets.append(w);
loadEditWidgets(action);
m_editWidgets.removeOrphans();
clearFinalWidgets();
setupFinalWidgets();
slotUpdateButtonState();
}
void TransactionEditor::clearFinalWidgets(void)
{
m_finalEditWidgets.clear();
}
void TransactionEditor::addFinalWidget(const TQWidget* w)
{
if(w) {
m_finalEditWidgets << w;
}
}
void TransactionEditor::slotReloadEditWidgets(void)
{
}
bool TransactionEditor::eventFilter(TQObject* o, TQEvent* e)
{
bool rc = false;
if(TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(haveWidget("number"))) {
if(e->type() == TQEvent::MouseButtonDblClick) {
emit assignNumber();
rc = true;
}
}
// if the object is a widget, the event is a key press event and
// the object is one of our edit widgets, then ....
if(o->isWidgetType()
&& (e->type() == TQEvent::KeyPress)
&& m_editWidgets.values().contains(dynamic_cast<TQWidget*>(o))) {
TQKeyEvent* k = dynamic_cast<TQKeyEvent*>(e);
if((k->state() & TQt::KeyButtonMask) == 0) {
bool isFinal = false;
TQValueList<const TQWidget*>::const_iterator it_w;
switch(k->key()) {
case TQt::Key_Return:
case TQt::Key_Enter:
// we check, if the object is one of the m_finalEditWidgets and if it's
// a kMyMoneyEdit object that the value is not 0. If any of that is the
// case, it's the final object. In other cases, we convert the enter
// key into a TAB key to move between the fields. Of course, we only need
// to do this as long as the appropriate option is set. In all other cases,
// we treat the return/enter key as such.
if(KMyMoneyGlobalSettings::enterMovesBetweenFields()) {
for(it_w = m_finalEditWidgets.begin(); !isFinal && it_w != m_finalEditWidgets.end(); ++it_w) {
if(TQT_BASE_OBJECT_CONST(*it_w) == TQT_BASE_OBJECT(o)) {
if(dynamic_cast<const kMyMoneyEdit*>(*it_w)) {
isFinal = !(dynamic_cast<const kMyMoneyEdit*>(*it_w)->value().isZero());
} else
isFinal = true;
}
}
} else
isFinal = true;
// for the non-final objects, we treat the return key as a TAB
if(!isFinal) {
TQKeyEvent evt(e->type(),
Key_Tab, 0, k->state(), TQString(),
k->isAutoRepeat(), k->count());
TQApplication::sendEvent( o, &evt );
// in case of a category item and the split button is visible
// send a second event so that we get passed the button.
if(dynamic_cast<KMyMoneyCategory*>(o) && dynamic_cast<KMyMoneyCategory*>(o)->splitButton())
TQApplication::sendEvent( o, &evt );
} else {
TQTimer::singleShot(0, this, TQT_SIGNAL(returnPressed()));
}
// don't process any further
rc = true;
break;
case TQt::Key_Escape:
TQTimer::singleShot(0, this, TQT_SIGNAL(escapePressed()));
break;
}
}
}
return rc;
}
void TransactionEditor::slotNumberChanged(const TQString& txt)
{
kMyMoneyLineEdit* number = dynamic_cast<kMyMoneyLineEdit*>(haveWidget("number"));
if(number) {
if(MyMoneyFile::instance()->checkNoUsed(m_account.id(), txt)) {
if(KMessageBox::questionYesNo(m_regForm, TQString("<qt>")+i18n("The number <b>%1</b> has already been used in account <b>%2</b>. Do you want to replace it with the next available number?").tqarg(txt).tqarg(m_account.name())+TQString("</qt>"), i18n("Duplicate number")) == KMessageBox::Yes) {
number->loadText(KMyMoneyUtils::nextCheckNumber(m_account));
}
}
}
}
void TransactionEditor::slotUpdateButtonState(void)
{
TQString reason;
emit transactionDataSufficient(isComplete(reason));
}
TQWidget* TransactionEditor::haveWidget(const TQString& name) const
{
return m_editWidgets.haveWidget(name);
}
int TransactionEditor::slotEditSplits(void)
{
return TQDialog::Rejected;
}
#if 0
// If we deal with multiple currencies we make sure, that for
// transactions with two splits, the transaction's commodity is the
// currency of the currently selected account. This saves us from a
// lot of grieve later on.
// Editing a transaction which has more than two splits and a commodity
// that differs from the currency of the current selected account is
// not a good idea. We will warn the user and give him a hint if there
// is an account where he can perfom the edit operation much better.
if(m_transaction.commodity() != m_account.currencyId()) {
if(m_transaction.splitCount() == 2) {
// in case of two splits, it's easy. We just have to switch the
// transactions commodity. Let's assume the following scenario:
// - transactions commodity is CA
// - account's currencyId is CB
// - second split is of course in CA (otherwise we have a real problem)
// - Value is V in both splits
// - Shares in this account's split is SB
// - Shares in the other account's split is SA (and equal to V)
//
// We do the following:
// - change transactions commodity to CB
// - set V in both splits to SB
// - modify the splits in the transaction
try {
MyMoneySplit split = m_transaction.splitByAccount(m_account.id(), false);
m_transaction.setCommodity(m_account.currencyId());
m_split.setValue(m_split.shares());
split.setValue(-m_split.shares());
m_transaction.modifySplit(m_split);
m_transaction.modifySplit(split);
if(m_transactionPtr) {
KMyMoneyTransaction k(m_transaction);
k.setSplitId(m_split.id());
*m_transactionPtr = k;
}
} catch(MyMoneyException *e) {
qDebug("Unable to update commodity to second splits currency in %s: '%s'", m_transaction.id().data(), e->what().data());
delete e;
}
} else {
// Find a suitable account
MyMoneySecurity sec = MyMoneyFile::instance()->currency(m_transaction.commodity());
MyMoneyAccount acc;
for(it = m_transaction.splits().begin(); it != m_transaction.splits().end(); ++it) {
if((*it).id() == m_split.id())
continue;
acc = MyMoneyFile::instance()->account((*it).accountId());
if((acc.accountGroup() == MyMoneyAccount::Asset
|| acc.accountGroup() == MyMoneyAccount::Liability)
&& acc.accountType() != MyMoneyAccount::Stock) {
if(m_transaction.commodity() == acc.currencyId())
break;
}
acc = MyMoneyAccount();
}
TQString msg;
msg = TQString("<p>")+i18n("This transaction has more than two splits and is based on a different currency (%1). Using this account to modify the transaction is currently not very well supported by KMyMoney and may result in false results.").tqarg(sec.name())+TQString(" ");
if(acc.id().isEmpty()) {
msg += i18n("KMyMoney was not able to find a more appropriate account to edit this transaction. Nevertheless, you are allowed to modify the transaction. If you don't want to edit this transaction, please cancel from editing next.");
} else {
msg += i18n("Using e.g. <b>%1</b> to edit this transaction is a better choice. Nevertheless, you are allowed to modify the transaction. If you want to use the suggested account instead, please cancel from editing next and change the view to the suggested account.").tqarg(acc.name());
}
KMessageBox::information(0, msg);
}
}
#endif
void TransactionEditor::setTransaction(const MyMoneyTransaction& t, const MyMoneySplit& s)
{
m_transaction = t;
m_split = s;
loadEditWidgets();
}
bool TransactionEditor::fixTransactionCommodity(const MyMoneyAccount& account)
{
bool rc = true;
bool firstTimeMultiCurrency = true;
m_account = account;
MyMoneyFile* file = MyMoneyFile::instance();
// determine the max fraction for this account
MyMoneySecurity sec = file->security(m_account.currencyId());
int fract = m_account.fraction();
// scan the list of selected transactions
KMyMoneyRegister::SelectedTransactions::iterator it_t;
for(it_t = m_transactions.begin(); (rc == true) && (it_t != m_transactions.end()); ++it_t) {
// there was a time when the schedule editor did not setup the transaction commodity
// let's give a helping hand here for those old schedules
if((*it_t).transaction().commodity().isEmpty())
(*it_t).transaction().setCommodity(m_account.currencyId());
// we need to check things only if a different commodity is used
if(m_account.currencyId() != (*it_t).transaction().commodity()) {
MyMoneySecurity osec = file->security((*it_t).transaction().commodity());
switch((*it_t).transaction().splitCount()) {
case 0:
// new transaction, guess nothing's here yet ;)
break;
case 1:
try {
// make sure, that the value is equal to the shares, don't forget our own copy
MyMoneySplit& splitB = (*it_t).split(); // reference usage wanted here
if(m_split == splitB)
m_split.setValue(splitB.shares());
splitB.setValue(splitB.shares());
(*it_t).transaction().modifySplit(splitB);
} catch(MyMoneyException *e) {
qDebug("Unable to update commodity to second splits currency in %s: '%s'", (*it_t).transaction().id().data(), e->what().data());
delete e;
}
break;
case 2:
// If we deal with multiple currencies we make sure, that for
// transactions with two splits, the transaction's commodity is the
// currency of the currently selected account. This saves us from a
// lot of grieve later on. We just have to switch the
// transactions commodity. Let's assume the following scenario:
// - transactions commodity is CA
// - splitB and account's currencyId is CB
// - splitA is of course in CA (otherwise we have a real problem)
// - Value is V in both splits
// - Shares in splitB is SB
// - Shares in splitA is SA (and equal to V)
//
// We do the following:
// - change transactions commodity to CB
// - set V in both splits to SB
// - modify the splits in the transaction
try {
// retrieve the splits
MyMoneySplit& splitB = (*it_t).split(); // reference usage wanted here
MyMoneySplit splitA = (*it_t).transaction().splitByAccount(m_account.id(), false);
// - set V in both splits to SB. Don't forget our own copy
if(m_split == splitB) {
m_split.setValue(splitB.shares());
}
splitB.setValue(splitB.shares());
splitA.setValue(-splitB.shares());
(*it_t).transaction().modifySplit(splitA);
(*it_t).transaction().modifySplit(splitB);
} catch(MyMoneyException *e) {
qDebug("Unable to update commodity to second splits currency in %s: '%s'", (*it_t).transaction().id().data(), e->what().data());
delete e;
}
break;
default:
// TODO: use new logic by adjusting all splits by the price
// extracted from the selected split. Inform the user that
// this will happen and allow him to stop the processing (rc = false)
try {
TQString msg;
if(firstTimeMultiCurrency) {
firstTimeMultiCurrency = false;
if(!isMultiSelection()) {
msg = i18n("This transaction has more than two splits and is originally based on a different currency (%1). Using this account to modify the transaction may result in rounding errors. Do you want to continue?").tqarg(osec.name());
} else {
msg = i18n("At least one of the selected transactions has more than two splits and is originally based on a different currency (%1). Using this account to modify the transactions may result in rounding errors. Do you want to continue?").tqarg(osec.name());
}
if(KMessageBox::warningContinueCancel(0, TQString("<qt>%1</qt>").tqarg(msg)) == KMessageBox::Cancel) {
rc = false;
}
}
if(rc == true) {
MyMoneyMoney price;
if(!(*it_t).split().shares().isZero() && !(*it_t).split().value().isZero())
price = (*it_t).split().shares() / (*it_t).split().value();
TQValueList<MyMoneySplit>::iterator it_s;
MyMoneySplit& mySplit = (*it_t).split();
for(it_s = (*it_t).transaction().splits().begin(); it_s != (*it_t).transaction().splits().end(); ++it_s) {
MyMoneySplit s = (*it_s);
if(s == mySplit) {
s.setValue(s.shares());
if(mySplit == m_split) {
m_split = s;
}
mySplit = s;
} else {
s.setValue((s.value() * price).convert(fract));
}
(*it_t).transaction().modifySplit(s);
}
}
} catch(MyMoneyException *e) {
qDebug("Unable to update commodity of split currency in %s: '%s'", (*it_t).transaction().id().data(), e->what().data());
delete e;
}
break;
}
// set the transaction's ommodity to this account's currency
(*it_t).transaction().setCommodity(m_account.currencyId());
// update our copy of the transaction that has the focus
if((*it_t).transaction().id() == m_transaction.id()) {
m_transaction = (*it_t).transaction();
}
}
}
return rc;
}
void TransactionEditor::assignNextNumber(void)
{
if(canAssignNumber()) {
kMyMoneyLineEdit* number = dynamic_cast<kMyMoneyLineEdit*>(haveWidget("number"));
number->loadText(KMyMoneyUtils::nextCheckNumber(m_account));
}
}
bool TransactionEditor::canAssignNumber(void) const
{
kMyMoneyLineEdit* number = dynamic_cast<kMyMoneyLineEdit*>(haveWidget("number"));
return (number != 0) && (number->text().isEmpty());
}
void TransactionEditor::setupCategoryWidget(KMyMoneyCategory* category, const TQValueList<MyMoneySplit>& splits, TQString& categoryId, const char* splitEditSlot, bool /* allowObjectCreation */)
{
disconnect(category, TQT_SIGNAL(focusIn()), this, splitEditSlot);
#if 0
// FIXME must deal with the logic that suppressObjectCreation is
// automatically turned off when the createItem() signal is connected
if(allowObjectCreation)
category->setSuppressObjectCreation(false);
#endif
switch(splits.count()) {
case 0:
categoryId = TQString();
if(!category->currentText().isEmpty()) {
category->setCurrentText();
// make sure, we don't see the selector
category->completion()->hide();
}
category->completion()->setSelected(TQString());
break;
case 1:
categoryId = splits[0].accountId();
category->completion()->setSelected(categoryId);
category->slotItemSelected(categoryId);
break;
default:
categoryId = TQString();
category->setSplitTransaction();
connect(category, TQT_SIGNAL(focusIn()), this, splitEditSlot);
#if 0
// FIXME must deal with the logic that suppressObjectCreation is
// automatically turned off when the createItem() signal is connected
if(allowObjectCreation)
category->setSuppressObjectCreation(true);
#endif
break;
}
}
bool TransactionEditor::enterTransactions(TQString& newId, bool askForSchedule, bool suppressBalanceWarnings)
{
newId = TQString();
MyMoneyFile* file = MyMoneyFile::instance();
// make sure to run through all stuff that is tied to 'focusout events'.
m_regForm->parentWidget()->setFocus();
TQApplication::eventLoop()->processEvents(TQEventLoop::ExcludeUserInput, 10);
// we don't need to update our widgets anymore, so we just disconnect the signal
disconnect(file, TQT_SIGNAL(dataChanged()), this, TQT_SLOT(slotReloadEditWidgets()));
KMyMoneyRegister::SelectedTransactions::iterator it_t;
MyMoneyTransaction t;
bool newTransactionCreated = false;
// make sure, that only a single new transaction can be created.
// we need to update m_transactions to contain the new transaction
// which is then stored in the variable t when we leave the loop.
// m_transactions will be sent out in finishEdit() and forces
// the new transaction to be selected in the ledger view
// collect the transactions to be stored in the engine in a local
// list first, so that the user has a chance to interrupt the storage
// process
TQValueList<MyMoneyTransaction> list;
bool storeTransactions = true;
// collect transactions
for(it_t = m_transactions.begin(); storeTransactions && !newTransactionCreated && it_t != m_transactions.end(); ++it_t) {
storeTransactions = createTransaction(t, (*it_t).transaction(), (*it_t).split());
// if the transaction was created successfully, append it to the list
if(storeTransactions)
list.append(t);
// if we created a new transaction keep that in mind
if(t.id().isEmpty())
newTransactionCreated = true;
}
// if not interrupted by user, continue to store them in the engine
if(storeTransactions) {
int i = 0;
emit statusMsg(i18n("Storing transactions"));
emit statusProgress(0, list.count());
MyMoneyFileTransaction ft;
try {
TQValueList<MyMoneyTransaction>::iterator it_ts;
TQMap<TQString, bool> minBalanceEarly;
TQMap<TQString, bool> minBalanceAbsolute;
TQMap<TQString, bool> maxCreditEarly;
TQMap<TQString, bool> maxCreditAbsolute;
TQMap<TQString, bool> accountIds;
for(it_ts = list.begin(); it_ts != list.end(); ++it_ts) {
// if we have a categorization, make sure we remove
// the 'imported' flag automagically
if((*it_ts).splitCount() > 1)
(*it_ts).setImported(false);
// create information about min and max balances
TQValueList<MyMoneySplit>::const_iterator it_s;
for(it_s = (*it_ts).splits().begin(); it_s != (*it_ts).splits().end(); ++it_s) {
MyMoneyAccount acc = file->account((*it_s).accountId());
accountIds[acc.id()] = true;
MyMoneyMoney balance = file->balance(acc.id());
if(!acc.value("minBalanceEarly").isEmpty()) {
minBalanceEarly[acc.id()] = balance < MyMoneyMoney(acc.value("minBalanceEarly"));
}
if(!acc.value("minBalanceAbsolute").isEmpty()) {
minBalanceAbsolute[acc.id()] = balance < MyMoneyMoney(acc.value("minBalanceAbsolute"));
minBalanceEarly[acc.id()] = false;
}
if(!acc.value("maxCreditEarly").isEmpty()) {
maxCreditEarly[acc.id()] = balance < MyMoneyMoney(acc.value("maxCreditEarly"));
}
if(!acc.value("maxCreditAbsolute").isEmpty()) {
maxCreditAbsolute[acc.id()] = balance < MyMoneyMoney(acc.value("maxCreditAbsolute"));
maxCreditEarly[acc.id()] = false;
}
}
if((*it_ts).id().isEmpty()) {
bool enter = true;
if(askForSchedule && (*it_ts).postDate() > TQDate::tqcurrentDate()) {
KGuiItem enterItem;
KIconLoader* il = KGlobal::iconLoader();
KGuiItem enterButton( i18n("&Enter" ),
TQIconSet(il->loadIcon("kontact_journal", KIcon::Small, KIcon::SizeSmall)),
i18n("Accepts the entered data and stores it"),
i18n("Use this to enter the transaction into the ledger."));
KGuiItem scheduleButton( i18n("&Schedule" ),
TQIconSet(il->loadIcon("kontact_date", KIcon::Small, KIcon::SizeSmall)),
i18n("Accepts the entered data and stores it as schedule"),
i18n("Use this to schedule the transaction for later entry into the ledger."));
enter = KMessageBox::questionYesNo(m_regForm, TQString("<qt>%1</qt>").tqarg(i18n("The transaction you are about to enter has a post date in the future.<br/><br/>Do you want to enter it in the ledger or add it to the schedules?")), i18n("Dialog caption for 'Enter or schedule' dialog", "Enter or schedule?"), enterButton, scheduleButton, "EnterOrScheduleTransactionInFuture") == KMessageBox::Yes;
}
if(enter) {
// add new transaction
file->addTransaction(*it_ts);
// pass the newly assigned id on to the caller
newId = (*it_ts).id();
// refresh account and transaction object because they might have changed
m_account = file->account(m_account.id());
t = (*it_ts);
// if a new transaction has a valid number, keep it with the account
TQString number = (*it_ts).splits()[0].number();
if(!number.isEmpty()) {
m_account.setValue("lastNumberUsed", number);
file->modifyAccount(m_account);
}
} else {
// turn object creation on, so that moving the focus does
// not screw up the dialog that might be popping up
emit objectCreation(true);
emit scheduleTransaction(*it_ts, MyMoneySchedule::OCCUR_ONCE);
emit objectCreation(false);
newTransactionCreated = false;
}
// send out the post date of this transaction
emit lastPostDateUsed((*it_ts).postDate());
} else {
// modify existing transaction
file->modifyTransaction(*it_ts);
}
}
emit statusProgress(i++, 0);
// update m_transactions to contain the newly created transaction so that
// it is selected as the current one
// we need to do that before we commit the transaction to the engine
// as we need it during the update of the views that is caused by committing already.
if(newTransactionCreated) {
m_transactions.clear();
MyMoneySplit s;
// a transaction w/o a single split should not exist and adding it
// should throw an exception in MyMoneyFile::addTransaction, but we
// remain on the save side of things to check for it
if(t.splitCount() > 0)
s = t.splits()[0];
KMyMoneyRegister::SelectedTransaction st(t, s);
m_transactions.append(st);
}
ft.commit();
// now analyse the balances and spit out warnings to the user
TQMap<TQString, bool>::const_iterator it_a;
if(!suppressBalanceWarnings) {
for(it_a = accountIds.begin(); it_a != accountIds.end(); ++it_a) {
TQString msg;
MyMoneyAccount acc = file->account(it_a.key());
MyMoneyMoney balance = file->balance(acc.id());
const MyMoneySecurity& sec = file->security(acc.currencyId());
TQString key;
key = "minBalanceEarly";
if(!acc.value(key).isEmpty()) {
if(minBalanceEarly[acc.id()] == false && balance < MyMoneyMoney(acc.value(key))) {
msg = TQString("<qt>%1</qt>").tqarg(i18n("The balance of account <b>%1</b> dropped below the warning balance of %2.").tqarg(acc.name(), MyMoneyMoney(acc.value(key)).formatMoney(acc, sec)));
}
}
key = "minBalanceAbsolute";
if(!acc.value(key).isEmpty()) {
if(minBalanceAbsolute[acc.id()] == false && balance < MyMoneyMoney(acc.value(key))) {
msg = TQString("<qt>%1</qt>").tqarg(i18n("The balance of account <b>%1</b> dropped below the minimum balance of %2.").tqarg(acc.name(), MyMoneyMoney(acc.value(key)).formatMoney(acc, sec)));
}
}
key = "maxCreditEarly";
if(!acc.value(key).isEmpty()) {
if(maxCreditEarly[acc.id()] == false && balance < MyMoneyMoney(acc.value(key))) {
msg = TQString("<qt>%1</qt>").tqarg(i18n("The balance of account <b>%1</b> dropped below the maximum credit warning limit of %2.").tqarg(acc.name(), MyMoneyMoney(acc.value(key)).formatMoney(acc, sec)));
}
}
key = "maxCreditAbsolute";
if(!acc.value(key).isEmpty()) {
if(maxCreditAbsolute[acc.id()] == false && balance < MyMoneyMoney(acc.value(key))) {
msg = TQString("<qt>%1</qt>").tqarg(i18n("The balance of account <b>%1</b> dropped below the maximum credit limit of %2.").tqarg(acc.name(), MyMoneyMoney(acc.value(key)).formatMoney(acc, sec)));
}
}
if(!msg.isEmpty()) {
emit balanceWarning(m_regForm, acc, msg);
}
}
}
} catch(MyMoneyException * e) {
qDebug("Unable to store transaction within engine: %s", e->what().latin1());
delete e;
newTransactionCreated = false;
}
emit statusProgress(-1, -1);
emit statusMsg(TQString());
}
return storeTransactions;
}
StdTransactionEditor::StdTransactionEditor()
{
}
StdTransactionEditor::StdTransactionEditor(TransactionEditorContainer* regForm, KMyMoneyRegister::Transaction* item, const KMyMoneyRegister::SelectedTransactions& list, const TQDate& lastPostDate) :
TransactionEditor(regForm, item, list, lastPostDate),
m_inUpdateVat(false)
{
}
StdTransactionEditor::~StdTransactionEditor()
{
KMyMoneyTransactionForm::TransactionForm* form = dynamic_cast<KMyMoneyTransactionForm::TransactionForm*>(m_regForm);
if(form) {
form->enableTabBar(true);
}
}
bool StdTransactionEditor::eventFilter(TQObject* o, TQEvent* e)
{
bool rc = TransactionEditor::eventFilter(o, e);
#if 0
// this is sofar dead code here, as the focus out event for Comboboxes
// never comes along here. I don't know why (ipwizard - 2009-10-03)
if((e->type() == TQEvent::FocusOut)
&& (haveWidget("payee") == dynamic_cast<TQWidget*>(o))) {
// loosing the focus on the payee widget?
qDebug("Loosing focus on payee");
KMyMoneyPayeeCombo* p = dynamic_cast<KMyMoneyPayeeCombo*>(haveWidget("payee"));
if(!p->selectedItem().isEmpty())
slotUpdatePayee(p->selectedItem());
}
#endif
return rc;
}
void StdTransactionEditor::createEditWidgets(void)
{
KMyMoneyCategory* account = new KMyMoneyCategory;
account->setHint(i18n("Account"));
m_editWidgets["account"] = account;
connect(account, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(slotUpdateButtonState()));
connect(account, TQT_SIGNAL(itemSelected(const TQString&)), this, TQT_SLOT(slotUpdateAccount(const TQString&)));
KMyMoneyPayeeCombo* payee = new KMyMoneyPayeeCombo;
payee->setHint(i18n("Payer/Receiver"));
m_editWidgets["payee"] = payee;
connect(payee, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(slotUpdateButtonState()));
connect(payee, TQT_SIGNAL(createItem(const TQString&, TQString&)), this, TQT_SIGNAL(createPayee(const TQString&, TQString&)));
connect(payee, TQT_SIGNAL(objectCreation(bool)), this, TQT_SIGNAL(objectCreation(bool)));
connect(payee, TQT_SIGNAL(itemSelected(const TQString&)), this, TQT_SLOT(slotUpdatePayee(const TQString&)));
KMyMoneyCategory* category = new KMyMoneyCategory(0, 0, true);
category->setHint(i18n("Category/Account"));
m_editWidgets["category"] = category;
connect(category, TQT_SIGNAL(itemSelected(const TQString&)), this, TQT_SLOT(slotUpdateCategory(const TQString&)));
connect(category, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(slotUpdateButtonState()));
connect(category, TQT_SIGNAL(createItem(const TQString&, TQString&)), this, TQT_SLOT(slotCreateCategory(const TQString&, TQString&)));
connect(category, TQT_SIGNAL(objectCreation(bool)), this, TQT_SIGNAL(objectCreation(bool)));
connect(category->splitButton(), TQT_SIGNAL(clicked()), this, TQT_SLOT(slotEditSplits()));
category->splitButton()->setDisabled(true);
KTextEdit* memo = new KTextEdit;
memo->setTabChangesFocus(true);
m_editWidgets["memo"] = memo;
bool showNumberField = true;
switch(m_account.accountType()) {
case MyMoneyAccount::Savings:
case MyMoneyAccount::Cash:
case MyMoneyAccount::Loan:
case MyMoneyAccount::AssetLoan:
case MyMoneyAccount::Asset:
case MyMoneyAccount::Liability:
case MyMoneyAccount::Equity:
showNumberField = KMyMoneyGlobalSettings::alwaysShowNrField();
break;
case MyMoneyAccount::Income:
case MyMoneyAccount::Expense:
showNumberField = false;
break;
default:
break;
}
if(showNumberField) {
kMyMoneyLineEdit* number = new kMyMoneyLineEdit;
number->setHint(i18n("Number"));
m_editWidgets["number"] = number;
connect(number, TQT_SIGNAL(lineChanged(const TQString&)), this, TQT_SLOT(slotNumberChanged(const TQString&)));
// number->installEventFilter(this);
}
m_editWidgets["postdate"] = new kMyMoneyDateInput;
connect(m_editWidgets["postdate"], TQT_SIGNAL(dateChanged(const TQDate&)), this, TQT_SLOT(slotUpdateButtonState()));
kMyMoneyEdit* value = new kMyMoneyEdit;
m_editWidgets["amount"] = value;
value->setResetButtonVisible(false);
connect(value, TQT_SIGNAL(valueChanged(const TQString&)), this, TQT_SLOT(slotUpdateAmount(const TQString&)));
connect(value, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(slotUpdateButtonState()));
value = new kMyMoneyEdit;
m_editWidgets["payment"] = value;
value->setResetButtonVisible(false);
connect(value, TQT_SIGNAL(valueChanged(const TQString&)), this, TQT_SLOT(slotUpdatePayment(const TQString&)));
connect(value, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(slotUpdateButtonState()));
value = new kMyMoneyEdit;
m_editWidgets["deposit"] = value;
value->setResetButtonVisible(false);
connect(value, TQT_SIGNAL(valueChanged(const TQString&)), this, TQT_SLOT(slotUpdateDeposit(const TQString&)));
connect(value, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(slotUpdateButtonState()));
KMyMoneyCashFlowCombo* cashflow = new KMyMoneyCashFlowCombo(0, 0, m_account.accountGroup());
m_editWidgets["cashflow"] = cashflow;
connect(cashflow, TQT_SIGNAL(directionSelected(KMyMoneyRegister::CashFlowDirection)), this, TQT_SLOT(slotUpdateCashFlow(KMyMoneyRegister::CashFlowDirection)));
connect(cashflow, TQT_SIGNAL(directionSelected(KMyMoneyRegister::CashFlowDirection)), this, TQT_SLOT(slotUpdateButtonState()));
KMyMoneyReconcileCombo* reconcile = new KMyMoneyReconcileCombo;
m_editWidgets["status"] = reconcile;
connect(reconcile, TQT_SIGNAL(itemSelected(const TQString&)), this, TQT_SLOT(slotUpdateButtonState()));
KMyMoneyRegister::TQWidgetContainer::iterator it_w;
for(it_w = m_editWidgets.begin(); it_w != m_editWidgets.end(); ++it_w) {
(*it_w)->installEventFilter(this);
}
// if we don't have more than 1 selected transaction, we don't need
// the "don't change" item in some of the combo widgets
if(!isMultiSelection()) {
reconcile->removeDontCare();
cashflow->removeDontCare();
}
TQLabel* label;
m_editWidgets["category-label"] = label = new TQLabel(i18n("Category"), 0);
label->tqsetAlignment(TQt::AlignVCenter | TQt::DontClip);
// create a copy of tabbar above the form (if we are created for a form)
KMyMoneyTransactionForm::TransactionForm* form = dynamic_cast<KMyMoneyTransactionForm::TransactionForm*>(m_regForm);
if(form) {
form->enableTabBar(false);
KMyMoneyTransactionForm::TabBar* tabbar = new KMyMoneyTransactionForm::TabBar;
m_editWidgets["tabbar"] = tabbar;
tabbar->copyTabs(form->tabBar());
connect(tabbar, TQT_SIGNAL(tabSelected(int)), this, TQT_SLOT(slotUpdateAction(int)));
}
label = new TQLabel(i18n("Date"), 0);
label->tqsetAlignment(TQt::AlignVCenter | TQt::DontClip);
m_editWidgets["date-label"] = label;
label = new TQLabel(i18n("Number"), 0);
label->tqsetAlignment(TQt::AlignVCenter | TQt::DontClip);
m_editWidgets["number-label"] = label;
setupPrecision();
}
void StdTransactionEditor::setupCategoryWidget(TQString& categoryId)
{
TransactionEditor::setupCategoryWidget(dynamic_cast<KMyMoneyCategory*>(m_editWidgets["category"]), m_splits, categoryId, TQT_SLOT(slotEditSplits()));
if(m_splits.count() == 1)
m_shares = m_splits[0].shares();
}
bool StdTransactionEditor::isTransfer(const TQString& accId1, const TQString& accId2) const
{
if(accId1.isEmpty() || accId2.isEmpty())
return false;
return MyMoneyFile::instance()->account(accId1).isIncomeExpense() == MyMoneyFile::instance()->account(accId2).isIncomeExpense();
}
void StdTransactionEditor::loadEditWidgets(KMyMoneyRegister::Action action)
{
// don't kick off VAT processing from here
m_inUpdateVat = true;
TQMap<TQString, TQWidget*>::const_iterator it_w;
TQWidget* w;
AccountSet aSet;
// load the account widget
KMyMoneyCategory* account = dynamic_cast<KMyMoneyCategory*>(haveWidget("account"));
if(account) {
aSet.addAccountGroup(MyMoneyAccount::Asset);
aSet.addAccountGroup(MyMoneyAccount::Liability);
aSet.removeAccountType(MyMoneyAccount::AssetLoan);
aSet.removeAccountType(MyMoneyAccount::CertificateDep);
aSet.removeAccountType(MyMoneyAccount::Investment);
aSet.removeAccountType(MyMoneyAccount::Stock);
aSet.removeAccountType(MyMoneyAccount::MoneyMarket);
aSet.removeAccountType(MyMoneyAccount::Loan);
aSet.load(account->selector());
account->completion()->setSelected(m_account.id());
account->slotItemSelected(m_account.id());
}
// load the payee widget
KMyMoneyPayeeCombo* payee = dynamic_cast<KMyMoneyPayeeCombo*>(m_editWidgets["payee"]);
payee->loadPayees(MyMoneyFile::instance()->payeeList());
// load the category widget
KMyMoneyCategory* category = dynamic_cast<KMyMoneyCategory*>(m_editWidgets["category"]);
disconnect(category, TQT_SIGNAL(focusIn()), this, TQT_SLOT(slotEditSplits()));
// check if the current transaction has a reference to an equity account
bool haveEquityAccount = false;
TQValueList<MyMoneySplit>::const_iterator it_s;
for(it_s = m_transaction.splits().begin(); !haveEquityAccount && it_s != m_transaction.splits().end(); ++it_s) {
MyMoneyAccount acc = MyMoneyFile::instance()->account((*it_s).accountId());
if(acc.accountType() == MyMoneyAccount::Equity)
haveEquityAccount = true;
}
aSet.clear();
aSet.addAccountGroup(MyMoneyAccount::Asset);
aSet.addAccountGroup(MyMoneyAccount::Liability);
aSet.addAccountGroup(MyMoneyAccount::Income);
aSet.addAccountGroup(MyMoneyAccount::Expense);
if(KMyMoneyGlobalSettings::expertMode() || haveEquityAccount)
aSet.addAccountGroup(MyMoneyAccount::Equity);
aSet.removeAccountType(MyMoneyAccount::CertificateDep);
aSet.removeAccountType(MyMoneyAccount::Investment);
aSet.removeAccountType(MyMoneyAccount::Stock);
aSet.removeAccountType(MyMoneyAccount::MoneyMarket);
aSet.load(category->selector());
// if an account is specified then remove it from the widget so that the user
// cannot create a transfer with from and to account being the same account
if(!m_account.id().isEmpty())
category->selector()->removeItem(m_account.id());
if(!isMultiSelection()) {
dynamic_cast<KTextEdit*>(m_editWidgets["memo"])->setText(m_split.memo());
if(m_transaction.postDate().isValid())
dynamic_cast<kMyMoneyDateInput*>(m_editWidgets["postdate"])->setDate(m_transaction.postDate());
else if(m_lastPostDate.isValid())
dynamic_cast<kMyMoneyDateInput*>(m_editWidgets["postdate"])->setDate(m_lastPostDate);
else
dynamic_cast<kMyMoneyDateInput*>(m_editWidgets["postdate"])->setDate(TQDate::tqcurrentDate());
if((w = haveWidget("number")) != 0) {
dynamic_cast<kMyMoneyLineEdit*>(w)->loadText(m_split.number());
if(m_transaction.id().isEmpty() // new transaction
&& dynamic_cast<kMyMoneyLineEdit*>(w)->text().isEmpty() // no number filled in
&& m_account.accountType() == MyMoneyAccount::Checkings // checkings account
&& KMyMoneyGlobalSettings::autoIncCheckNumber()) { // and auto inc number turned on?
assignNextNumber();
}
}
dynamic_cast<KMyMoneyReconcileCombo*>(m_editWidgets["status"])->setState(m_split.reconcileFlag());
TQString payeeId = m_split.payeeId();
if(!payeeId.isEmpty()) {
payee->setSelectedItem(payeeId);
}
m_splits.clear();
if(m_transaction.splitCount() < 2) {
category->completion()->setSelected(TQString());
} else {
TQValueList<MyMoneySplit>::const_iterator it_s;
for(it_s = m_transaction.splits().begin(); it_s != m_transaction.splits().end(); ++it_s) {
if((*it_s) == m_split)
continue;
m_splits.append(*it_s);
}
}
TQString categoryId;
setupCategoryWidget(categoryId);
if((w = haveWidget("cashflow")) != 0) {
KMyMoneyCashFlowCombo* cashflow = dynamic_cast<KMyMoneyCashFlowCombo*>(w);
cashflow->setDirection(m_split.value().isNegative() ? KMyMoneyRegister::Payment : KMyMoneyRegister::Deposit);
}
if((w = haveWidget("category-label")) != 0) {
TQLabel *categoryLabel = dynamic_cast<TQLabel*>(w);
if(isTransfer(m_split.accountId(), categoryId)) {
if(m_split.value().isPositive())
categoryLabel->setText(i18n("Transfer from"));
else
categoryLabel->setText(i18n("Transfer to"));
}
}
MyMoneyMoney value = m_split.shares();
if(haveWidget("deposit")) {
if(m_split.shares().isNegative()) {
dynamic_cast<kMyMoneyEdit*>(m_editWidgets["deposit"])->loadText("");
dynamic_cast<kMyMoneyEdit*>(m_editWidgets["payment"])->setValue(value.abs());
} else {
dynamic_cast<kMyMoneyEdit*>(m_editWidgets["deposit"])->setValue(value.abs());
dynamic_cast<kMyMoneyEdit*>(m_editWidgets["payment"])->loadText("");
}
}
if((w = haveWidget("amount")) != 0) {
dynamic_cast<kMyMoneyEdit*>(w)->setValue(value.abs());
}
slotUpdateCategory(categoryId);
// try to preset for specific action if a new transaction is being started
if(m_transaction.id().isEmpty()) {
if((w = haveWidget("category-label")) != 0) {
TabBar* tabbar = dynamic_cast<TabBar*>(haveWidget("tabbar"));
if(action == KMyMoneyRegister::ActionNone) {
if(tabbar) {
action = static_cast<KMyMoneyRegister::Action>(tabbar->currentTab());
}
}
if(action != KMyMoneyRegister::ActionNone) {
TQLabel *categoryLabel = dynamic_cast<TQLabel*>(w);
if(action == KMyMoneyRegister::ActionTransfer) {
if(m_split.value().isPositive())
categoryLabel->setText(i18n("Transfer from"));
else
categoryLabel->setText(i18n("Transfer to"));
}
if((w = haveWidget("cashflow")) != 0) {
KMyMoneyCashFlowCombo* cashflow = dynamic_cast<KMyMoneyCashFlowCombo*>(w);
if(action == KMyMoneyRegister::ActionDeposit || (action == KMyMoneyRegister::ActionTransfer && m_split.value().isPositive()))
cashflow->setDirection(KMyMoneyRegister::Deposit);
else
cashflow->setDirection(KMyMoneyRegister::Payment);
}
if(tabbar) {
tabbar->setCurrentTab(action);
}
}
}
} else {
TabBar* tabbar = dynamic_cast<TabBar*>(haveWidget("tabbar"));
if(tabbar) {
if(!isTransfer(m_split.accountId(), categoryId)) {
tabbar->setCurrentTab(m_split.value().isNegative() ? KMyMoneyRegister::ActionWithdrawal : KMyMoneyRegister::ActionDeposit);
} else {
tabbar->setCurrentTab(KMyMoneyRegister::ActionTransfer);
}
}
}
} else {
dynamic_cast<kMyMoneyDateInput*>(m_editWidgets["postdate"])->loadDate(TQDate());
dynamic_cast<KMyMoneyReconcileCombo*>(m_editWidgets["status"])->setState(MyMoneySplit::Unknown);
if(haveWidget("deposit")) {
dynamic_cast<kMyMoneyEdit*>(m_editWidgets["deposit"])->loadText("");
dynamic_cast<kMyMoneyEdit*>(m_editWidgets["deposit"])->setAllowEmpty();
dynamic_cast<kMyMoneyEdit*>(m_editWidgets["payment"])->loadText("");
dynamic_cast<kMyMoneyEdit*>(m_editWidgets["payment"])->setAllowEmpty();
}
if((w = haveWidget("amount")) != 0) {
dynamic_cast<kMyMoneyEdit*>(w)->loadText("");
dynamic_cast<kMyMoneyEdit*>(w)->setAllowEmpty();
}
if((w = haveWidget("cashflow")) != 0) {
KMyMoneyCashFlowCombo* cashflow = dynamic_cast<KMyMoneyCashFlowCombo*>(w);
cashflow->setDirection(KMyMoneyRegister::Unknown);
}
if((w = haveWidget("tabbar")) != 0) {
w->setEnabled(false);
}
category->completion()->setSelected(TQString());
}
// allow kick off VAT processing again
m_inUpdateVat = false;
}
TQWidget* StdTransactionEditor::firstWidget(void) const
{
TQWidget* w = 0;
if(m_initialAction != KMyMoneyRegister::ActionNone)
w = haveWidget("payee");
return w;
}
void StdTransactionEditor::slotReloadEditWidgets(void)
{
// reload category widget
KMyMoneyCategory* category = dynamic_cast<KMyMoneyCategory*>(m_editWidgets["category"]);
TQString categoryId = category->selectedItem();
AccountSet aSet;
aSet.addAccountGroup(MyMoneyAccount::Asset);
aSet.addAccountGroup(MyMoneyAccount::Liability);
aSet.addAccountGroup(MyMoneyAccount::Income);
aSet.addAccountGroup(MyMoneyAccount::Expense);
if(KMyMoneyGlobalSettings::expertMode())
aSet.addAccountGroup(MyMoneyAccount::Equity);
aSet.load(category->selector());
// if an account is specified then remove it from the widget so that the user
// cannot create a transfer with from and to account being the same account
if(!m_account.id().isEmpty())
category->selector()->removeItem(m_account.id());
if(!categoryId.isEmpty())
category->setSelectedItem(categoryId);
// reload payee widget
KMyMoneyPayeeCombo* payee = dynamic_cast<KMyMoneyPayeeCombo*>(m_editWidgets["payee"]);
TQString payeeId = payee->selectedItem();
payee->loadPayees(MyMoneyFile::instance()->payeeList());
if(!payeeId.isEmpty()) {
payee->setSelectedItem(payeeId);
}
}
void StdTransactionEditor::slotUpdatePayee(const TQString& payeeId)
{
// we have a new payee assigned to this transaction.
// retrieve some information about the state of the category widget
KMyMoneyCategory* category = dynamic_cast<KMyMoneyCategory*>(m_editWidgets["category"]);
TQStringList list;
category->selectedItems(list);
// If payee has associated default account (category), set that now if
// category is not filled
const MyMoneyPayee& payeeObj = MyMoneyFile::instance()->payee(payeeId);
if (payeeObj.defaultAccountEnabled() && list.isEmpty()) {
KMyMoneyCategory* category = dynamic_cast<KMyMoneyCategory*>(m_editWidgets["category"]);
category->slotItemSelected(payeeObj.defaultAccountId());
return;
}
// in case there is no category assigned, no value entered and no
// memo available, we search for the last transaction of this payee
// in the account.
if(m_transaction.id().isEmpty()
&& m_splits.count() == 0
&& KMyMoneyGlobalSettings::autoFillTransaction() != 0
&& list.isEmpty()) {
// check if memo is empty
KTextEdit* memo = dynamic_cast<KTextEdit*>(m_editWidgets["memo"]);
if(memo && !memo->text().isEmpty())
return;
// check if all value fields are empty
kMyMoneyEdit* amount;
TQStringList fields;
fields << "amount" << "payment" << "deposit";
TQStringList::const_iterator it_f;
for(it_f = fields.begin(); it_f != fields.end(); ++it_f) {
amount = dynamic_cast<kMyMoneyEdit*>(haveWidget(*it_f));
if(amount && !amount->value().isZero())
return;
}
#if 0
// Tony mentioned, that autofill does not work when he changed the date. Well,
// that certainly makes sense when you enter transactions in register mode as
// opposed to form mode, because the date field is located prior to the date
// field in the tab order of the widgets and the user might have already
// changed it.
//
// So I commented out the code that checks the date but left it in for reference.
// (ipwizard, 2008-04-07)
// check if date has been altered by user
kMyMoneyDateInput* postDate = dynamic_cast<kMyMoneyDateInput*>(m_editWidgets["postdate"]);
if((m_lastPostDate.isValid() && (postDate->date() != m_lastPostDate))
|| (!m_lastPostDate.isValid() && (postDate->date() != TQDate::tqcurrentDate())))
return;
#endif
// if we got here, we have to autofill
autoFill(payeeId);
}
}
MyMoneyMoney StdTransactionEditor::shares(const MyMoneyTransaction& t) const
{
MyMoneyMoney result;
TQValueList<MyMoneySplit>::const_iterator it_s;
for(it_s = t.splits().begin(); it_s != t.splits().end(); ++it_s) {
if((*it_s).accountId() == m_account.id()) {
result += (*it_s).shares();
}
}
return result;
}
struct uniqTransaction {
const MyMoneyTransaction* t;
int cnt;
};
void StdTransactionEditor::autoFill(const TQString& payeeId)
{
TQValueList<TQPair<MyMoneyTransaction, MyMoneySplit> > list;
MyMoneyTransactionFilter filter(m_account.id());
filter.addPayee(payeeId);
MyMoneyFile::instance()->transactionList(list, filter);
if(!list.empty()) {
// ok, we found at least one previous transaction. now we clear out
// what we have collected so far and add those splits from
// the previous transaction.
TQValueList<TQPair<MyMoneyTransaction, MyMoneySplit> >::const_iterator it_t;
TQMap<TQString, struct uniqTransaction> uniqList;
// collect the transactions and see if we have any duplicates
for(it_t = list.begin(); it_t != list.end(); ++it_t) {
TQString key = (*it_t).first.accountSignature();
int cnt = 0;
TQMap<TQString, struct uniqTransaction>::iterator it_u;
do {
TQString ukey = TQString("%1-%2").tqarg(key).tqarg(cnt);
it_u = uniqList.find(ukey);
if(it_u == uniqList.end()) {
uniqList[ukey].t = &((*it_t).first);
uniqList[ukey].cnt = 1;
} else if (KMyMoneyGlobalSettings::autoFillTransaction() == 1) {
// we already have a transaction with this signature. we must
// now check, if we should really treat it as a duplicate according
// to the value comparison delta.
MyMoneyMoney s1 = shares(*((*it_u).t));
MyMoneyMoney s2 = shares((*it_t).first);
if(s2.abs() > s1.abs()) {
MyMoneyMoney t(s1);
s1 = s2;
s2 = t;
}
MyMoneyMoney diff;
if(s2.isZero())
diff = s1.abs();
else
diff = ((s1 - s2) / s2).convert(10000);
if(diff.isPositive() && diff <= MyMoneyMoney(KMyMoneyGlobalSettings::autoFillDifference(),100)) {
uniqList[ukey].t = &((*it_t).first);
break; // end while loop
}
} else if (KMyMoneyGlobalSettings::autoFillTransaction() == 2) {
(*it_u).cnt++;
break; // end while loop
}
++cnt;
} while(it_u != uniqList.end());
}
MyMoneyTransaction t;
if (KMyMoneyGlobalSettings::autoFillTransaction() != 2) {
#if 0
// I removed this code to allow cancellation of an autofill if
// it does not match even if there is only a single matching
// transaction for the payee in question. In case, we want to revert
// to the old behavior, don't forget to uncomment the closing
// brace further down in the code as well. (ipwizard 2009-01-16)
if(uniqList.count() == 1) {
t = list.last().first;
} else {
#endif
KSelectTransactionsDlg dlg(m_account, m_regForm);
dlg.setCaption(i18n("Select autofill transaction"));
TQMap<TQString, struct uniqTransaction>::const_iterator it_u;
for(it_u = uniqList.begin(); it_u != uniqList.end(); ++it_u) {
dlg.addTransaction(*(*it_u).t);
}
// setup sort order
dlg.m_register->setSortOrder("1,-9,-4");
// sort the transactions according to the sort setting
dlg.m_register->sortItems();
// and select the last item
if(dlg.m_register->lastItem())
dlg.m_register->selectItem(dlg.m_register->lastItem());
if(dlg.exec() == TQDialog::Accepted) {
t = dlg.transaction();
}
#if 0
}
#endif
} else {
int maxCnt = 0;
TQMap<TQString, struct uniqTransaction>::const_iterator it_u;
for(it_u = uniqList.begin(); it_u != uniqList.end(); ++it_u) {
if((*it_u).cnt > maxCnt) {
t = *(*it_u).t;
maxCnt = (*it_u).cnt;
}
}
}
if(t != MyMoneyTransaction()) {
m_transaction.removeSplits();
m_split = MyMoneySplit();
MyMoneySplit otherSplit;
TQValueList<MyMoneySplit>::ConstIterator it;
for(it = t.splits().begin(); it != t.splits().end(); ++it) {
MyMoneySplit s(*it);
s.setReconcileFlag(MyMoneySplit::NotReconciled);
s.setReconcileDate(TQDate());
s.clearId();
s.setBankID(TQString());
// older versions of KMyMoney used to set the action
// we don't need this anymore
if(s.action() != MyMoneySplit::ActionAmortization
&& s.action() != MyMoneySplit::ActionInterest) {
s.setAction(TQString());
}
// FIXME update check number. The old comment contained
//
// <quote>
// If a check number is already specified by the user it is
// used. If the input field is empty and the previous transaction
// contains a checknumber, the next usuable check no will be assigned
// to the transaction.
// </quote>
kMyMoneyLineEdit* editNr = dynamic_cast<kMyMoneyLineEdit*>(haveWidget("number"));
if(editNr && !editNr->text().isEmpty()) {
s.setNumber(editNr->text());
} else if(!s.number().isEmpty()) {
s.setNumber(KMyMoneyUtils::nextCheckNumber(m_account));
}
// if the transaction has exactly two splits, remove
// the memo text of the split that does not reference
// the current account. This allows the user to change
// the autofilled memo text which will then also be used
// in this split. See createTransaction() for this logic.
if(s.accountId() != m_account.id() && t.splitCount() == 2)
s.setMemo(TQString());
m_transaction.addSplit(s);
if(s.accountId() == m_account.id() && m_split == MyMoneySplit()) {
m_split = s;
} else {
otherSplit = s;
}
}
// make sure to extract the right action
KMyMoneyRegister::Action action;
action = m_split.shares().isNegative() ? KMyMoneyRegister::ActionWithdrawal : KMyMoneyRegister::ActionDeposit;
if(m_transaction.splitCount() == 2) {
MyMoneyAccount acc = MyMoneyFile::instance()->account(otherSplit.accountId());
if(acc.isAssetLiability())
action = KMyMoneyRegister::ActionTransfer;
}
// now setup the widgets with the new data but keep the date
TQDate date = dynamic_cast<kMyMoneyDateInput*>(m_editWidgets["postdate"])->date();
loadEditWidgets(action);
dynamic_cast<kMyMoneyDateInput*>(m_editWidgets["postdate"])->setDate(date);
}
}
// focus jumps into the category field
TQWidget* w;
if((w = haveWidget("payee")) != 0) {
w->setFocus();
}
}
void StdTransactionEditor::slotUpdateAction(int action)
{
TabBar* tabbar = dynamic_cast<TabBar*>(haveWidget("tabbar"));
if(tabbar) {
TQLabel* categoryLabel = dynamic_cast<TQLabel*>(haveWidget("category-label"));
KMyMoneyCashFlowCombo* cashflow = dynamic_cast<KMyMoneyCashFlowCombo*>(m_editWidgets["cashflow"]);
switch(action) {
case KMyMoneyRegister::ActionDeposit:
categoryLabel->setText(i18n("Category"));
cashflow->setDirection(KMyMoneyRegister::Deposit);
break;
case KMyMoneyRegister::ActionTransfer:
categoryLabel->setText(i18n("Transfer from"));
slotUpdateCashFlow(cashflow->direction());
break;
case KMyMoneyRegister::ActionWithdrawal:
categoryLabel->setText(i18n("Category"));
cashflow->setDirection(KMyMoneyRegister::Payment);
break;
}
}
}
void StdTransactionEditor::slotUpdateCashFlow(KMyMoneyRegister::CashFlowDirection dir)
{
TQLabel* categoryLabel = dynamic_cast<TQLabel*>(haveWidget("category-label"));
// qDebug("Update cashflow to %d", dir);
if(categoryLabel) {
TabBar* tabbar = dynamic_cast<TabBar*>(haveWidget("tabbar"));
if(categoryLabel->text() != i18n("Category")) {
tabbar->setCurrentTab(KMyMoneyRegister::ActionTransfer);
if(dir == KMyMoneyRegister::Deposit) {
categoryLabel->setText(i18n("Transfer from"));
} else {
categoryLabel->setText(i18n("Transfer to"));
}
} else {
if(dir == KMyMoneyRegister::Deposit)
tabbar->setCurrentTab(KMyMoneyRegister::ActionDeposit);
else
tabbar->setCurrentTab(KMyMoneyRegister::ActionWithdrawal);
}
}
}
void StdTransactionEditor::slotUpdateCategory(const TQString& id)
{
TQLabel *categoryLabel = dynamic_cast<TQLabel*>(haveWidget("category-label"));
// qDebug("Update category to %s", id.data());
if(categoryLabel) {
TabBar* tabbar = dynamic_cast<TabBar*>(haveWidget("tabbar"));
kMyMoneyEdit* amount = dynamic_cast<kMyMoneyEdit*>(m_editWidgets["amount"]);
MyMoneyMoney val = amount->value();
if(categoryLabel->text() == i18n("Transfer from")) {
val = -val;
} else {
val = val.abs();
}
if(tabbar) {
tabbar->tab(KMyMoneyRegister::ActionTransfer)->setEnabled(true);
tabbar->tab(KMyMoneyRegister::ActionDeposit)->setEnabled(true);
tabbar->tab(KMyMoneyRegister::ActionWithdrawal)->setEnabled(true);
}
if(!id.isEmpty()) {
MyMoneyAccount acc = MyMoneyFile::instance()->account(id);
if(acc.isAssetLiability()
|| acc.accountGroup() == MyMoneyAccount::Equity) {
if(tabbar) {
tabbar->tab(KMyMoneyRegister::ActionDeposit)->setEnabled(false);
tabbar->tab(KMyMoneyRegister::ActionWithdrawal)->setEnabled(false);
}
// only change the label if an amount is already filled in
if(!val.isZero()) {
if(val.isNegative())
categoryLabel->setText(i18n("Transfer from"));
else
categoryLabel->setText(i18n("Transfer to"));
}
} else {
if(tabbar)
tabbar->tab(KMyMoneyRegister::ActionTransfer)->setEnabled(false);
categoryLabel->setText(i18n("Category"));
}
updateAmount(val.abs());
} else {
KMyMoneyCategory* category = dynamic_cast<KMyMoneyCategory*>(m_editWidgets["category"]);
if(!category->currentText().isEmpty() && tabbar)
tabbar->tab(KMyMoneyRegister::ActionTransfer)->setEnabled(false);
categoryLabel->setText(i18n("Category"));
}
if(tabbar)
tabbar->update();
}
updateVAT(false);
}
void StdTransactionEditor::slotUpdatePayment(const TQString& txt)
{
MyMoneyMoney val(txt);
if(val.isNegative()) {
dynamic_cast<kMyMoneyEdit*>(m_editWidgets["deposit"])->setValue(val.abs());
dynamic_cast<kMyMoneyEdit*>(m_editWidgets["payment"])->clearText();
} else {
dynamic_cast<kMyMoneyEdit*>(m_editWidgets["deposit"])->clearText();
}
updateVAT();
}
void StdTransactionEditor::slotUpdateDeposit(const TQString& txt)
{
MyMoneyMoney val(txt);
if(val.isNegative()) {
dynamic_cast<kMyMoneyEdit*>(m_editWidgets["payment"])->setValue(val.abs());
dynamic_cast<kMyMoneyEdit*>(m_editWidgets["deposit"])->clearText();
} else {
dynamic_cast<kMyMoneyEdit*>(m_editWidgets["payment"])->clearText();
}
updateVAT();
}
void StdTransactionEditor::slotUpdateAmount(const TQString& txt)
{
// qDebug("Update amount to %s", txt.data());
MyMoneyMoney val(txt);
updateAmount(val);
updateVAT(true);
}
void StdTransactionEditor::updateAmount(const MyMoneyMoney& val)
{
TQLabel *categoryLabel = dynamic_cast<TQLabel*>(haveWidget("category-label"));
if(categoryLabel) {
KMyMoneyCashFlowCombo* cashflow = dynamic_cast<KMyMoneyCashFlowCombo*>(m_editWidgets["cashflow"]);
if(val.isNegative()) {
if(categoryLabel->text() != i18n("Category")) {
if(categoryLabel->text() == i18n("Transfer from")) {
categoryLabel->setText(i18n("Transfer to"));
cashflow->setDirection(KMyMoneyRegister::Payment);
} else {
categoryLabel->setText(i18n("Transfer from"));
cashflow->setDirection(KMyMoneyRegister::Deposit);
}
} else {
if(cashflow->direction() == KMyMoneyRegister::Deposit)
cashflow->setDirection(KMyMoneyRegister::Payment);
else
cashflow->setDirection(KMyMoneyRegister::Deposit);
slotUpdateCashFlow(cashflow->direction());
}
dynamic_cast<kMyMoneyEdit*>(m_editWidgets["amount"])->setValue(val.abs());
}
}
}
void StdTransactionEditor::updateVAT(bool amountChanged)
{
// make sure that we don't do this recursively
if(m_inUpdateVat)
return;
// we don't do anything if we have multiple transactions selected
if(isMultiSelection())
return;
// if auto vat assignment for this account is turned off
// we don't care about taxes
if(m_account.value("NoVat") == "Yes")
return;
// more splits than category and tax are not supported
if(m_splits.count() > 2)
return;
// in order to do anything, we need an amount
MyMoneyMoney amount, newAmount;
bool amountOk;
amount = amountFromWidget(&amountOk);
if(!amountOk)
return;
// If the transaction has a tax and a category split, remove the tax split
if(m_splits.count() == 2) {
newAmount = removeVatSplit();
if(m_splits.count() == 2) // not removed?
return;
} else {
// otherwise, we need a category
KMyMoneyCategory* category = dynamic_cast<KMyMoneyCategory*>(m_editWidgets["category"]);
if(category->selectedItem().isEmpty())
return;
// if no VAT account is associated with this category/account, then we bail out
MyMoneyAccount cat = MyMoneyFile::instance()->account(category->selectedItem());
if(cat.value("VatAccount").isEmpty())
return;
newAmount = amount;
}
// seems we have everything we need
if(amountChanged)
newAmount = amount;
MyMoneyTransaction transaction;
if(createTransaction(transaction, m_transaction, m_split)) {
if(addVatSplit(transaction, newAmount)) {
m_transaction = transaction;
m_split = m_transaction.splits()[0];
loadEditWidgets();
// if we made this a split transaction, then move the
// focus to the memo field
if(tqApp->tqfocusWidget() == haveWidget("category")) {
TQWidget* w = haveWidget("memo");
if(w)
w->setFocus();
}
}
}
}
bool StdTransactionEditor::addVatSplit(MyMoneyTransaction& tr, const MyMoneyMoney& amount)
{
if(tr.splitCount() != 2)
return false;
bool rc = false;
MyMoneyFile* file = MyMoneyFile::instance();
try {
MyMoneySplit cat; // category
MyMoneySplit tax; // tax
// extract the category split from the transaction
MyMoneyAccount category = file->account(tr.splitByAccount(m_account.id(), false).accountId());
if(category.value("VatAccount").isEmpty())
return false;
MyMoneyAccount vatAcc = file->account(category.value("VatAccount").latin1());
const MyMoneySecurity& asec = file->security(m_account.currencyId());
const MyMoneySecurity& csec = file->security(category.currencyId());
const MyMoneySecurity& vsec = file->security(vatAcc.currencyId());
if(asec.id() != csec.id() || asec.id() != vsec.id()) {
qDebug("Auto VAT assignment only works if all three accounts use the same currency.");
return false;
}
MyMoneyMoney vatRate(vatAcc.value("VatRate"));
MyMoneyMoney gv, nv; // gross value, net value
int fract = m_account.fraction();
if(!vatRate.isZero()) {
tax.setAccountId(vatAcc.id());
// qDebug("vat amount is '%s'", category.value("VatAmount").latin1());
if(category.value("VatAmount").lower() != TQString("net")) {
// split value is the gross value
gv = amount;
nv = gv / (MyMoneyMoney(1,1) + vatRate);
MyMoneySplit catSplit = tr.splitByAccount(m_account.id(), false);
catSplit.setShares(-nv.convert(fract));
catSplit.setValue(catSplit.shares());
tr.modifySplit(catSplit);
} else {
// split value is the net value
nv = amount;
gv = nv * (MyMoneyMoney(1,1) + vatRate);
MyMoneySplit accSplit = tr.splitByAccount(m_account.id());
accSplit.setValue(gv.convert(fract));
accSplit.setShares(accSplit.value());
tr.modifySplit(accSplit);
}
tax.setValue(-(gv - nv).convert(fract));
tax.setShares(tax.value());
tr.addSplit(tax);
rc = true;
}
} catch(MyMoneyException *e) {
delete e;
}
return rc;
}
MyMoneyMoney StdTransactionEditor::removeVatSplit(void)
{
// we only deal with splits that have three splits
if(m_splits.count() != 2)
return amountFromWidget();
MyMoneySplit c; // category split
MyMoneySplit t; // tax split
bool netValue = false;
TQValueList<MyMoneySplit>::const_iterator it_s;
for(it_s = m_splits.begin(); it_s != m_splits.end(); ++it_s) {
MyMoneyAccount acc = MyMoneyFile::instance()->account((*it_s).accountId());
if(!acc.value("VatAccount").isEmpty()) {
netValue = (acc.value("VatAmount").lower() == "net");
c = (*it_s);
} else if(!acc.value("VatRate").isEmpty()) {
t = (*it_s);
}
}
// bail out if not all splits are setup
if(c.id().isEmpty() || t.id().isEmpty())
return amountFromWidget();
MyMoneyMoney amount;
// reduce the splits
if(netValue) {
amount = -c.shares();
} else {
amount = -(c.shares() + t.shares());
}
// remove tax split from the list, ...
m_splits.clear();
m_splits.append(c);
// ... make sure that the widget is updated ...
// block the signals to avoid popping up the split editor dialog
// for nothing
m_editWidgets["category"]->blockSignals(true);
TQString id;
setupCategoryWidget(id);
m_editWidgets["category"]->blockSignals(false);
// ... and return the updated amount
return amount;
}
bool StdTransactionEditor::isComplete(TQString& reason) const
{
reason = TQString();
// reason.clear(); // for TQt4
TQMap<TQString, TQWidget*>::const_iterator it_w;
kMyMoneyDateInput* postDate = dynamic_cast<kMyMoneyDateInput*>(m_editWidgets["postdate"]);
if(postDate) {
postDate->markAsBadDate();
TQToolTip::remove(postDate);
if(postDate->date().isValid() && (postDate->date() < m_account.openingDate())) {
postDate->markAsBadDate(true, KMyMoneyGlobalSettings::listNegativeValueColor());
reason = i18n("Cannot enter transaction with postdate prior to account's opening date.");
TQToolTip::add(postDate, reason);
return false;
}
}
for(it_w = m_editWidgets.begin(); it_w != m_editWidgets.end(); ++it_w) {
KMyMoneyPayeeCombo* payee = dynamic_cast<KMyMoneyPayeeCombo*>(*it_w);
KMyMoneyCategory* category = dynamic_cast<KMyMoneyCategory*>(*it_w);
kMyMoneyEdit* amount = dynamic_cast<kMyMoneyEdit*>(*it_w);
KMyMoneyReconcileCombo* reconcile = dynamic_cast<KMyMoneyReconcileCombo*>(*it_w);
KMyMoneyCashFlowCombo* cashflow = dynamic_cast<KMyMoneyCashFlowCombo*>(*it_w);
if(payee && !(payee->currentText().isEmpty()))
break;
if(category && !category->lineEdit()->text().isEmpty())
break;
if(amount && !(amount->value().isZero()))
break;
// the following two widgets are only checked if we are editing multiple transactions
if(isMultiSelection()) {
if(reconcile && reconcile->state() != MyMoneySplit::Unknown)
break;
if(cashflow && cashflow->direction() != KMyMoneyRegister::Unknown)
break;
}
}
return it_w != m_editWidgets.end();
}
void StdTransactionEditor::slotCreateCategory(const TQString& name, TQString& id)
{
MyMoneyAccount acc, parent;
acc.setName(name);
KMyMoneyCashFlowCombo* cashflow = dynamic_cast<KMyMoneyCashFlowCombo*>(haveWidget("cashflow"));
if(cashflow) {
// form based input
if(cashflow->direction() == KMyMoneyRegister::Deposit)
parent = MyMoneyFile::instance()->income();
else
parent = MyMoneyFile::instance()->expense();
} else if(haveWidget("deposit")) {
// register based input
kMyMoneyEdit* deposit = dynamic_cast<kMyMoneyEdit*>(m_editWidgets["deposit"]);
if(deposit->value().isPositive())
parent = MyMoneyFile::instance()->income();
else
parent = MyMoneyFile::instance()->expense();
} else
parent = MyMoneyFile::instance()->expense();
// TODO extract possible first part of a hierarchy and check if it is one
// of our top categories. If so, remove it and select the parent
// according to this information.
emit createCategory(acc, parent);
// return id
id = acc.id();
}
int StdTransactionEditor::slotEditSplits(void)
{
int rc = TQDialog::Rejected;
if(!m_openEditSplits) {
// only get in here in a single instance
m_openEditSplits = true;
// force focus change to update all data
TQWidget* w = dynamic_cast<KMyMoneyCategory*>(m_editWidgets["category"])->splitButton();
if(w)
w->setFocus();
kMyMoneyEdit* amount = dynamic_cast<kMyMoneyEdit*>(haveWidget("amount"));
kMyMoneyEdit* deposit = dynamic_cast<kMyMoneyEdit*>(haveWidget("deposit"));
kMyMoneyEdit* payment = dynamic_cast<kMyMoneyEdit*>(haveWidget("payment"));
KMyMoneyCashFlowCombo* cashflow = 0;
KMyMoneyRegister::CashFlowDirection dir = KMyMoneyRegister::Unknown;
bool isValidAmount = false;
if(amount) {
isValidAmount = amount->lineedit()->text().length() != 0;
cashflow = dynamic_cast<KMyMoneyCashFlowCombo*>(haveWidget("cashflow"));
if(cashflow)
dir = cashflow->direction();
} else {
if(deposit) {
if (deposit->lineedit()->text().length() != 0) {
isValidAmount = true;
dir = KMyMoneyRegister::Deposit;
}
}
if(payment) {
if (payment->lineedit()->text().length() != 0) {
isValidAmount = true;
dir = KMyMoneyRegister::Payment;
}
}
if(!deposit || !payment) {
qDebug("Internal error: deposit(%p) & payment(%p) widgets not found but required", deposit, payment);
return rc;
}
}
if(dir == KMyMoneyRegister::Unknown)
dir = KMyMoneyRegister::Payment;
MyMoneyTransaction transaction;
if(createTransaction(transaction, m_transaction, m_split)) {
MyMoneyMoney value;
KSplitTransactionDlg* dlg = new KSplitTransactionDlg(transaction,
transaction.splits()[0],
m_account,
isValidAmount,
dir == KMyMoneyRegister::Deposit,
0,
m_priceInfo,
m_regForm);
connect(dlg, TQT_SIGNAL(objectCreation(bool)), this, TQT_SIGNAL(objectCreation(bool)));
connect(dlg, TQT_SIGNAL(createCategory(MyMoneyAccount&, const MyMoneyAccount&)), this, TQT_SIGNAL(createCategory(MyMoneyAccount&, const MyMoneyAccount&)));
if((rc = dlg->exec()) == TQDialog::Accepted) {
m_transaction = dlg->transaction();
m_split = m_transaction.splits()[0];
loadEditWidgets();
}
delete dlg;
}
// focus jumps into the memo field
if((w = haveWidget("memo")) != 0) {
w->setFocus();
}
m_openEditSplits = false;
}
return rc;
}
void StdTransactionEditor::checkPayeeInSplit(MyMoneySplit& s, const TQString& payeeId)
{
if(s.accountId().isEmpty())
return;
MyMoneyAccount acc = MyMoneyFile::instance()->account(s.accountId());
if(acc.isIncomeExpense()) {
s.setPayeeId(payeeId);
} else {
if(s.payeeId().isEmpty())
s.setPayeeId(payeeId);
}
}
MyMoneyMoney StdTransactionEditor::amountFromWidget(bool* update) const
{
bool updateValue = false;
MyMoneyMoney value;
KMyMoneyCashFlowCombo* cashflow = dynamic_cast<KMyMoneyCashFlowCombo*>(haveWidget("cashflow"));
if(cashflow) {
// form based input
kMyMoneyEdit* amount = dynamic_cast<kMyMoneyEdit*>(m_editWidgets["amount"]);
// if both fields do not contain changes -> no need to update
if(cashflow->direction() != KMyMoneyRegister::Unknown
&& !amount->lineedit()->text().isEmpty())
updateValue = true;
value = amount->value();
if(cashflow->direction() == KMyMoneyRegister::Payment)
value = -value;
} else if(haveWidget("deposit")) {
// register based input
kMyMoneyEdit* deposit = dynamic_cast<kMyMoneyEdit*>(m_editWidgets["deposit"]);
kMyMoneyEdit* payment = dynamic_cast<kMyMoneyEdit*>(m_editWidgets["payment"]);
// if both fields do not contain text -> no need to update
if(!(deposit->lineedit()->text().isEmpty() && payment->lineedit()->text().isEmpty()))
updateValue = true;
if(deposit->value().isPositive())
value = deposit->value();
else
value = -(payment->value());
}
if(update)
*update = updateValue;
// determine the max fraction for this account and
// adjust the value accordingly
return value.convert(m_account.fraction());
}
bool StdTransactionEditor::createTransaction(MyMoneyTransaction& t, const MyMoneyTransaction& torig, const MyMoneySplit& sorig, bool skipPriceDialog)
{
// extract price info from original transaction
m_priceInfo.clear();
TQValueList<MyMoneySplit>::const_iterator it_s;
if(!torig.id().isEmpty()) {
for(it_s = torig.splits().begin(); it_s != torig.splits().end(); ++it_s) {
if((*it_s).id() != sorig.id()) {
MyMoneyAccount cat = MyMoneyFile::instance()->account((*it_s).accountId());
if(cat.currencyId() != m_account.currencyId()) {
if(!(*it_s).shares().isZero() && !(*it_s).value().isZero()) {
m_priceInfo[cat.currencyId()] = ((*it_s).shares() / (*it_s).value()).reduce();
}
}
}
}
}
t = torig;
t.removeSplits();
t.setCommodity(m_account.currencyId());
kMyMoneyDateInput* postDate = dynamic_cast<kMyMoneyDateInput*>(m_editWidgets["postdate"]);
if(postDate->date().isValid()) {
t.setPostDate(postDate->date());
}
// we start with the previous values, make sure we can add them later on
MyMoneySplit s0 = sorig;
s0.clearId();
// make sure we reference this account here
s0.setAccountId(m_account.id());
// memo and number field are special: if we have multiple transactions selected
// and the edit field is empty, we treat it as "not modified".
// FIXME a better approach would be to have a 'dirty' flag with the widgets
// which identifies if the originally loaded value has been modified
// by the user
KTextEdit* memo = dynamic_cast<KTextEdit*>(m_editWidgets["memo"]);
if(memo) {
if(!isMultiSelection() || (isMultiSelection() && !memo->text().isEmpty() ) )
s0.setMemo(memo->text());
}
kMyMoneyLineEdit* number = dynamic_cast<kMyMoneyLineEdit*>(haveWidget("number"));
if(number) {
if(!isMultiSelection() || (isMultiSelection() && !number->text().isEmpty() ) )
s0.setNumber(number->text());
}
KMyMoneyPayeeCombo* payee = dynamic_cast<KMyMoneyPayeeCombo*>(m_editWidgets["payee"]);
TQString payeeId;
if(!isMultiSelection() || (isMultiSelection() && !payee->currentText().isEmpty())) {
payeeId = payee->selectedItem();
s0.setPayeeId(payeeId);
}
bool updateValue;
MyMoneyMoney value = amountFromWidget(&updateValue);
if(updateValue) {
// for this account, the shares and value is the same
s0.setValue(value);
s0.setShares(value);
} else {
value = s0.value();
}
// if we mark the split reconciled here, we'll use today's date if no reconciliation date is given
KMyMoneyReconcileCombo* status = dynamic_cast<KMyMoneyReconcileCombo*>(m_editWidgets["status"]);
if(status->state() != MyMoneySplit::Unknown)
s0.setReconcileFlag(status->state());
if(s0.reconcileFlag() == MyMoneySplit::Reconciled && !s0.reconcileDate().isValid())
s0.setReconcileDate(TQDate::tqcurrentDate());
checkPayeeInSplit(s0, payeeId);
// add the split to the transaction
t.addSplit(s0);
// if we have no other split we create it
// if we have none or only one other split, we reconstruct it here
// if we have more than one other split, we take them as they are
// make sure to perform all those changes on a local copy
TQValueList<MyMoneySplit> splits = m_splits;
MyMoneySplit s1;
if(splits.count() == 0) {
s1.setMemo(s0.memo());
splits.append(s1);
// make sure we will fill the value and share fields later on
updateValue = true;
}
// FIXME in multiSelection we currently only support transactions with one
// or two splits. So we check the original transaction and extract the other
// split or create it
if(isMultiSelection()) {
if(torig.splitCount() == 2) {
TQValueList<MyMoneySplit>::const_iterator it_s;
for(it_s = torig.splits().begin(); it_s != torig.splits().end(); ++it_s) {
if((*it_s).id() == sorig.id())
continue;
s1 = *it_s;
s1.clearId();
break;
}
}
} else {
if(splits.count() == 1) {
s1 = splits[0];
s1.clearId();
}
}
if(isMultiSelection() || splits.count() == 1) {
KMyMoneyCategory* category = dynamic_cast<KMyMoneyCategory*>(m_editWidgets["category"]);
if(!isMultiSelection() || (isMultiSelection() && !category->currentText().isEmpty())) {
s1.setAccountId(category->selectedItem());
}
// if the first split has a memo but the second split is empty,
// we just copy the memo text over
if(memo) {
if(!isMultiSelection() || (isMultiSelection() && !memo->text().isEmpty())) {
// if the memo is filled, we check if the
// account referenced by s1 is a regular account or a category.
// in case of a regular account, we just leave the memo as is
// in case of a category we simply copy the new value over the old.
// in case we don't even have an account id, we just skip because
// the split will be removed later on anyway.
if(!s1.memo().isEmpty()) {
if(!s1.accountId().isEmpty()) {
MyMoneyAccount acc = MyMoneyFile::instance()->account(s1.accountId());
if(acc.isIncomeExpense()) {
s1.setMemo(s0.memo());
}
}
} else {
s1.setMemo(s0.memo());
}
}
}
if(updateValue && !s1.accountId().isEmpty()) {
s1.setValue(-value);
MyMoneyMoney shares;
if(!skipPriceDialog) {
if(!KCurrencyCalculator::setupSplitPrice(shares, t, s1, m_priceInfo, m_regForm))
return false;
} else {
MyMoneyAccount cat = MyMoneyFile::instance()->account(s1.accountId());
if(m_priceInfo.find(cat.currencyId()) != m_priceInfo.end()) {
shares = (s1.value() * m_priceInfo[cat.currencyId()]).reduce().convert(cat.fraction());
}
else
shares = s1.value();
}
s1.setShares(shares);
}
checkPayeeInSplit(s1, payeeId);
if(!s1.accountId().isEmpty())
t.addSplit(s1);
} else {
TQValueList<MyMoneySplit>::iterator it_s;
for(it_s = splits.begin(); it_s != splits.end(); ++it_s) {
s1 = *it_s;
s1.clearId();
checkPayeeInSplit(s1, payeeId);
t.addSplit(s1);
}
}
return true;
}
void StdTransactionEditor::setupFinalWidgets(void)
{
addFinalWidget(haveWidget("deposit"));
addFinalWidget(haveWidget("payment"));
addFinalWidget(haveWidget("amount"));
addFinalWidget(haveWidget("status"));
}
void StdTransactionEditor::slotUpdateAccount(const TQString& id)
{
TransactionEditor::slotUpdateAccount(id);
KMyMoneyCategory* category = dynamic_cast<KMyMoneyCategory*>(m_editWidgets["category"]);
if(category && category->splitButton()) {
category->splitButton()->setDisabled(id.isEmpty());
}
}
#include "transactioneditor.moc"
|