GnuCash  5.6-150-g038405b370+
gnc-import-tx.cpp
1 /********************************************************************\
2  * gnc-import-tx.cpp - import transactions from csv or fixed-width *
3  * files *
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 #include <guid.hpp>
25 
26 #include <platform.h>
27 #if PLATFORM(WINDOWS)
28 #include <windows.h>
29 #endif
30 
31 #include <glib/gi18n.h>
32 
33 #include <algorithm>
34 #include <exception>
35 #include <iostream>
36 #include <map>
37 #include <memory>
38 #include <numeric>
39 #include <optional>
40 #include <string>
41 #include <tuple>
42 #include <utility>
43 #include <vector>
44 
45 #include <boost/regex.hpp>
46 #include <boost/regex/icu.hpp>
47 
48 #include "gnc-import-tx.hpp"
49 #include "gnc-imp-props-tx.hpp"
50 #include "gnc-tokenizer-csv.hpp"
51 #include "gnc-tokenizer-fw.hpp"
53 
54 G_GNUC_UNUSED static QofLogModule log_module = GNC_MOD_IMPORT;
55 
56 const int num_currency_formats = 3;
57 const gchar* currency_format_user[] = {N_("Locale"),
58  N_("Period: 123,456.78"),
59  N_("Comma: 123.456,78")
60  };
61 
62 
66 {
67  /* All of the data pointers are initially NULL. This is so that, if
68  * gnc_csv_parse_data_free is called before all of the data is
69  * initialized, only the data that needs to be freed is freed. */
70  m_skip_errors = false;
71  file_format(m_settings.m_file_format = format);
72 }
73 
77 {
78 }
79 
88 {
89  if (m_tokenizer && m_settings.m_file_format == format)
90  return;
91 
92  auto new_encoding = std::string("UTF-8");
93  auto new_imp_file = std::string();
94 
95  // Recover common settings from old tokenizer
96  if (m_tokenizer)
97  {
98  new_encoding = m_tokenizer->encoding();
99  new_imp_file = m_tokenizer->current_file();
100  if (file_format() == GncImpFileFormat::FIXED_WIDTH)
101  {
102  auto fwtok = dynamic_cast<GncFwTokenizer*>(m_tokenizer.get());
103  if (!fwtok->get_columns().empty())
104  m_settings.m_column_widths = fwtok->get_columns();
105  }
106  }
107 
108  m_settings.m_file_format = format;
109  m_tokenizer = gnc_tokenizer_factory(m_settings.m_file_format);
110 
111  // Set up new tokenizer with common settings
112  // recovered from old tokenizer
113  m_tokenizer->encoding(new_encoding);
114  load_file(new_imp_file);
115 
116  // Restore potentially previously set separators or column_widths
117  if ((file_format() == GncImpFileFormat::CSV)
118  && !m_settings.m_separators.empty())
119  separators (m_settings.m_separators);
120  else if ((file_format() == GncImpFileFormat::FIXED_WIDTH)
121  && !m_settings.m_column_widths.empty())
122  {
123  auto fwtok = dynamic_cast<GncFwTokenizer*>(m_tokenizer.get());
124  fwtok->columns (m_settings.m_column_widths);
125  }
126 
127 }
128 
129 GncImpFileFormat GncTxImport::file_format()
130 {
131  return m_settings.m_file_format;
132 }
133 
145 void GncTxImport::multi_split (bool multi_split)
146 {
147  auto trans_prop_seen = false;
148  m_settings.m_multi_split = multi_split;
149  for (uint32_t i = 0; i < m_settings.m_column_types.size(); i++)
150  {
151  auto old_prop = m_settings.m_column_types[i];
152  auto is_trans_prop = ((old_prop > GncTransPropType::NONE)
153  && (old_prop <= GncTransPropType::TRANS_PROPS));
154  auto san_prop = sanitize_trans_prop (old_prop, m_settings.m_multi_split);
155  if (san_prop != old_prop)
156  set_column_type (i, san_prop);
157  else if (is_trans_prop && !trans_prop_seen)
158  set_column_type (i, old_prop, true);
159  trans_prop_seen |= is_trans_prop;
160 
161  }
162  if (m_settings.m_multi_split)
163  m_settings.m_base_account = nullptr;
164 }
165 
166 bool GncTxImport::multi_split () { return m_settings.m_multi_split; }
167 
177 void GncTxImport::base_account (Account* base_account)
178 {
179  if (m_settings.m_multi_split)
180  {
181  m_settings.m_base_account = nullptr;
182  return;
183  }
184 
185  auto base_account_is_new = m_settings.m_base_account == nullptr;
186  m_settings.m_base_account = base_account;
187 
188  if (m_settings.m_base_account)
189  {
190  auto col_type_it = std::find (m_settings.m_column_types.begin(),
191  m_settings.m_column_types.end(), GncTransPropType::ACCOUNT);
192  if (col_type_it != m_settings.m_column_types.end())
193  set_column_type(col_type_it - m_settings.m_column_types.begin(),
194  GncTransPropType::NONE);
195 
196  if (base_account_is_new)
197  {
198  /* Set default account for each line's split properties */
199  for (auto line : m_parsed_lines)
200  std::get<PL_PRESPLIT>(line)->set_account (m_settings.m_base_account);
201  }
202  else
203  {
204  /* Reparse all of the lines with the new base account's commodity. */
205  tokenize(false);
206  }
207 
208  }
209 }
210 
211 Account *GncTxImport::base_account () { return m_settings.m_base_account; }
212 
213 void GncTxImport::reset_formatted_column (std::vector<GncTransPropType>& col_types)
214 {
215  for (auto col_type: col_types)
216  {
217  auto col = std::find (m_settings.m_column_types.begin(),
218  m_settings.m_column_types.end(), col_type);
219  if (col != m_settings.m_column_types.end())
220  set_column_type (col - m_settings.m_column_types.begin(), col_type, true);
221  }
222 }
223 
224 void GncTxImport::currency_format (int currency_format)
225 {
226  m_settings.m_currency_format = currency_format;
227 
228  /* Reparse all currency related columns */
229  std::vector<GncTransPropType> commodities = {
230  GncTransPropType::AMOUNT,
231  GncTransPropType::AMOUNT_NEG,
232  GncTransPropType::TAMOUNT,
233  GncTransPropType::TAMOUNT_NEG,
234  GncTransPropType::PRICE};
235  reset_formatted_column (commodities);
236 }
237 int GncTxImport::currency_format () { return m_settings.m_currency_format; }
238 
239 void GncTxImport::date_format (int date_format)
240 {
241  m_settings.m_date_format = date_format;
242 
243  /* Reparse all date related columns */
244  std::vector<GncTransPropType> dates = { GncTransPropType::DATE,
245  GncTransPropType::REC_DATE,
246  GncTransPropType::TREC_DATE};
247  reset_formatted_column (dates);
248 }
249 int GncTxImport::date_format () { return m_settings.m_date_format; }
250 
256 void GncTxImport::encoding (const std::string& encoding)
257 {
258 
259  // TODO investigate if we can catch conversion errors and report them
260  if (m_tokenizer)
261  {
262  m_tokenizer->encoding(encoding); // May throw
263  try
264  {
265  tokenize(false);
266  }
267  catch (...)
268  { };
269  }
270 
271  m_settings.m_encoding = encoding;
272 }
273 
274 std::string GncTxImport::encoding () { return m_settings.m_encoding; }
275 
276 void GncTxImport::update_skipped_lines(std::optional<uint32_t> start, std::optional<uint32_t> end,
277  std::optional<bool> alt, std::optional<bool> errors)
278 {
279  if (start)
280  m_settings.m_skip_start_lines = *start;
281  if (end)
282  m_settings.m_skip_end_lines = *end;
283  if (alt)
284  m_settings.m_skip_alt_lines = *alt;
285  if (errors)
286  m_skip_errors = *errors;
287 
288  for (uint32_t i = 0; i < m_parsed_lines.size(); i++)
289  {
290  std::get<PL_SKIP>(m_parsed_lines[i]) =
291  ((i < skip_start_lines()) || // start rows to skip
292  (i >= m_parsed_lines.size() - skip_end_lines()) || // end rows to skip
293  (((i - skip_start_lines()) % 2 == 1) && // skip every second row...
294  skip_alt_lines()) || // ...if requested
295  (m_skip_errors && !std::get<PL_ERROR>(m_parsed_lines[i]).empty())); // skip lines with errors
296  }
297 }
298 
299 uint32_t GncTxImport::skip_start_lines () { return m_settings.m_skip_start_lines; }
300 uint32_t GncTxImport::skip_end_lines () { return m_settings.m_skip_end_lines; }
301 bool GncTxImport::skip_alt_lines () { return m_settings.m_skip_alt_lines; }
302 bool GncTxImport::skip_err_lines () { return m_skip_errors; }
303 
304 void GncTxImport::separators (std::string separators)
305 {
306  if (file_format() != GncImpFileFormat::CSV)
307  return;
308 
309  m_settings.m_separators = separators;
310  auto csvtok = dynamic_cast<GncCsvTokenizer*>(m_tokenizer.get());
311  csvtok->set_separators (separators);
312 
313 }
314 std::string GncTxImport::separators () { return m_settings.m_separators; }
315 
316 void GncTxImport::settings (const CsvTransImpSettings& settings)
317 {
318  /* First apply file format as this may recreate the tokenizer */
319  file_format (settings.m_file_format);
320  /* Only then apply the other settings */
321  m_settings = settings;
322  multi_split (m_settings.m_multi_split);
323  base_account (m_settings.m_base_account);
324  encoding (m_settings.m_encoding);
325 
326  if (file_format() == GncImpFileFormat::CSV)
327  separators (m_settings.m_separators);
328  else if (file_format() == GncImpFileFormat::FIXED_WIDTH)
329  {
330  auto fwtok = dynamic_cast<GncFwTokenizer*>(m_tokenizer.get());
331  fwtok->columns (m_settings.m_column_widths);
332  }
333  try
334  {
335  tokenize(false);
336  }
337  catch (...)
338  { };
339 
340  /* Tokenizing will clear column types, reset them here
341  * based on the loaded settings.
342  */
343  std::copy_n (settings.m_column_types.begin(),
344  std::min (m_settings.m_column_types.size(), settings.m_column_types.size()),
345  m_settings.m_column_types.begin());
346 
347 }
348 
349 bool GncTxImport::save_settings ()
350 {
351 
352  if (preset_is_reserved_name (m_settings.m_name))
353  return true;
354 
355  /* separators are already copied to m_settings in the separators
356  * function above. However this is not the case for the column
357  * widths in fw mode, so do this now.
358  */
359  if (file_format() == GncImpFileFormat::FIXED_WIDTH)
360  {
361  auto fwtok = dynamic_cast<GncFwTokenizer*>(m_tokenizer.get());
362  m_settings.m_column_widths = fwtok->get_columns();
363  }
364 
365  return m_settings.save();
366 }
367 
368 void GncTxImport::settings_name (std::string name) { m_settings.m_name = name; }
369 std::string GncTxImport::settings_name () { return m_settings.m_name; }
370 
377 void GncTxImport::load_file (const std::string& filename)
378 {
379 
380  /* Get the raw data first and handle an error if one occurs. */
381  try
382  {
383  m_tokenizer->load_file (filename);
384  return;
385  }
386  catch (std::ifstream::failure& ios_err)
387  {
388  // Just log the error and pass it on the call stack for proper handling
389  PWARN ("Error: %s", ios_err.what());
390  throw;
391  }
392 }
393 
405 void GncTxImport::tokenize (bool guessColTypes)
406 {
407  if (!m_tokenizer)
408  return;
409 
410  uint32_t max_cols = 0;
411  m_tokenizer->tokenize();
412  m_parsed_lines.clear();
413  for (auto tokenized_line : m_tokenizer->get_tokens())
414  {
415  auto length = tokenized_line.size();
416  if (length > 0)
417  {
418  auto pretrans = std::make_shared<GncPreTrans>(date_format(), m_settings.m_multi_split);
419  auto presplit = std::make_shared<GncPreSplit>(date_format(), currency_format());
420  presplit->set_pre_trans (std::move (pretrans));
421  m_parsed_lines.push_back (std::make_tuple (tokenized_line, ErrMap(),
422  presplit->get_pre_trans(), std::move (presplit), false));
423  }
424  if (length > max_cols)
425  max_cols = length;
426  }
427 
428  /* If it failed, generate an error. */
429  if (m_parsed_lines.size() == 0)
430  {
431  throw (std::range_error (N_("There was an error parsing the file.")));
432  return;
433  }
434 
435  m_settings.m_column_types.resize(max_cols, GncTransPropType::NONE);
436 
437  /* Force reinterpretation of already set columns and/or base_account */
438  for (uint32_t i = 0; i < m_settings.m_column_types.size(); i++)
439  set_column_type (i, m_settings.m_column_types[i], true);
440  if (m_settings.m_base_account)
441  {
442  for (auto line : m_parsed_lines)
443  std::get<PL_PRESPLIT>(line)->set_account (m_settings.m_base_account);
444  }
445 
446  if (guessColTypes)
447  {
448  /* Guess column_types based
449  * on the contents of each column. */
450  /* TODO Make it actually guess. */
451  }
452 }
453 
454 
455 struct ErrorList
456 {
457 public:
458  void add_error (std::string msg);
459  std::string str();
460 private:
461  StrVec m_error;
462 };
463 
464 void ErrorList::add_error (std::string msg)
465 {
466  m_error.emplace_back (msg);
467 }
468 
469 std::string ErrorList::str()
470 {
471  auto err_msg = std::string();
472  if (!m_error.empty())
473  {
474  auto add_bullet_item = [](std::string& a, std::string& b)->std::string { return std::move(a) + "\n• " + b; };
475  err_msg = std::accumulate (m_error.begin(), m_error.end(), std::move (err_msg), add_bullet_item);
476  err_msg.erase (0, 1);
477  }
478 
479  return err_msg;
480 }
481 
482 
483 /* Test for the required minimum number of columns selected and
484  * the selection is consistent.
485  * @param An ErrorList object to which all found issues are added.
486  */
487 void GncTxImport::verify_column_selections (ErrorList& error_msg)
488 {
489 
490  /* Verify if a date column is selected and it's parsable.
491  */
492  if (!check_for_column_type(GncTransPropType::DATE))
493  error_msg.add_error( _("Please select a date column."));
494 
495  /* Verify if an account is selected either in the base account selector
496  * or via a column in the import data.
497  */
498  if (!check_for_column_type(GncTransPropType::ACCOUNT))
499  {
500  if (m_settings.m_multi_split)
501  error_msg.add_error( _("Please select an account column."));
502  else if (!m_settings.m_base_account)
503  error_msg.add_error( _("Please select an account column or set a base account in the Account field."));
504  }
505 
506  /* Verify a description column is selected.
507  */
508  if (!check_for_column_type(GncTransPropType::DESCRIPTION))
509  error_msg.add_error( _("Please select a description column."));
510 
511  /* Verify at least one amount column (amount or amount_neg) column is selected.
512  */
513  if (!check_for_column_type(GncTransPropType::AMOUNT) &&
514  !check_for_column_type(GncTransPropType::AMOUNT_NEG))
515  error_msg.add_error( _("Please select a (negated) amount column."));
516 
517  /* In multisplit mode and where current account selections imply multi-
518  * currency transactions, we require extra columns to ensure each split is
519  * fully defined.
520  * Note this check only involves splits created by the csv importer
521  * code. The generic import matcher may add a balancing split
522  * optionally using Transfer <something> properties. The generic
523  * import matcher has its own tools to balance that split so
524  * we won't concern ourselves with that one here.
525  */
526  if (m_multi_currency)
527  {
528  if (m_settings.m_multi_split &&
529  !check_for_column_type(GncTransPropType::PRICE) &&
530  !check_for_column_type(GncTransPropType::VALUE) &&
531  !check_for_column_type(GncTransPropType::VALUE_NEG))
532  error_msg.add_error( _("The current account selections will generate multi-currency transactions. Please select one of the following columns: price, (negated) value."));
533  else if (!m_settings.m_multi_split &&
534  !check_for_column_type(GncTransPropType::PRICE) &&
535  !check_for_column_type(GncTransPropType::TAMOUNT) &&
536  !check_for_column_type(GncTransPropType::TAMOUNT_NEG) &&
537  !check_for_column_type(GncTransPropType::VALUE) &&
538  !check_for_column_type(GncTransPropType::VALUE_NEG))
539  error_msg.add_error( _("The current account selections will generate multi-currency transactions. Please select one of the following columns: price, (negated) value, (negated) transfer amount."));
540  }
541 }
542 
543 
544 /* Check whether the chosen settings can successfully parse
545  * the import data. This will check:
546  * - there's at least one line selected for import
547  * - the minimum number of columns is selected
548  * - the values in the selected columns can be parsed meaningfully.
549  * @return An empty string if all checks passed or the reason
550  * verification failed otherwise.
551  */
552 std::string GncTxImport::verify (bool with_acct_errors)
553 {
554  auto newline = std::string();
555  auto error_msg = ErrorList();
556 
557  /* Check if the import file did actually contain any information */
558  if (m_parsed_lines.size() == 0)
559  {
560  error_msg.add_error(_("No valid data found in the selected file. It may be empty or the selected encoding is wrong."));
561  return error_msg.str();
562  }
563 
564  /* Check if at least one line is selected for importing */
565  auto skip_alt_offset = m_settings.m_skip_alt_lines ? 1 : 0;
566  if (m_settings.m_skip_start_lines + m_settings.m_skip_end_lines + skip_alt_offset >= m_parsed_lines.size())
567  {
568  error_msg.add_error(_("No lines are selected for importing. Please reduce the number of lines to skip."));
569  return error_msg.str();
570  }
571 
572  verify_column_selections (error_msg);
573 
574  update_skipped_lines (std::nullopt, std::nullopt, std::nullopt, std::nullopt);
575 
576  auto have_line_errors = false;
577  for (auto line : m_parsed_lines)
578  {
579  auto errors = std::get<PL_ERROR>(line);
580  if (std::get<PL_SKIP>(line))
581  continue;
582  if (with_acct_errors && !errors.empty())
583  {
584  have_line_errors = true;
585  break;
586  }
587  auto non_acct_error = [](ErrPair curr_err)
588  {
589  return !((curr_err.first == GncTransPropType::ACCOUNT) ||
590  (curr_err.first == GncTransPropType::TACCOUNT));
591  };
592  if (!with_acct_errors &&
593  std::any_of(errors.cbegin(), errors.cend(), non_acct_error))
594  {
595  have_line_errors = true;
596  break;
597  }
598  }
599 
600  if (have_line_errors)
601  error_msg.add_error( _("Not all fields could be parsed. Please correct the issues reported for each line or adjust the lines to skip."));
602 
603  return error_msg.str();
604 }
605 
606 
613 std::shared_ptr<DraftTransaction> GncTxImport::trans_properties_to_trans (std::vector<parse_line_t>::iterator& parsed_line)
614 {
615  auto created_trans = false;
616  std::shared_ptr<GncPreSplit> split_props;
617  std::tie(std::ignore, std::ignore, std::ignore, split_props, std::ignore) = *parsed_line;
618  auto trans_props = split_props->get_pre_trans();
619  auto account = split_props->get_account();
620 
621  QofBook* book = gnc_account_get_book (account);
622  gnc_commodity* currency = xaccAccountGetCommodity (account);
623  if (!gnc_commodity_is_currency(currency))
624  currency = gnc_account_get_currency_or_parent (account);
625 
626  auto draft_trans = trans_props->create_trans (book, currency);
627 
628  if (draft_trans)
629  {
630  /* We're about to continue with a new transaction
631  * Time to do some closing actions on the previous one
632  */
633  if (m_current_draft && m_current_draft->void_reason)
634  {
635  /* The import data specifies this transaction was voided.
636  * So void the created transaction as well.
637  * Attention: this assumes the imported transaction was balanced.
638  * If not, this will cause an imbalance split to be added automatically!
639  */
640  xaccTransCommitEdit (m_current_draft->trans);
641  xaccTransVoid (m_current_draft->trans, m_current_draft->void_reason->c_str());
642  }
643  m_current_draft = draft_trans;
644  m_current_draft->void_reason = trans_props->get_void_reason();
645  created_trans = true;
646  }
647  else if (m_settings.m_multi_split) // in multi_split mode create_trans will return a nullptr for all but the first split
648  draft_trans = m_current_draft;
649  else // in non-multi-split mode each line should be a transaction, so not having one here is an error
650  throw std::invalid_argument ("Failed to create transaction from selected columns.");
651 
652  if (!draft_trans)
653  return nullptr;
654 
655  split_props->create_split (draft_trans);
656 
657  /* Only return the draft transaction if we really created a new one
658  * The return value will be added to a list for further processing,
659  * we want each transaction to appear only once in that list.
660  */
661  return created_trans ? m_current_draft : nullptr;
662 }
663 
664 void GncTxImport::create_transaction (std::vector<parse_line_t>::iterator& parsed_line)
665 {
666  ErrMap errors;
667  std::shared_ptr<GncPreSplit> split_props = nullptr;
668  bool skip_line = false;
669  std::tie(std::ignore, errors, std::ignore, split_props, skip_line) = *parsed_line;
670  auto trans_props = split_props->get_pre_trans();
671 
672  if (skip_line)
673  return;
674 
675  // We still have errors for this line. That shouldn't happen!
676  if(!errors.empty())
677  {
678  auto error_message = _("Current line still has parse errors.\n"
679  "This should never happen. Please report this as a bug.");
680  throw GncCsvImpParseError(error_message, errors);
681  }
682 
683  // Add an ACCOUNT property with the default account if no account column was set by the user
684  auto line_acct = split_props->get_account();
685  if (!line_acct)
686  {
687  // Oops - the user didn't select an Account column *and* we didn't get a default value either!
688  // Note if you get here this suggests a bug in the code!
689  auto error_message = _("No account column selected and no base account specified either.\n"
690  "This should never happen. Please report this as a bug.");
691  PINFO("User warning: %s", error_message);
692  auto errs = ErrMap { ErrPair { GncTransPropType::NONE, error_message},};
693  throw GncCsvImpParseError(_("Parse Error"), errs);
694  }
695 
696  /* If column parsing was successful, convert trans properties into a draft transaction. */
697  try
698  {
699  /* If all went well, add this transaction to the list. */
700  auto draft_trans = trans_properties_to_trans (parsed_line);
701  if (draft_trans)
702  {
703  auto trans_date = xaccTransGetDate (draft_trans->trans);
704  m_transactions.insert (std::pair<time64, std::shared_ptr<DraftTransaction>>(trans_date,std::move(draft_trans)));
705  }
706  }
707  catch (const std::invalid_argument& e)
708  {
709  auto err_str = _("Problem creating preliminary transaction");
710  PINFO("%s: %s", err_str, e.what());
711  auto errs = ErrMap { ErrPair { GncTransPropType::NONE, err_str},};
712  throw (GncCsvImpParseError(err_str, errs));
713  }
714 }
715 
716 
725 {
726  /* Start with verifying the current data. */
727  auto verify_result = verify (true);
728  if (!verify_result.empty())
729  throw std::invalid_argument (verify_result);
730 
731  /* Drop all existing draft transactions */
732  m_transactions.clear();
733 
734  m_parent = nullptr;
735 
736  /* Iterate over all parsed lines */
737  for (auto parsed_lines_it = m_parsed_lines.begin();
738  parsed_lines_it != m_parsed_lines.end();
739  ++parsed_lines_it)
740  {
741  /* Skip current line if the user specified so */
742  if ((std::get<PL_SKIP>(*parsed_lines_it)))
743  continue;
744 
745  /* Should not throw anymore, otherwise verify needs revision */
746  create_transaction (parsed_lines_it);
747  }
748 }
749 
750 
751 bool
752 GncTxImport::check_for_column_type (GncTransPropType type)
753 {
754  return (std::find (m_settings.m_column_types.begin(),
755  m_settings.m_column_types.end(), type)
756  != m_settings.m_column_types.end());
757 }
758 
759 
760 void GncTxImport::update_pre_split_multi_col_prop (parse_line_t& parsed_line, GncTransPropType col_type)
761 {
762  if (!is_multi_col_prop(col_type))
763  return;
764 
765  auto input_vec = std::get<PL_INPUT>(parsed_line);
766  auto split_props = std::get<PL_PRESPLIT> (parsed_line);
767 
768  /* All amount columns may appear more than once. The net amount
769  * needs to be recalculated rather than just reset if one column
770  * is unset. */
771  for (auto col_it = m_settings.m_column_types.cbegin();
772  col_it < m_settings.m_column_types.cend();
773  col_it++)
774  if (*col_it == col_type)
775  {
776  auto value = std::string();
777  auto col_num = static_cast<uint32_t>(col_it - m_settings.m_column_types.cbegin());
778 
779  if (col_num < input_vec.size())
780  value = input_vec.at(col_num);
781  split_props->add (col_type, value);
782  }
783 }
784 
785 void GncTxImport::update_pre_trans_props (parse_line_t& parsed_line, uint32_t col, GncTransPropType old_type, GncTransPropType new_type)
786 {
787  auto input_vec = std::get<PL_INPUT>(parsed_line);
788  auto trans_props = std::get<PL_PRETRANS> (parsed_line);
789 
790  /* Reset date format for each trans props object
791  * to ensure column updates use the most recent one */
792  trans_props->set_date_format (m_settings.m_date_format);
793  trans_props->set_multi_split (m_settings.m_multi_split);
794 
795  if ((old_type > GncTransPropType::NONE) && (old_type <= GncTransPropType::TRANS_PROPS))
796  trans_props->reset (old_type);
797  if ((new_type > GncTransPropType::NONE) && (new_type <= GncTransPropType::TRANS_PROPS))
798  {
799  auto value = std::string();
800 
801  if (col < input_vec.size())
802  value = input_vec.at(col);
803 
804  trans_props->set(new_type, value);
805  }
806 
807  /* In the trans_props we also keep track of currencies/commodities for further
808  * multi-currency checks. These come from a PreSplit's account property.
809  * If that's the property that we are about to modify, the current
810  * counters should be reset. */
811  if ((old_type == GncTransPropType::ACCOUNT) || (new_type == GncTransPropType::ACCOUNT))
812  trans_props->reset_cross_split_counters();
813 }
814 
815 void GncTxImport::update_pre_split_props (parse_line_t& parsed_line, uint32_t col, GncTransPropType old_type, GncTransPropType new_type)
816 {
817  /* With multi-split input data this line may be part of a transaction
818  * that has already been started by a previous parsed line.
819  * If so
820  * - set the GncPreTrans from that previous line (which we track
821  * in m_parent) as this GncPreSplit's pre_trans.
822  * In all other cases
823  * - set the GncPreTrans that's unique to this line
824  * as this GncPreSplit's pre_trans
825  * - mark it as the new potential m_parent for subsequent lines.
826  */
827  auto split_props = std::get<PL_PRESPLIT> (parsed_line);
828  auto trans_props = std::get<PL_PRETRANS> (parsed_line);
829  /* Reset date format for each split props object
830  * to ensure column updates use the most recent one */
831  split_props->set_date_format (m_settings.m_date_format);
832  if (m_settings.m_multi_split && trans_props->is_part_of( m_parent))
833  split_props->set_pre_trans (m_parent);
834  else
835  {
836  split_props->set_pre_trans (trans_props);
837  m_parent = trans_props;
838  }
839 
840  if ((old_type > GncTransPropType::TRANS_PROPS) && (old_type <= GncTransPropType::SPLIT_PROPS))
841  {
842  split_props->reset (old_type);
843  if (is_multi_col_prop(old_type))
844  update_pre_split_multi_col_prop (parsed_line, old_type);
845  }
846 
847  if ((new_type > GncTransPropType::TRANS_PROPS) && (new_type <= GncTransPropType::SPLIT_PROPS))
848  {
849  if (is_multi_col_prop(new_type))
850  {
851  split_props->reset(new_type);
852  update_pre_split_multi_col_prop (parsed_line, new_type);
853  }
854  else
855  {
856  auto input_vec = std::get<PL_INPUT>(parsed_line);
857  auto value = std::string();
858  if (col < input_vec.size())
859  value = input_vec.at(col);
860  split_props->set(new_type, value);
861  }
862  }
863  m_multi_currency |= split_props->get_pre_trans()->is_multi_currency();
864 
865  /* Collect errors from this line's GncPreSplit and its embedded GncPreTrans */
866  auto all_errors = split_props->get_pre_trans()->errors();
867  all_errors.merge (split_props->errors());
868  std::get<PL_ERROR>(parsed_line) = std::move(all_errors);
869 }
870 
871 
872 void
873 GncTxImport::set_column_type (uint32_t position, GncTransPropType type, bool force)
874 {
875  if (position >= m_settings.m_column_types.size())
876  return;
877 
878  auto old_type = m_settings.m_column_types[position];
879  if ((type == old_type) && !force)
880  return; /* Nothing to do */
881 
882  // Column types except amount and negated amount should be unique,
883  // so remove any previous occurrence of the new type
884  if (!is_multi_col_prop(type))
885  std::replace(m_settings.m_column_types.begin(), m_settings.m_column_types.end(),
886  type, GncTransPropType::NONE);
887 
888  m_settings.m_column_types.at (position) = type;
889 
890  // If the user has set an Account column, we can't have a base account set
891  if (type == GncTransPropType::ACCOUNT)
892  base_account (nullptr);
893 
894  /* Update the preparsed data */
895  m_parent = nullptr;
896  m_multi_currency = false;
897  for (auto& parsed_lines_it: m_parsed_lines)
898  {
899  update_pre_trans_props (parsed_lines_it, position, old_type, type);
900  update_pre_split_props (parsed_lines_it, position, old_type, type);
901  }
902 }
903 
904 std::vector<GncTransPropType> GncTxImport::column_types ()
905 {
906  return m_settings.m_column_types;
907 }
908 
909 std::set<std::string>
910 GncTxImport::accounts ()
911 {
912  auto accts = std::set<std::string>();
913  auto acct_col_it = std::find (m_settings.m_column_types.begin(),
914  m_settings.m_column_types.end(), GncTransPropType::ACCOUNT);
915  uint32_t acct_col = acct_col_it - m_settings.m_column_types.begin();
916  auto tacct_col_it = std::find (m_settings.m_column_types.begin(),
917  m_settings.m_column_types.end(), GncTransPropType::TACCOUNT);
918  uint32_t tacct_col = tacct_col_it - m_settings.m_column_types.begin();
919 
920  /* Iterate over all parsed lines */
921  for (auto parsed_line : m_parsed_lines)
922  {
923  /* Skip current line if the user specified so */
924  if ((std::get<PL_SKIP>(parsed_line)))
925  continue;
926 
927  auto col_strs = std::get<PL_INPUT>(parsed_line);
928  if ((acct_col_it != m_settings.m_column_types.end()) &&
929  (acct_col < col_strs.size()) &&
930  !col_strs[acct_col].empty())
931  accts.insert(col_strs[acct_col]);
932  if ((tacct_col_it != m_settings.m_column_types.end()) &&
933  (tacct_col < col_strs.size()) &&
934  !col_strs[tacct_col].empty())
935  accts.insert(col_strs[tacct_col]);
936  }
937 
938  return accts;
939 }
CSV Import Settings.
gboolean gnc_commodity_is_currency(const gnc_commodity *cm)
Checks to see if the specified commodity is an ISO 4217 recognized currency or a legacy currency...
time64 xaccTransGetDate(const Transaction *trans)
Retrieve the posted date of the transaction.
~GncTxImport()
Destructor for GncTxImport.
bool save(void)
Save the gathered widget properties to a key File.
#define PINFO(format, args...)
Print an informational note.
Definition: qoflog.h:256
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.
Class to convert a csv file into vector of string vectors.
Exception that will be thrown whenever a parsing error is encountered.
Class to import transactions from CSV or fixed width files.
void load_file(const std::string &filename)
Loads a file into a GncTxImport.
GncTxImport(GncImpFileFormat format=GncImpFileFormat::UNKNOWN)
Constructor for GncTxImport.
#define PWARN(format, args...)
Log a warning.
Definition: qoflog.h:250
void multi_split(bool multi_split)
Toggles the multi-split state of the importer and will subsequently sanitize the column_types list...
bool preset_is_reserved_name(const std::string &name)
Check whether name can be used as a preset name.
void xaccTransVoid(Transaction *trans, const char *reason)
xaccTransVoid voids a transaction.
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.
void xaccTransCommitEdit(Transaction *trans)
The xaccTransCommitEdit() method indicates that the changes to the transaction and its splits are com...
gnc_commodity * gnc_account_get_currency_or_parent(const Account *account)
Returns a gnc_commodity that is a currency, suitable for being a Transaction&#39;s currency.
Definition: Account.cpp:3378
Class convert a file with fixed with delimited contents into vector of string vectors.
gnc_commodity * xaccAccountGetCommodity(const Account *acc)
Get the account&#39;s commodity.
Definition: Account.cpp:3371
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
Definition: gnc-date.h:87
std::vector< parse_line_t > m_parsed_lines
source file parsed into a two-dimensional array of strings.