31 #include <glib/gi18n.h> 45 #include <boost/regex.hpp> 46 #include <boost/regex/icu.hpp> 49 #include "gnc-imp-props-tx.hpp" 54 G_GNUC_UNUSED
static QofLogModule log_module = GNC_MOD_IMPORT;
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")
70 m_skip_errors =
false;
89 if (
m_tokenizer && m_settings.m_file_format == format)
92 auto new_encoding = std::string(
"UTF-8");
93 auto new_imp_file = std::string();
100 if (
file_format() == GncImpFileFormat::FIXED_WIDTH)
103 if (!fwtok->get_columns().empty())
104 m_settings.m_column_widths = fwtok->get_columns();
108 m_settings.m_file_format = format;
109 m_tokenizer = gnc_tokenizer_factory(m_settings.m_file_format);
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())
124 fwtok->columns (m_settings.m_column_widths);
131 return m_settings.m_file_format;
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++)
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;
162 if (m_settings.m_multi_split)
163 m_settings.m_base_account =
nullptr;
166 bool GncTxImport::multi_split () {
return m_settings.m_multi_split; }
179 if (m_settings.m_multi_split)
181 m_settings.m_base_account =
nullptr;
185 m_settings.m_base_account = base_account;
187 if (m_settings.m_base_account)
189 auto col_type_it = std::find (m_settings.m_column_types.begin(),
190 m_settings.m_column_types.end(), GncTransPropType::ACCOUNT);
191 if (col_type_it != m_settings.m_column_types.end())
192 set_column_type(col_type_it - m_settings.m_column_types.begin(),
193 GncTransPropType::NONE);
197 std::get<PL_PRESPLIT>(line)->set_account (m_settings.m_base_account);
203 Account *GncTxImport::base_account () {
return m_settings.m_base_account; }
205 void GncTxImport::reset_formatted_column (std::vector<GncTransPropType>& col_types)
207 for (
auto col_type: col_types)
209 auto col = std::find (m_settings.m_column_types.begin(),
210 m_settings.m_column_types.end(), col_type);
211 if (col != m_settings.m_column_types.end())
212 set_column_type (col - m_settings.m_column_types.begin(), col_type,
true);
216 void GncTxImport::currency_format (
int currency_format)
218 m_settings.m_currency_format = currency_format;
221 std::vector<GncTransPropType> commodities = {
222 GncTransPropType::AMOUNT,
223 GncTransPropType::AMOUNT_NEG,
224 GncTransPropType::TAMOUNT,
225 GncTransPropType::TAMOUNT_NEG,
226 GncTransPropType::PRICE};
227 reset_formatted_column (commodities);
229 int GncTxImport::currency_format () {
return m_settings.m_currency_format; }
231 void GncTxImport::date_format (
int date_format)
233 m_settings.m_date_format = date_format;
236 std::vector<GncTransPropType> dates = { GncTransPropType::DATE,
237 GncTransPropType::REC_DATE,
238 GncTransPropType::TREC_DATE};
239 reset_formatted_column (dates);
241 int GncTxImport::date_format () {
return m_settings.m_date_format; }
263 m_settings.m_encoding = encoding;
266 std::string GncTxImport::encoding () {
return m_settings.m_encoding; }
268 void GncTxImport::update_skipped_lines(std::optional<uint32_t> start, std::optional<uint32_t> end,
269 std::optional<bool> alt, std::optional<bool> errors)
272 m_settings.m_skip_start_lines = *start;
274 m_settings.m_skip_end_lines = *end;
276 m_settings.m_skip_alt_lines = *alt;
278 m_skip_errors = *errors;
283 ((i < skip_start_lines()) ||
285 (((i - skip_start_lines()) % 2 == 1) &&
287 (m_skip_errors && !std::get<PL_ERROR>(
m_parsed_lines[i]).empty()));
291 uint32_t GncTxImport::skip_start_lines () {
return m_settings.m_skip_start_lines; }
292 uint32_t GncTxImport::skip_end_lines () {
return m_settings.m_skip_end_lines; }
293 bool GncTxImport::skip_alt_lines () {
return m_settings.m_skip_alt_lines; }
294 bool GncTxImport::skip_err_lines () {
return m_skip_errors; }
296 void GncTxImport::separators (std::string separators)
301 m_settings.m_separators = separators;
303 csvtok->set_separators (separators);
306 std::string GncTxImport::separators () {
return m_settings.m_separators; }
313 m_settings = settings;
319 separators (m_settings.m_separators);
320 else if (
file_format() == GncImpFileFormat::FIXED_WIDTH)
323 fwtok->columns (m_settings.m_column_widths);
335 std::copy_n (settings.m_column_types.begin(),
336 std::min (m_settings.m_column_types.size(), settings.m_column_types.size()),
337 m_settings.m_column_types.begin());
341 bool GncTxImport::save_settings ()
351 if (
file_format() == GncImpFileFormat::FIXED_WIDTH)
354 m_settings.m_column_widths = fwtok->get_columns();
357 return m_settings.
save();
360 void GncTxImport::settings_name (std::string name) { m_settings.m_name = name; }
361 std::string GncTxImport::settings_name () {
return m_settings.m_name; }
378 catch (std::ifstream::failure& ios_err)
381 PWARN (
"Error: %s", ios_err.what());
402 uint32_t max_cols = 0;
405 for (
auto tokenized_line :
m_tokenizer->get_tokens())
407 auto length = tokenized_line.size();
410 auto pretrans = std::make_shared<GncPreTrans>(date_format(), m_settings.m_multi_split);
411 auto presplit = std::make_shared<GncPreSplit>(date_format(), currency_format());
412 presplit->set_pre_trans (std::move (pretrans));
413 m_parsed_lines.push_back (std::make_tuple (tokenized_line, ErrMap(),
414 presplit->get_pre_trans(), std::move (presplit),
false));
416 if (length > max_cols)
423 throw (std::range_error (N_(
"There was an error parsing the file.")));
427 m_settings.m_column_types.resize(max_cols, GncTransPropType::NONE);
430 for (uint32_t i = 0; i < m_settings.m_column_types.size(); i++)
431 set_column_type (i, m_settings.m_column_types[i],
true);
432 if (m_settings.m_base_account)
435 std::get<PL_PRESPLIT>(line)->set_account (m_settings.m_base_account);
450 void add_error (std::string msg);
456 void ErrorList::add_error (std::string msg)
458 m_error.emplace_back (msg);
461 std::string ErrorList::str()
463 auto err_msg = std::string();
464 if (!m_error.empty())
466 auto add_bullet_item = [](std::string& a, std::string& b)->std::string {
return std::move(a) +
"\n• " + b; };
467 err_msg = std::accumulate (m_error.begin(), m_error.end(), std::move (err_msg), add_bullet_item);
468 err_msg.erase (0, 1);
479 void GncTxImport::verify_column_selections (
ErrorList& error_msg)
484 if (!check_for_column_type(GncTransPropType::DATE))
485 error_msg.add_error( _(
"Please select a date column."));
490 if (!check_for_column_type(GncTransPropType::ACCOUNT))
492 if (m_settings.m_multi_split)
493 error_msg.add_error( _(
"Please select an account column."));
494 else if (!m_settings.m_base_account)
495 error_msg.add_error( _(
"Please select an account column or set a base account in the Account field."));
500 if (!check_for_column_type(GncTransPropType::DESCRIPTION))
501 error_msg.add_error( _(
"Please select a description column."));
505 if (!check_for_column_type(GncTransPropType::AMOUNT) &&
506 !check_for_column_type(GncTransPropType::AMOUNT_NEG))
507 error_msg.add_error( _(
"Please select a (negated) amount column."));
518 if (m_multi_currency)
520 if (m_settings.m_multi_split &&
521 !check_for_column_type(GncTransPropType::PRICE) &&
522 !check_for_column_type(GncTransPropType::VALUE) &&
523 !check_for_column_type(GncTransPropType::VALUE_NEG))
524 error_msg.add_error( _(
"The current account selections will generate multi-currency transactions. Please select one of the following columns: price, (negated) value."));
525 else if (!m_settings.m_multi_split &&
526 !check_for_column_type(GncTransPropType::PRICE) &&
527 !check_for_column_type(GncTransPropType::TAMOUNT) &&
528 !check_for_column_type(GncTransPropType::TAMOUNT_NEG) &&
529 !check_for_column_type(GncTransPropType::VALUE) &&
530 !check_for_column_type(GncTransPropType::VALUE_NEG))
531 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."));
544 std::string GncTxImport::verify (
bool with_acct_errors)
546 auto newline = std::string();
552 error_msg.add_error(_(
"No valid data found in the selected file. It may be empty or the selected encoding is wrong."));
553 return error_msg.str();
557 auto skip_alt_offset = m_settings.m_skip_alt_lines ? 1 : 0;
558 if (m_settings.m_skip_start_lines + m_settings.m_skip_end_lines + skip_alt_offset >=
m_parsed_lines.size())
560 error_msg.add_error(_(
"No lines are selected for importing. Please reduce the number of lines to skip."));
561 return error_msg.str();
564 verify_column_selections (error_msg);
566 update_skipped_lines (std::nullopt, std::nullopt, std::nullopt, std::nullopt);
568 auto have_line_errors =
false;
571 auto errors = std::get<PL_ERROR>(line);
572 if (std::get<PL_SKIP>(line))
574 if (with_acct_errors && !errors.empty())
576 have_line_errors =
true;
579 auto non_acct_error = [](ErrPair curr_err)
581 return !((curr_err.first == GncTransPropType::ACCOUNT) ||
582 (curr_err.first == GncTransPropType::TACCOUNT));
584 if (!with_acct_errors &&
585 std::any_of(errors.cbegin(), errors.cend(), non_acct_error))
587 have_line_errors =
true;
592 if (have_line_errors)
593 error_msg.add_error( _(
"Not all fields could be parsed. Please correct the issues reported for each line or adjust the lines to skip."));
595 return error_msg.str();
605 std::shared_ptr<DraftTransaction> GncTxImport::trans_properties_to_trans (std::vector<parse_line_t>::iterator& parsed_line)
607 auto created_trans =
false;
608 std::shared_ptr<GncPreSplit> split_props;
609 std::tie(std::ignore, std::ignore, std::ignore, split_props, std::ignore) = *parsed_line;
610 auto trans_props = split_props->get_pre_trans();
611 auto account = split_props->get_account();
613 QofBook* book = gnc_account_get_book (account);
618 auto draft_trans = trans_props->create_trans (book, currency);
625 if (m_current_draft && m_current_draft->void_reason)
633 xaccTransVoid (m_current_draft->trans, m_current_draft->void_reason->c_str());
635 m_current_draft = draft_trans;
636 m_current_draft->void_reason = trans_props->get_void_reason();
637 created_trans =
true;
639 else if (m_settings.m_multi_split)
640 draft_trans = m_current_draft;
642 throw std::invalid_argument (
"Failed to create transaction from selected columns.");
647 split_props->create_split (draft_trans);
653 return created_trans ? m_current_draft :
nullptr;
656 void GncTxImport::create_transaction (std::vector<parse_line_t>::iterator& parsed_line)
659 std::shared_ptr<GncPreSplit> split_props =
nullptr;
660 bool skip_line =
false;
661 std::tie(std::ignore, errors, std::ignore, split_props, skip_line) = *parsed_line;
662 auto trans_props = split_props->get_pre_trans();
670 auto error_message = _(
"Current line still has parse errors.\n" 671 "This should never happen. Please report this as a bug.");
676 auto line_acct = split_props->get_account();
681 auto error_message = _(
"No account column selected and no base account specified either.\n" 682 "This should never happen. Please report this as a bug.");
683 PINFO(
"User warning: %s", error_message);
684 auto errs = ErrMap { ErrPair { GncTransPropType::NONE, error_message},};
692 auto draft_trans = trans_properties_to_trans (parsed_line);
696 m_transactions.insert (std::pair<
time64, std::shared_ptr<DraftTransaction>>(trans_date,std::move(draft_trans)));
699 catch (
const std::invalid_argument& e)
701 auto err_str = _(
"Problem creating preliminary transaction");
702 PINFO(
"%s: %s", err_str, e.what());
703 auto errs = ErrMap { ErrPair { GncTransPropType::NONE, err_str},};
719 auto verify_result = verify (
true);
720 if (!verify_result.empty())
721 throw std::invalid_argument (verify_result);
734 if ((std::get<PL_SKIP>(*parsed_lines_it)))
738 create_transaction (parsed_lines_it);
744 GncTxImport::check_for_column_type (GncTransPropType type)
746 return (std::find (m_settings.m_column_types.begin(),
747 m_settings.m_column_types.end(), type)
748 != m_settings.m_column_types.end());
752 void GncTxImport::update_pre_split_multi_col_prop (
parse_line_t& parsed_line, GncTransPropType col_type)
754 if (!is_multi_col_prop(col_type))
757 auto input_vec = std::get<PL_INPUT>(parsed_line);
758 auto split_props = std::get<PL_PRESPLIT> (parsed_line);
763 for (
auto col_it = m_settings.m_column_types.cbegin();
764 col_it < m_settings.m_column_types.cend();
766 if (*col_it == col_type)
768 auto value = std::string();
769 auto col_num =
static_cast<uint32_t
>(col_it - m_settings.m_column_types.cbegin());
771 if (col_num < input_vec.size())
772 value = input_vec.at(col_num);
773 split_props->add (col_type, value);
777 void GncTxImport::update_pre_trans_props (
parse_line_t& parsed_line, uint32_t col, GncTransPropType old_type, GncTransPropType new_type)
779 auto input_vec = std::get<PL_INPUT>(parsed_line);
780 auto trans_props = std::get<PL_PRETRANS> (parsed_line);
784 trans_props->set_date_format (m_settings.m_date_format);
785 trans_props->set_multi_split (m_settings.m_multi_split);
787 if ((old_type > GncTransPropType::NONE) && (old_type <= GncTransPropType::TRANS_PROPS))
788 trans_props->reset (old_type);
789 if ((new_type > GncTransPropType::NONE) && (new_type <= GncTransPropType::TRANS_PROPS))
791 auto value = std::string();
793 if (col < input_vec.size())
794 value = input_vec.at(col);
796 trans_props->set(new_type, value);
803 if ((old_type == GncTransPropType::ACCOUNT) || (new_type == GncTransPropType::ACCOUNT))
804 trans_props->reset_cross_split_counters();
807 void GncTxImport::update_pre_split_props (
parse_line_t& parsed_line, uint32_t col, GncTransPropType old_type, GncTransPropType new_type)
819 auto split_props = std::get<PL_PRESPLIT> (parsed_line);
820 auto trans_props = std::get<PL_PRETRANS> (parsed_line);
823 split_props->set_date_format (m_settings.m_date_format);
824 if (m_settings.m_multi_split && trans_props->is_part_of( m_parent))
825 split_props->set_pre_trans (m_parent);
828 split_props->set_pre_trans (trans_props);
829 m_parent = trans_props;
832 if ((old_type > GncTransPropType::TRANS_PROPS) && (old_type <= GncTransPropType::SPLIT_PROPS))
834 split_props->reset (old_type);
835 if (is_multi_col_prop(old_type))
836 update_pre_split_multi_col_prop (parsed_line, old_type);
839 if ((new_type > GncTransPropType::TRANS_PROPS) && (new_type <= GncTransPropType::SPLIT_PROPS))
841 if (is_multi_col_prop(new_type))
843 split_props->reset(new_type);
844 update_pre_split_multi_col_prop (parsed_line, new_type);
848 auto input_vec = std::get<PL_INPUT>(parsed_line);
849 auto value = std::string();
850 if (col < input_vec.size())
851 value = input_vec.at(col);
852 split_props->set(new_type, value);
855 m_multi_currency |= split_props->get_pre_trans()->is_multi_currency();
858 auto all_errors = split_props->get_pre_trans()->errors();
859 all_errors.merge (split_props->errors());
860 std::get<PL_ERROR>(parsed_line) = std::move(all_errors);
865 GncTxImport::set_column_type (uint32_t position, GncTransPropType type,
bool force)
867 if (position >= m_settings.m_column_types.size())
870 auto old_type = m_settings.m_column_types[position];
871 if ((type == old_type) && !force)
876 if (!is_multi_col_prop(type))
877 std::replace(m_settings.m_column_types.begin(), m_settings.m_column_types.end(),
878 type, GncTransPropType::NONE);
880 m_settings.m_column_types.at (position) = type;
883 if (type == GncTransPropType::ACCOUNT)
888 m_multi_currency =
false;
891 update_pre_trans_props (parsed_lines_it, position, old_type, type);
892 update_pre_split_props (parsed_lines_it, position, old_type, type);
896 std::vector<GncTransPropType> GncTxImport::column_types ()
898 return m_settings.m_column_types;
901 std::set<std::string>
902 GncTxImport::accounts ()
904 auto accts = std::set<std::string>();
905 auto acct_col_it = std::find (m_settings.m_column_types.begin(),
906 m_settings.m_column_types.end(), GncTransPropType::ACCOUNT);
907 uint32_t acct_col = acct_col_it - m_settings.m_column_types.begin();
908 auto tacct_col_it = std::find (m_settings.m_column_types.begin(),
909 m_settings.m_column_types.end(), GncTransPropType::TACCOUNT);
910 uint32_t tacct_col = tacct_col_it - m_settings.m_column_types.begin();
916 if ((std::get<PL_SKIP>(parsed_line)))
919 auto col_strs = std::get<PL_INPUT>(parsed_line);
920 if ((acct_col_it != m_settings.m_column_types.end()) &&
921 (acct_col < col_strs.size()) &&
922 !col_strs[acct_col].empty())
923 accts.insert(col_strs[acct_col]);
924 if ((tacct_col_it != m_settings.m_column_types.end()) &&
925 (tacct_col < col_strs.size()) &&
926 !col_strs[tacct_col].empty())
927 accts.insert(col_strs[tacct_col]);
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.
std::tuple< StrVec, std::string, std::shared_ptr< GncImportPrice >, bool > parse_line_t
Tuple to hold.
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.
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's currency.
Class convert a file with fixed with delimited contents into vector of string vectors.
gnc_commodity * xaccAccountGetCommodity(const Account *acc)
Get the account's commodity.
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
std::vector< parse_line_t > m_parsed_lines
source file parsed into a two-dimensional array of strings.