Skip to content

Commit

Permalink
Bug 798600 - CSV import of multi-split security transactions fails to…
Browse files Browse the repository at this point in the history
… load capital gain - part 2

Update csv transaction export format to include split values.
In the csv importer the 'GnuCash Export Format' option will
now include values. For compatibility with filex exported
from older gnucash versions the previous preset is still
available under the name 'GnuCash Export Format (4.x and older)'.
  • Loading branch information
gjanssens committed Feb 14, 2023
1 parent 9e1268d commit 003f379
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
33 changes: 31 additions & 2 deletions gnucash/import-export/csv-exp/csv-transactions-export.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,28 @@ add_amount (gchar *so_far, Split *split, gboolean t_void, gboolean symbol, CsvEx
return result;
}

// Value with Symbol or not
static gchar*
add_value (gchar *so_far, Split *split, gboolean t_void, gboolean symbol, CsvExportInfo *info)
{
const gchar *amt;
gchar *conv;
gchar *result;

Transaction *trans = xaccSplitGetParent(split);
gnc_commodity *tcurr = xaccTransGetCurrency (trans);
GNCPrintAmountInfo pai = gnc_commodity_print_info (tcurr, symbol);
if (t_void)
amt = xaccPrintAmount (xaccSplitVoidFormerValue (split), pai);
else
amt = xaccPrintAmount (xaccSplitGetValue (split), pai);
conv = csv_txn_test_field_string (info, amt);
result = g_strconcat (so_far, conv, info->mid_sep, NULL);
g_free (conv);
g_free (so_far);
return result;
}

// Share Price / Conversion factor
static gchar*
add_rate (gchar *so_far, Split *split, gboolean t_void, CsvExportInfo *info)
Expand Down Expand Up @@ -413,6 +435,8 @@ make_simple_trans_line (Account *acc, Transaction *trans, Split *split, CsvExpor
exp_line = add_reconcile (exp_line, split, info);
exp_line = add_amount (exp_line, split, t_void, TRUE, info);
exp_line = add_amount (exp_line, split, t_void, FALSE, info);
exp_line = add_value (exp_line, split, t_void, TRUE, info);
exp_line = add_value (exp_line, split, t_void, FALSE, info);
exp_line = add_rate (exp_line, split, t_void, info);
return exp_line;
}
Expand All @@ -426,6 +450,8 @@ make_split_part (gchar* exp_line, Split *split, gboolean t_void, CsvExportInfo *
exp_line = add_account_name (exp_line, split, FALSE, info);
exp_line = add_amount (exp_line, split, t_void, TRUE, info);
exp_line = add_amount (exp_line, split, t_void, FALSE, info);
exp_line = add_value (exp_line, split, t_void, TRUE, info);
exp_line = add_value (exp_line, split, t_void, FALSE, info);
exp_line = add_reconcile (exp_line, split, info);
exp_line = add_reconcile_date (exp_line, split, info);
exp_line = add_price (exp_line, split, t_void, info);
Expand Down Expand Up @@ -614,8 +640,10 @@ void csv_transactions_export (CsvExportInfo *info)
_("Date"), info->mid_sep, _("Account Name"),
info->mid_sep, (num_action ? _("Transaction Number") : _("Number")),
info->mid_sep, _("Description"), info->mid_sep, _("Full Category Path"),
info->mid_sep, _("Reconcile"), info->mid_sep, _("Amount With Sym"),
info->mid_sep, _("Amount Num."), info->mid_sep, _("Rate/Price"),
info->mid_sep, _("Reconcile"),
info->mid_sep, _("Amount With Sym"), info->mid_sep, _("Amount Num."),
info->mid_sep, _("Value With Sym"), info->mid_sep, _("Value Num."),
info->mid_sep, _("Rate/Price"),
info->end_sep, EOLSTR, NULL);
}
else
Expand All @@ -627,6 +655,7 @@ void csv_transactions_export (CsvExportInfo *info)
info->mid_sep, (num_action ? _("Number/Action") : _("Action")), info->mid_sep, _("Memo"),
info->mid_sep, _("Full Account Name"), info->mid_sep, _("Account Name"),
info->mid_sep, _("Amount With Sym"), info->mid_sep, _("Amount Num."),
info->mid_sep, _("Value With Sym"), info->mid_sep, _("Value Num."),
info->mid_sep, _("Reconcile"), info->mid_sep, _("Reconcile Date"), info->mid_sep, _("Rate/Price"),
info->end_sep, EOLSTR, NULL);
}
Expand Down
37 changes: 37 additions & 0 deletions gnucash/import-export/csv-imp/gnc-imp-settings-csv-tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,49 @@ static std::shared_ptr<CsvTransImpSettings> create_int_gnc_exp_preset(void)
GncTransPropType::NONE,
GncTransPropType::NONE,
GncTransPropType::AMOUNT,
GncTransPropType::NONE,
GncTransPropType::VALUE,
GncTransPropType::REC_STATE,
GncTransPropType::REC_DATE,
GncTransPropType::PRICE
};
return preset;
}

