Changeset View
Changeset View
Standalone View
Standalone View
kmymoney/plugins/csvimport/csvimporter.h
- This file was added.
1 | /*************************************************************************** | ||||
---|---|---|---|---|---|
2 | csvimporter.h | ||||
3 | ------------------- | ||||
4 | begin : Sun May 21 2017 | ||||
5 | copyright : (C) 2015 by Allan Anderson | ||||
6 | email : agander93@gmail.com | ||||
7 | copyright : (C) 2017 by Łukasz Wojniłowicz | ||||
8 | email : lukasz.wojnilowicz@gmail.com | ||||
9 | ***************************************************************************/ | ||||
10 | | ||||
11 | /*************************************************************************** | ||||
12 | * * | ||||
13 | * This program is free software; you can redistribute it and/or modify * | ||||
14 | * it under the terms of the GNU General Public License as published by * | ||||
15 | * the Free Software Foundation; either version 2 of the License, or * | ||||
16 | * (at your option) any later version. * | ||||
17 | * * | ||||
18 | ***************************************************************************/ | ||||
19 | | ||||
20 | #ifndef CSVIMPORTER_H | ||||
21 | #define CSVIMPORTER_H | ||||
22 | | ||||
23 | // ---------------------------------------------------------------------------- | ||||
24 | // KDE Includes | ||||
25 | #include <KSharedConfig> | ||||
26 | #include <KConfigGroup> | ||||
27 | | ||||
28 | // ---------------------------------------------------------------------------- | ||||
29 | // QT Includes | ||||
30 | | ||||
31 | #include <QStandardItemModel> | ||||
32 | | ||||
33 | // Project Includes | ||||
34 | | ||||
35 | #include "convdate.h" | ||||
36 | #include "csvutil.h" | ||||
37 | #include "mymoneystatement.h" | ||||
38 | | ||||
39 | #include <QWizardPage> | ||||
40 | | ||||
41 | class CSVWizard; | ||||
42 | class CSVImporter; | ||||
43 | | ||||
44 | class CSVWizardPage : public QWizardPage | ||||
45 | { | ||||
46 | public: | ||||
47 | CSVWizardPage(CSVWizard *dlg, CSVImporter *imp) : QWizardPage(nullptr), m_dlg(dlg), m_imp(imp) {} | ||||
48 | | ||||
49 | protected: | ||||
50 | CSVWizard *m_dlg; | ||||
51 | CSVImporter *m_imp; | ||||
52 | }; | ||||
53 | | ||||
54 | enum profileTypeE { ProfileBank, ProfileInvest, | ||||
55 | ProfileCurrencyPrices, ProfileStockPrices | ||||
56 | }; | ||||
57 | | ||||
58 | enum profilesActionE { ProfilesAdd, ProfilesRemove, ProfilesRename, ProfilesUpdateLastUsed }; | ||||
59 | | ||||
60 | enum autodetectTypeE { AutoFieldDelimiter, AutoDecimalSymbol, AutoDateFormat, | ||||
61 | AutoAccountInvest, AutoAccountBank | ||||
62 | }; | ||||
63 | | ||||
64 | enum columnTypeE { ColumnDate, ColumnMemo, | ||||
65 | ColumnNumber, ColumnPayee, ColumnAmount, | ||||
66 | ColumnCredit, ColumnDebit, ColumnCategory, | ||||
67 | ColumnType, ColumnPrice, ColumnQuantity, | ||||
68 | ColumnFee, ColumnSymbol, ColumnName, | ||||
69 | ColumnEmpty = 0xFE, ColumnInvalid = 0xFF | ||||
70 | }; | ||||
71 | | ||||
72 | enum miscSettingsE { ConfDirectory, ConfEncoding, ConfDateFormat, | ||||
73 | ConfFieldDelimiter, ConfTextDeimiter, ConfDecimalSymbol, | ||||
74 | ConfStartLine, ConfTrailerLines, | ||||
75 | ConfOppositeSigns, | ||||
76 | ConfFeeIsPercentage, ConfFeeRate, ConfMinFee, | ||||
77 | ConfSecurityName, ConfSecuritySymbol, ConfCurrencySymbol, | ||||
78 | ConfPriceFraction, ConfDontAsk, | ||||
79 | ConfHeight, ConfWidth | ||||
80 | }; | ||||
81 | | ||||
82 | enum validationResultE { ValidActionType, InvalidActionValues, NoActionType }; | ||||
83 | | ||||
84 | | ||||
85 | class CSVProfile | ||||
86 | { | ||||
87 | protected: | ||||
88 | CSVProfile() {} | ||||
89 | CSVProfile(const QString &profileName, int encodingMIBEnum, | ||||
90 | int startLine, int trailerLines, | ||||
91 | int dateFormatIndex, int fieldDelimiterIndex, int textDelimiterIndex, int decimalSymbolIndex, | ||||
92 | QMap<columnTypeE, int> &colTypeNum) : | ||||
93 | m_profileName(profileName), m_encodingMIBEnum(encodingMIBEnum), | ||||
94 | m_startLine(startLine), m_trailerLines(trailerLines), | ||||
95 | m_dateFormatIndex(dateFormatIndex), m_fieldDelimiterIndex(fieldDelimiterIndex), | ||||
96 | m_textDelimiterIndex(textDelimiterIndex), m_decimalSymbolIndex(decimalSymbolIndex), | ||||
97 | m_colTypeNum(colTypeNum) | ||||
98 | { | ||||
99 | initColNumType(); | ||||
100 | } | ||||
101 | void readSettings(const KConfigGroup &profilesGroup); | ||||
102 | void writeSettings(KConfigGroup &profilesGroup); | ||||
103 | void initColNumType() { | ||||
104 | for (auto it = m_colTypeNum.constBegin(); it != m_colTypeNum.constEnd(); ++it) | ||||
105 | m_colNumType.insert(it.value(), it.key()); | ||||
106 | } | ||||
107 | | ||||
108 | public: | ||||
109 | virtual ~CSVProfile() {} | ||||
110 | virtual profileTypeE type() const = 0; | ||||
111 | virtual bool readSettings(const KSharedConfigPtr &config) = 0; | ||||
112 | virtual void writeSettings(const KSharedConfigPtr &config) = 0; | ||||
113 | | ||||
114 | QString m_profileName; | ||||
115 | QString m_lastUsedDirectory; | ||||
116 | | ||||
117 | int m_encodingMIBEnum; | ||||
118 | | ||||
119 | int m_startLine; | ||||
120 | int m_endLine; | ||||
121 | int m_trailerLines; | ||||
122 | | ||||
123 | int m_dateFormatIndex; | ||||
124 | int m_fieldDelimiterIndex; | ||||
125 | int m_textDelimiterIndex; | ||||
126 | int m_decimalSymbolIndex; | ||||
127 | | ||||
128 | QMap<columnTypeE, int> m_colTypeNum; | ||||
129 | QMap<int, columnTypeE> m_colNumType; | ||||
130 | }; | ||||
131 | | ||||
132 | class BankingProfile : public CSVProfile | ||||
133 | { | ||||
134 | public: | ||||
135 | profileTypeE type() const { return ProfileBank; } | ||||
136 | bool readSettings(const KSharedConfigPtr &config); | ||||
137 | void writeSettings(const KSharedConfigPtr &config); | ||||
138 | | ||||
139 | QList<int> m_memoColList; | ||||
140 | | ||||
141 | bool m_oppositeSigns; | ||||
142 | }; | ||||
143 | | ||||
144 | class InvestmentProfile : public CSVProfile | ||||
145 | { | ||||
146 | public: | ||||
147 | profileTypeE type() const { return ProfileInvest; } | ||||
148 | bool readSettings(const KSharedConfigPtr &config); | ||||
149 | void writeSettings(const KSharedConfigPtr &config); | ||||
150 | | ||||
151 | QMap <MyMoneyStatement::Transaction::EAction, QStringList> m_transactionNames; | ||||
152 | | ||||
153 | QString m_feeRate; | ||||
154 | QString m_minFee; | ||||
155 | QString m_securityName; | ||||
156 | QString m_securitySymbol; | ||||
157 | | ||||
158 | QList<int> m_memoColList; | ||||
159 | | ||||
160 | int m_priceFraction; | ||||
161 | int m_feeIsPercentage; | ||||
162 | int m_dontAsk; | ||||
163 | }; | ||||
164 | | ||||
165 | class PricesProfile : public CSVProfile | ||||
166 | { | ||||
167 | public: | ||||
168 | explicit PricesProfile() : CSVProfile() {} | ||||
169 | explicit PricesProfile(const profileTypeE profileType) : CSVProfile(), m_profileType(profileType) {} | ||||
170 | PricesProfile(QString profileName, int encodingMIBEnum, | ||||
171 | int startLine, int trailerLines, | ||||
172 | int dateFormatIndex, int fieldDelimiterIndex, int textDelimiterIndex, int decimalSymbolIndex, | ||||
173 | QMap<columnTypeE, int> colTypeNum, | ||||
174 | int priceFraction, profileTypeE profileType) : | ||||
175 | CSVProfile(profileName, encodingMIBEnum, | ||||
176 | startLine, trailerLines, | ||||
177 | dateFormatIndex, fieldDelimiterIndex, textDelimiterIndex, decimalSymbolIndex, | ||||
178 | colTypeNum), | ||||
179 | m_priceFraction(priceFraction), m_profileType(profileType) {} | ||||
180 | | ||||
181 | profileTypeE type() const { return m_profileType; } | ||||
182 | bool readSettings(const KSharedConfigPtr &config); | ||||
183 | void writeSettings(const KSharedConfigPtr &config); | ||||
184 | | ||||
185 | QString m_securityName; | ||||
186 | QString m_securitySymbol; | ||||
187 | QString m_currencySymbol; | ||||
188 | | ||||
189 | int m_dontAsk; | ||||
190 | int m_priceFraction; | ||||
191 | | ||||
192 | profileTypeE m_profileType; | ||||
193 | }; | ||||
194 | | ||||
195 | class CSVFile | ||||
196 | { | ||||
197 | public: | ||||
198 | explicit CSVFile(); | ||||
199 | ~CSVFile(); | ||||
200 | | ||||
201 | void getStartEndRow(CSVProfile *profile); | ||||
202 | /** | ||||
203 | * If delimiter = -1 this method tries different field | ||||
204 | * delimiters to get the one with which file has the most columns. | ||||
205 | * Otherwise it gets only column count for specified delimiter. | ||||
206 | */ | ||||
207 | void getColumnCount(CSVProfile *profile, const QStringList &rows); | ||||
208 | | ||||
209 | /** | ||||
210 | * This method gets the filename of | ||||
211 | * the financial statement. | ||||
212 | */ | ||||
213 | bool getInFileName(QString startDir = QString()); | ||||
214 | | ||||
215 | void setupParser(CSVProfile *profile); | ||||
216 | | ||||
217 | /** | ||||
218 | * This method gets file into buffer | ||||
219 | * It will laso store file's end column and row. | ||||
220 | */ | ||||
221 | void readFile(CSVProfile *profile); | ||||
222 | | ||||
223 | CsvUtil *m_csvUtil; | ||||
224 | Parse *m_parse; | ||||
225 | QStandardItemModel *m_model; | ||||
226 | | ||||
227 | QString m_inFileName; | ||||
228 | | ||||
229 | int m_columnCount; | ||||
230 | int m_rowCount; | ||||
231 | }; | ||||
232 | | ||||
233 | class CSVImporter : public QObject | ||||
234 | { | ||||
235 | Q_OBJECT | ||||
236 | public: | ||||
237 | explicit CSVImporter(); | ||||
238 | ~CSVImporter(); | ||||
239 | | ||||
240 | /** | ||||
241 | * This method will silently import csv file. Main purpose of this method are online quotes. | ||||
242 | */ | ||||
243 | MyMoneyStatement unattendedPricesImport(const QString &filename, PricesProfile *profile); | ||||
244 | | ||||
245 | static KSharedConfigPtr configFile(); | ||||
246 | void profileFactory(const profileTypeE type, const QString &name); | ||||
247 | void readMiscSettings(); | ||||
248 | | ||||
249 | /** | ||||
250 | * This method ensures that configuration file contains all neccesary fields | ||||
251 | * and that it is up to date. | ||||
252 | */ | ||||
253 | void validateConfigFile(); | ||||
254 | | ||||
255 | /** | ||||
256 | * This method contains routines to update configuration file | ||||
257 | * from kmmVer to latest. | ||||
258 | */ | ||||
259 | bool updateConfigFile(QList<int> &confVer); | ||||
260 | | ||||
261 | /** | ||||
262 | * This method will update [ProfileNames] in csvimporterrrc | ||||
263 | */ | ||||
264 | static bool profilesAction(const profileTypeE type, const profilesActionE action, const QString &name, const QString &newname); | ||||
265 | | ||||
266 | /** | ||||
267 | * This methods will ensure that fields of input rows are correct. | ||||
268 | */ | ||||
269 | bool validateDateFormat(const int col); | ||||
270 | bool validateDecimalSymbols(const QList<int> &columns); | ||||
271 | bool validateCurrencies(const PricesProfile *profile); | ||||
272 | bool validateSecurity(const PricesProfile *profile); | ||||
273 | bool validateSecurity(const InvestmentProfile *profile); | ||||
274 | bool validateSecurities(); | ||||
275 | validationResultE validateActionType(MyMoneyStatement::Transaction &tr); | ||||
276 | | ||||
277 | /** | ||||
278 | * This method will try to detect decimal symbol in input column. | ||||
279 | */ | ||||
280 | int detectDecimalSymbols(const QList<int> &columns); | ||||
281 | int detectDecimalSymbol(const int col, const QString &exclude); | ||||
282 | | ||||
283 | /** | ||||
284 | * This method will try to detect account from csv header. | ||||
285 | */ | ||||
286 | QList<MyMoneyAccount> findAccounts(const QList<MyMoneyAccount::accountTypeE> &accountTypes, const QString &statementHeader); | ||||
287 | bool detectAccount(MyMoneyStatement &st); | ||||
288 | | ||||
289 | /** | ||||
290 | * This methods will evaluate input row and append it to a statement. | ||||
291 | */ | ||||
292 | bool processBankRow(MyMoneyStatement &st, const BankingProfile *profile, const int row); | ||||
293 | bool processInvestRow(MyMoneyStatement &st, const InvestmentProfile *profile, const int row); | ||||
294 | bool processPriceRow(MyMoneyStatement &st, const PricesProfile *profile, const int row); | ||||
295 | | ||||
296 | /** | ||||
297 | * This methods will evaluate fields of input row and return statement's useful value. | ||||
298 | */ | ||||
299 | QDate processDateField(const int row, const int col); | ||||
300 | MyMoneyMoney processCreditDebit(QString &credit, QString &debit ); | ||||
301 | MyMoneyMoney processPriceField(const InvestmentProfile *profile, const int row, const int col); | ||||
302 | MyMoneyMoney processPriceField(const PricesProfile *profile, const int row, const int col); | ||||
303 | MyMoneyMoney processAmountField(const CSVProfile *profile, const int row, const int col); | ||||
304 | MyMoneyMoney processQuantityField(const CSVProfile *profile, const int row, const int col); | ||||
305 | MyMoneyStatement::Transaction::EAction processActionTypeField(const InvestmentProfile *profile, const int row, const int col); | ||||
306 | | ||||
307 | /** | ||||
308 | * This method creates valid set of possible transactions | ||||
309 | * according to quantity, amount and price | ||||
310 | */ | ||||
311 | QList<MyMoneyStatement::Transaction::EAction> createValidActionTypes(MyMoneyStatement::Transaction &tr); | ||||
312 | | ||||
313 | /** | ||||
314 | * This method will add fee column to model based on amount and fee rate. | ||||
315 | */ | ||||
316 | bool calculateFee(); | ||||
317 | | ||||
318 | /** | ||||
319 | * This method gets securities from investment statement and | ||||
320 | * tries to get pairs of symbol and name either | ||||
321 | * from KMM or from statement data. | ||||
322 | * In case it's not successfull onlySymbols and onlyNames won't be empty. | ||||
323 | */ | ||||
324 | bool sortSecurities(QSet<QString>& onlySymbols, QSet<QString>& onlyNames, QMap<QString, QString>& mapSymbolName); | ||||
325 | | ||||
326 | /** | ||||
327 | * Helper method to set decimal symbol in case it was set to autodetect. | ||||
328 | */ | ||||
329 | void setupFieldDecimalSymbol(int col); | ||||
330 | | ||||
331 | /** | ||||
332 | * Helper method to get all column numbers that were pointed as nummeric | ||||
333 | */ | ||||
334 | QList<int> getNumericalColumns(); | ||||
335 | | ||||
336 | bool createStatement(MyMoneyStatement &st); | ||||
337 | | ||||
338 | ConvertDate *m_convertDate; | ||||
339 | CSVFile *m_file; | ||||
340 | CSVProfile *m_profile; | ||||
341 | KSharedConfigPtr m_config; | ||||
342 | | ||||
343 | bool m_isActionTypeValidated; | ||||
344 | | ||||
345 | QList<MyMoneyMoney> m_priceFractions; | ||||
346 | QSet<QString> m_hashSet; | ||||
347 | QMap<int, int> m_decimalSymbolIndexMap; | ||||
348 | QMap<QString, QString> m_mapSymbolName; | ||||
349 | QMap<autodetectTypeE, bool> m_autodetect; | ||||
350 | | ||||
351 | static const QMap<columnTypeE, QString> m_colTypeConfName; | ||||
352 | static const QMap<profileTypeE, QString> m_profileConfPrefix; | ||||
353 | static const QMap<MyMoneyStatement::Transaction::EAction, QString> m_transactionConfName; | ||||
354 | static const QMap<miscSettingsE, QString> m_miscSettingsConfName; | ||||
355 | static const QString m_confProfileNames; | ||||
356 | static const QString m_confPriorName; | ||||
357 | static const QString m_confMiscName; | ||||
358 | | ||||
359 | signals: | ||||
360 | void statementReady(MyMoneyStatement&); | ||||
361 | }; | ||||
362 | | ||||
363 | #endif |