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
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
|
/***************************************************************************
mymoneyqifreader.cpp
-------------------
begin : Mon Jan 27 2003
copyright : (C) 2000-2003 by Michael Edwardes
email : [email protected]
Javier Campos Morales <[email protected]>
Felix Rodriguez <[email protected]>
John C <[email protected]>
Thomas Baumgart <[email protected]>
Kevin Tambascio <[email protected]>
Ace Jones <[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. *
* *
***************************************************************************/
#include <iostream>
// ----------------------------------------------------------------------------
// QT Headers
#include <tqfile.h>
#include <tqstringlist.h>
#include <tqtimer.h>
#include <tqtextedit.h>
#include <tqregexp.h>
#include <tqbuffer.h>
// ----------------------------------------------------------------------------
// KDE Headers
#include <klocale.h>
#include <kmessagebox.h>
#include <kconfig.h>
#include <kdebug.h>
#include <kprogress.h>
#include <kinputdialog.h>
#include <kio/netaccess.h>
// ----------------------------------------------------------------------------
// Project Headers
#include "mymoneyqifreader.h"
#include "../mymoney/mymoneyfile.h"
#include "../dialogs/kaccountselectdlg.h"
#include "../kmymoney2.h"
#include "kmymoneyglobalsettings.h"
#include "mymoneystatementreader.h"
#include <kmymoney/mymoneystatement.h>
// define this to debug the code. Using external filters
// while debugging did not work too good for me, so I added
// this code.
// #define DEBUG_IMPORT
#ifdef DEBUG_IMPORT
#warning "DEBUG_IMPORT defined --> external filter not available!!!!!!!"
#endif
class MyMoneyQifReader::Private {
public:
Private() :
accountType(MyMoneyAccount::Checkings),
mapCategories(true)
{}
TQString accountTypeToQif(MyMoneyAccount::accountTypeE type) const;
/**
* finalize the current statement and add it to the statement list
*/
void finishStatement(void);
bool isTransfer(TQString& name, const TQString& leftDelim, const TQString& rightDelim);
/**
* Converts the TQIF specific N-record of investment transactions into
* a category name
*/
TQString typeToAccountName(const TQString& type) const;
/**
* Converts the TQIF reconcile state to the KMyMoney reconcile state
*/
MyMoneySplit::reconcileFlagE reconcileState(const TQString& state) const;
/**
*/
void fixMultiLineMemo(TQString& memo) const;
public:
/**
* the statement that is currently collected/processed
*/
MyMoneyStatement st;
/**
* the list of all statements to be sent to MyMoneyStatementReader
*/
TQValueList<MyMoneyStatement> statements;
/**
* a list of already used hashes in this file
*/
TQMap<TQString, bool> m_hashMap;
TQString st_AccountName;
TQString st_AccountId;
MyMoneyAccount::accountTypeE accountType;
bool firstTransaction;
bool mapCategories;
MyMoneyQifReader::QifEntryTypeE transactionType;
};
void MyMoneyQifReader::Private::fixMultiLineMemo(TQString& memo) const
{
memo.replace("\\n", "\n");
}
void MyMoneyQifReader::Private::finishStatement(void)
{
// in case we have collected any data in the statement, we keep it
if((st.m_listTransactions.count() + st.m_listPrices.count() + st.m_listSecurities.count()) > 0) {
statements += st;
tqDebug("Statement with %d transactions, %d prices and %d securities added to the statement list",
st.m_listTransactions.count(), st.m_listPrices.count(), st.m_listSecurities.count());
}
// start with a fresh statement
st = MyMoneyStatement();
st.m_skipCategoryMatching = !mapCategories;
st.m_eType = (transactionType == MyMoneyQifReader::EntryTransaction) ? MyMoneyStatement::etCheckings : MyMoneyStatement::etInvestment;
}
TQString MyMoneyQifReader::Private::accountTypeToQif(MyMoneyAccount::accountTypeE type) const
{
TQString rc = "Bank";
switch(type) {
default:
break;
case MyMoneyAccount::Cash:
rc = "Cash";
break;
case MyMoneyAccount::CreditCard:
rc = "CCard";
break;
case MyMoneyAccount::Asset:
rc = "Oth A";
break;
case MyMoneyAccount::Liability:
rc = "Oth L";
break;
case MyMoneyAccount::Investment:
rc = "Port";
break;
}
return rc;
}
TQString MyMoneyQifReader::Private::typeToAccountName(const TQString& type) const
{
if(type == "reinvdiv")
return i18n("Category name", "Reinvested dividend");
if(type == "reinvlg")
return i18n("Category name", "Reinvested dividend (long term)");
if(type == "reinvsh")
return i18n("Category name", "Reinvested dividend (short term)");
if (type == "div")
return i18n("Category name", "Dividend");
if(type == "intinc")
return i18n("Category name", "Interest");
if(type == "cgshort")
return i18n("Category name", "Capital Gain (short term)");
if( type == "cgmid")
return i18n("Category name", "Capital Gain (mid term)");
if(type == "cglong")
return i18n("Category name", "Capital Gain (long term)");
if(type == "rtrncap")
return i18n("Category name", "Returned capital");
if(type == "miscinc")
return i18n("Category name", "Miscellaneous income");
if(type == "miscexp")
return i18n("Category name", "Miscellaneous expense");
if(type == "sell" || type == "buy")
return i18n("Category name", "Investment fees");
return i18n("Unknown TQIF type %1").arg(type);
}
bool MyMoneyQifReader::Private::isTransfer(TQString& tmp, const TQString& leftDelim, const TQString& rightDelim)
{
// it's a transfer, extract the account name
// I've seen entries like this
//
// S[Mehrwertsteuer]/_VATCode_N_I
//
// so extracting is a bit more complex and we use a regexp for it
TQRegExp exp(TQString("\\%1(.*)\\%2(.*)").arg(leftDelim, rightDelim));
bool rc;
if((rc = (exp.search(tmp) != -1)) == true) {
tmp = exp.cap(1)+exp.cap(2);
tmp = tmp.stripWhiteSpace();
}
return rc;
}
MyMoneySplit::reconcileFlagE MyMoneyQifReader::Private::reconcileState(const TQString& state) const
{
if(state == "X" || state == "R") // Reconciled
return MyMoneySplit::Reconciled;
if(state == "*") // Cleared
return MyMoneySplit::Cleared;
return MyMoneySplit::NotReconciled;
}
MyMoneyQifReader::MyMoneyQifReader() :
d(new Private)
{
m_skipAccount = false;
m_transactionsProcessed =
m_transactionsSkipped = 0;
m_progressCallback = 0;
m_file = 0;
m_entryType = EntryUnknown;
m_processingData = false;
m_userAbort = false;
m_warnedInvestment = false;
m_warnedSecurity = false;
m_warnedPrice = false;
connect(&m_filter, TQT_SIGNAL(wroteStdin(KProcess*)), this, TQT_SLOT(slotSendDataToFilter()));
connect(&m_filter, TQT_SIGNAL(receivedStdout(KProcess*, char*, int)), this, TQT_SLOT(slotReceivedDataFromFilter(KProcess*, char*, int)));
connect(&m_filter, TQT_SIGNAL(processExited(KProcess*)), this, TQT_SLOT(slotImportFinished()));
connect(&m_filter, TQT_SIGNAL(receivedStderr(KProcess*, char*, int)), this, TQT_SLOT(slotReceivedErrorFromFilter(KProcess*, char*, int)));
}
MyMoneyQifReader::~MyMoneyQifReader()
{
if(m_file)
delete m_file;
delete d;
}
void MyMoneyQifReader::setCategoryMapping(bool map)
{
d->mapCategories = map;
}
void MyMoneyQifReader::setURL(const KURL& url)
{
m_url = url;
}
void MyMoneyQifReader::setProfile(const TQString& profile)
{
m_qifProfile.loadProfile("Profile-" + profile);
}
void MyMoneyQifReader::slotSendDataToFilter(void)
{
TQ_LONG len;
if(m_file->atEnd()) {
// m_filter.flushStdin();
m_filter.closeStdin();
} else {
len = m_file->readBlock(m_buffer, sizeof(m_buffer));
if(len == -1) {
tqWarning("Failed to read block from TQIF import file");
m_filter.closeStdin();
m_filter.kill();
} else {
m_filter.writeStdin(m_buffer, len);
}
}
}
void MyMoneyQifReader::slotReceivedErrorFromFilter(KProcess* /* proc */, char *buff, int len)
{
TQByteArray data;
data.duplicate(buff, len);
tqWarning("%s",static_cast<const char*>(data));
}
void MyMoneyQifReader::slotReceivedDataFromFilter(KProcess* /* proc */, char *buff, int len)
{
m_pos += len;
// signalProgress(m_pos, 0);
while(len) {
// process char
if(*buff == '\n' || *buff == '\r') {
// found EOL
if(!m_lineBuffer.isEmpty()) {
m_qifLines << TQString::fromUtf8(m_lineBuffer.stripWhiteSpace());
}
m_lineBuffer = TQCString();
} else {
// collect all others
m_lineBuffer += (*buff);
}
++buff;
--len;
}
}
void MyMoneyQifReader::slotImportFinished(void)
{
// check if the last EOL char was missing and add the trailing line
if(!m_lineBuffer.isEmpty()) {
m_qifLines << TQString::fromUtf8(m_lineBuffer.stripWhiteSpace());
}
tqDebug("Read %d bytes", m_pos);
TQTimer::singleShot(0, this, TQT_SLOT(slotProcessData()));
}
void MyMoneyQifReader::slotProcessData(void)
{
signalProgress(-1, -1);
// scan the file and try to determine numeric and date formats
m_qifProfile.autoDetect(m_qifLines);
// the detection is accurate for numeric values, but it could be
// that the dates were too ambiguous so that we have to let the user
// decide which one to pick.
TQStringList dateFormats;
m_qifProfile.possibleDateFormats(dateFormats);
TQStringList list;
if(dateFormats.count() > 1) {
list << dateFormats.first();
bool ok;
list = KInputDialog::getItemList(i18n("Date format selection"), i18n("Pick the date format that suits your input file"), dateFormats, list, false, &ok);
if(!ok) {
m_userAbort = true;
}
} else
list = dateFormats;
m_qifProfile.setInputDateFormat(list.first());
tqDebug("Selected date format: '%s'", list.first().data());
signalProgress(0, m_qifLines.count(), i18n("Importing TQIF ..."));
TQStringList::iterator it;
for(it = m_qifLines.begin(); m_userAbort == false && it != m_qifLines.end(); ++it) {
++m_linenumber;
// tqDebug("Proc: '%s'", (*it).data());
if((*it).startsWith("!")) {
processQifSpecial(*it);
m_qifEntry.clear();
} else if(*it == "^") {
if(m_qifEntry.count() > 0) {
signalProgress(m_linenumber, 0);
processQifEntry();
m_qifEntry.clear();
}
} else {
m_qifEntry += *it;
}
}
d->finishStatement();
tqDebug("%d lines processed", m_linenumber);
signalProgress(-1, -1);
emit importFinished();
}
bool MyMoneyQifReader::startImport(void)
{
bool rc = false;
d->st = MyMoneyStatement();
d->st.m_skipCategoryMatching = !d->mapCategories;
m_dontAskAgain.clear();
m_accountTranslation.clear();
m_userAbort = false;
m_pos = 0;
m_linenumber = 0;
m_filename = TQString();
m_data.clear();
if(!KIO::NetAccess::download(m_url, m_filename, NULL)) {
KMessageBox::detailedError(0,
i18n("Error while loading file '%1'!").arg(m_url.prettyURL()),
KIO::NetAccess::lastErrorString(),
i18n("File access error"));
return false;
}
m_file = new TQFile(m_filename);
if(m_file->open(IO_ReadOnly)) {
#ifdef DEBUG_IMPORT
TQ_LONG len;
while(!m_file->atEnd()) {
len = m_file->readBlock(m_buffer, sizeof(m_buffer));
if(len == -1) {
tqWarning("Failed to read block from TQIF import file");
} else {
slotReceivedDataFromFilter(0, m_buffer, len);
}
}
slotImportFinished();
#else
// start filter process, use 'cat -' as the default filter
m_filter.clearArguments();
if(m_qifProfile.filterScriptImport().isEmpty()) {
m_filter << "cat";
m_filter << "-";
} else {
m_filter << TQStringList::split(" ", m_qifProfile.filterScriptImport(), true);
}
m_entryType = EntryUnknown;
if(m_filter.start(KProcess::NotifyOnExit, KProcess::All)) {
m_filter.resume();
signalProgress(0, m_file->size(), i18n("Reading TQIF ..."));
slotSendDataToFilter();
rc = true;
} else {
tqDebug("starting filter failed :-(");
}
#endif
}
return rc;
}
bool MyMoneyQifReader::finishImport(void)
{
bool rc = false;
#ifdef DEBUG_IMPORT
delete m_file;
m_file = 0;
// remove the Don't ask again entries
KConfig* config = KGlobal::config();
config->setGroup(TQString::fromLatin1("Notification Messages"));
TQStringList::ConstIterator it;
for(it = m_dontAskAgain.begin(); it != m_dontAskAgain.end(); ++it) {
config->deleteEntry(*it);
}
config->sync();
m_dontAskAgain.clear();
m_accountTranslation.clear();
signalProgress(-1, -1);
rc = !m_userAbort;
#else
if(!m_filter.isRunning()) {
delete m_file;
m_file = 0;
// remove the Don't ask again entries
KConfig* config = KGlobal::config();
config->setGroup(TQString::fromLatin1("Notification Messages"));
TQStringList::ConstIterator it;
for(it = m_dontAskAgain.begin(); it != m_dontAskAgain.end(); ++it) {
config->deleteEntry(*it);
}
config->sync();
m_dontAskAgain.clear();
m_accountTranslation.clear();
signalProgress(-1, -1);
rc = !m_userAbort && m_filter.normalExit();
} else {
tqWarning("MyMoneyQifReader::finishImport() must not be called while the filter\n\tprocess is still running.");
}
#endif
// if a temporary file was constructed by NetAccess::download,
// then it will be removed with the next call. Otherwise, it
// stays untouched on the local filesystem
KIO::NetAccess::removeTempFile(m_filename);
#if 0
// Add the transaction entries
KProgressDialog dlg(0,"transactionaddprogress",i18n("Adding transactions"),i18n("Now adding the transactions to your ledger..."));
dlg.progressBar()->setTotalSteps(m_transactionCache.count());
dlg.progressBar()->setTextEnabled(true);
dlg.setAllowCancel(true);
dlg.show();
kapp->processEvents();
MyMoneyFile* file = MyMoneyFile::instance();
TQValueList<MyMoneyTransaction>::iterator it = m_transactionCache.begin();
MyMoneyFileTransaction ft;
try
{
while( it != m_transactionCache.end() )
{
if ( dlg.wasCancelled() )
{
m_userAbort = true;
rc = false;
break;
}
file->addTransaction(*it);
dlg.progressBar()->advance(1);
++it;
}
if(rc)
ft.commit();
} catch(MyMoneyException *e) {
KMessageBox::detailedSorry(0, i18n("Unable to add transactions"),
(e->what() + " " + i18n("thrown in") + " " + e->file()+ ":%1").arg(e->line()));
delete e;
rc = false;
}
#endif
// Now to import the statements
TQValueList<MyMoneyStatement>::const_iterator it_st;
for(it_st = d->statements.begin(); it_st != d->statements.end(); ++it_st)
kmymoney2->slotStatementImport(*it_st);
return rc;
}
void MyMoneyQifReader::processQifSpecial(const TQString& _line)
{
TQString line = _line.mid(1); // get rid of exclamation mark
// TQString test = line.left(5).lower();
if(line.left(5).lower() == TQString("type:")) {
line = line.mid(5);
// exportable accounts
if(line.lower() == "ccard" || KMyMoneyGlobalSettings::qifCreditCard().lower().contains(line.lower())) {
d->accountType = MyMoneyAccount::CreditCard;
d->firstTransaction = true;
d->transactionType = m_entryType = EntryTransaction;
} else if(line.lower() == "bank" || KMyMoneyGlobalSettings::qifBank().lower().contains(line.lower())) {
d->accountType = MyMoneyAccount::Checkings;
d->firstTransaction = true;
d->transactionType = m_entryType = EntryTransaction;
} else if(line.lower() == "cash" || KMyMoneyGlobalSettings::qifCash().lower().contains(line.lower())) {
d->accountType = MyMoneyAccount::Cash;
d->firstTransaction = true;
d->transactionType = m_entryType = EntryTransaction;
} else if(line.lower() == "oth a" || KMyMoneyGlobalSettings::qifAsset().lower().contains(line.lower())) {
d->accountType = MyMoneyAccount::Asset;
d->firstTransaction = true;
d->transactionType = m_entryType = EntryTransaction;
} else if(line.lower() == "oth l" || line.lower() == i18n("TQIF tag for liability account", "Oth L").lower()) {
d->accountType = MyMoneyAccount::Liability;
d->firstTransaction = true;
d->transactionType = m_entryType = EntryTransaction;
} else if(line.lower() == "invst" || line.lower() == i18n("TQIF tag for investment account", "Invst").lower()) {
d->transactionType = m_entryType = EntryInvestmentTransaction;
} else if(line.lower() == "invoice" || KMyMoneyGlobalSettings::qifInvoice().lower().contains(line.lower())) {
m_entryType = EntrySkip;
} else if(line.lower() == "tax") {
m_entryType = EntrySkip;
} else if(line.lower() == "bill") {
m_entryType = EntrySkip;
// exportable lists
} else if(line.lower() == "cat" || line.lower() == i18n("TQIF tag for category", "Cat").lower()) {
m_entryType = EntryCategory;
} else if(line.lower() == "security" || line.lower() == i18n("TQIF tag for security", "Security").lower()) {
m_entryType = EntrySecurity;
} else if(line.lower() == "prices" || line.lower() == i18n("TQIF tag for prices", "Prices").lower()) {
m_entryType = EntryPrice;
} else if(line.lower() == "payee") {
m_entryType = EntryPayee;
} else if(line.lower() == "class" || line.lower() == i18n("TQIF tag for a class", "Class").lower()) {
m_entryType = EntryClass;
} else if(line.lower() == "memorized") {
m_entryType = EntryMemorizedTransaction;
} else if(line.lower() == "budget") {
m_entryType = EntrySkip;
} else if(line.lower() == "invitem") {
m_entryType = EntrySkip;
} else if(line.lower() == "template") {
m_entryType = EntrySkip;
} else {
tqWarning("Unknown export header '!Type:%s' in TQIF file on line %d: Skipping section.", line.data(), m_linenumber);
m_entryType = EntrySkip;
}
// account headers
} else if(line.lower() == "account") {
m_entryType = EntryAccount;
} else if(line.lower() == "option:autoswitch") {
m_entryType = EntryAccount;
} else if(line.lower() == "clear:autoswitch") {
m_entryType = d->transactionType;
}
}
void MyMoneyQifReader::processQifEntry(void)
{
// This method processes a 'TQIF Entry' which is everything between two caret
// signs
//
try {
switch(m_entryType) {
case EntryCategory:
processCategoryEntry();
break;
case EntryUnknown:
kdDebug(2) << "Line " << m_linenumber << ": Warning: Found an entry without a type being specified. Checking assumed." << endl;
processTransactionEntry();
break;
case EntryTransaction:
processTransactionEntry();
break;
case EntryInvestmentTransaction:
processInvestmentTransactionEntry();
break;
case EntryAccount:
processAccountEntry();
break;
case EntrySecurity:
processSecurityEntry();
break;
case EntryPrice:
processPriceEntry();
break;
case EntryPayee:
processPayeeEntry();
break;
case EntryClass:
kdDebug(2) << "Line " << m_linenumber << ": Classes are not yet supported!" << endl;
break;
case EntryMemorizedTransaction:
kdDebug(2) << "Line " << m_linenumber << ": Memorized transactions are not yet implemented!" << endl;
break;
case EntrySkip:
break;
default:
kdDebug(2) << "Line " << m_linenumber<< ": EntryType " << m_entryType <<" not yet implemented!" << endl;
break;
}
} catch(MyMoneyException *e) {
if(e->what() != "USERABORT") {
kdDebug(2) << "Line " << m_linenumber << ": Unhandled error: " << e->what() << endl;
} else {
m_userAbort = true;
}
delete e;
}
}
const TQString MyMoneyQifReader::extractLine(const TQChar id, int cnt)
{
TQStringList::ConstIterator it;
m_extractedLine = -1;
for(it = m_qifEntry.begin(); it != m_qifEntry.end(); ++it) {
m_extractedLine++;
if((*it)[0] == id) {
if(cnt-- == 1) {
if((*it).mid(1).isEmpty())
return TQString(" ");
return (*it).mid(1);
}
}
}
m_extractedLine = -1;
return TQString::null;
}
void MyMoneyQifReader::extractSplits(TQValueList<qSplit>& listqSplits) const
{
// *** With apologies to TQString MyMoneyQifReader::extractLine ***
TQStringList::ConstIterator it;
for(it = m_qifEntry.begin(); it != m_qifEntry.end(); ++it) {
if((*it)[0] == "S") {
qSplit q;
q.m_strCategoryName = (*it++).mid(1); // 'S'
if((*it)[0] == "E") {
q.m_strMemo = (*it++).mid(1); // 'E'
d->fixMultiLineMemo(q.m_strMemo);
}
if((*it)[0] == "$") {
q.m_amount = (*it).mid(1); // '$'
}
listqSplits += q;
}
}
}
#if 0
void MyMoneyQifReader::processMSAccountEntry(const MyMoneyAccount::accountTypeE accountType)
{
if(extractLine('P').lower() == m_qifProfile.openingBalanceText().lower()) {
m_account = MyMoneyAccount();
m_account.setAccountType(accountType);
TQString txt = extractLine('T');
MyMoneyMoney balance = m_qifProfile.value('T', txt);
TQDate date = m_qifProfile.date(extractLine('D'));
m_account.setOpeningDate(date);
TQString name = extractLine('L');
if(name.left(1) == m_qifProfile.accountDelimiter().left(1)) {
name = name.mid(1, name.length()-2);
}
d->st_AccountName = name;
m_account.setName(name);
selectOrCreateAccount(Select, m_account, balance);
d->st.m_accountId = m_account.id();
if ( ! balance.isZero() )
{
MyMoneyFile* file = MyMoneyFile::instance();
TQString openingtxid = file->openingBalanceTransaction(m_account);
MyMoneyFileTransaction ft;
if ( ! openingtxid.isEmpty() )
{
MyMoneyTransaction openingtx = file->transaction(openingtxid);
MyMoneySplit split = openingtx.splitByAccount(m_account.id());
if ( split.shares() != balance )
{
const MyMoneySecurity& sec = file->security(m_account.currencyId());
if ( KMessageBox::questionYesNo(
tqApp->mainWidget(),
i18n("The %1 account currently has an opening balance of %2. This TQIF file reports an opening balance of %3. Would you like to overwrite the current balance with the one from the TQIF file?").arg(m_account.name(), split.shares().formatMoney(m_account, sec),balance.formatMoney(m_account, sec)),
i18n("Overwrite opening balance"),
KStdGuiItem::yes(),
KStdGuiItem::no(),
"OverwriteOpeningBalance" )
== KMessageBox::Yes )
{
file->removeTransaction( openingtx );
m_account.setOpeningDate( date );
file->createOpeningBalanceTransaction( m_account, balance );
}
}
}
else
{
// Add an opening balance
m_account.setOpeningDate( date );
file->createOpeningBalanceTransaction( m_account, balance );
}
ft.commit();
}
} else {
// for some unknown reason, Quicken 2001 generates the following (somewhat
// misleading) sequence of lines:
//
// 1: !Account
// 2: NAT&T Universal
// 3: DAT&T Univers(...xxxx) [CLOSED]
// 4: TCCard
// 5: ^
// 6: !Type:CCard
// 7: !Account
// 8: NCFCU Visa
// 9: DRick's CFCU Visa card (...xxxx)
// 10: TCCard
// 11: ^
// 12: !Type:CCard
// 13: D1/ 4' 1
//
// Lines 1-5 are processed via processQifEntry() and processAccountEntry()
// Then Quicken issues line 6 but since the account does not carry any
// transaction does not write an end delimiter. Arrrgh! So we end up with
// a TQIF entry comprising of lines 6-11 and end up in this routine. Actually,
// lines 7-11 are the leadin for the next account. So we check here if
// the !Type:xxx record also contains an !Account line and process the
// entry as required.
//
// (Ace) I think a better solution here is to handle exclamation point
// lines separately from entries. In the above case:
// Line 1 would set the mode to "account entries".
// Lines 2-5 would be interpreted as an account entry. This would set m_account.
// Line 6 would set the mode to "cc transaction entries".
// Line 7 would immediately set the mode to "account entries" again
// Lines 8-11 would be interpreted as an account entry. This would set m_account.
// Line 12 would set the mode to "cc transaction entries"
// Lines 13+ would be interpreted as cc transaction entries, and life is good
int exclamationCnt = 1;
TQString category;
do {
category = extractLine('!', exclamationCnt++);
} while(!category.isEmpty() && category != "Account");
// we have such a weird empty account
if(category == "Account") {
processAccountEntry();
} else
{
selectOrCreateAccount(Select, m_account);
d->st_AccountName = m_account.name();
d->st.m_strAccountName = m_account.name();
d->st.m_accountId = m_account.id();
d->st.m_strAccountNumber = m_account.id();
m_account.setNumber(m_account.id());
if ( m_entryType == EntryInvestmentTransaction )
processInvestmentTransactionEntry();
else
processTransactionEntry();
}
}
}
#endif
void MyMoneyQifReader::processPayeeEntry(void)
{
// TODO
}
void MyMoneyQifReader::processCategoryEntry(void)
{
MyMoneyFile* file = MyMoneyFile::instance();
MyMoneyAccount account = MyMoneyAccount();
account.setName(extractLine('N'));
account.setDescription(extractLine('D'));
MyMoneyAccount parentAccount;
if(!extractLine('I').isEmpty()) {
account.setAccountType(MyMoneyAccount::Income);
parentAccount = file->income();
} else if(!extractLine('E').isEmpty()) {
account.setAccountType(MyMoneyAccount::Expense);
parentAccount = file->expense();
}
// check if we can find the account already in the file
MyMoneyAccount acc = kmymoney2->findAccount(account, MyMoneyAccount());
// if not, we just create it
if(acc.id().isEmpty()) {
MyMoneyAccount brokerage;
MyMoneyMoney balance;
kmymoney2->createAccount(account, parentAccount, brokerage, balance);
}
}
TQString MyMoneyQifReader::transferAccount(TQString name, bool useBrokerage)
{
TQString accountId;
TQStringList tmpEntry = m_qifEntry; // keep temp copies
MyMoneyAccount tmpAccount = m_account;
m_qifEntry.clear(); // and construct a temp entry to create/search the account
m_qifEntry << TQString("N%1").arg(name);
m_qifEntry << TQString("Tunknown");
m_qifEntry << TQString("D%1").arg(i18n("Autogenerated by TQIF importer"));
accountId = processAccountEntry(false);
// in case we found a reference to an investment account, we need
// to switch to the brokerage account instead.
MyMoneyAccount acc = MyMoneyFile::instance()->account(accountId);
if(useBrokerage && (acc.accountType() == MyMoneyAccount::Investment)) {
name = acc.brokerageName();
m_qifEntry.clear(); // and construct a temp entry to create/search the account
m_qifEntry << TQString("N%1").arg(name);
m_qifEntry << TQString("Tunknown");
m_qifEntry << TQString("D%1").arg(i18n("Autogenerated by TQIF importer"));
accountId = processAccountEntry(false);
}
m_qifEntry = tmpEntry; // restore local copies
m_account = tmpAccount;
return accountId;
}
void MyMoneyQifReader::createOpeningBalance(MyMoneyAccount::_accountTypeE accType)
{
MyMoneyFile* file = MyMoneyFile::instance();
// if we don't have a name for the current account we need to extract the name from the L-record
if(m_account.name().isEmpty()) {
TQString name = extractLine('L');
if(name.isEmpty()) {
name = i18n("TQIF imported, no account name supplied");
}
d->isTransfer(name, m_qifProfile.accountDelimiter().left(1), m_qifProfile.accountDelimiter().mid(1,1));
TQStringList entry = m_qifEntry; // keep a temp copy
m_qifEntry.clear(); // and construct a temp entry to create/search the account
m_qifEntry << TQString("N%1").arg(name);
m_qifEntry << TQString("T%1").arg(d->accountTypeToQif(accType));
m_qifEntry << TQString("D%1").arg(i18n("Autogenerated by TQIF importer"));
processAccountEntry();
m_qifEntry = entry; // restore local copy
}
MyMoneyFileTransaction ft;
try {
bool needCreate = true;
MyMoneyAccount acc = m_account;
// in case we're dealing with an investment account, we better use
// the accompanying brokerage account for the opening balance
acc = file->accountByName(m_account.brokerageName());
// check if we already have an opening balance transaction
TQString tid = file->openingBalanceTransaction(acc);
MyMoneyTransaction ot;
if(!tid.isEmpty()) {
ot = file->transaction(tid);
MyMoneySplit s0 = ot.splitByAccount(acc.id());
// if the value is the same, we can silently skip this transaction
if(s0.shares() == m_qifProfile.value('T', extractLine('T'))) {
needCreate = false;
}
if(needCreate) {
// in case we create it anyway, we issue a warning to the user to check it manually
KMessageBox::sorry(0, TQString("<qt>%1</qt>").arg(i18n("KMyMoney has imported a second opening balance transaction into account <b>%1</b> which differs from the one found already on file. Please correct this manually once the import is done.").arg(acc.name())), i18n("Opening balance problem"));
}
}
if(needCreate) {
acc.setOpeningDate(m_qifProfile.date(extractLine('D')));
file->modifyAccount(acc);
MyMoneyTransaction t = file->createOpeningBalanceTransaction(acc, m_qifProfile.value('T', extractLine('T')));
if(!t.id().isEmpty()) {
t.setImported();
file->modifyTransaction(t);
}
ft.commit();
}
// make sure to use the updated version of the account
if(m_account.id() == acc.id())
m_account = acc;
// remember which account we created
d->st.m_accountId = m_account.id();
} catch(MyMoneyException* e) {
KMessageBox::detailedError(0,
i18n("Error while creating opening balance transaction"),
TQString("%1(%2):%3").arg(e->file()).arg(e->line()).arg(e->what()),
i18n("File access error"));
delete e;
}
}
void MyMoneyQifReader::processTransactionEntry(void)
{
++m_transactionsProcessed;
// in case the user selected to skip the account or the account
// was not found we skip this transaction
/*
if(m_account.id().isEmpty()) {
m_transactionsSkipped++;
return;
}
*/
MyMoneyFile* file = MyMoneyFile::instance();
MyMoneyStatement::Split s1;
MyMoneyStatement::Transaction tr;
TQString tmp;
TQString accountId;
int pos;
TQString payee = extractLine('P');
unsigned long h;
h = MyMoneyTransaction::hash(m_qifEntry.join(";"));
TQString hashBase;
hashBase.sprintf("%s-%07lx", m_qifProfile.date(extractLine('D')).toString(Qt::ISODate).data(), h);
int idx = 1;
TQString hash;
for(;;) {
hash = TQString("%1-%2").arg(hashBase).arg(idx);
TQMap<TQString, bool>::const_iterator it;
it = d->m_hashMap.find(hash);
if(it == d->m_hashMap.end()) {
d->m_hashMap[hash] = true;
break;
}
++idx;
}
tr.m_strBankID = hash;
if(d->firstTransaction) {
// check if this is an opening balance transaction and process it out of the statement
if(!payee.isEmpty() && ((payee.lower() == "opening balance") || KMyMoneyGlobalSettings::qifOpeningBalance().lower().contains(payee.lower()))) {
createOpeningBalance();
d->firstTransaction = false;
return;
}
}
// Process general transaction data
if(d->st.m_accountId.isEmpty())
d->st.m_accountId = m_account.id();
s1.m_accountId = d->st.m_accountId;
d->st.m_eType = MyMoneyStatement::etCheckings;
tr.m_datePosted = (m_qifProfile.date(extractLine('D')));
if(!tr.m_datePosted.isValid())
{
int rc = KMessageBox::warningContinueCancel(0,
i18n("The date entry \"%1\" read from the file cannot be interpreted through the current "
"date profile setting of \"%2\".\n\nPressing \"Continue\" will "
"assign todays date to the transaction. Pressing \"Cancel\" will abort "
"the import operation. You can then restart the import and select a different "
"TQIF profile or create a new one.")
.arg(extractLine('D')).arg(m_qifProfile.inputDateFormat()),
i18n("Invalid date format"));
switch(rc) {
case KMessageBox::Continue:
tr.m_datePosted = (TQDate::currentDate());
break;
case KMessageBox::Cancel:
throw new MYMONEYEXCEPTION("USERABORT");
break;
}
}
tmp = extractLine('L');
pos = tmp.findRev("--");
if(tmp.left(1) == m_qifProfile.accountDelimiter().left(1)) {
// it's a transfer, so we wipe the memo
// tmp = ""; why??
// st.m_strAccountName = tmp;
} else if(pos != -1) {
// what's this?
// t.setValue("Dialog", tmp.mid(pos+2));
tmp = tmp.left(pos);
}
// t.setMemo(tmp);
// Assign the "#" field to the transaction's bank id
// This is the custom KMM extension to TQIF for a unique ID
tmp = extractLine('#');
if(!tmp.isEmpty())
{
tr.m_strBankID = TQString("ID %1").arg(tmp);
}
#if 0
// Collect data for the account's split
s1.m_accountId = m_account.id();
tmp = extractLine('S');
pos = tmp.findRev("--");
if(pos != -1) {
tmp = tmp.left(pos);
}
if(tmp.left(1) == m_qifProfile.accountDelimiter().left(1))
// it's a transfer, extract the account name
tmp = tmp.mid(1, tmp.length()-2);
s1.m_strCategoryName = tmp;
#endif
// TODO (Ace) Deal with currencies more gracefully. TQIF cannot deal with multiple
// currencies, so we should assume that transactions imported into a given
// account are in THAT ACCOUNT's currency. If one of those involves a transfer
// to an account with a different currency, value and shares should be
// different. (Shares is in the target account's currency, value is in the
// transaction's)
s1.m_amount = m_qifProfile.value('T', extractLine('T'));
tr.m_amount = m_qifProfile.value('T', extractLine('T'));
tr.m_shares = m_qifProfile.value('T', extractLine('T'));
tmp = extractLine('N');
if (!tmp.isEmpty())
tr.m_strNumber = tmp;
if(!payee.isEmpty()) {
tr.m_strPayee = payee;
}
tr.m_reconcile = d->reconcileState(extractLine('C'));
tr.m_strMemo = extractLine('M');
d->fixMultiLineMemo(tr.m_strMemo);
s1.m_strMemo = tr.m_strMemo;
// tr.m_listSplits.append(s1);
if(extractLine('$').isEmpty()) {
MyMoneyAccount account;
// use the same values for the second split, but clear the ID and reverse the value
MyMoneyStatement::Split s2 = s1;
s2.m_reconcile = tr.m_reconcile;
s2.m_amount = (-s1.m_amount);
// s2.clearId();
// standard transaction
tmp = extractLine('L');
if(d->isTransfer(tmp, m_qifProfile.accountDelimiter().left(1), m_qifProfile.accountDelimiter().mid(1, 1))) {
accountId = transferAccount(tmp, false);
} else {
/* pos = tmp.findRev("--");
if(pos != -1) {
t.setValue("Dialog", tmp.mid(pos+2));
tmp = tmp.left(pos);
}*/
// it's an expense / income
tmp = tmp.stripWhiteSpace();
accountId = checkCategory(tmp, s1.m_amount, s2.m_amount);
}
if(!accountId.isEmpty()) {
try {
MyMoneyAccount account = file->account(accountId);
// FIXME: check that the type matches and ask if not
if ( account.accountType() == MyMoneyAccount::Investment )
{
kdDebug(0) << "Line " << m_linenumber << ": Cannot transfer to/from an investment account. Transaction ignored." << endl;
return;
}
if ( account.id() == m_account.id() )
{
kdDebug(0) << "Line " << m_linenumber << ": Cannot transfer to the same account. Transfer ignored." << endl;
accountId = TQString();
}
} catch (MyMoneyException *e) {
kdDebug(0) << "Line " << m_linenumber << ": Account with id " << accountId.data() << " not found" << endl;
accountId = TQString();
delete e;
}
}
if(!accountId.isEmpty()) {
s2.m_accountId = accountId;
s2.m_strCategoryName = tmp;
tr.m_listSplits.append(s2);
}
} else {
// split transaction
TQValueList<qSplit> listqSplits;
extractSplits(listqSplits); // ****** ensure each field is ******
// * attached to correct split *
int count;
for(count = 1; !extractLine('$', count).isEmpty(); ++count)
{
MyMoneyStatement::Split s2 = s1;
s2.m_amount = (-m_qifProfile.value('$', listqSplits[count-1].m_amount)); // Amount of split
s2.m_strMemo = listqSplits[count-1].m_strMemo; // Memo in split
tmp = listqSplits[count-1].m_strCategoryName; // Category in split
if(d->isTransfer(tmp, m_qifProfile.accountDelimiter().left(1), m_qifProfile.accountDelimiter().mid(1, 1)))
{
accountId = transferAccount(tmp, false);
} else {
pos = tmp.findRev("--");
if(pos != -1) {
/// t.setValue("Dialog", tmp.mid(pos+2));
tmp = tmp.left(pos);
}
tmp = tmp.stripWhiteSpace();
accountId = checkCategory(tmp, s1.m_amount, s2.m_amount);
}
if(!accountId.isEmpty()) {
try {
MyMoneyAccount account = file->account(accountId);
// FIXME: check that the type matches and ask if not
if ( account.accountType() == MyMoneyAccount::Investment )
{
kdDebug(0) << "Line " << m_linenumber << ": Cannot convert a split transfer to/from an investment account. Split removed. Total amount adjusted from " << tr.m_amount.formatMoney("", 2) << " to " << (tr.m_amount + s2.m_amount).formatMoney("", 2) << "\n";
tr.m_amount += s2.m_amount;
continue;
}
if ( account.id() == m_account.id() )
{
kdDebug(0) << "Line " << m_linenumber << ": Cannot transfer to the same account. Transfer ignored." << endl;
accountId = TQString();
}
} catch (MyMoneyException *e) {
kdDebug(0) << "Line " << m_linenumber << ": Account with id " << accountId.data() << " not found" << endl;
accountId = TQString();
delete e;
}
}
if(!accountId.isEmpty())
{
s2.m_accountId = accountId;
s2.m_strCategoryName = tmp;
tr.m_listSplits += s2;
// in case the transaction does not have a memo and we
// process the first split just copy the memo over
if(tr.m_listSplits.count() == 1 && tr.m_strMemo.isEmpty())
tr.m_strMemo = s2.m_strMemo;
}
else
{
// TODO add an option to create a "Unassigned" category
// for now, we just drop the split which will show up as unbalanced
// transaction in the KMyMoney ledger view
}
}
}
// Add the transaction to the statement
d->st.m_listTransactions +=tr;
}
void MyMoneyQifReader::processInvestmentTransactionEntry(void)
{
// kdDebug(2) << "Investment Transaction:" << m_qifEntry.count() << " lines" << endl;
/*
Items for Investment Accounts
Field Indicator Explanation
D Date
N Action
Y Security (NAME, not symbol)
I Price
Q Quantity (number of shares or split ratio)
T Transaction amount
C Cleared status
P Text in the first line for transfers and reminders (Payee)
M Memo
O Commission
L Account for the transfer
$ Amount transferred
^ End of the entry
It will be presumed all transactions are to the associated cash account, if
one exists, unless otherwise noted by the 'L' field.
Expense/Income categories will be automatically generated, "_Dividend",
"_InterestIncome", etc.
*/
MyMoneyFile* file = MyMoneyFile::instance();
MyMoneyStatement::Transaction tr;
d->st.m_eType = MyMoneyStatement::etInvestment;
// t.setCommodity(m_account.currencyId());
// 'D' field: Date
TQDate date = m_qifProfile.date(extractLine('D'));
if(date.isValid())
tr.m_datePosted = date;
else
{
int rc = KMessageBox::warningContinueCancel(0,
i18n("The date entry \"%1\" read from the file cannot be interpreted through the current "
"date profile setting of \"%2\".\n\nPressing \"Continue\" will "
"assign todays date to the transaction. Pressing \"Cancel\" will abort "
"the import operation. You can then restart the import and select a different "
"TQIF profile or create a new one.")
.arg(extractLine('D')).arg(m_qifProfile.inputDateFormat()),
i18n("Invalid date format"));
switch(rc) {
case KMessageBox::Continue:
tr.m_datePosted = TQDate::currentDate();
break;
case KMessageBox::Cancel:
throw new MYMONEYEXCEPTION("USERABORT");
break;
}
}
// 'M' field: Memo
TQString memo = extractLine('M');
d->fixMultiLineMemo(memo);
tr.m_strMemo = memo;
unsigned long h;
h = MyMoneyTransaction::hash(m_qifEntry.join(";"));
TQString hashBase;
hashBase.sprintf("%s-%07lx", m_qifProfile.date(extractLine('D')).toString(Qt::ISODate).data(), h);
int idx = 1;
TQString hash;
for(;;) {
hash = TQString("%1-%2").arg(hashBase).arg(idx);
TQMap<TQString, bool>::const_iterator it;
it = d->m_hashMap.find(hash);
if(it == d->m_hashMap.end()) {
d->m_hashMap[hash] = true;
break;
}
++idx;
}
tr.m_strBankID = hash;
// '#' field: BankID
TQString tmp = extractLine('#');
if ( ! tmp.isEmpty() )
tr.m_strBankID = TQString("ID %1").arg(tmp);
// Reconciliation flag
tr.m_reconcile = d->reconcileState(extractLine('C'));
// 'O' field: Fees
tr.m_fees = m_qifProfile.value('T', extractLine('O'));
// 'T' field: Amount
MyMoneyMoney amount = m_qifProfile.value('T', extractLine('T'));
tr.m_amount = amount;
MyMoneyStatement::Price price;
price.m_date = date;
price.m_strSecurity = extractLine('Y');
price.m_amount = m_qifProfile.value('T', extractLine('I'));
#if 0 // we must check for that later, because certain activities don't need a security
// 'Y' field: Security name
TQString securityname = extractLine('Y').lower();
if ( securityname.isEmpty() )
{
kdDebug(2) << "Line " << m_linenumber << ": Investment transaction without a security is not supported." << endl;
return;
}
tr.m_strSecurity = securityname;
#endif
#if 0
// For now, we let the statement reader take care of that.
// The big problem here is that the Y field is not the SYMBOL, it's the NAME.
// The name is not very unique, because people could have used slightly different
// abbreviations or ordered words differently, etc.
//
// If there is a perfect name match with a subordinate stock account, great.
// More likely, we have to rely on the TQIF file containing !Type:Security
// records, which tell us the mapping from name to symbol.
//
// Therefore, generally it is not recommended to import a TQIF file containing
// investment transactions but NOT containing security records.
TQString securitysymbol = m_investmentMap[securityname];
// the correct account is the stock account which matches two criteria:
// (1) it is a sub-account of the selected investment account, and either
// (2a) the security name of the transaction matches the name of the security, OR
// (2b) the security name of the transaction maps to a symbol which matches the symbol of the security
// search through each subordinate account
bool found = false;
MyMoneyAccount thisaccount = m_account;
TQStringList accounts = thisaccount.accountList();
TQStringList::const_iterator it_account = accounts.begin();
while( !found && it_account != accounts.end() )
{
TQString currencyid = file->account(*it_account).currencyId();
MyMoneySecurity security = file->security( currencyid );
TQString symbol = security.tradingSymbol().lower();
TQString name = security.name().lower();
if ( securityname == name || securitysymbol == symbol )
{
d->st_AccountId = *it_account;
s1.m_accountId = *it_account;
thisaccount = file->account(*it_account);
found = true;
#if 0
// update the price, while we're here. in the future, this should be
// an option
TQString basecurrencyid = file->baseCurrency().id();
MyMoneyPrice price = file->price( currencyid, basecurrencyid, t_in.m_datePosted, true );
if ( !price.isValid() )
{
MyMoneyPrice newprice( currencyid, basecurrencyid, t_in.m_datePosted, t_in.m_moneyAmount / t_in.m_dShares, i18n("Statement Importer") );
file->addPrice(newprice);
}
#endif
}
++it_account;
}
if (!found)
{
kdDebug(2) << "Line " << m_linenumber << ": Security " << securityname << " not found in this account. Transaction ignored." << endl;
// If the security is not known, notify the user
// TODO (Ace) A "SelectOrCreateAccount" interface for investments
KMessageBox::information(0, i18n("This investment account does not contain the \"%1\" security. "
"Transactions involving this security will be ignored.").arg(securityname),
i18n("Security not found"),
TQString("MissingSecurity%1").arg(securityname.stripWhiteSpace()));
return;
}
#endif
// 'Y' field: Security
tr.m_strSecurity = extractLine('Y');
// 'Q' field: Quantity
MyMoneyMoney quantity = m_qifProfile.value('T', extractLine('Q'));
// 'N' field: Action
TQString action = extractLine('N').lower();
// remove trailing X, which seems to have no purpose (?!)
bool xAction = false;
if ( action.endsWith("x") ) {
action = action.left( action.length() - 1 );
xAction = true;
}
// Whether to create a cash split for the other side of the value
TQString accountname ;//= extractLine('L');
if ( action == "reinvdiv" || action == "reinvlg" || action == "reinvsh" )
{
d->st.m_listPrices += price;
tr.m_shares = quantity;
tr.m_eAction = (MyMoneyStatement::Transaction::eaReinvestDividend);
tr.m_price = m_qifProfile.value('I', extractLine('I'));
tr.m_strInterestCategory = extractLine('L');
if(tr.m_strInterestCategory.isEmpty()) {
tr.m_strInterestCategory = d->typeToAccountName(action);
}
}
else if ( action == "div" || action == "cgshort" || action == "cgmid" || action == "cglong" || action == "rtrncap")
{
tr.m_eAction = (MyMoneyStatement::Transaction::eaCashDividend);
TQString tmp = extractLine('L');
// if the action ends in an X, the L-Record contains the asset account
// to which the dividend should be transferred. In the other cases, it
// may contain a category that identifies the income category for the
// dividend payment
if((xAction == true)
&& (d->isTransfer(tmp, m_qifProfile.accountDelimiter().left(1), m_qifProfile.accountDelimiter().mid(1, 1)) == true)) {
tr.m_strBrokerageAccount = tmp;
transferAccount(tmp); // make sure the account exists
} else {
tr.m_strInterestCategory = tmp;
}
// make sure, we have valid category. Either taken from the L-Record above,
// or derived from the action code
if(tr.m_strInterestCategory.isEmpty()) {
tr.m_strInterestCategory = d->typeToAccountName(action);
}
// For historic reasons (coming from the OFX importer) the statement
// reader expects the dividend with a reverse sign. So we just do that.
tr.m_amount = -(amount - tr.m_fees);
// We need an extra split which will be the zero-amount investment split
// that serves to mark this transaction as a cash dividend and note which
// stock account it belongs to.
MyMoneyStatement::Split s2;
s2.m_amount = MyMoneyMoney();
s2.m_strCategoryName = extractLine('Y');
tr.m_listSplits.append(s2);
}
else if ( action == "intinc" || action == "miscinc" || action == "miscexp")
{
tr.m_eAction = (MyMoneyStatement::Transaction::eaInterest);
if(action == "miscexp")
tr.m_eAction = (MyMoneyStatement::Transaction::eaFees);
TQString tmp = extractLine('L');
// if the action ends in an X, the L-Record contains the asset account
// to which the dividend should be transferred. In the other cases, it
// may contain a category that identifies the income category for the
// payment
if((xAction == true)
&& (d->isTransfer(tmp, m_qifProfile.accountDelimiter().left(1), m_qifProfile.accountDelimiter().mid(1, 1)) == true)) {
tr.m_strBrokerageAccount = tmp;
transferAccount(tmp); // make sure the account exists
} else {
tr.m_strInterestCategory = tmp;
}
// make sure, we have a valid category. Either taken from the L-Record above,
// or derived from the action code
if(tr.m_strInterestCategory.isEmpty()) {
tr.m_strInterestCategory = d->typeToAccountName(action);
}
// For historic reasons (coming from the OFX importer) the statement
// reader expects the dividend with a reverse sign. So we just do that.
if(action != "miscexp")
tr.m_amount = -(amount - tr.m_fees);
if(tr.m_strMemo.isEmpty())
tr.m_strMemo = (TQString("%1 %2").arg(extractLine('Y')).arg(d->typeToAccountName(action))).stripWhiteSpace();
}
else if (action == "xin" || action == "xout")
{
TQString payee = extractLine('P');
if(!payee.isEmpty() && ((payee.lower() == "opening balance") || KMyMoneyGlobalSettings::qifOpeningBalance().lower().contains(payee.lower()))) {
createOpeningBalance(MyMoneyAccount::Investment);
return;
}
tr.m_eAction = (MyMoneyStatement::Transaction::eaNone);
MyMoneyStatement::Split s2;
TQString tmp = extractLine('L');
if(d->isTransfer(tmp, m_qifProfile.accountDelimiter().left(1), m_qifProfile.accountDelimiter().mid(1, 1))) {
s2.m_accountId = transferAccount(tmp);
s2.m_strCategoryName = tmp;
} else {
s2.m_strCategoryName = extractLine('L');
if(tr.m_strInterestCategory.isEmpty()) {
s2.m_strCategoryName = d->typeToAccountName(action);
}
}
if(action == "xout")
tr.m_amount = -tr.m_amount;
s2.m_amount = -tr.m_amount;
tr.m_listSplits.append(s2);
}
else if (action == "buy")
{
TQString tmp = extractLine('L');
if(d->isTransfer(tmp, m_qifProfile.accountDelimiter().left(1), m_qifProfile.accountDelimiter().mid(1, 1)) == true) {
tr.m_strBrokerageAccount = tmp;
transferAccount(tmp); // make sure the account exists
}
d->st.m_listPrices += price;
tr.m_shares = quantity;
tr.m_eAction = (MyMoneyStatement::Transaction::eaBuy);
}
else if (action == "sell")
{
TQString tmp = extractLine('L');
if(d->isTransfer(tmp, m_qifProfile.accountDelimiter().left(1), m_qifProfile.accountDelimiter().mid(1, 1)) == true) {
tr.m_strBrokerageAccount = tmp;
transferAccount(tmp); // make sure the account exists
}
d->st.m_listPrices += price;
tr.m_shares = -quantity;
tr.m_amount = -amount;
tr.m_eAction = (MyMoneyStatement::Transaction::eaSell);
}
else if ( action == "shrsin" )
{
tr.m_shares = quantity;
tr.m_eAction = (MyMoneyStatement::Transaction::eaShrsin);
}
else if ( action == "shrsout" )
{
tr.m_shares = -quantity;
tr.m_eAction = (MyMoneyStatement::Transaction::eaShrsout);
}
else if ( action == "stksplit" )
{
MyMoneyMoney splitfactor = (quantity / MyMoneyMoney(10,1)).reduce();
// Stock splits not supported
// kdDebug(2) << "Line " << m_linenumber << ": Stock split not supported (date=" << date << " security=" << securityname << " factor=" << splitfactor.toString() << ")" << endl;
// s1.setShares(splitfactor);
// s1.setValue(0);
// s1.setAction(MyMoneySplit::ActionSplitShares);
// return;
}
else
{
// Unsupported action type
kdDebug(0) << "Line " << m_linenumber << ": Unsupported transaction action (" << action << ")" << endl;
return;
}
d->st.m_strAccountName = accountname;
d->st.m_listTransactions +=tr;
/*************************************************************************
*
* These transactions are natively supported by KMyMoney
*
*************************************************************************/
/*
D1/ 3' 5
NShrsIn
YGENERAL MOTORS CORP 52BR1
I20
Q200
U4,000.00
T4,000.00
M200 shares added to account @ $20/share
^
*/
/*
^
D1/14' 5
NShrsOut
YTEMPLETON GROWTH 97GJ0
Q50
90 ^
*/
/*
D1/28' 5
NBuy
YGENERAL MOTORS CORP 52BR1
I24.35
Q100
U2,435.00
T2,435.00
^
*/
/*
D1/ 5' 5
NSell
YUnited Vanguard
I8.41
Q50
U420.50
T420.50
^
*/
/*
D1/ 7' 5
NReinvDiv
YFRANKLIN INCOME 97GM2
I38
Q1
U38.00
T38.00
^
*/
/*************************************************************************
*
* These transactions are all different kinds of income. (Anything that
* follows the DNYUT pattern). They are all handled the same, the only
* difference is which income account the income is placed into. By
* default, it's placed into _xxx where xxx is the right side of the
* N field. e.g. NDiv transaction goes into the _Div account
*
*************************************************************************/
/*
D1/10' 5
NDiv
YTEMPLETON GROWTH 97GJ0
U10.00
T10.00
^
*/
/*
D1/10' 5
NIntInc
YTEMPLETON GROWTH 97GJ0
U20.00
T20.00
^
*/
/*
D1/10' 5
NCGShort
YTEMPLETON GROWTH 97GJ0
U111.00
T111.00
^
*/
/*
D1/10' 5
NCGLong
YTEMPLETON GROWTH 97GJ0
U333.00
T333.00
^
*/
/*
D1/10' 5
NCGMid
YTEMPLETON GROWTH 97GJ0
U222.00
T222.00
^
*/
/*
D2/ 2' 5
NRtrnCap
YFRANKLIN INCOME 97GM2
U1,234.00
T1,234.00
^
*/
/*************************************************************************
*
* These transactions deal with miscellaneous activity that KMyMoney
* does not support, but may support in the future.
*
*************************************************************************/
/* Note the Q field is the split ratio per 10 shares, so Q12.5 is a
12.5:10 split, otherwise known as 5:4.
D1/14' 5
NStkSplit
YIBM
Q12.5
^
*/
/*************************************************************************
*
* These transactions deal with short positions and options, which are
* not supported at all by KMyMoney. They will be ignored for now.
* There may be a way to hack around this, by creating a new security
* "IBM_Short".
*
*************************************************************************/
/*
D1/21' 5
NShtSell
YIBM
I92.38
Q100
U9,238.00
T9,238.00
^
*/
/*
D1/28' 5
NCvrShrt
YIBM
I92.89
Q100
U9,339.00
T9,339.00
O50.00
^
*/
/*
D6/ 1' 5
NVest
YIBM Option
Q20
^
*/
/*
D6/ 8' 5
NExercise
YIBM Option
I60.952381
Q20
MFrom IBM Option Grant 6/1/2004
^
*/
/*
D6/ 1'14
NExpire
YIBM Option
Q5
^
*/
/*************************************************************************
*
* These transactions do not have an associated investment ("Y" field)
* so presumably they are only valid for the cash account. Once I
* understand how these are really implemented, they can probably be
* handled without much trouble.
*
*************************************************************************/
/*
D1/14' 5
NCash
U-100.00
T-100.00
LBank Chrg
^
*/
/*
D1/15' 5
NXOut
U500.00
T500.00
L[CU Savings]
$500.00
^
*/
/*
D1/28' 5
NXIn
U1,000.00
T1,000.00
L[CU Checking]
$1,000.00
^
*/
/*
D1/25' 5
NMargInt
U25.00
T25.00
^
*/
}
const TQString MyMoneyQifReader::findOrCreateIncomeAccount(const TQString& searchname)
{
TQString result;
MyMoneyFile *file = MyMoneyFile::instance();
// First, try to find this account as an income account
MyMoneyAccount acc = file->income();
TQStringList list = acc.accountList();
TQStringList::ConstIterator it_accid = list.begin();
while ( it_accid != list.end() )
{
acc = file->account(*it_accid);
if ( acc.name() == searchname )
{
result = *it_accid;
break;
}
++it_accid;
}
// If we did not find the account, now we must create one.
if ( result.isEmpty() )
{
MyMoneyAccount acc;
acc.setName( searchname );
acc.setAccountType( MyMoneyAccount::Income );
MyMoneyAccount income = file->income();
MyMoneyFileTransaction ft;
file->addAccount( acc, income );
ft.commit();
result = acc.id();
}
return result;
}
// TODO (Ace) Combine this and the previous function
const TQString MyMoneyQifReader::findOrCreateExpenseAccount(const TQString& searchname)
{
TQString result;
MyMoneyFile *file = MyMoneyFile::instance();
// First, try to find this account as an income account
MyMoneyAccount acc = file->expense();
TQStringList list = acc.accountList();
TQStringList::ConstIterator it_accid = list.begin();
while ( it_accid != list.end() )
{
acc = file->account(*it_accid);
if ( acc.name() == searchname )
{
result = *it_accid;
break;
}
++it_accid;
}
// If we did not find the account, now we must create one.
if ( result.isEmpty() )
{
MyMoneyAccount acc;
acc.setName( searchname );
acc.setAccountType( MyMoneyAccount::Expense );
MyMoneyFileTransaction ft;
MyMoneyAccount expense = file->expense();
file->addAccount( acc, expense );
ft.commit();
result = acc.id();
}
return result;
}
TQString MyMoneyQifReader::checkCategory(const TQString& name, const MyMoneyMoney value, const MyMoneyMoney value2)
{
TQString accountId;
MyMoneyFile *file = MyMoneyFile::instance();
MyMoneyAccount account;
bool found = true;
if(!name.isEmpty()) {
// The category might be constructed with an arbitraty depth (number of
// colon delimited fields). We try to find a parent account within this
// hierarchy by searching the following sequence:
//
// aaaa:bbbb:cccc:ddddd
//
// 1. search aaaa:bbbb:cccc:dddd, create nothing
// 2. search aaaa:bbbb:cccc , create dddd
// 3. search aaaa:bbbb , create cccc:dddd
// 4. search aaaa , create bbbb:cccc:dddd
// 5. don't search , create aaaa:bbbb:cccc:dddd
account.setName(name);
TQString accName; // part to be created (right side in above list)
TQString parent(name); // a possible parent part (left side in above list)
do {
accountId = file->categoryToAccount(parent);
if(accountId.isEmpty()) {
found = false;
// prepare next step
if(!accName.isEmpty())
accName.prepend(':');
accName.prepend(parent.section(':', -1));
account.setName(accName);
parent = parent.section(':', 0, -2);
} else if(!accName.isEmpty()) {
account.setParentAccountId(accountId);
}
}
while(!parent.isEmpty() && accountId.isEmpty());
// if we did not find the category, we create it
if(!found) {
MyMoneyAccount parent;
if(account.parentAccountId().isEmpty()) {
if(!value.isNegative() && value2.isNegative())
parent = file->income();
else
parent = file->expense();
} else {
parent = file->account(account.parentAccountId());
}
account.setAccountType((!value.isNegative() && value2.isNegative()) ? MyMoneyAccount::Income : MyMoneyAccount::Expense);
MyMoneyAccount brokerage;
// clear out the parent id, because createAccount() does not like that
account.setParentAccountId(TQString());
kmymoney2->createAccount(account, parent, brokerage, MyMoneyMoney());
accountId = account.id();
}
}
return accountId;
}
TQString MyMoneyQifReader::processAccountEntry(bool resetAccountId)
{
MyMoneyFile* file = MyMoneyFile::instance();
MyMoneyAccount account;
TQString tmp;
account.setName(extractLine('N'));
// tqDebug("Process account '%s'", account.name().data());
account.setDescription(extractLine('D'));
tmp = extractLine('$');
if(tmp.length() > 0)
account.setValue("lastStatementBalance", tmp);
tmp = extractLine('/');
if(tmp.length() > 0)
account.setValue("lastStatementDate", m_qifProfile.date(tmp).toString("yyyy-MM-dd"));
QifEntryTypeE transactionType = EntryTransaction;
TQString type = extractLine('T').lower().remove(TQRegExp("\\s+"));
if(type == m_qifProfile.profileType().lower().remove(TQRegExp("\\s+"))) {
account.setAccountType(MyMoneyAccount::Checkings);
} else if(type == "ccard" || type == "creditcard") {
account.setAccountType(MyMoneyAccount::CreditCard);
} else if(type == "cash") {
account.setAccountType(MyMoneyAccount::Cash);
} else if(type == "otha") {
account.setAccountType(MyMoneyAccount::Asset);
} else if(type == "othl") {
account.setAccountType(MyMoneyAccount::Liability);
} else if(type == "invst" || type == "port") {
account.setAccountType(MyMoneyAccount::Investment);
transactionType = EntryInvestmentTransaction;
} else if(type == "mutual") { // stock account w/o umbrella investment account
account.setAccountType(MyMoneyAccount::Stock);
transactionType = EntryInvestmentTransaction;
} else if(type == "unknown") {
// don't do anything with the type, leave it unknown
} else {
account.setAccountType(MyMoneyAccount::Checkings);
kdDebug(2) << "Line " << m_linenumber << ": Unknown account type '" << type << "', checkings assumed" << endl;
}
// check if we can find the account already in the file
MyMoneyAccount acc = kmymoney2->findAccount(account, MyMoneyAccount());
if(acc.id().isEmpty()) {
// in case the account is not found by name and the type is
// unknown, we have to assume something and create a checking account.
// this might be wrong, but we have no choice at this point.
if(account.accountType() == MyMoneyAccount::UnknownAccountType)
account.setAccountType(MyMoneyAccount::Checkings);
MyMoneyAccount parentAccount;
MyMoneyAccount brokerage;
MyMoneyMoney balance;
// in case it's a stock account, we need to setup a fix investment account
if(account.isInvest()) {
acc.setName(i18n("%1 (Investment)").arg(account.name())); // use the same name for the investment account
acc.setDescription(i18n("Autogenerated by TQIF importer from type Mutual account entry"));
acc.setAccountType(MyMoneyAccount::Investment);
parentAccount = file->asset();
kmymoney2->createAccount(acc, parentAccount, brokerage, MyMoneyMoney());
parentAccount = acc;
tqDebug("We still need to create the stock account in MyMoneyQifReader::processAccountEntry()");
} else {
// setup parent according the type of the account
switch(account.accountGroup()) {
case MyMoneyAccount::Asset:
default:
parentAccount = file->asset();
break;
case MyMoneyAccount::Liability:
parentAccount = file->liability();
break;
case MyMoneyAccount::Equity:
parentAccount = file->equity();
break;
}
}
// investment accounts will receive a brokerage account, as KMyMoney
// currently does not allow to store funds in the investment account directly
if(account.accountType() == MyMoneyAccount::Investment) {
brokerage.setName(account.brokerageName());
brokerage.setAccountType(MyMoneyAccount::Checkings);
brokerage.setCurrencyId(MyMoneyFile::instance()->baseCurrency().id());
}
kmymoney2->createAccount(account, parentAccount, brokerage, balance);
acc = account;
// tqDebug("Account created");
} else {
// tqDebug("Existing account found");
}
if(resetAccountId) {
// possibly start a new statement
d->finishStatement();
m_account = acc;
d->st.m_accountId = m_account.id();
d->transactionType = transactionType;
}
return acc.id();
}
void MyMoneyQifReader::selectOrCreateAccount(const SelectCreateMode mode, MyMoneyAccount& account, const MyMoneyMoney& balance)
{
MyMoneyFile* file = MyMoneyFile::instance();
TQString accountId;
TQString msg;
TQString typeStr;
TQString leadIn;
KMyMoneyUtils::categoryTypeE type;
TQMap<TQString, TQString>::ConstIterator it;
type = KMyMoneyUtils::none;
switch(account.accountGroup()) {
default:
type = KMyMoneyUtils::asset;
type = (KMyMoneyUtils::categoryTypeE) (type | KMyMoneyUtils::liability);
typeStr = i18n("account");
leadIn = i18n("al");
break;
case MyMoneyAccount::Income:
case MyMoneyAccount::Expense:
type = KMyMoneyUtils::income;
type = (KMyMoneyUtils::categoryTypeE) (type | KMyMoneyUtils::expense);
typeStr = i18n("category");
leadIn = i18n("ei");
msg = i18n("Category selection");
break;
}
KAccountSelectDlg accountSelect(type, "QifImport", kmymoney2);
if(!msg.isEmpty())
accountSelect.setCaption(msg);
it = m_accountTranslation.find((leadIn + MyMoneyFile::AccountSeperator + account.name()).lower());
if(it != m_accountTranslation.end()) {
try {
account = file->account(*it);
return;
} catch (MyMoneyException *e) {
TQString message(i18n("Account \"%1\" disappeared: ").arg(account.name()));
message += e->what();
KMessageBox::error(0, message);
delete e;
}
}
if(!account.name().isEmpty()) {
if(type & (KMyMoneyUtils::income | KMyMoneyUtils::expense)) {
accountId = file->categoryToAccount(account.name());
} else {
accountId = file->nameToAccount(account.name());
}
if(mode == Create) {
if(!accountId.isEmpty()) {
account = file->account(accountId);
return;
} else {
switch(KMessageBox::questionYesNo(0,
i18n("The %1 '%2' does not exist. Do you "
"want to create it?").arg(typeStr).arg(account.name()))) {
case KMessageBox::Yes:
break;
case KMessageBox::No:
return;
}
}
} else {
accountSelect.setHeader(i18n("Select %1").arg(typeStr));
if(!accountId.isEmpty()) {
msg = i18n("The %1 <b>%2</b> currently exists. Do you want "
"to import transactions to this account?")
.arg(typeStr).arg(account.name());
} else {
msg = i18n("The %1 <b>%2</b> currently does not exist. You can "
"create a new %3 by pressing the <b>Create</b> button "
"or select another %4 manually from the selection box.")
.arg(typeStr).arg(account.name()).arg(typeStr).arg(typeStr);
}
}
} else {
accountSelect.setHeader(i18n("Import transactions to %1").arg(typeStr));
msg = i18n("No %1 information has been found in the selected TQIF file. "
"Please select an account using the selection box in the dialog or "
"create a new %2 by pressing the <b>Create</b> button.")
.arg(typeStr).arg(typeStr);
}
accountSelect.setDescription(msg);
accountSelect.setAccount(account, accountId);
accountSelect.setMode(mode == Create);
accountSelect.showAbortButton(true);
// display current entry in widget, the offending line (if any) will be shown in red
TQStringList::Iterator it_e;
int i = 0;
for(it_e = m_qifEntry.begin(); it_e != m_qifEntry.end(); ++it_e) {
if(m_extractedLine == i)
accountSelect.m_qifEntry->setColor(TQColor("red"));
accountSelect.m_qifEntry->append(*it_e);
accountSelect.m_qifEntry->setColor(TQColor("black"));
++i;
}
for(;;) {
if(accountSelect.exec() == TQDialog::Accepted) {
if(!accountSelect.selectedAccount().isEmpty()) {
accountId = accountSelect.selectedAccount();
m_accountTranslation[(leadIn + MyMoneyFile::AccountSeperator + account.name()).lower()] = accountId;
// MMAccount::openingBalance() is where the accountSelect dialog has
// stashed the opening balance that the user chose.
MyMoneyAccount importedAccountData(account);
// MyMoneyMoney balance = importedAccountData.openingBalance();
account = file->account(accountId);
if ( ! balance.isZero() )
{
TQString openingtxid = file->openingBalanceTransaction(account);
MyMoneyFileTransaction ft;
if ( ! openingtxid.isEmpty() )
{
MyMoneyTransaction openingtx = file->transaction(openingtxid);
MyMoneySplit split = openingtx.splitByAccount(account.id());
if ( split.shares() != balance )
{
const MyMoneySecurity& sec = file->security(account.currencyId());
if ( KMessageBox::questionYesNo(
tqApp->mainWidget(),
i18n("The %1 account currently has an opening balance of %2. This TQIF file reports an opening balance of %3. Would you like to overwrite the current balance with the one from the TQIF file?").arg(account.name(), split.shares().formatMoney(account, sec), balance.formatMoney(account, sec)),
i18n("Overwrite opening balance"),
KStdGuiItem::yes(),
KStdGuiItem::no(),
"OverwriteOpeningBalance" )
== KMessageBox::Yes )
{
file->removeTransaction( openingtx );
file->createOpeningBalanceTransaction( account, balance );
}
}
}
else
{
// Add an opening balance
file->createOpeningBalanceTransaction( account, balance );
}
ft.commit();
}
break;
}
} else if(accountSelect.aborted())
throw new MYMONEYEXCEPTION("USERABORT");
if(typeStr == i18n("account")) {
KMessageBox::error(0, i18n("You must select or create an account."));
} else {
KMessageBox::error(0, i18n("You must select or create a category."));
}
}
}
void MyMoneyQifReader::setProgressCallback(void(*callback)(int, int, const TQString&))
{
m_progressCallback = callback;
}
void MyMoneyQifReader::signalProgress(int current, int total, const TQString& msg)
{
if(m_progressCallback != 0)
(*m_progressCallback)(current, total, msg);
}
void MyMoneyQifReader::processPriceEntry(void)
{
/*
!Type:Prices
"IBM",141 9/16,"10/23/98"
^
!Type:Prices
"GMW",21.28," 3/17' 5"
^
!Type:Prices
"GMW",71652181.001,"67/128/ 0"
^
Note that Quicken will often put in a price with a bogus date and number. We will ignore
prices with bogus dates. Hopefully that will catch all of these.
Also note that prices can be in fractional units, e.g. 141 9/16.
*/
TQStringList::const_iterator it_line = m_qifEntry.begin();
// Make a price for each line
TQRegExp priceExp("\"(.*)\",(.*),\"(.*)\"");
while ( it_line != m_qifEntry.end() )
{
if(priceExp.search(*it_line) != -1) {
MyMoneyStatement::Price price;
price.m_strSecurity = priceExp.cap(1);
TQString pricestr = priceExp.cap(2);
TQString datestr = priceExp.cap(3);
kdDebug(0) << "Price:" << price.m_strSecurity << " / " << pricestr << " / " << datestr << endl;
// Only add the price if the date is valid. If invalid, fail silently. See note above.
// Also require the price value to not have any slashes. Old prices will be something like
// "25 9/16", which we do not support. So we'll skip the price for now.
TQDate date = m_qifProfile.date(datestr);
MyMoneyMoney rate(m_qifProfile.value('P', pricestr));
if(date.isValid() && !rate.isZero())
{
price.m_amount = rate;
price.m_date = date;
d->st.m_listPrices += price;
}
}
++it_line;
}
}
void MyMoneyQifReader::processSecurityEntry(void)
{
/*
!Type:Security
NVANGUARD 500 INDEX
SVFINX
TMutual Fund
^
*/
MyMoneyStatement::Security security;
security.m_strName = extractLine('N');
security.m_strSymbol = extractLine('S');
d->st.m_listSecurities += security;
}
#include "mymoneyqifreader.moc"
|