GnuCash  5.6-150-g038405b370+
gnc-imp-props-tx.hpp
1 /********************************************************************\
2  * gnc-imp-props-tx.hpp - encapsulate transaction properties for *
3  * use in the csv importer *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License*
16  * along with this program; if not, contact: *
17  * *
18  * Free Software Foundation Voice: +1-617-542-5942 *
19  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
20  * Boston, MA 02110-1301, USA gnu@gnu.org *
21  * *
22 \********************************************************************/
23 
24 #ifndef GNC_TRANS_PROPS_HPP
25 #define GNC_TRANS_PROPS_HPP
26 
27 #include <platform.h>
28 #if PLATFORM(WINDOWS)
29 #include <windows.h>
30 #endif
31 
32 #include <glib/gi18n.h>
33 
34 #include "Account.h"
35 #include "Transaction.h"
36 #include "gnc-commodity.h"
37 
38 #include <string>
39 #include <map>
40 #include <memory>
41 #include <optional>
42 #include <gnc-datetime.hpp>
43 #include <gnc-numeric.hpp>
44 
49 enum class GncTransPropType {
50  NONE,
51  UNIQUE_ID,
52  DATE,
53  NUM,
54  DESCRIPTION,
55  NOTES,
56  COMMODITY,
57  VOID_REASON,
58  TRANS_PROPS = VOID_REASON,
59 
60  ACTION,
61  ACCOUNT,
62  AMOUNT,
63  AMOUNT_NEG,
64  VALUE,
65  VALUE_NEG,
66  PRICE,
67  MEMO,
68  REC_STATE,
69  REC_DATE,
70  TACTION,
71  TACCOUNT,
72  TAMOUNT,
73  TAMOUNT_NEG,
74  TMEMO,
75  TREC_STATE,
76  TREC_DATE,
77  SPLIT_PROPS = TREC_DATE
78 };
79 
80 #define IMAP_CAT_CSV "csv-account-map"
81 
82 using StrVec = std::vector<std::string>;
83 using ErrMap = std::map<GncTransPropType, std::string>;
84 using ErrPair = std::pair<GncTransPropType, std::string>;
85 
90 extern std::map<GncTransPropType, const char*> gnc_csv_col_type_strs;
91 
96 {
97  test_prop_type_str( const char* name ) : m_name(name) {}
98  bool operator()( const std::pair<GncTransPropType, const char*>& v ) const
99  {
100  return !g_strcmp0(v.second, m_name);
101  }
102 private:
103  const char *m_name;
104 };
105 
109 bool is_multi_col_prop (GncTransPropType prop);
110 
117 GncTransPropType sanitize_trans_prop (GncTransPropType prop, bool multi_split);
118 
119 
120 gnc_commodity* parse_commodity (const std::string& comm_str);
121 GncNumeric parse_monetary (const std::string &str, int currency_format);
122 
123 
138 {
139  DraftTransaction (Transaction* tx) : trans(tx) {}
140  ~DraftTransaction () { if (trans) { xaccTransDestroy (trans); trans = nullptr; } }
141  Transaction* trans;
142 
143  std::optional<GncNumeric> m_price;
144  std::optional<std::string> m_taction;
145  std::optional<std::string> m_tmemo;
146  std::optional<GncNumeric> m_tamount;
147  std::optional<Account*> m_taccount;
148  std::optional<char> m_trec_state;
149  std::optional<GncDate> m_trec_date;
150 
151  std::optional<std::string> void_reason;
152 };
153 
155 {
156 public:
157  GncPreTrans(int date_format, bool multi_split)
158  : m_date_format{date_format}, m_multi_split{multi_split}, m_currency{nullptr} {};
159 
160  void set (GncTransPropType prop_type, const std::string& value);
161  void set_date_format (int date_format) { m_date_format = date_format ;}
162  void set_multi_split (bool multi_split) { m_multi_split = multi_split ;}
163  void reset (GncTransPropType prop_type);
164  StrVec verify_essentials (void);
165  std::shared_ptr<DraftTransaction> create_trans (QofBook* book, gnc_commodity* currency);
166 
181  bool is_part_of (std::shared_ptr<GncPreTrans> parent);
182  std::optional<std::string> get_void_reason() { return m_void_reason; }
183 
184  ErrMap errors();
185  void reset_cross_split_counters();
186  /* Some import errors need info from multiple splits. This function
187  * will evaluate possible multi-line errors and set the proper error
188  * message(s) for them. */
189  bool is_multi_currency();
190 
191 
192 private:
193  int m_date_format;
194  bool m_multi_split;
195  std::optional<std::string> m_differ;
196  std::optional<GncDate> m_date;
197  std::optional<std::string> m_num;
198  std::optional<std::string> m_desc;
199  std::optional<std::string> m_notes;
200  gnc_commodity *m_currency;
201  std::optional<std::string> m_void_reason;
202  bool created = false;
203 
204 
205  ErrMap m_errors;
206 
207  /* m_alt_currencies will be filled with all PreSplit account's
208  * commodities that are currencies. If the account is denominated in a
209  * non-currency, its parent account currency is added instead.
210  * This list will be used to check for multi-currency inconsistencies
211  * and whether extra columns are required. */
212  std::vector<gnc_commodity*> m_alt_currencies;
213  /* m_acct_commodities will be filled with all PreSplit account's
214  * commodities that aren't currencies. The result will be used to check for
215  * a multi-currency situation (which requires extra columns to be set). */
216  std::vector<gnc_commodity*> m_acct_commodities;
217 
218  friend class GncPreSplit;
219 };
220 
222 {
223 public:
224  GncPreSplit (int date_format, int currency_format) : m_date_format{date_format},
225  m_currency_format{currency_format} {};
226  void set (GncTransPropType prop_type, const std::string& value);
227  void reset (GncTransPropType prop_type);
228  void add (GncTransPropType prop_type, const std::string& value);
229  void set_date_format (int date_format) { m_date_format = date_format ;}
230  void set_currency_format (int currency_format) { m_currency_format = currency_format; }
231  void set_pre_trans (std::shared_ptr<GncPreTrans> pre_trans) { m_pre_trans = pre_trans; }
232  std::shared_ptr<GncPreTrans> get_pre_trans (void) { return m_pre_trans; }
233  StrVec verify_essentials (void);
234  void create_split(std::shared_ptr<DraftTransaction> draft_trans);
235 
236  Account* get_account () { if (m_account) return *m_account; else return nullptr; }
237  void set_account (Account* acct);
238  ErrMap errors();
239 
240 private:
241  void UpdateCrossSplitCounters ();
242 
243  std::shared_ptr<GncPreTrans> m_pre_trans;
244  int m_date_format;
245  int m_currency_format;
246  std::optional<std::string> m_action;
247  std::optional<Account*> m_account;
248  std::optional<GncNumeric> m_amount;
249  std::optional<GncNumeric> m_amount_neg;
250  std::optional<GncNumeric> m_value;
251  std::optional<GncNumeric> m_value_neg;
252  std::optional<GncNumeric> m_price;
253  std::optional<std::string> m_memo;
254  std::optional<char> m_rec_state;
255  std::optional<GncDate> m_rec_date;
256  std::optional<std::string> m_taction;
257  std::optional<Account*> m_taccount;
258  std::optional<GncNumeric> m_tamount;
259  std::optional<GncNumeric> m_tamount_neg;
260  std::optional<std::string> m_tmemo;
261  std::optional<char> m_trec_state;
262  std::optional<GncDate> m_trec_date;
263  bool created = false;
264 
265  ErrMap m_errors;
266 };
267 
268 #endif
STRUCTS.
The primary numeric class for representing amounts and values.
Definition: gnc-numeric.hpp:60
void xaccTransDestroy(Transaction *trans)
Destroys a transaction.
Account handling public routines.
Functor to check if the above map has an element of which the value equals name.
The final form of a transaction to import before it is passed on to the generic importer.
bool is_part_of(std::shared_ptr< GncPreTrans > parent)
Check whether the harvested transaction properties for this instance match those of another one (the ...
API for Transactions and Splits (journal entries)
Commodity handling public routines.