CsvTxImp - rename 'Deposit' and 'Withdrawal' columns

Multiple reasons:
- they only have meaning in bank or cash contexts, but the importer is meant
  to be more generic.
- 'Withdrawal' is misleading in that the code behind it caters very
  generically for cases where values should be negated before being
  used. A 'Withdrawal' is only a single use case of this behaviour.

New names are 'Amount' (iso 'Deposit')
and 'Amount (Negated)' (iso 'Withdrawal')
This commit is contained in:
Geert Janssens 2023-02-01 12:47:37 +01:00
parent 4e6637e67e
commit 1c2c184e2e
4 changed files with 48 additions and 44 deletions

View File

@ -57,15 +57,15 @@ std::map<GncTransPropType, const char*> gnc_csv_col_type_strs = {
{ GncTransPropType::NONE, N_("None") }, { GncTransPropType::NONE, N_("None") },
{ GncTransPropType::UNIQUE_ID, N_("Transaction ID") }, { GncTransPropType::UNIQUE_ID, N_("Transaction ID") },
{ GncTransPropType::DATE, N_("Date") }, { GncTransPropType::DATE, N_("Date") },
{ GncTransPropType::NUM, N_("Num") }, { GncTransPropType::NUM, N_("Number") },
{ GncTransPropType::DESCRIPTION, N_("Description") }, { GncTransPropType::DESCRIPTION, N_("Description") },
{ GncTransPropType::NOTES, N_("Notes") }, { GncTransPropType::NOTES, N_("Notes") },
{ GncTransPropType::COMMODITY, N_("Transaction Commodity") }, { GncTransPropType::COMMODITY, N_("Transaction Commodity") },
{ GncTransPropType::VOID_REASON, N_("Void Reason") }, { GncTransPropType::VOID_REASON, N_("Void Reason") },
{ GncTransPropType::ACTION, N_("Action") }, { GncTransPropType::ACTION, N_("Action") },
{ GncTransPropType::ACCOUNT, N_("Account") }, { GncTransPropType::ACCOUNT, N_("Account") },
{ GncTransPropType::DEPOSIT, N_("Deposit") }, { GncTransPropType::AMOUNT, N_("Amount") },
{ GncTransPropType::WITHDRAWAL, N_("Withdrawal") }, { GncTransPropType::AMOUNT_NEG, N_("Amount (Negated)") },
{ GncTransPropType::PRICE, N_("Price") }, { GncTransPropType::PRICE, N_("Price") },
{ GncTransPropType::MEMO, N_("Memo") }, { GncTransPropType::MEMO, N_("Memo") },
{ GncTransPropType::REC_STATE, N_("Reconciled") }, { GncTransPropType::REC_STATE, N_("Reconciled") },
@ -442,13 +442,13 @@ void GncPreSplit::set (GncTransPropType prop_type, const std::string& value)
m_tmemo = value; m_tmemo = value;
break; break;
case GncTransPropType::DEPOSIT: case GncTransPropType::AMOUNT:
m_deposit = boost::none; m_amount = boost::none;
m_deposit = parse_monetary (value, m_currency_format); // Will throw if parsing fails m_amount = parse_monetary (value, m_currency_format); // Will throw if parsing fails
break; break;
case GncTransPropType::WITHDRAWAL: case GncTransPropType::AMOUNT_NEG:
m_withdrawal = boost::none; m_amount_neg = boost::none;
m_withdrawal = parse_monetary (value, m_currency_format); // Will throw if parsing fails m_amount_neg = parse_monetary (value, m_currency_format); // Will throw if parsing fails
break; break;
case GncTransPropType::PRICE: case GncTransPropType::PRICE:
@ -532,18 +532,18 @@ void GncPreSplit::add (GncTransPropType prop_type, const std::string& value)
auto num_val = GncNumeric(); auto num_val = GncNumeric();
switch (prop_type) switch (prop_type)
{ {
case GncTransPropType::DEPOSIT: case GncTransPropType::AMOUNT:
num_val = parse_monetary (value, m_currency_format); // Will throw if parsing fails num_val = parse_monetary (value, m_currency_format); // Will throw if parsing fails
if (m_deposit) if (m_amount)
num_val += *m_deposit; num_val += *m_amount;
m_deposit = num_val; m_amount = num_val;
break; break;
case GncTransPropType::WITHDRAWAL: case GncTransPropType::AMOUNT_NEG:
num_val = parse_monetary (value, m_currency_format); // Will throw if parsing fails num_val = parse_monetary (value, m_currency_format); // Will throw if parsing fails
if (m_withdrawal) if (m_amount_neg)
num_val += *m_withdrawal; num_val += *m_amount_neg;
m_withdrawal = num_val; m_amount_neg = num_val;
break; break;
default: default:
@ -574,8 +574,8 @@ std::string GncPreSplit::verify_essentials (void)
{ {
auto err_msg = std::string(); auto err_msg = std::string();
/* Make sure this split has the minimum required set of properties defined. */ /* Make sure this split has the minimum required set of properties defined. */
if (!m_deposit && !m_withdrawal) if (!m_amount && !m_amount_neg)
err_msg = _("No deposit or withdrawal column."); err_msg = _("No amount or negated amount column.");
if (m_rec_state && *m_rec_state == YREC && !m_rec_date) if (m_rec_state && *m_rec_state == YREC && !m_rec_date)
{ {
@ -679,20 +679,16 @@ void GncPreSplit::create_split (Transaction* trans)
Account *account = nullptr; Account *account = nullptr;
Account *taccount = nullptr; Account *taccount = nullptr;
auto deposit = GncNumeric();
auto withdrawal = GncNumeric();
auto amount = GncNumeric(); auto amount = GncNumeric();
if (m_account) if (m_account)
account = *m_account; account = *m_account;
if (m_taccount) if (m_taccount)
taccount = *m_taccount; taccount = *m_taccount;
if (m_deposit) if (m_amount)
deposit = *m_deposit; amount += *m_amount;
if (m_withdrawal) if (m_amount_neg)
withdrawal = *m_withdrawal; amount -= *m_amount_neg;
amount = deposit - withdrawal;
/* Add a split with the cumulative amount value. */ /* Add a split with the cumulative amount value. */
trans_add_split (trans, account, amount, m_action, m_memo, m_rec_state, m_rec_date, m_price); trans_add_split (trans, account, amount, m_action, m_memo, m_rec_state, m_rec_date, m_price);

View File

@ -59,8 +59,8 @@ enum class GncTransPropType {
ACTION, ACTION,
ACCOUNT, ACCOUNT,
DEPOSIT, AMOUNT,
WITHDRAWAL, AMOUNT_NEG,
PRICE, PRICE,
MEMO, MEMO,
REC_STATE, REC_STATE,
@ -173,8 +173,8 @@ private:
int m_currency_format; int m_currency_format;
boost::optional<std::string> m_action; boost::optional<std::string> m_action;
boost::optional<Account*> m_account; boost::optional<Account*> m_account;
boost::optional<GncNumeric> m_deposit; boost::optional<GncNumeric> m_amount;
boost::optional<GncNumeric> m_withdrawal; boost::optional<GncNumeric> m_amount_neg;
boost::optional<GncNumeric> m_price; boost::optional<GncNumeric> m_price;
boost::optional<std::string> m_memo; boost::optional<std::string> m_memo;
boost::optional<char> m_rec_state; boost::optional<char> m_rec_state;

View File

@ -89,7 +89,7 @@ static std::shared_ptr<CsvTransImpSettings> create_int_gnc_exp_preset(void)
GncTransPropType::ACCOUNT, GncTransPropType::ACCOUNT,
GncTransPropType::NONE, GncTransPropType::NONE,
GncTransPropType::NONE, GncTransPropType::NONE,
GncTransPropType::DEPOSIT, GncTransPropType::AMOUNT,
GncTransPropType::REC_STATE, GncTransPropType::REC_STATE,
GncTransPropType::REC_DATE, GncTransPropType::REC_DATE,
GncTransPropType::PRICE GncTransPropType::PRICE
@ -214,8 +214,16 @@ CsvTransImpSettings::load (void)
&list_len, &key_error); &list_len, &key_error);
for (uint32_t i = 0; i < list_len; i++) for (uint32_t i = 0; i < list_len; i++)
{ {
/* Special case a few legacy column names */
const char *col_type_str = col_types_str[i];
if (!g_strcmp0(col_type_str, "Deposit")) // -> "Amount"
col_type_str = gnc_csv_col_type_strs[GncTransPropType::AMOUNT];
if (!g_strcmp0(col_type_str, "Withdrawal")) // -> "Amount (Negated)"
col_type_str = gnc_csv_col_type_strs[GncTransPropType::AMOUNT_NEG];
if (!g_strcmp0(col_type_str, "Num")) // -> "Number"
col_type_str = gnc_csv_col_type_strs[GncTransPropType::NUM];
auto col_types_it = std::find_if (gnc_csv_col_type_strs.begin(), auto col_types_it = std::find_if (gnc_csv_col_type_strs.begin(),
gnc_csv_col_type_strs.end(), test_prop_type_str (col_types_str[i])); gnc_csv_col_type_strs.end(), test_prop_type_str (col_type_str));
if (col_types_it != gnc_csv_col_type_strs.end()) if (col_types_it != gnc_csv_col_type_strs.end())
{ {
/* Found a valid column type. Now check whether it is allowed /* Found a valid column type. Now check whether it is allowed

View File

@ -216,8 +216,8 @@ void GncTxImport::currency_format (int currency_format)
m_settings.m_currency_format = currency_format; m_settings.m_currency_format = currency_format;
/* Reparse all currency related columns */ /* Reparse all currency related columns */
std::vector<GncTransPropType> commodities = { GncTransPropType::DEPOSIT, std::vector<GncTransPropType> commodities = { GncTransPropType::AMOUNT,
GncTransPropType::WITHDRAWAL, GncTransPropType::AMOUNT_NEG,
GncTransPropType::PRICE}; GncTransPropType::PRICE};
reset_formatted_column (commodities); reset_formatted_column (commodities);
} }
@ -485,11 +485,11 @@ void GncTxImport::verify_column_selections (ErrorList& error_msg)
if (!check_for_column_type(GncTransPropType::DESCRIPTION)) if (!check_for_column_type(GncTransPropType::DESCRIPTION))
error_msg.add_error( _("Please select a description column.")); error_msg.add_error( _("Please select a description column."));
/* Verify at least one amount column (deposit or withdrawal) column is selected. /* Verify at least one amount column (amount or amount_neg) column is selected.
*/ */
if (!check_for_column_type(GncTransPropType::DEPOSIT) && if (!check_for_column_type(GncTransPropType::AMOUNT) &&
!check_for_column_type(GncTransPropType::WITHDRAWAL)) !check_for_column_type(GncTransPropType::AMOUNT_NEG))
error_msg.add_error( _("Please select a deposit or withdrawal column.")); error_msg.add_error( _("Please select a amount or negated amount column."));
/* Verify a transfer account is selected if any of the other transfer properties /* Verify a transfer account is selected if any of the other transfer properties
* are selected. * are selected.
@ -769,8 +769,8 @@ void GncTxImport::update_pre_trans_split_props (uint32_t row, uint32_t col, GncT
{ {
/* Except for Deposit or Withdrawal lines there can only be /* Except for Deposit or Withdrawal lines there can only be
* one column with a given property type. */ * one column with a given property type. */
if ((new_type != GncTransPropType::DEPOSIT) && if ((new_type != GncTransPropType::AMOUNT) &&
(new_type != GncTransPropType::WITHDRAWAL)) (new_type != GncTransPropType::AMOUNT_NEG))
{ {
auto value = std::get<PL_INPUT>(m_parsed_lines[row]).at(col); auto value = std::get<PL_INPUT>(m_parsed_lines[row]).at(col);
split_props->set(new_type, value); split_props->set(new_type, value);
@ -827,10 +827,10 @@ GncTxImport::set_column_type (uint32_t position, GncTransPropType type, bool for
if ((type == old_type) && !force) if ((type == old_type) && !force)
return; /* Nothing to do */ return; /* Nothing to do */
// Column types except deposit and withdrawal should be unique, // Column types except amount and negated amount should be unique,
// so remove any previous occurrence of the new type // so remove any previous occurrence of the new type
if ((type != GncTransPropType::DEPOSIT) && if ((type != GncTransPropType::AMOUNT) &&
(type != GncTransPropType::WITHDRAWAL)) (type != GncTransPropType::AMOUNT_NEG))
std::replace(m_settings.m_column_types.begin(), m_settings.m_column_types.end(), std::replace(m_settings.m_column_types.begin(), m_settings.m_column_types.end(),
type, GncTransPropType::NONE); type, GncTransPropType::NONE);