Bug 798600 - CSV import of multi-split security transactions fails to 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)'.
This commit is contained in:
Geert Janssens 2023-02-14 11:43:46 +01:00
parent 9e1268d934
commit 003f379d88
4 changed files with 75 additions and 2 deletions

View File

@ -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)
@ -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;
}
@ -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);
@ -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
@ -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);
}

View File

@ -90,6 +90,8 @@ 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
@ -97,6 +99,40 @@ static std::shared_ptr<CsvTransImpSettings> create_int_gnc_exp_preset(void)
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
*
@ -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)

View File

@ -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"
@ -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
*

View File

@ -90,6 +90,7 @@ protected:
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.