GnuCash c935c2f+
Loading...
Searching...
No Matches
Public Member Functions | Friends
GncPreTrans Class Reference

Public Member Functions

 GncPreTrans (int date_format, bool multi_split)
 
void set (GncTransPropType prop_type, const std::string &value)
 
void set_date_format (int date_format)
 
void set_multi_split (bool multi_split)
 
void reset (GncTransPropType prop_type)
 
StrVec verify_essentials (void)
 
std::shared_ptr< DraftTransactioncreate_trans (QofBook *book, gnc_commodity *currency)
 
bool is_part_of (std::shared_ptr< GncPreTrans > parent)
 Check whether the harvested transaction properties for this instance match those of another one (the "parent").
 
std::optional< std::string > get_void_reason ()
 
ErrMap errors ()
 
void reset_cross_split_counters ()
 
bool is_multi_currency ()
 

Friends

class GncPreSplit
 

Detailed Description

Definition at line 154 of file gnc-imp-props-tx.hpp.

Constructor & Destructor Documentation

◆ GncPreTrans()

GncPreTrans::GncPreTrans ( int  date_format,
bool  multi_split 
)
inline

Definition at line 157 of file gnc-imp-props-tx.hpp.

158 : m_date_format{date_format}, m_multi_split{multi_split}, m_currency{nullptr} {};

Member Function Documentation

◆ create_trans()

std::shared_ptr< DraftTransaction > GncPreTrans::create_trans ( QofBook book,
gnc_commodity *  currency 
)

Definition at line 323 of file gnc-imp-props-tx.cpp.

324{
325 if (created)
326 return nullptr;
327
328 /* Gently refuse to create the transaction if the basics are not set correctly
329 * This should have been tested before calling this function though!
330 */
331 auto check = verify_essentials();
332 if (!check.empty())
333 {
334 auto err_msg = std::string("Not creating transaction because essentials not set properly:");
335 auto add_bullet_item = [](std::string& a, std::string& b)->std::string { return std::move(a) + "\n• " + b; };
336 err_msg = std::accumulate (check.begin(), check.end(), std::move (err_msg), add_bullet_item);
337 PWARN ("%s", err_msg.c_str());
338 return nullptr;
339 }
340
341 auto trans = xaccMallocTransaction (book);
342 xaccTransBeginEdit (trans);
343
344 if (gnc_commodity_is_currency(m_currency))
345 xaccTransSetCurrency (trans, m_currency);
346 else
347 xaccTransSetCurrency (trans, currency);
349 static_cast<time64>(GncDateTime(*m_date, DayPart::neutral)));
350
351 if (m_num)
352 xaccTransSetNum (trans, m_num->c_str());
353
354 if (m_desc)
355 xaccTransSetDescription (trans, m_desc->c_str());
356
357 if (m_notes)
358 xaccTransSetNotes (trans, m_notes->c_str());
359
360 created = true;
361 return std::make_shared<DraftTransaction>(trans);
362}
GnuCash DateTime class.
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.
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
Definition gnc-date.h:87
void xaccTransSetDescription(Transaction *trans, const char *desc)
Sets the transaction Description.
void xaccTransSetNum(Transaction *trans, const char *xnum)
Sets the transaction Number (or ID) field; rather than use this function directly,...
Transaction * xaccMallocTransaction(QofBook *book)
The xaccMallocTransaction() will malloc memory and initialize it.
void xaccTransSetDatePostedSecsNormalized(Transaction *trans, time64 time)
This function sets the posted date of the transaction, specified by a time64 (see ctime(3)).
void xaccTransSetCurrency(Transaction *trans, gnc_commodity *curr)
Set a new currency on a transaction.
void xaccTransBeginEdit(Transaction *trans)
The xaccTransBeginEdit() method must be called before any changes are made to a transaction or any of...
void xaccTransSetNotes(Transaction *trans, const char *notes)
Sets the transaction Notes.
#define PWARN(format, args...)
Log a warning.
Definition qoflog.h:250

◆ errors()

ErrMap GncPreTrans::errors ( )

Definition at line 379 of file gnc-imp-props-tx.cpp.

380{
381 return m_errors;
382}

◆ get_void_reason()

std::optional< std::string > GncPreTrans::get_void_reason ( )
inline

Definition at line 182 of file gnc-imp-props-tx.hpp.

182{ return m_void_reason; }

◆ is_multi_currency()

bool GncPreTrans::is_multi_currency ( )

Definition at line 391 of file gnc-imp-props-tx.cpp.

392{
393 auto num_comm = m_acct_commodities.size() + m_alt_currencies.size();
394 if (m_currency && (std::find (m_alt_currencies.cbegin(),m_alt_currencies.cend(), m_currency) == m_alt_currencies.cend()))
395 num_comm++;
396 return (num_comm > 1);
397}

◆ is_part_of()

bool GncPreTrans::is_part_of ( std::shared_ptr< GncPreTrans parent)

Check whether the harvested transaction properties for this instance match those of another one (the "parent").

Note this function is not symmetrical. This instance can have empty properties and still be considered part of the parent if the other properties match the parent's. A fully empty instance will equally be considered part of the parent.

