GnuCash  5.6-150-g038405b370+
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. */
50 extern const int num_currency_formats;
51 extern const gchar* currency_format_user[];
52 
62  PL_INPUT,
63  PL_ERROR,
64  PL_PRETRANS,
65  PL_PRESPLIT,
66  PL_SKIP
67 };
68 
69 using StrVec = std::vector<std::string>;
70 
74 using parse_line_t = std::tuple<StrVec,
75  ErrMap,
76  std::shared_ptr<GncPreTrans>,
77  std::shared_ptr<GncPreSplit>,
78  bool>;
79 
80 struct ErrorList;
81 
85 struct 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 
90 private:
91  ErrMap m_errors;
92 };
93 
105 {
106 public:
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 settings (const CsvTransImpSettings& settings);
140  bool save_settings ();
141 
142  void settings_name (std::string name);
143  std::string settings_name ();
144 
145 
146  void load_file (const std::string& filename);
147 
148  void tokenize (bool guessColTypes);
149 
150  std::string verify(bool with_acct_errors);
151 
155  void create_transactions ();
156  bool check_for_column_type (GncTransPropType type);
157  void set_column_type (uint32_t position, GncTransPropType type, bool force = false);
158  std::vector<GncTransPropType> column_types ();
159 
160  std::set<std::string> accounts ();
161 
162  std::unique_ptr<GncTokenizer> m_tokenizer;
163  std::vector<parse_line_t> m_parsed_lines;
166  std::multimap <time64, std::shared_ptr<DraftTransaction>> m_transactions;
169 private:
174  void create_transaction (std::vector<parse_line_t>::iterator& parsed_line);
175 
176  void verify_column_selections (ErrorList& error_msg);
177 
178  /* Internal helper function to force reparsing of columns subject to format changes */
179  void reset_formatted_column (std::vector<GncTransPropType>& col_types);
180 
181  /* Internal helper function that does the actual conversion from property lists
182  * to real (possibly unbalanced) transaction with splits.
183  */
184  std::shared_ptr<DraftTransaction> trans_properties_to_trans (std::vector<parse_line_t>::iterator& parsed_line);
185 
186  /* Internal helper functions that should only be called from within
187  * set_column_type for consistency (otherwise error messages may not be (re)set)
188  */
189  void update_pre_split_multi_col_prop (parse_line_t& parsed_line, GncTransPropType col_type);
190  void update_pre_trans_props (parse_line_t& parsed_line, uint32_t col, GncTransPropType old_type, GncTransPropType new_type);
191  void update_pre_split_props (parse_line_t& parsed_line, uint32_t col, GncTransPropType old_type, GncTransPropType new_type);
192 
193  CsvTransImpSettings m_settings;
194  bool m_skip_errors;
195  /* Field used internally to track whether some transactions are multi-currency */
196  bool m_multi_currency;
197 
198  /* The parameters below are only used while creating
199  * transactions. They keep state information while processing multi-split
200  * transactions.
201  */
202  std::shared_ptr<GncPreTrans> m_parent = nullptr;
203  std::shared_ptr<DraftTransaction> m_current_draft = nullptr;
204 };
205 
206 
207 #endif
CSV Import Settings.
~GncTxImport()
Destructor for GncTxImport.
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.
STRUCTS.
void base_account(Account *base_account)
Sets a base account.
Exception that will be thrown whenever a parsing error is encountered.
void load_file(const std::string &filename)
Loads a file into a GncTxImport.
GncTxImport(GncImpFileFormat format=GncImpFileFormat::UNKNOWN)
Constructor for GncTxImport.
void multi_split(bool multi_split)
Toggles the multi-split state of the importer and will subsequently sanitize the column_types list...
Account handling public routines.
Class convert a file into vector of string vectors.
std::multimap< time64, std::shared_ptr< DraftTransaction > > m_transactions
map of transaction objects created from parsed_lines and column_types, ordered by date ...
void encoding(const std::string &encoding)
Converts raw file data using a new encoding.
GncImpFileFormat
Enumeration for file formats supported by this importer.
std::unique_ptr< GncTokenizer > m_tokenizer
Will handle file loading/encoding conversion/splitting into fields.
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 create_transactions()
This function will attempt to convert all tokenized lines into transactions using the column types th...
void tokenize(bool guessColTypes)
Splits a file into cells.
The actual TxImport class It&#39;s intended to use in the following sequence of actions: ...
std::vector< parse_line_t > m_parsed_lines
source file parsed into a two-dimensional array of strings.
API for Transactions and Splits (journal entries)