Skip to content

Commit

Permalink
Bug 797756 - Currency format setting is ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
gjanssens committed Feb 4, 2023
1 parent 1ce5ace commit 7fa4966
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 32 deletions.
40 changes: 9 additions & 31 deletions gnucash/import-export/csv-imp/gnc-imp-props-tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ GncTransPropType sanitize_trans_prop (GncTransPropType prop, bool multi_split)
* @return a GncNumeric
* @exception May throw std::invalid argument if string can't be parsed properly
*/
GncNumeric parse_amount (const std::string &str, int currency_format)
GncNumeric parse_monetary (const std::string &str, int currency_format)
{
/* An empty field is treated as zero */
if (str.empty())
Expand Down Expand Up @@ -200,31 +200,6 @@ gnc_commodity* parse_commodity (const std::string& comm_str)
return comm;
}


static GncNumeric parse_price (const std::string &str)
{
/* An empty field is treated as zero */
if (str.empty())
return GncNumeric{};

/* Strings otherwise containing not digits will be considered invalid */
if(!boost::regex_search(str, boost::regex("[0-9]")))
throw std::invalid_argument (_("Value doesn't appear to contain a valid number."));

auto expr = boost::make_u32regex("[[:Sc:]]");
std::string str_no_symbols = boost::u32regex_replace(str, expr, "");

/* Convert based on user chosen currency format */
gnc_numeric val = gnc_numeric_zero();
char *endptr;

auto success = gnc_exp_parser_parse (str.c_str(), &val, &endptr);
if (!success)
throw std::invalid_argument (_("Price can't be parsed into a number."));

return GncNumeric(val);
}

void GncPreTrans::set (GncTransPropType prop_type, const std::string& value)
{
try
Expand Down Expand Up @@ -460,16 +435,19 @@ void GncPreSplit::set (GncTransPropType prop_type, const std::string& value)

case GncTransPropType::DEPOSIT:
m_deposit = boost::none;
m_deposit = parse_amount (value, m_currency_format); // Will throw if parsing fails
m_deposit = parse_monetary (value, m_currency_format); // Will throw if parsing fails
break;
case GncTransPropType::WITHDRAWAL:
m_withdrawal = boost::none;
m_withdrawal = parse_amount (value, m_currency_format); // Will throw if parsing fails
m_withdrawal = parse_monetary (value, m_currency_format); // Will throw if parsing fails
break;

case GncTransPropType::PRICE:
/* Note while a price is not stricly a currency, it will likely use
* the same decimal point as currencies in the csv file, so parse
* using the same parser */
m_price = boost::none;
m_price = parse_price (value); // Will throw if parsing fails
m_price = parse_monetary (value, m_currency_format); // Will throw if parsing fails
break;

case GncTransPropType::REC_STATE:
Expand Down Expand Up @@ -546,14 +524,14 @@ void GncPreSplit::add (GncTransPropType prop_type, const std::string& value)
switch (prop_type)
{
case GncTransPropType::DEPOSIT:
num_val = parse_amount (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)
num_val += *m_deposit;
m_deposit = num_val;
break;

case GncTransPropType::WITHDRAWAL:
num_val = parse_amount (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)
num_val += *m_withdrawal;
m_withdrawal = num_val;
Expand Down
2 changes: 1 addition & 1 deletion gnucash/import-export/csv-imp/gnc-imp-props-tx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ GncTransPropType sanitize_trans_prop (GncTransPropType prop, bool multi_split);


gnc_commodity* parse_commodity (const std::string& comm_str);
GncNumeric parse_amount (const std::string &str, int currency_format);
GncNumeric parse_monetary (const std::string &str, int currency_format);

struct GncPreTrans
{
Expand Down

0 comments on commit 7fa4966

Please sign in to comment.