GnuCash c935c2f+
Loading...
Searching...
No Matches
gnc-import-tx.hpp
Go to the documentation of this file.
1/********************************************************************\
2 * gnc-import-tx.hpp - import transactions from csv files *
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, contact: *
16 * *
17 * Free Software Foundation Voice: +1-617-542-5942 *
18 * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
19 * Boston, MA 02110-1301, USA gnu@gnu.org *
20\********************************************************************/
21
29#ifndef GNC_TX_IMPORT_HPP
30#define GNC_TX_IMPORT_HPP
31
32#include <config.h>
33
34#include "Account.h"
35#include "Transaction.h"
36
37#include <vector>
38#include <set>
39#include <map>
40#include <memory>
41#include <optional>
42#include <cstdint>
43
44#include "gnc-tokenizer.hpp"
45#include "gnc-imp-props-tx.hpp"
47
48
49/* A set of currency formats that the user sees. */
50extern const int num_currency_formats;
51extern const gchar* currency_format_user[];
52
62 PL_INPUT,
63 PL_ERROR,
64 PL_PRETRANS,
65 PL_PRESPLIT,
66 PL_SKIP
67};
68
69using StrVec = std::vector<std::string>;
70
74using parse_line_t = std::tuple<StrVec,
75 ErrMap,
76 std::shared_ptr<GncPreTrans>,
77 std::shared_ptr<GncPreSplit>,
78 bool>;
79
80struct ErrorList;
81
85struct GncCsvImpParseError : public std::runtime_error
86{
87 GncCsvImpParseError(const std::string& err, ErrMap err_vec) : std::runtime_error(err), m_errors{err_vec} {}
88 ErrMap errors() const {return m_errors;}
89
90private:
91 ErrMap m_errors;
92};
93
105{
106public:
107 // Constructor - Destructor
108 GncTxImport(GncImpFileFormat format = GncImpFileFormat::UNKNOWN);
109 ~GncTxImport();
110
111 void file_format(GncImpFileFormat format);
112 GncImpFileFormat file_format();
113
114 void multi_split (bool multi_split);
115 bool multi_split ();
116
118 Account *base_account ();
119
120 void currency_format (int currency_format);
121 int currency_format ();
122
123 void date_format (int date_format);
124 int date_format ();
125
126 void encoding (const std::string& encoding);
127 std::string encoding ();
128
129 void update_skipped_lines (std::optional<uint32_t> start, std::optional<uint32_t> end,
130 std::optional<bool> alt, std::optional<bool> errors);
131 uint32_t skip_start_lines ();
132 uint32_t skip_end_lines ();
133 bool skip_alt_lines ();
134 bool skip_err_lines ();
135
136 void separators (std::string separators);
137 std::string separators ();
138
139 void enable_escape (bool enable);
140 bool enable_escape ();
141
142 void settings (const CsvTransImpSettings& settings);
143 bool save_settings ();
144
145 void settings_name (std::string name);
146 std::string settings_name ();
147
148
149 void load_file (const std::string& filename);
150
151 void tokenize (bool guessColTypes);
152
153 std::string verify(bool with_acct_errors);
154
158 void create_transactions ();
159 bool check_for_column_type (GncTransPropType type);
160 void set_column_type (uint32_t position, GncTransPropType type, bool force = false);
161 std::vector<GncTransPropType> column_types ();
162
163 std::set<std::string> accounts ();
164
165 std::unique_ptr<GncTokenizer> m_tokenizer;
166 std::vector<parse_line_t> m_parsed_lines;
169 std::multimap <time64, std::shared_ptr<DraftTransaction>> m_transactions;
172private:
177 void create_transaction (std::vector<parse_line_t>::iterator& parsed_line);
178
179 void verify_column_selections (ErrorList& error_msg);
180
181 /* Internal helper function to force reparsing of columns subject to format changes */
182 void reset_formatted_column (std::vector<GncTransPropType>& col_types);
183
184 /* Internal helper function that does the actual conversion from property lists
185 * to real (possibly unbalanced) transaction with splits.
186 */
187 std::shared_ptr<DraftTransaction> trans_properties_to_trans (std::vector<parse_line_t>::iterator& parsed_line);
188
189 /* Internal helper functions that should only be called from within
190 * set_column_type for consistency (otherwise error messages may not be (re)set)
191 */
192 void update_pre_split_multi_col_prop (parse_line_t& parsed_line, GncTransPropType col_type);
193 void update_pre_trans_props (parse_line_t& parsed_line, uint32_t col, GncTransPropType old_type, GncTransPropType new_type);
194 void update_pre_split_props (parse_line_t& parsed_line, uint32_t col, GncTransPropType old_type, GncTransPropType new_type);
195
196 CsvTransImpSettings m_settings;
197 bool m_skip_errors;
198 /* Field used internally to track whether some transactions are multi-currency */
199 bool m_multi_currency;
200
201 /* The parameters below are only used while creating
202 * transactions. They keep state information while processing multi-split
203 * transactions.
204 */
205 std::shared_ptr<GncPreTrans> m_parent = nullptr;
206 std::shared_ptr<DraftTransaction> m_current_draft = nullptr;
207};
208
209
210#endif
Account handling public routines.
API for Transactions and Splits (journal entries)
The actual TxImport class It's intended to use in the following sequence of actions:
~GncTxImport()
Destructor for GncTxImport.
void tokenize(bool guessColTypes)
Splits a file into cells.
void create_transactions()
This function will attempt to convert all tokenized lines into transactions using the column types th...
void encoding(const std::string &encoding)
Converts raw file data using a new encoding.
std::unique_ptr< GncTokenizer > m_tokenizer
Will handle file loading/encoding conversion/splitting into fields.
void multi_split(bool multi_split)
Toggles the multi-split state of the importer and will subsequently sanitize the column_types list.
std::vector< parse_line_t > m_parsed_lines
source file parsed into a two-dimensional array of strings.
void load_file(const std::string &filename)
Loads a file into a GncTxImport.
std::multimap< time64, std::shared_ptr< DraftTransaction > > m_transactions
map of transaction objects created from parsed_lines and column_types, ordered by date
void file_format(GncImpFileFormat format)
Sets the file format for the file to import, which may cause the file to be reloaded as well if the p...
void base_account(Account *base_account)
Sets a base account.
CSV Import Settings.
parse_line_cols
An enum describing the columns found in a parse_line_t.
std::tuple< StrVec, std::string, std::shared_ptr< GncImportPrice >, bool > parse_line_t
Tuple to hold.
std::tuple< StrVec, ErrMap, std::shared_ptr< GncPreTrans >, std::shared_ptr< GncPreSplit >, bool > parse_line_t
Tuple to hold all internal state for one parsed line.
Class convert a file into vector of string vectors.
GncImpFileFormat
Enumeration for file formats supported by this importer.
STRUCTS.
Exception that will be thrown whenever a parsing error is encountered.