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 #include <ctre.hpp>
45 
46 namespace bl = boost::locale;
47 
48 G_GNUC_UNUSED static QofLogModule log_module = GNC_MOD_IMPORT;
49 
50 /* This map contains a set of strings representing the different column types. */
51 std::map<GncPricePropType, const char*> gnc_price_col_type_strs = {
52  { GncPricePropType::NONE, N_("None") },
53  { GncPricePropType::DATE, N_("Date") },
54  { GncPricePropType::AMOUNT, N_("Amount") },
55  { GncPricePropType::FROM_SYMBOL, N_("From Symbol") },
56  { GncPricePropType::FROM_NAMESPACE, N_("From Namespace") },
57  { GncPricePropType::TO_CURRENCY, N_("Currency To") },
58 };
59 
66 GncNumeric parse_amount_price (const std::string &str, int currency_format)
67 {
68  /* If a cell is empty or just spaces return invalid amount */
69  static constexpr ctll::fixed_string digit_re{"[0-9]"};
70  if(!ctre::search<digit_re>(str))
71  throw std::invalid_argument (_("Value doesn't appear to contain a valid number."));
72 
73  static const auto expr = boost::make_u32regex("[[:Sc:]]");
74  std::string str_no_symbols;
75  boost::u32regex_replace(icu::UnicodeString::fromUTF8(str), expr, "").toUTF8String(str_no_symbols);
76 
77  /* Convert based on user chosen currency format */
78  gnc_numeric val = gnc_numeric_zero();
79  char *endptr;
80  switch (currency_format)
81  {
82  case 0:
83  /* Currency locale */
84  if (!(xaccParseAmountImport (str_no_symbols.c_str(), TRUE, &val, &endptr, TRUE)))
85  throw std::invalid_argument (_("Value can't be parsed into a number using the selected currency format."));
86  break;
87  case 1:
88  /* Currency decimal period */
89  if (!(xaccParseAmountExtImport (str_no_symbols.c_str(), TRUE, '-', '.', ',', "$+", &val, &endptr)))
90  throw std::invalid_argument (_("Value can't be parsed into a number using the selected currency format."));
91  break;
92  case 2:
93  /* Currency decimal comma */
94  if (!(xaccParseAmountExtImport (str_no_symbols.c_str(), TRUE, '-', ',', '.', "$+", &val, &endptr)))
95  throw std::invalid_argument (_("Value can't be parsed into a number using the selected currency format."));
96  break;
97  }
98 
99  return GncNumeric(val);
100 }
101 
108 gnc_commodity* parse_commodity_price_comm (const std::string& symbol_str, const std::string& namespace_str)
109 {
110  if (symbol_str.empty())
111  return nullptr;
112 
113  auto table = gnc_commodity_table_get_table (gnc_get_current_book());
114  gnc_commodity* comm = nullptr;
115 
116  /* First try commodity as a unique name, used in loading settings, returns null if not found */
117  comm = gnc_commodity_table_lookup_unique (table, symbol_str.c_str());
118 
119  /* Now lookup with namespace and symbol */
120  if (!comm)
121  {
122  comm = gnc_commodity_table_lookup (table,
123  namespace_str.c_str(), symbol_str.c_str());
124  }
125 
126  if (!comm)
127  throw std::invalid_argument (_("Value can't be parsed into a valid commodity."));
128  else
129  return comm;
130 }
131 
137 bool parse_namespace (const std::string& namespace_str)
138 {
139  if (namespace_str.empty())
140  return false;
141 
142  auto table = gnc_commodity_table_get_table (gnc_get_current_book());
143 
144  if (gnc_commodity_table_has_namespace (table, namespace_str.c_str()))
145  return true;
146  else
147  throw std::invalid_argument (_("Value can't be parsed into a valid namespace."));
148 
149  return false;
150 }
151 
152 void GncImportPrice::set (GncPricePropType prop_type, const std::string& value, bool enable_test_empty)
153 {
154  try
155  {
156  // Drop any existing error for the prop_type we're about to set
157  m_errors.erase(prop_type);
158 
159  // conditional test for empty values
160  if (value.empty() && enable_test_empty)
161  throw std::invalid_argument (_("Column value can not be empty."));
162 
163  gnc_commodity *comm = nullptr;
164  switch (prop_type)
165  {
166  case GncPricePropType::DATE:
167  m_date.reset();
168  m_date = GncDate(value, GncDate::c_formats[m_date_format].m_fmt); // Throws if parsing fails
169  break;
170 
171  case GncPricePropType::AMOUNT:
172  m_amount.reset();
173  m_amount = parse_amount_price (value, m_currency_format); // Throws if parsing fails
174  break;
175 
176  case GncPricePropType::FROM_SYMBOL:
177  m_from_symbol.reset();
178 
179  if (value.empty())
180  throw std::invalid_argument (_("'From Symbol' can not be empty."));
181  else
182  m_from_symbol = value;
183 
184  if (m_from_namespace)
185  {
186  comm = parse_commodity_price_comm (value, *m_from_namespace); // Throws if parsing fails
187  if (comm)
188  {
189  if (m_to_currency == comm)
190  throw std::invalid_argument (_("'Commodity From' can not be the same as 'Currency To'."));
191  m_from_commodity = comm;
192  }
193  }
194  break;
195 
196  case GncPricePropType::FROM_NAMESPACE:
197  m_from_namespace.reset();
198 
199  if (value.empty())
200  throw std::invalid_argument (_("'From Namespace' can not be empty."));
201 
202  if (parse_namespace (value)) // Throws if parsing fails
203  {
204  m_from_namespace = value;
205 
206  if (m_from_symbol)
207  {
208  comm = parse_commodity_price_comm (*m_from_symbol, *m_from_namespace); // Throws if parsing fails
209  if (comm)
210  {
211  if (m_to_currency == comm)
212  throw std::invalid_argument (_("'Commodity From' can not be the same as 'Currency To'."));
213  m_from_commodity = comm;
214  }
215  }
216  }
217  break;
218 
219  case GncPricePropType::TO_CURRENCY:
220  m_to_currency.reset();
221  comm = parse_commodity_price_comm (value, GNC_COMMODITY_NS_CURRENCY); // Throws if parsing fails
222  if (comm)
223  {
224  if (m_from_commodity == comm)
225  throw std::invalid_argument (_("'Currency To' can not be the same as 'Commodity From'."));
226  if (gnc_commodity_is_currency (comm) != true)
227  throw std::invalid_argument (_("Value parsed into an invalid currency for a currency column type."));
228  m_to_currency = comm;
229  }
230  break;
231 
232  default:
233  /* Issue a warning for all other prop_types. */
234  PWARN ("%d is an invalid property for a Price", static_cast<int>(prop_type));
235  break;
236  }
237  }
238  catch (const std::invalid_argument& e)
239  {
240  auto err_str = (bl::format (std::string{_("{1}: {2}")}) %
241  std::string{_(gnc_price_col_type_strs[prop_type])} %
242  e.what()).str();
243  m_errors.emplace(prop_type, err_str);
244  throw std::invalid_argument (err_str);
245  }
246  catch (const std::out_of_range& e)
247  {
248  auto err_str = (bl::format (std::string{_("{1}: {2}")}) %
249  std::string{_(gnc_price_col_type_strs[prop_type])} %
250  e.what()).str();
251  m_errors.emplace(prop_type, err_str);
252  throw std::invalid_argument (err_str);
253  }
254 }
255 
256 void GncImportPrice::reset (GncPricePropType prop_type)
257 {
258  try
259  {
260  if ((prop_type == GncPricePropType::FROM_NAMESPACE) ||
261  (prop_type == GncPricePropType::FROM_SYMBOL))
262  set_from_commodity (nullptr);
263 
264  if (prop_type == GncPricePropType::TO_CURRENCY)
265  set_to_currency (nullptr);
266 
267  // set enable_test_empty to false to allow empty values
268  set (prop_type, std::string(), false);
269  }
270  catch (...)
271  {
272  // Set with an empty string will effectively clear the property
273  // but can also set an error for the property. Clear that error here.
274  m_errors.erase(prop_type);
275  }
276 }
277 
278 std::string GncImportPrice::verify_essentials (void)
279 {
280  /* Make sure this price has the minimum required set of properties defined */
281  if (!m_date)
282  return _("No date column.");
283  else if (!m_amount)
284  return _("No amount column.");
285  else if (!m_to_currency)
286  return _("No 'Currency to'.");
287  else if (!m_from_commodity)
288  return _("No 'Commodity from'.");
289  else if (gnc_commodity_equal (*m_from_commodity, *m_to_currency))
290  return _("'Commodity From' can not be the same as 'Currency To'.");
291  else
292  return std::string();
293 }
294 
295 Result GncImportPrice::create_price (QofBook* book, GNCPriceDB *pdb, bool over)
296 {
297  /* Gently refuse to create the price if the basics are not set correctly
298  * This should have been tested before calling this function though!
299  */
300  auto check = verify_essentials();
301  if (!check.empty())
302  {
303  PWARN ("Refusing to create price because essentials not set properly: %s", check.c_str());
304  return FAILED;
305  }
306 
307  auto date = static_cast<time64>(GncDateTime(*m_date, DayPart::neutral));
308 
309  auto amount = *m_amount;
310  Result ret_val = ADDED;
311 
312  GNCPrice *old_price = gnc_pricedb_lookup_day_t64 (pdb, *m_from_commodity,
313  *m_to_currency, date);
314 
315  // Should old price be over written
316  if ((old_price != nullptr) && (over == true))
317  {
318  DEBUG("Over write");
319  gnc_pricedb_remove_price (pdb, old_price);
320  gnc_price_unref (old_price);
321  old_price = nullptr;
322  ret_val = REPLACED;
323  }
324 
325  char date_str [MAX_DATE_LENGTH + 1];
326  memset (date_str, 0, sizeof(date_str));
327  qof_print_date_buff (date_str, MAX_DATE_LENGTH, date);
328  DEBUG("Date is %s, Commodity from is '%s', Currency is '%s', "
329  "Amount is %s", date_str,
330  gnc_commodity_get_fullname (*m_from_commodity),
331  gnc_commodity_get_fullname (*m_to_currency),
332  amount.to_string().c_str());
333  // Create the new price
334  if (old_price == nullptr)
335  {
336  DEBUG("Create");
337  GNCPrice *price = gnc_price_create (book);
338  gnc_price_begin_edit (price);
339 
340  gnc_price_set_commodity (price, *m_from_commodity);
341  gnc_price_set_currency (price, *m_to_currency);
342 
343  int scu = gnc_commodity_get_fraction (*m_to_currency);
344  auto amount_conv = amount.convert<RoundType::half_up>(scu * COMMODITY_DENOM_MULT);
345 
346  gnc_price_set_value (price, static_cast<gnc_numeric>(amount_conv));
347 
348  gnc_price_set_time64 (price, date);
349  gnc_price_set_source (price, PRICE_SOURCE_USER_PRICE);
350  gnc_price_set_typestr (price, PRICE_TYPE_LAST);
351  gnc_price_commit_edit (price);
352 
353  bool perr = gnc_pricedb_add_price (pdb, price);
354 
355  gnc_price_unref (price);
356 
357  if (perr == false)
358  throw std::invalid_argument (_("Failed to create price from selected columns."));
359  }
360  else
361  {
362  gnc_price_unref (old_price);
363  ret_val = DUPLICATED;
364  }
365  return ret_val;
366 }
367 
368 static std::string gen_err_str (std::map<GncPricePropType, std::string>& errors)
369 {
370  auto full_error = std::string();
371  for (auto error : errors)
372  {
373  full_error += (full_error.empty() ? "" : "\n") + error.second;
374  }
375  return full_error;
376 }
377 
378 std::string GncImportPrice::errors ()
379 {
380  return gen_err_str (m_errors);
381 }
382 
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:574
static const std::vector< GncDateFormat > c_formats
A vector with all the date formats supported by the string constructor.
GnuCash Date class.