This function is intended to discover multi-split transaction lines in an import file where the first line defines the transaction (with a first split) and subsequent lines add splits. These subsequent lines can either have all transaction related columns be empty or the same as the first line.

Parameters
parentthe parent transaction property object to test against
Returns
true if this object is considered to be part of the parent, false otherwise.

Definition at line 364 of file gnc-imp-props-tx.cpp.

365{
366 if (!parent)
367 return false;
368
369 return (!m_differ || m_differ == parent->m_differ) &&
370 (!m_date || m_date == parent->m_date) &&
371 (!m_num || m_num == parent->m_num) &&
372 (!m_desc || m_desc == parent->m_desc) &&
373 (!m_notes || m_notes == parent->m_notes) &&
374 (!m_currency || m_currency == parent->m_currency) &&
375 (!m_void_reason || m_void_reason == parent->m_void_reason) &&
376 parent->m_errors.empty(); // A GncPreTrans with errors can never be a parent
377}

◆ reset()

void GncPreTrans::reset ( GncTransPropType  prop_type)

Definition at line 302 of file gnc-imp-props-tx.cpp.

303{
304 set (prop_type, std::string());
305 // Set with an empty string will effectively clear the property
306 // but can also set an error for the property. Clear that error here.
307 m_errors.erase(prop_type);
308}

◆ reset_cross_split_counters()

void GncPreTrans::reset_cross_split_counters ( )

Definition at line 384 of file gnc-imp-props-tx.cpp.

385{
386 m_alt_currencies.clear();
387 m_acct_commodities.clear();
388}

◆ set()

void GncPreTrans::set ( GncTransPropType  prop_type,
const std::string &  value 
)

Definition at line 228 of file gnc-imp-props-tx.cpp.

229{
230 try
231 {
232 // Drop any existing error for the prop_type we're about to set
233 m_errors.erase(prop_type);
234
235 switch (prop_type)
236 {
237 case GncTransPropType::UNIQUE_ID:
238 m_differ.reset();
239 if (!value.empty())
240 m_differ = value;
241 break;
242
243 case GncTransPropType::DATE:
244 m_date.reset();
245 if (!value.empty())
246 m_date = GncDate(value, GncDate::c_formats[m_date_format].m_fmt); // Throws if parsing fails
247 else if (!m_multi_split)
248 throw std::invalid_argument (
249 (bl::format (std::string{_("Date field can not be empty if 'Multi-split' option is unset.\n")}) %
250 std::string{_(gnc_csv_col_type_strs[prop_type])}).str());
251 break;
252
253 case GncTransPropType::NUM:
254 m_num.reset();
255 if (!value.empty())
256 m_num = value;
257 break;
258
259 case GncTransPropType::DESCRIPTION:
260 m_desc.reset();
261 if (!value.empty())
262 m_desc = value;
263 else if (!m_multi_split)
264 throw std::invalid_argument (
265 (bl::format (std::string{_("Description field can not be empty if 'Multi-split' option is unset.\n")}) %
266 std::string{_(gnc_csv_col_type_strs[prop_type])}).str());
267 break;
268
269 case GncTransPropType::NOTES:
270 m_notes.reset();
271 if (!value.empty())
272 m_notes = value;
273 break;
274
275 case GncTransPropType::COMMODITY:
276 m_currency = nullptr;
277 m_currency = parse_commodity (value);
278 break;
279
280 case GncTransPropType::VOID_REASON:
281 m_void_reason.reset();
282 if (!value.empty())
283 m_void_reason = value;
284 break;
285
286 default:
287 /* Issue a warning for all other prop_types. */
288 PWARN ("%d is an invalid property for a transaction", static_cast<int>(prop_type));
289 break;
290 }
291 }
292 catch (const std::exception& e)
293 {
294 auto err_str = (bl::format (std::string{_("{1}: {2}")}) %
295 std::string{_(gnc_csv_col_type_strs[prop_type])} %
296 e.what()).str();
297 m_errors.emplace(prop_type, err_str);
298 }
299
300}
GnuCash Date class.
static const std::vector< GncDateFormat > c_formats
A vector with all the date formats supported by the string constructor.

◆ set_date_format()

void GncPreTrans::set_date_format ( int  date_format)
inline

Definition at line 161 of file gnc-imp-props-tx.hpp.

161{ m_date_format = date_format ;}

◆ set_multi_split()

void GncPreTrans::set_multi_split ( bool  multi_split)
inline

Definition at line 162 of file gnc-imp-props-tx.hpp.

162{ m_multi_split = multi_split ;}

◆ verify_essentials()

StrVec GncPreTrans::verify_essentials ( void  )

Definition at line 310 of file gnc-imp-props-tx.cpp.

311{
312 auto errors = StrVec();
313
314 if (!m_date)
315 errors.emplace_back(_("No valid date."));
316
317 if (!m_desc)
318 errors.emplace_back(_("No valid description."));
319
320 return errors;
321}

Friends And Related Symbol Documentation

◆ GncPreSplit

friend class GncPreSplit
friend

Definition at line 218 of file gnc-imp-props-tx.hpp.


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