GnuCash  5.6-150-g038405b370+
gnc-imp-props-price.cpp
1 /********************************************************************\
2  * gnc-imp-props-price.cpp - encapsulate price properties for use *
3  * in the csv importer *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License*
16  * along with this program; if not, contact: *
17  * *
18  * Free Software Foundation Voice: +1-617-542-5942 *
19  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
20  * Boston, MA 02110-1301, USA gnu@gnu.org *
21  * *
22 \********************************************************************/
23 
24 #include <glib.h>
25 #include <glib/gi18n.h>
26 
27 #include <platform.h>
28 #if PLATFORM(WINDOWS)
29 #include <windows.h>
30 #endif
31 
32 #include "engine-helpers.h"
33 #include "gnc-ui-util.h"
34 
35 #include <exception>
36 #include <map>
37 #include <optional>
38 #include <string>
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"
44 
45 namespace bl = boost::locale;
46 
47 G_GNUC_UNUSED static QofLogModule log_module = GNC_MOD_IMPORT;
48 
49 /* This map contains a set of strings representing the different column types. */
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") },
57 };
58 
65 GncNumeric parse_amount_price (const std::string &str, int currency_format)
66 {
67  /* If a cell is empty or just spaces return invalid amount */
68  if(!boost::regex_search(str, boost::regex("[0-9]")))
69  throw std::invalid_argument (_("Value doesn't appear to contain a valid number."));
70 
71  auto expr = boost::make_u32regex("[[:Sc:]]");
72  std::string str_no_symbols = boost::u32regex_replace(str, expr, "");
73 
74  /* Convert based on user chosen currency format */
75  gnc_numeric val = gnc_numeric_zero();
76  char *endptr;
77  switch (currency_format)
78  {
79  case 0:
80  /* Currency locale */
81  if (!(xaccParseAmountImport (str_no_symbols.c_str(), TRUE, &val, &endptr, TRUE)))
82  throw std::invalid_argument (_("Value can't be parsed into a number using the selected currency format."));
83  break;
84  case 1:
85  /* Currency decimal period */
86  if (!(xaccParseAmountExtImport (str_no_symbols.c_str(), TRUE, '-', '.', ',', "$+", &val, &endptr)))
87  throw std::invalid_argument (_("Value can't be parsed into a number using the selected currency format."));
88  break;
89  case 2:
90  /* Currency decimal comma */
91  if (!(xaccParseAmountExtImport (str_no_symbols.c_str(), TRUE, '-', ',', '.', "$+", &val, &endptr)))
92  throw std::invalid_argument (_("Value can't be parsed into a number using the selected currency format."));
93  break;
94  }
95 
96  return GncNumeric(val);
97 }
98 
105 gnc_commodity* parse_commodity_price_comm (const std::string& symbol_str, const std::string& namespace_str)
106 {
107  if (symbol_str.empty())
108  return nullptr;
109 
110  auto table = gnc_commodity_table_get_table (gnc_get_current_book());
111  gnc_commodity* comm = nullptr;
112 
113  /* First try commodity as a unique name, used in loading settings, returns null if not found */
114  comm = gnc_commodity_table_lookup_unique (table, symbol_str.c_str());
115 
116  /* Now lookup with namespace and symbol */
117  if (!comm)
118  {
119  comm = gnc_commodity_table_lookup (table,
120  namespace_str.c_str(), symbol_str.c_str());
121  }
122 
123  if (!comm)
124  throw std::invalid_argument (_("Value can't be parsed into a valid commodity."));
125  else
126  return comm;
127 }
128 
134 bool parse_namespace (const std::string& namespace_str)
135 {
136  if (namespace_str.empty())
137  return false;
138 
139  auto table = gnc_commodity_table_get_table (gnc_get_current_book());
140 
141  if (gnc_commodity_table_has_namespace (table, namespace_str.c_str()))
142  return true;
143  else
144  throw std::invalid_argument (_("Value can't be parsed into a valid namespace."));
145 
146  return false;
147 }
148 
149 void GncImportPrice::set (GncPricePropType prop_type, const std::string& value, bool enable_test_empty)
150 {
151  try
152  {
153  // Drop any existing error for the prop_type we're about to set
154  m_errors.erase(prop_type);
155 
156  // conditional test for empty values
157  if (value.empty() && enable_test_empty)
158  throw std::invalid_argument (_("Column value can not be empty."));
159 
160  gnc_commodity *comm = nullptr;
161  switch (prop_type)
162  {
163  case GncPricePropType::DATE:
164  m_date.reset();
165  m_date = GncDate(value, GncDate::c_formats[m_date_format].m_fmt); // Throws if parsing fails
166  break;
167 
168  case GncPricePropType::AMOUNT:
169  m_amount.reset();
170  m_amount = parse_amount_price (value, m_currency_format); // Throws if parsing fails
171  break;
172 
173  case GncPricePropType::FROM_SYMBOL:
174  m_from_symbol.reset();
175 
176  if (value.empty())
177  throw std::invalid_argument (_("'From Symbol' can not be empty."));
178  else
179  m_from_symbol = value;
180 
181  if (m_from_namespace)
182  {
183  comm = parse_commodity_price_comm (value, *m_from_namespace); // Throws if parsing fails
184  if (comm)
185  {
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;
189  }
190  }
191  break;
192 
193  case GncPricePropType::FROM_NAMESPACE:
194  m_from_namespace.reset();
195 
196  if (value.empty())
197  throw std::invalid_argument (_("'From Namespace' can not be empty."));
198 
199  if (parse_namespace (value)) // Throws if parsing fails
200  {
201  m_from_namespace = value;
202 
203  if (m_from_symbol)
204  {
205  comm = parse_commodity_price_comm (*m_from_symbol, *m_from_namespace); // Throws if parsing fails
206  if (comm)
207  {
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;
211  }
212  }
213  }
214  break;
215 
216  case GncPricePropType::TO_CURRENCY:
217  m_to_currency.reset();
218  comm = parse_commodity_price_comm (value, GNC_COMMODITY_NS_CURRENCY); // Throws if parsing fails
219  if (comm)
220  {
221  if (m_from_commodity == comm)
222  throw std::invalid_argument (_("'Currency To' can not be the same as 'Commodity From'."));
223  if (gnc_commodity_is_currency (comm) != true)
224  throw std::invalid_argument (_("Value parsed into an invalid currency for a currency column type."));
225  m_to_currency = comm;
226  }
227  break;
228 
229  default:
230  /* Issue a warning for all other prop_types. */
231  PWARN ("%d is an invalid property for a Price", static_cast<int>(prop_type));
232  break;
233  }
234  }
235  catch (const std::invalid_argument& e)
236  {
237  auto err_str = (bl::format (std::string{_("{1}: {2}")}) %
238  std::string{_(gnc_price_col_type_strs[prop_type])} %
239  e.what()).str();
240  m_errors.emplace(prop_type, err_str);
241  throw std::invalid_argument (err_str);
242  }
243  catch (const std::out_of_range& e)
244  {
245  auto err_str = (bl::format (std::string{_("{1}: {2}")}) %
246  std::string{_(gnc_price_col_type_strs[prop_type])} %
247  e.what()).str();
248  m_errors.emplace(prop_type, err_str);
249  throw std::invalid_argument (err_str);
250  }
251 }
252 
253 void GncImportPrice::reset (GncPricePropType prop_type)
254 {
255  try
256  {
257  if ((prop_type == GncPricePropType::FROM_NAMESPACE) ||
258  (prop_type == GncPricePropType::FROM_SYMBOL))
259  set_from_commodity (nullptr);
260 
261  if (prop_type == GncPricePropType::TO_CURRENCY)
262  set_to_currency (nullptr);
263 
264  // set enable_test_empty to false to allow empty values
265  set (prop_type, std::string(), false);
266  }
267  catch (...)
268  {
269  // Set with an empty string will effectively clear the property
270  // but can also set an error for the property. Clear that error here.
271  m_errors.erase(prop_type);
272  }
273 }
274 
275 std::string GncImportPrice::verify_essentials (void)
276 {
277  /* Make sure this price has the minimum required set of properties defined */
278  if (!m_date)
279  return _("No date column.");
280  else if (!m_amount)
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'.");
286  else if (gnc_commodity_equal (*m_from_commodity, *m_to_currency))
287  return _("'Commodity From' can not be the same as 'Currency To'.");
288  else
289  return std::string();
290 }
291 
292 Result GncImportPrice::create_price (QofBook* book, GNCPriceDB *pdb, bool over)
293 {
294  /* Gently refuse to create the price if the basics are not set correctly
295  * This should have been tested before calling this function though!
296  */
297  auto check = verify_essentials();
298  if (!check.empty())
299  {
300  PWARN ("Refusing to create price because essentials not set properly: %s", check.c_str());
301  return FAILED;
302  }
303 
304  auto date = static_cast<time64>(GncDateTime(*m_date, DayPart::neutral));
305 
306  auto amount = *m_amount;
307  Result ret_val = ADDED;
308 
309  GNCPrice *old_price = gnc_pricedb_lookup_day_t64 (pdb, *m_from_commodity,
310  *m_to_currency, date);
311 
312  // Should old price be over written
313  if ((old_price != nullptr) && (over == true))
314  {
315  DEBUG("Over write");
316  gnc_pricedb_remove_price (pdb, old_price);
317  gnc_price_unref (old_price);
318  old_price = nullptr;
319  ret_val = REPLACED;
320  }
321 
322  char date_str [MAX_DATE_LENGTH + 1];
323  memset (date_str, 0, sizeof(date_str));
324  qof_print_date_buff (date_str, MAX_DATE_LENGTH, date);
325  DEBUG("Date is %s, Commodity from is '%s', Currency is '%s', "
326  "Amount is %s", date_str,
327  gnc_commodity_get_fullname (*m_from_commodity),
328  gnc_commodity_get_fullname (*m_to_currency),
329  amount.to_string().c_str());
330  // Create the new price
331  if (old_price == nullptr)
332  {
333  DEBUG("Create");
334  GNCPrice *price = gnc_price_create (book);
335  gnc_price_begin_edit (price);
336 
337  gnc_price_set_commodity (price, *m_from_commodity);
338  gnc_price_set_currency (price, *m_to_currency);
339 
340  int scu = gnc_commodity_get_fraction (*m_to_currency);
341  auto amount_conv = amount.convert<RoundType::half_up>(scu * COMMODITY_DENOM_MULT);
342 
343  gnc_price_set_value (price, static_cast<gnc_numeric>(amount_conv));
344 
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);
349 
350  bool perr = gnc_pricedb_add_price (pdb, price);
351 
352  gnc_price_unref (price);
353 
354  if (perr == false)
355  throw std::invalid_argument (_("Failed to create price from selected columns."));
356  }
357  else
358  {
359  gnc_price_unref (old_price);
360  ret_val = DUPLICATED;
361  }
362  return ret_val;
363 }
364 
365 static std::string gen_err_str (std::map<GncPricePropType, std::string>& errors)
366 {
367  auto full_error = std::string();
368  for (auto error : errors)
369  {
370  full_error += (full_error.empty() ? "" : "\n") + error.second;
371  }
372  return full_error;
373 }
374 
375 std::string GncImportPrice::errors ()
376 {
377  return gen_err_str (m_errors);
378 }
379 
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.
GnuCash DateTime class.
utility functions for the GnuCash UI
void gnc_price_unref(GNCPrice *p)
gnc_price_unref - indicate you&#39;re finished with a price (i.e.
#define DEBUG(format, args...)
Print a debugging message.
Definition: qoflog.h:264
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.
Definition: gnc-numeric.hpp:60
#define PWARN(format, args...)
Log a warning.
Definition: qoflog.h:250
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.
Definition: gnc-date.h:108
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...
Definition: gnc-date.h:87
size_t qof_print_date_buff(char *buff, size_t buflen, time64 secs)
Convenience: calls through to qof_print_date_dmy_buff().
Definition: gnc-date.cpp:573
static const std::vector< GncDateFormat > c_formats
A vector with all the date formats supported by the string constructor.
GnuCash Date class.