static std::shared_ptr<CsvTransImpSettings> create_int_gnc_exp_4_preset(void)
{
auto preset = std::make_shared<CsvTransImpSettings>();
preset->m_name = get_gnc_exp_4();
preset->m_skip_start_lines = 1;
preset->m_multi_split = true;

/* FIXME date and currency format should still be aligned with export format!
* That's currently hard to do, because the export uses whatever the user
* had set as global preference.
* preset->date_active = 0;
* preset->currency_active = 0;
*/
preset->m_column_types = {
GncTransPropType::DATE,
GncTransPropType::UNIQUE_ID,
GncTransPropType::NUM,
GncTransPropType::DESCRIPTION,
GncTransPropType::NOTES,
GncTransPropType::COMMODITY,
GncTransPropType::VOID_REASON,
GncTransPropType::ACTION,
GncTransPropType::MEMO,
GncTransPropType::ACCOUNT,
GncTransPropType::NONE,
GncTransPropType::NONE,
GncTransPropType::AMOUNT,
GncTransPropType::REC_STATE,
GncTransPropType::REC_DATE,
GncTransPropType::PRICE
};
return preset;
}

/**************************************************
* find
*
Expand Down Expand Up @@ -134,6 +170,7 @@ const preset_vec_trans& get_import_presets_trans (void)
/* Start with the internally generated ones */
presets_trans.push_back(create_int_no_preset());
presets_trans.push_back(create_int_gnc_exp_preset());
presets_trans.push_back(create_int_gnc_exp_4_preset());

/* Then add all the ones we found in the state file */
for (auto preset_name : preset_names)
Expand Down
6 changes: 6 additions & 0 deletions gnucash/import-export/csv-imp/gnc-imp-settings-csv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
const std::string csv_group_prefix{"CSV-"};
const std::string no_settings{N_("No Settings")};
const std::string gnc_exp{N_("GnuCash Export Format")};
const std::string gnc_exp_4{N_("GnuCash Export Format (4.x and older)")};

#define CSV_NAME "Name"
#define CSV_FORMAT "CsvFormat"
Expand Down Expand Up @@ -107,6 +108,11 @@ std::string get_gnc_exp (void)
return gnc_exp;
}

std::string get_gnc_exp_4 (void)
{
return gnc_exp_4;
}

/**************************************************
* load_common
*
Expand Down
1 change: 1 addition & 0 deletions gnucash/import-export/csv-imp/gnc-imp-settings-csv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ std::vector<uint32_t> m_column_widths; // The Column widths

std::string get_no_settings (void);
std::string get_gnc_exp (void);
std::string get_gnc_exp_4 (void);

/** Check whether name can be used as a preset name.
* The names of the internal presets are considered reserved.
Expand Down

0 comments on commit 003f379

Please sign in to comment.