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 = boost::u32regex_replace(str, expr,
"");
75 gnc_numeric val = gnc_numeric_zero();
77 switch (currency_format)
82 throw std::invalid_argument (_(
"Value can't be parsed into a number using the selected currency format."));
87 throw std::invalid_argument (_(
"Value can't be parsed into a number using the selected currency format."));
92 throw std::invalid_argument (_(
"Value can't be parsed into a number using the selected currency format."));
105 gnc_commodity* parse_commodity_price_comm (
const std::string& symbol_str,
const std::string& namespace_str)
107 if (symbol_str.empty())
111 gnc_commodity* comm =
nullptr;
114 comm = gnc_commodity_table_lookup_unique (
table, symbol_str.c_str());
119 comm = gnc_commodity_table_lookup (
table,
120 namespace_str.c_str(), symbol_str.c_str());
124 throw std::invalid_argument (_(
"Value can't be parsed into a valid commodity."));
134 bool parse_namespace (
const std::string& namespace_str)
136 if (namespace_str.empty())
144 throw std::invalid_argument (_(
"Value can't be parsed into a valid namespace."));
149 void GncImportPrice::set (GncPricePropType prop_type,
const std::string& value,
bool enable_test_empty)
154 m_errors.erase(prop_type);
157 if (value.empty() && enable_test_empty)
158 throw std::invalid_argument (_(
"Column value can not be empty."));
160 gnc_commodity *comm =
nullptr;
163 case GncPricePropType::DATE:
168 case GncPricePropType::AMOUNT:
170 m_amount = parse_amount_price (value, m_currency_format);
173 case GncPricePropType::FROM_SYMBOL:
174 m_from_symbol.reset();
177 throw std::invalid_argument (_(
"'From Symbol' can not be empty."));
179 m_from_symbol = value;
181 if (m_from_namespace)
183 comm = parse_commodity_price_comm (value, *m_from_namespace);
186 if (m_to_currency == comm)
187 throw std::invalid_argument (_(
"'Commodity From' can not be the same as 'Currency To'."));
188 m_from_commodity = comm;
193 case GncPricePropType::FROM_NAMESPACE:
194 m_from_namespace.reset();
197 throw std::invalid_argument (_(
"'From Namespace' can not be empty."));
199 if (parse_namespace (value))
201 m_from_namespace = value;
205 comm = parse_commodity_price_comm (*m_from_symbol, *m_from_namespace);
208 if (m_to_currency == comm)
209 throw std::invalid_argument (_(
"'Commodity From' can not be the same as 'Currency To'."));
210 m_from_commodity = comm;
216 case GncPricePropType::TO_CURRENCY:
217 m_to_currency.reset();
218 comm = parse_commodity_price_comm (value, GNC_COMMODITY_NS_CURRENCY);
221 if (m_from_commodity == comm)
222 throw std::invalid_argument (_(
"'Currency To' can not be the same as 'Commodity From'."));
224 throw std::invalid_argument (_(
"Value parsed into an invalid currency for a currency column type."));
225 m_to_currency = comm;
231 PWARN (
"%d is an invalid property for a Price", static_cast<int>(prop_type));
235 catch (
const std::invalid_argument& e)
237 auto err_str = (bl::format (std::string{_(
"{1}: {2}")}) %
238 std::string{_(gnc_price_col_type_strs[prop_type])} %
240 m_errors.emplace(prop_type, err_str);
241 throw std::invalid_argument (err_str);
243 catch (
const std::out_of_range& e)
245 auto err_str = (bl::format (std::string{_(
"{1}: {2}")}) %
246 std::string{_(gnc_price_col_type_strs[prop_type])} %
248 m_errors.emplace(prop_type, err_str);
249 throw std::invalid_argument (err_str);
253 void GncImportPrice::reset (GncPricePropType prop_type)
257 if ((prop_type == GncPricePropType::FROM_NAMESPACE) ||
258 (prop_type == GncPricePropType::FROM_SYMBOL))
259 set_from_commodity (
nullptr);
261 if (prop_type == GncPricePropType::TO_CURRENCY)
262 set_to_currency (
nullptr);
265 set (prop_type, std::string(),
false);
271 m_errors.erase(prop_type);
275 std::string GncImportPrice::verify_essentials (
void)
279 return _(
"No date column.");
281 return _(
"No amount column.");
282 else if (!m_to_currency)
283 return _(
"No 'Currency to'.");
284 else if (!m_from_commodity)
285 return _(
"No 'Commodity from'.");
287 return _(
"'Commodity From' can not be the same as 'Currency To'.");
289 return std::string();
292 Result GncImportPrice::create_price (QofBook* book, GNCPriceDB *pdb,
bool over)
297 auto check = verify_essentials();
300 PWARN (
"Refusing to create price because essentials not set properly: %s", check.c_str());
306 auto amount = *m_amount;
307 Result ret_val = ADDED;
310 *m_to_currency, date);
313 if ((old_price !=
nullptr) && (over ==
true))
323 memset (date_str, 0,
sizeof(date_str));
325 DEBUG(
"Date is %s, Commodity from is '%s', Currency is '%s', " 326 "Amount is %s", date_str,
329 amount.to_string().c_str());
331 if (old_price ==
nullptr)
335 gnc_price_begin_edit (price);
337 gnc_price_set_commodity (price, *m_from_commodity);
338 gnc_price_set_currency (price, *m_to_currency);
341 auto amount_conv = amount.convert<RoundType::half_up>(scu * COMMODITY_DENOM_MULT);
343 gnc_price_set_value (price, static_cast<gnc_numeric>(amount_conv));
345 gnc_price_set_time64 (price, date);
346 gnc_price_set_source (price, PRICE_SOURCE_USER_PRICE);
347 gnc_price_set_typestr (price, PRICE_TYPE_LAST);
348 gnc_price_commit_edit (price);
355 throw std::invalid_argument (_(
"Failed to create price from selected columns."));
360 ret_val = DUPLICATED;
365 static std::string gen_err_str (std::map<GncPricePropType, std::string>& errors)
367 auto full_error = std::string();
368 for (
auto error : errors)
370 full_error += (full_error.empty() ?
"" :
"\n") + error.second;
375 std::string GncImportPrice::errors ()
377 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.