25 #include <glib/gi18n.h> 32 #include "engine-helpers.h" 39 #include <boost/locale.hpp> 40 #include <boost/regex.hpp> 41 #include <boost/regex/icu.hpp> 42 #include <gnc-locale-utils.hpp> 43 #include "gnc-imp-props-price.hpp" 45 namespace bl = boost::locale;
47 G_GNUC_UNUSED
static QofLogModule log_module = GNC_MOD_IMPORT;
50 std::map<GncPricePropType, const char*> gnc_price_col_type_strs = {
51 { GncPricePropType::NONE, N_(
"None") },
52 { GncPricePropType::DATE, N_(
"Date") },
53 { GncPricePropType::AMOUNT, N_(
"Amount") },
54 { GncPricePropType::FROM_SYMBOL, N_(
"From Symbol") },
55 { GncPricePropType::FROM_NAMESPACE, N_(
"From Namespace") },
56 { GncPricePropType::TO_CURRENCY, N_(
"Currency To") },
65 GncNumeric parse_amount_price (
const std::string &str,
int currency_format)
68 if(!boost::regex_search(str, boost::regex(
"[0-9]")))
69 throw std::invalid_argument (_(
"Value doesn't appear to contain a valid number."));
71 auto expr = boost::make_u32regex(
"[[:Sc:]]");
72 std::string str_no_symbols;
73 boost::u32regex_replace(icu::UnicodeString::fromUTF8(str), expr,
"").toUTF8String(str_no_symbols);
76 gnc_numeric val = gnc_numeric_zero();
78 switch (currency_format)
83 throw std::invalid_argument (_(
"Value can't be parsed into a number using the selected currency format."));
88 throw std::invalid_argument (_(
"Value can't be parsed into a number using the selected currency format."));
93 throw std::invalid_argument (_(
"Value can't be parsed into a number using the selected currency format."));
106 gnc_commodity* parse_commodity_price_comm (
const std::string& symbol_str,
const std::string& namespace_str)
108 if (symbol_str.empty())
112 gnc_commodity* comm =
nullptr;
115 comm = gnc_commodity_table_lookup_unique (
table, symbol_str.c_str());
120 comm = gnc_commodity_table_lookup (
table,
121 namespace_str.c_str(), symbol_str.c_str());
125 throw std::invalid_argument (_(
"Value can't be parsed into a valid commodity."));
135 bool parse_namespace (
const std::string& namespace_str)
137 if (namespace_str.empty())
145 throw std::invalid_argument (_(
"Value can't be parsed into a valid namespace."));
150 void GncImportPrice::set (GncPricePropType prop_type,
const std::string& value,
bool enable_test_empty)
155 m_errors.erase(prop_type);
158 if (value.empty() && enable_test_empty)
159 throw std::invalid_argument (_(
"Column value can not be empty."));
161 gnc_commodity *comm =
nullptr;
164 case GncPricePropType::DATE:
169 case GncPricePropType::AMOUNT:
171 m_amount = parse_amount_price (value, m_currency_format);
174 case GncPricePropType::FROM_SYMBOL:
175 m_from_symbol.reset();
178 throw std::invalid_argument (_(
"'From Symbol' can not be empty."));
180 m_from_symbol = value;
182 if (m_from_namespace)
184 comm = parse_commodity_price_comm (value, *m_from_namespace);
187 if (m_to_currency == comm)
188 throw std::invalid_argument (_(
"'Commodity From' can not be the same as 'Currency To'."));
189 m_from_commodity = comm;
194 case GncPricePropType::FROM_NAMESPACE:
195 m_from_namespace.reset();
198 throw std::invalid_argument (_(
"'From Namespace' can not be empty."));
200 if (parse_namespace (value))
202 m_from_namespace = value;
206 comm = parse_commodity_price_comm (*m_from_symbol, *m_from_namespace);
209 if (m_to_currency == comm)
210 throw std::invalid_argument (_(
"'Commodity From' can not be the same as 'Currency To'."));
211 m_from_commodity = comm;
217 case GncPricePropType::TO_CURRENCY:
218 m_to_currency.reset();
219 comm = parse_commodity_price_comm (value, GNC_COMMODITY_NS_CURRENCY);
222 if (m_from_commodity == comm)
223 throw std::invalid_argument (_(
"'Currency To' can not be the same as 'Commodity From'."));
225 throw std::invalid_argument (_(
"Value parsed into an invalid currency for a currency column type."));
226 m_to_currency = comm;
232 PWARN (
"%d is an invalid property for a Price", static_cast<int>(prop_type));
236 catch (
const std::invalid_argument& e)
238 auto err_str = (bl::format (std::string{_(
"{1}: {2}")}) %
239 std::string{_(gnc_price_col_type_strs[prop_type])} %
241 m_errors.emplace(prop_type, err_str);
242 throw std::invalid_argument (err_str);
244 catch (
const std::out_of_range& e)
246 auto err_str = (bl::format (std::string{_(
"{1}: {2}")}) %
247 std::string{_(gnc_price_col_type_strs[prop_type])} %
249 m_errors.emplace(prop_type, err_str);
250 throw std::invalid_argument (err_str);
254 void GncImportPrice::reset (GncPricePropType prop_type)
258 if ((prop_type == GncPricePropType::FROM_NAMESPACE) ||
259 (prop_type == GncPricePropType::FROM_SYMBOL))
260 set_from_commodity (
nullptr);
262 if (prop_type == GncPricePropType::TO_CURRENCY)
263 set_to_currency (
nullptr);
266 set (prop_type, std::string(),
false);
272 m_errors.erase(prop_type);
276 std::string GncImportPrice::verify_essentials (
void)
280 return _(
"No date column.");
282 return _(
"No amount column.");
283 else if (!m_to_currency)
284 return _(
"No 'Currency to'.");
285 else if (!m_from_commodity)
286 return _(
"No 'Commodity from'.");
288 return _(
"'Commodity From' can not be the same as 'Currency To'.");
290 return std::string();
293 Result GncImportPrice::create_price (QofBook* book, GNCPriceDB *pdb,
bool over)
298 auto check = verify_essentials();
301 PWARN (
"Refusing to create price because essentials not set properly: %s", check.c_str());
307 auto amount = *m_amount;
308 Result ret_val = ADDED;
311 *m_to_currency, date);
314 if ((old_price !=
nullptr) && (over ==
true))
324 memset (date_str, 0,
sizeof(date_str));
326 DEBUG(
"Date is %s, Commodity from is '%s', Currency is '%s', " 327 "Amount is %s", date_str,
330 amount.to_string().c_str());
332 if (old_price ==
nullptr)
336 gnc_price_begin_edit (price);
338 gnc_price_set_commodity (price, *m_from_commodity);
339 gnc_price_set_currency (price, *m_to_currency);
342 auto amount_conv = amount.convert<RoundType::half_up>(scu * COMMODITY_DENOM_MULT);
344 gnc_price_set_value (price, static_cast<gnc_numeric>(amount_conv));
346 gnc_price_set_time64 (price, date);
347 gnc_price_set_source (price, PRICE_SOURCE_USER_PRICE);
348 gnc_price_set_typestr (price, PRICE_TYPE_LAST);
349 gnc_price_commit_edit (price);
356 throw std::invalid_argument (_(
"Failed to create price from selected columns."));
361 ret_val = DUPLICATED;
366 static std::string gen_err_str (std::map<GncPricePropType, std::string>& errors)
368 auto full_error = std::string();
369 for (
auto error : errors)
371 full_error += (full_error.empty() ?
"" :
"\n") + error.second;
376 std::string GncImportPrice::errors ()
378 return gen_err_str (m_errors);
GNCPrice * gnc_pricedb_lookup_day_t64(GNCPriceDB *db, const gnc_commodity *c, const gnc_commodity *currency, time64 t)
Return the price between the two commodities on the indicated day.
GNCPrice * gnc_price_create(QofBook *book)
gnc_price_create - returns a newly allocated and initialized price with a reference count of 1...
gnc_commodity_table * gnc_commodity_table_get_table(QofBook *book)
Returns the commodity table associated with a book.
gboolean xaccParseAmountImport(const char *in_str, gboolean monetary, gnc_numeric *result, char **endstr, gboolean skip)
Similar to xaccParseAmount, but with two differences.
gboolean gnc_commodity_is_currency(const gnc_commodity *cm)
Checks to see if the specified commodity is an ISO 4217 recognized currency or a legacy currency...
int gnc_commodity_get_fraction(const gnc_commodity *cm)
Retrieve the fraction for the specified commodity.
utility functions for the GnuCash UI
void gnc_price_unref(GNCPrice *p)
gnc_price_unref - indicate you're finished with a price (i.e.
#define DEBUG(format, args...)
Print a debugging message.
gboolean gnc_pricedb_add_price(GNCPriceDB *db, GNCPrice *p)
Add a price to the pricedb.
gboolean gnc_commodity_equal(const gnc_commodity *a, const gnc_commodity *b)
This routine returns TRUE if the two commodities are equal.
The primary numeric class for representing amounts and values.
#define PWARN(format, args...)
Log a warning.
gboolean xaccParseAmountExtImport(const char *in_str, gboolean monetary, gunichar negative_sign, gunichar decimal_point, gunichar group_separator, const char *ignore_list, gnc_numeric *result, char **endstr)
Similar to xaccParseAmountExtended, but will not automatically set a decimal point, regardless of what the user has set for this option.
int gnc_commodity_table_has_namespace(const gnc_commodity_table *table, const char *name_space)
Test to see if the indicated namespace exits in the commodity table.
const char * gnc_commodity_get_fullname(const gnc_commodity *cm)
Retrieve the full name for the specified commodity.
#define MAX_DATE_LENGTH
The maximum length of a string created by the date printers.
gboolean gnc_pricedb_remove_price(GNCPriceDB *db, GNCPrice *p)
Remove a price from the pricedb and unref the price.
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
size_t qof_print_date_buff(char *buff, size_t buflen, time64 secs)
Convenience: calls through to qof_print_date_dmy_buff().
static const std::vector< GncDateFormat > c_formats
A vector with all the date formats supported by the string constructor.