Bug 797756 - Currency format setting is ignored

This commit is contained in:
Geert Janssens 2023-01-31 18:45:30 +01:00
parent 1ce5ace25f
commit 7fa4966e57
2 changed files with 10 additions and 32 deletions

View File

@ -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())
@ -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
@ -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:
@ -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;

View File

@ -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
{