GnuCash c935c2f+
Loading...
Searching...
No Matches
Public Member Functions
GncImportPrice Struct Reference

Public Member Functions

 GncImportPrice (int date_format, int currency_format)
 
void set (GncPricePropType prop_type, const std::string &value, bool enable_test_empty)
 
void set_date_format (int date_format)
 
void set_currency_format (int currency_format)
 
void reset (GncPricePropType prop_type)
 
std::string verify_essentials (void)
 
Result create_price (QofBook *book, GNCPriceDB *pdb, bool over)
 
gnc_commodity * get_from_commodity ()
 
void set_from_commodity (gnc_commodity *comm)
 
gnc_commodity * get_to_currency ()
 
void set_to_currency (gnc_commodity *curr)
 
std::string errors ()
 

Detailed Description

Definition at line 83 of file gnc-imp-props-price.hpp.

Constructor & Destructor Documentation

◆ GncImportPrice()

GncImportPrice::GncImportPrice ( int  date_format,
int  currency_format 
)
inline

Definition at line 86 of file gnc-imp-props-price.hpp.

86 : m_date_format{date_format},
87 m_currency_format{currency_format}{};

Member Function Documentation

◆ create_price()

Result GncImportPrice::create_price ( QofBook book,
GNCPriceDB *  pdb,
bool  over 
)

Definition at line 295 of file gnc-imp-props-price.cpp.

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}
GnuCash DateTime class.
const char * gnc_commodity_get_fullname(const gnc_commodity *cm)
Retrieve the full name for the specified commodity.
int gnc_commodity_get_fraction(const gnc_commodity *cm)
Retrieve the fraction for the specified commodity.
#define MAX_DATE_LENGTH
The maximum length of a string created by the date printers.
Definition gnc-date.h:108
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, const size_t len, time64 t)
Convenience: calls through to qof_print_date_dmy_buff().
Definition gnc-date.cpp:574
#define DEBUG(format, args...)
Print a debugging message.
Definition qoflog.h:264
#define PWARN(format, args...)
Log a warning.
Definition qoflog.h:250
gboolean gnc_pricedb_remove_price(GNCPriceDB *db, GNCPrice *p)
Remove a price from the pricedb and unref the price.
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.
gboolean gnc_pricedb_add_price(GNCPriceDB *db, GNCPrice *p)
Add a price to the pricedb.
GNCPrice * gnc_price_create(QofBook *book)
gnc_price_create - returns a newly allocated and initialized price with a reference count of 1.
void gnc_price_unref(GNCPrice *p)
gnc_price_unref - indicate you're finished with a price (i.e.

◆ errors()

std::string GncImportPrice::errors ( )

Definition at line 378 of file gnc-imp-props-price.cpp.

379{
380 return gen_err_str (m_errors);
381}

◆ get_from_commodity()

gnc_commodity * GncImportPrice::get_from_commodity ( )
inline

Definition at line 96 of file gnc-imp-props-price.hpp.

96{ if (m_from_commodity) return *m_from_commodity; else return nullptr; }

◆ get_to_currency()

gnc_commodity * GncImportPrice::get_to_currency ( )
inline

Definition at line 99 of file gnc-imp-props-price.hpp.

99{ if (m_to_currency) return *m_to_currency; else return nullptr; }

◆ reset()

void GncImportPrice::reset ( GncPricePropType  prop_type)

Definition at line 256 of file gnc-imp-props-price.cpp.

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}

◆ set()

void GncImportPrice::set ( GncPricePropType  prop_type,
const std::string &  value,
bool  enable_test_empty 
)

Definition at line 152 of file gnc-imp-props-price.cpp.

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}
GnuCash Date class.
static const std::vector< GncDateFormat > c_formats
A vector with all the date formats supported by the string constructor.
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.

◆ set_currency_format()

void GncImportPrice::set_currency_format ( int  currency_format)
inline

Definition at line 91 of file gnc-imp-props-price.hpp.

91{ m_currency_format = currency_format ;}

◆ set_date_format()

void GncImportPrice::set_date_format ( int  date_format)
inline

Definition at line 90 of file gnc-imp-props-price.hpp.

90{ m_date_format = date_format ;}

◆ set_from_commodity()

void GncImportPrice::set_from_commodity ( gnc_commodity *  comm)
inline

Definition at line 97 of file gnc-imp-props-price.hpp.

97{ if (comm) m_from_commodity = comm; else m_from_commodity.reset(); }

◆ set_to_currency()

void GncImportPrice::set_to_currency ( gnc_commodity *  curr)
inline

Definition at line 100 of file gnc-imp-props-price.hpp.

100{ if (curr) m_to_currency = curr; else m_to_currency.reset(); }

◆ verify_essentials()

std::string GncImportPrice::verify_essentials ( void  )

Definition at line 278 of file gnc-imp-props-price.cpp.

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}
gboolean gnc_commodity_equal(const gnc_commodity *a, const gnc_commodity *b)
This routine returns TRUE if the two commodities are equal.

The documentation for this struct was generated from the following files: