1 /*
2  * Copyright 2013-2014  Allan Anderson <agander93@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef CSVWRITER_H
19 #define CSVWRITER_H
20 
21 // ----------------------------------------------------------------------------
22 // QT Headers
23 
24 #include <QObject>
25 #include <QStringList>
26 #include <QMap>
27 
28 // ----------------------------------------------------------------------------
29 // KDE Headers
30 
31 // ----------------------------------------------------------------------------
32 // Project Headers
33 
34 class QTextStream;
35 class MyMoneyTransaction;
36 class MyMoneySplit;
37 class CSVExporter;
38 class MyMoneyMoney;
39 
40 /**
41   * @author Thomas Baumgart
42   * @author Allan Anderson
43   */
44 
45 /**
46   * This class represents the CSV writer. All conversion between the
47   * internal representation of accounts, transactions is handled in this
48   * object.
49   */
50 class CsvWriter : public QObject
51 {
52   Q_OBJECT
53 
54 public:
55   CsvWriter();
56   ~CsvWriter();
57 
58   CSVExporter* m_plugin;
59 
60   /**
61     * This method is used to start the conversion. The parameters control
62     * the destination of the data and the parts that will be exported.
63     * Individual errors will be reported using message boxes.
64     *
65     * @param filename The name of the output file with full path information
66     * @param accountId The id of the account that will be exported
67     * @param accountData If true, the transactions will be exported
68     * @param categoryData If true, the categories will be exported as well
69     * @param startDate Transactions before this date will not be exported
70     * @param endDate Transactions after this date will not be exported
71     * @param fieldSeparator Current field separator
72     */
73   void write(const QString& filename,
74              const QString& accountId, const bool accountData,
75              const bool categoryData, const QDate& startDate, const QDate& endDate,
76              const QString& separator);
77 
78 private:
79   bool m_firstSplit;
80 
81   QMap<QString, QString> m_map;
82   /**
83     * This method writes the entries necessary for an account. First
84     * the leadin, and then the transactions that are in the account
85     * specified by @p accountId in the range from @p startDate to @p
86     * endDate.
87     *
88     * @param s reference to textstream
89     * @param accountId id of the account to be written
90     * @param startDate date from which entries are written
91     * @param endDate date until which entries are written
92     */
93   void writeAccountEntry(QTextStream &s, const QString &accountId, const QDate &startDate, const QDate &endDate);
94 
95   /**
96     * This method writes the category entries to the stream
97     * @p s. It writes the leadin and uses writeCategoryEntries()
98     * to write the entries and emits signalProgess() where needed.
99     *
100     * @param s reference to textstream
101     */
102   void writeCategoryEntries(QTextStream &s);
103 
104   /**
105     * This method writes the category entry for account with
106     * the ID @p accountId to the stream @p s. All subaccounts
107     * are processed as well.
108     *
109     * @param s reference to textstream
110     * @param accountId id of the account to be written
111     * @param leadIn constant text that will be prepended to the account's name
112     */
113   void writeCategoryEntry(QTextStream &s, const QString &accountId, const QString &leadIn);
114   void writeTransactionEntry(const MyMoneyTransaction& t, const QString& accountId, const int count);
115   void writeSplitEntry(QString& str, const MyMoneySplit& split, const int splitCount, const int lastEntry);
116   void extractInvestmentEntries(const QString& accountId, const QDate& startDate, const QDate& endDate);
117   void writeInvestmentEntry(const MyMoneyTransaction& t, const int count);
118 
119 Q_SIGNALS:
120   /**
121     * This signal is emitted while the operation progresses.
122     * When the operation starts, the signal is emitted with
123     * @p current being 0 and @p max having the maximum value.
124     *
125     * During the operation, the signal is emitted with @p current
126     * containing the current value on the way to the maximum value.
127     * @p max will be 0 in this case.
128     *
129     * When the operation is finished, the signal is emitted with
130     * @p current and @p max set to -1 to identify the end of the
131     * operation.
132     *
133     * @param current see above
134     * @param max see above
135     */
136   void signalProgress(int current, int max);
137 
138 private:
139   QStringList m_headerLine;
140 
141   QString m_separator;
142 
143   int m_highestSplitCount;
144 
145   bool m_noError;
146 
147   QString format(const QString &s, bool withSeparator = true);
148   QString format(const MyMoneyMoney &value, int prec = 2, bool withSeparator = true);
149 };
150 
151 #endif
152