24 #include <glib/gi18n.h> 31 #include "engine-helpers.h" 36 #include <gnc-exp-parser.h> 45 #include <boost/locale.hpp> 46 #include <boost/regex.hpp> 47 #include <boost/regex/icu.hpp> 48 #include <gnc-locale-utils.hpp> 49 #include "gnc-imp-props-tx.hpp" 51 namespace bl = boost::locale;
53 G_GNUC_UNUSED
static QofLogModule log_module = GNC_MOD_IMPORT;
56 std::map<GncTransPropType, const char*> gnc_csv_col_type_strs = {
57 { GncTransPropType::NONE, N_(
"None") },
58 { GncTransPropType::UNIQUE_ID, N_(
"Transaction ID") },
59 { GncTransPropType::DATE, N_(
"Date") },
60 { GncTransPropType::NUM, N_(
"Number") },
61 { GncTransPropType::DESCRIPTION, N_(
"Description") },
62 { GncTransPropType::NOTES, N_(
"Notes") },
63 { GncTransPropType::COMMODITY, N_(
"Transaction Commodity") },
64 { GncTransPropType::VOID_REASON, N_(
"Void Reason") },
65 { GncTransPropType::ACTION, N_(
"Action") },
66 { GncTransPropType::ACCOUNT, N_(
"Account") },
67 { GncTransPropType::AMOUNT, N_(
"Amount") },
68 { GncTransPropType::AMOUNT_NEG, N_(
"Amount (Negated)") },
69 { GncTransPropType::VALUE, N_(
"Value") },
70 { GncTransPropType::VALUE_NEG, N_(
"Value (Negated)") },
71 { GncTransPropType::PRICE, N_(
"Price") },
72 { GncTransPropType::MEMO, N_(
"Memo") },
73 { GncTransPropType::REC_STATE, N_(
"Reconciled") },
74 { GncTransPropType::REC_DATE, N_(
"Reconcile Date") },
75 { GncTransPropType::TACTION, N_(
"Transfer Action") },
76 { GncTransPropType::TACCOUNT, N_(
"Transfer Account") },
77 { GncTransPropType::TAMOUNT, N_(
"Transfer Amount") },
78 { GncTransPropType::TAMOUNT_NEG, N_(
"Transfer Amount (Negated)") },
79 { GncTransPropType::TMEMO, N_(
"Transfer Memo") },
80 { GncTransPropType::TREC_STATE, N_(
"Transfer Reconciled") },
81 { GncTransPropType::TREC_DATE, N_(
"Transfer Reconcile Date") }
88 std::vector<GncTransPropType> twosplit_blacklist = {
89 GncTransPropType::UNIQUE_ID };
90 std::vector<GncTransPropType> multisplit_blacklist = {
91 GncTransPropType::TACTION,
92 GncTransPropType::TACCOUNT,
93 GncTransPropType::TAMOUNT,
94 GncTransPropType::TAMOUNT_NEG,
95 GncTransPropType::TMEMO,
96 GncTransPropType::TREC_STATE,
97 GncTransPropType::TREC_DATE
100 std::vector<GncTransPropType> multi_col_props = {
101 GncTransPropType::AMOUNT,
102 GncTransPropType::AMOUNT_NEG,
103 GncTransPropType::TAMOUNT,
104 GncTransPropType::TAMOUNT_NEG,
105 GncTransPropType::VALUE,
106 GncTransPropType::VALUE_NEG
109 bool is_multi_col_prop (GncTransPropType prop)
111 return (std::find (multi_col_props.cbegin(),
112 multi_col_props.cend(), prop) != multi_col_props.cend());
115 GncTransPropType sanitize_trans_prop (GncTransPropType prop,
bool multi_split)
117 auto bl = multi_split ? multisplit_blacklist : twosplit_blacklist;
118 if (std::find(bl.begin(), bl.end(), prop) == bl.end())
121 return GncTransPropType::NONE;
131 GncNumeric parse_monetary (
const std::string &str,
int currency_format)
138 if(!boost::regex_search(str, boost::regex(
"[0-9]")))
139 throw std::invalid_argument (_(
"Value doesn't appear to contain a valid number."));
141 auto expr = boost::make_u32regex(
"[[:Sc:][:blank:]]|--");
142 std::string str_no_symbols;
143 boost::u32regex_replace(icu::UnicodeString::fromUTF8(str), expr,
"").toUTF8String(str_no_symbols);
146 gnc_numeric val = gnc_numeric_zero();
148 switch (currency_format)
153 throw std::invalid_argument (_(
"Value can't be parsed into a number using the selected currency format."));
158 throw std::invalid_argument (_(
"Value can't be parsed into a number using the selected currency format."));
163 throw std::invalid_argument (_(
"Value can't be parsed into a number using the selected currency format."));
170 static char parse_reconciled (
const std::string& reconcile)
172 if (g_strcmp0 (reconcile.c_str(), gnc_get_reconcile_str(
NREC)) == 0)
174 else if (g_strcmp0 (reconcile.c_str(), gnc_get_reconcile_str(
CREC)) == 0)
176 else if (g_strcmp0 (reconcile.c_str(), gnc_get_reconcile_str(
YREC)) == 0)
178 else if (g_strcmp0 (reconcile.c_str(), gnc_get_reconcile_str(
FREC)) == 0)
180 else if (g_strcmp0 (reconcile.c_str(), gnc_get_reconcile_str(
VREC)) == 0)
183 throw std::invalid_argument (_(
"Value can't be parsed into a valid reconcile state."));
186 gnc_commodity* parse_commodity (
const std::string& comm_str)
188 if (comm_str.empty())
192 gnc_commodity* comm =
nullptr;
195 comm = gnc_commodity_table_lookup_unique (
table, comm_str.c_str());
199 comm = gnc_commodity_table_lookup (
table,
200 GNC_COMMODITY_NS_CURRENCY, comm_str.c_str());
206 for (
auto ns = namespaces; ns; ns = ns->next)
208 gchar* ns_str = (gchar*)ns->data;
209 if (g_utf8_collate(ns_str, GNC_COMMODITY_NS_CURRENCY) == 0)
212 comm = gnc_commodity_table_lookup (
table,
213 ns_str, comm_str.c_str());
217 g_list_free (namespaces);
221 throw std::invalid_argument (_(
"Value can't be parsed into a valid commodity."));
226 void GncPreTrans::set (GncTransPropType prop_type,
const std::string& value)
231 m_errors.erase(prop_type);
235 case GncTransPropType::UNIQUE_ID:
241 case GncTransPropType::DATE:
245 else if (!m_multi_split)
246 throw std::invalid_argument (
247 (bl::format (std::string{_(
"Date field can not be empty if 'Multi-split' option is unset.\n")}) %
248 std::string{_(gnc_csv_col_type_strs[prop_type])}).str());
251 case GncTransPropType::NUM:
257 case GncTransPropType::DESCRIPTION:
261 else if (!m_multi_split)
262 throw std::invalid_argument (
263 (bl::format (std::string{_(
"Description field can not be empty if 'Multi-split' option is unset.\n")}) %
264 std::string{_(gnc_csv_col_type_strs[prop_type])}).str());
267 case GncTransPropType::NOTES:
273 case GncTransPropType::COMMODITY:
274 m_currency =
nullptr;
275 m_currency = parse_commodity (value);
278 case GncTransPropType::VOID_REASON:
279 m_void_reason.reset();
281 m_void_reason = value;
286 PWARN (
"%d is an invalid property for a transaction", static_cast<int>(prop_type));
290 catch (
const std::exception& e)
292 auto err_str = (bl::format (std::string{_(
"{1}: {2}")}) %
293 std::string{_(gnc_csv_col_type_strs[prop_type])} %
295 m_errors.emplace(prop_type, err_str);
300 void GncPreTrans::reset (GncTransPropType prop_type)
302 set (prop_type, std::string());
305 m_errors.erase(prop_type);
308 StrVec GncPreTrans::verify_essentials (
void)
310 auto errors = StrVec();
313 errors.emplace_back(_(
"No valid date."));
316 errors.emplace_back(_(
"No valid description."));
321 std::shared_ptr<DraftTransaction> GncPreTrans::create_trans (QofBook* book, gnc_commodity* currency)
329 auto check = verify_essentials();
332 auto err_msg = std::string(
"Not creating transaction because essentials not set properly:");
333 auto add_bullet_item = [](std::string& a, std::string& b)->std::string {
return std::move(a) +
"\n• " + b; };
334 err_msg = std::accumulate (check.begin(), check.end(), std::move (err_msg), add_bullet_item);
335 PWARN (
"%s", err_msg.c_str());
347 static_cast<time64>(
GncDateTime(*m_date, DayPart::neutral)));
359 return std::make_shared<DraftTransaction>(trans);
367 return (!m_differ || m_differ == parent->m_differ) &&
368 (!m_date || m_date == parent->m_date) &&
369 (!m_num || m_num == parent->m_num) &&
370 (!m_desc || m_desc == parent->m_desc) &&
371 (!m_notes || m_notes == parent->m_notes) &&
372 (!m_currency || m_currency == parent->m_currency) &&
373 (!m_void_reason || m_void_reason == parent->m_void_reason) &&
374 parent->m_errors.empty();
377 ErrMap GncPreTrans::errors ()
382 void GncPreTrans::reset_cross_split_counters()
384 m_alt_currencies.clear();
385 m_acct_commodities.clear();
389 bool GncPreTrans::is_multi_currency()
391 auto num_comm = m_acct_commodities.size() + m_alt_currencies.size();
392 if (m_currency && (std::find (m_alt_currencies.cbegin(),m_alt_currencies.cend(), m_currency) == m_alt_currencies.cend()))
394 return (num_comm > 1);
398 void GncPreSplit::UpdateCrossSplitCounters ()
400 if (m_account && *m_account)
402 auto acct = *m_account;
404 auto alt_currs = m_pre_trans->m_alt_currencies;
405 auto acct_comms = m_pre_trans->m_acct_commodities;
406 auto curr =
static_cast<gnc_commodity*
> (
nullptr);
415 auto has_curr = [curr] (
const gnc_commodity *vec_curr) {
return gnc_commodity_equiv (curr, vec_curr); };
416 if (curr && std::none_of (alt_currs.cbegin(), alt_currs.cend(), has_curr))
417 m_pre_trans->m_alt_currencies.push_back(curr);
418 auto has_comm = [comm] (
const gnc_commodity *vec_comm) {
return gnc_commodity_equiv (comm, vec_comm); };
419 if (comm && std::none_of (acct_comms.cbegin(), acct_comms.cend(), has_comm))
420 m_pre_trans->m_acct_commodities.push_back(comm);
424 void GncPreSplit::set (GncTransPropType prop_type,
const std::string& value)
429 m_errors.erase(prop_type);
434 case GncTransPropType::ACTION:
440 case GncTransPropType::TACTION:
446 case GncTransPropType::ACCOUNT:
449 throw std::invalid_argument (_(
"Account value can't be empty."));
450 if ((acct = gnc_account_imap_find_any (gnc_get_current_book(), IMAP_CAT_CSV, value.c_str())) ||
454 throw std::invalid_argument (_(
"Account value can't be mapped back to an account."));
457 case GncTransPropType::TACCOUNT:
460 throw std::invalid_argument (_(
"Transfer account value can't be empty."));
462 if ((acct = gnc_account_imap_find_any (gnc_get_current_book(), IMAP_CAT_CSV,value.c_str())) ||
466 throw std::invalid_argument (_(
"Transfer account value can't be mapped back to an account."));
469 case GncTransPropType::MEMO:
475 case GncTransPropType::TMEMO:
481 case GncTransPropType::AMOUNT:
483 m_amount = parse_monetary (value, m_currency_format);
486 case GncTransPropType::AMOUNT_NEG:
487 m_amount_neg.reset();
488 m_amount_neg = parse_monetary (value, m_currency_format);
491 case GncTransPropType::VALUE:
493 m_value = parse_monetary (value, m_currency_format);
496 case GncTransPropType::VALUE_NEG:
498 m_value_neg = parse_monetary (value, m_currency_format);
501 case GncTransPropType::TAMOUNT:
503 m_tamount = parse_monetary (value, m_currency_format);
506 case GncTransPropType::TAMOUNT_NEG:
507 m_tamount_neg.reset();
508 m_tamount_neg = parse_monetary (value, m_currency_format);
511 case GncTransPropType::PRICE:
516 m_price = parse_monetary (value, m_currency_format);
519 case GncTransPropType::REC_STATE:
521 m_rec_state = parse_reconciled (value);
524 case GncTransPropType::TREC_STATE:
525 m_trec_state.reset();
526 m_trec_state = parse_reconciled (value);
529 case GncTransPropType::REC_DATE:
536 case GncTransPropType::TREC_DATE:
545 PWARN (
"%d is an invalid property for a split", static_cast<int>(prop_type));
549 catch (
const std::exception& e)
551 auto err_str = (bl::format (std::string{_(
"{1}: {2}")}) %
552 std::string{_(gnc_csv_col_type_strs[prop_type])} %
554 m_errors.emplace(prop_type, err_str);
558 if (prop_type == GncTransPropType::ACCOUNT)
559 UpdateCrossSplitCounters();
562 void GncPreSplit::reset (GncTransPropType prop_type)
564 set (prop_type, std::string());
567 m_errors.erase(prop_type);
570 void GncPreSplit::add (GncTransPropType prop_type,
const std::string& value)
575 if (m_errors.find(prop_type) != m_errors.cend())
581 case GncTransPropType::AMOUNT:
582 num_val = parse_monetary (value, m_currency_format);
584 num_val += *m_amount;
588 case GncTransPropType::AMOUNT_NEG:
589 num_val = parse_monetary (value, m_currency_format);
591 num_val += *m_amount_neg;
592 m_amount_neg = num_val;
595 case GncTransPropType::VALUE:
596 num_val = parse_monetary (value, m_currency_format);
602 case GncTransPropType::VALUE_NEG:
603 num_val = parse_monetary (value, m_currency_format);
605 num_val += *m_value_neg;
606 m_value_neg = num_val;
609 case GncTransPropType::TAMOUNT:
610 num_val = parse_monetary (value, m_currency_format);
612 num_val += *m_tamount;
616 case GncTransPropType::TAMOUNT_NEG:
617 num_val = parse_monetary (value, m_currency_format);
619 num_val += *m_tamount_neg;
620 m_tamount_neg = num_val;
625 PWARN (
"%d can't be used to add values in a split", static_cast<int>(prop_type));
629 catch (
const std::exception& e)
631 auto err_str = (bl::format (std::string{_(
"{1}: {2}")}) %
632 std::string{_(gnc_csv_col_type_strs[prop_type])} %
634 m_errors.emplace(prop_type, err_str);
638 StrVec GncPreSplit::verify_essentials()
640 auto err_msg = StrVec();
642 if (!m_amount && !m_amount_neg)
643 err_msg.emplace_back (_(
"No amount or negated amount column."));
645 if (m_rec_state && *m_rec_state ==
YREC && !m_rec_date)
646 err_msg.emplace_back (_(
"Split is reconciled but reconcile date column is missing or invalid."));
648 if (m_trec_state && *m_trec_state ==
YREC && !m_trec_date)
649 err_msg.emplace_back (_(
"Transfer split is reconciled but transfer reconcile date column is missing or invalid."));
661 if (m_pre_trans->is_multi_currency())
663 if (m_pre_trans->m_multi_split && !m_price && !m_value && !m_value_neg)
664 err_msg.emplace_back( _(
"Choice of accounts makes this a multi-currency transaction but price or (negated) value column is missing or invalid."));
665 else if (!m_pre_trans->m_multi_split &&
666 !m_price && !m_value && !m_value_neg && !m_tamount && !m_tamount_neg )
667 err_msg.emplace_back( _(
"Choice of accounts makes this a multi-currency transaction but price, (negated) value or (negated) transfer amount column is missing or invalid."));
681 static void trans_add_split (Transaction* trans,
Account* account,
683 const std::optional<std::string>& action,
684 const std::optional<std::string>& memo,
685 const std::optional<char>& rec_state,
686 const std::optional<GncDate>& rec_date)
690 xaccSplitSetAccount (split, account);
691 xaccSplitSetParent (split, trans);
702 if (rec_state && *rec_state !=
'n')
704 if (rec_state && *rec_state ==
YREC && rec_date)
706 static_cast<time64>(
GncDateTime(*rec_date, DayPart::neutral)));
710 void GncPreSplit::create_split (std::shared_ptr<DraftTransaction> draft_trans)
718 auto check = verify_essentials();
721 auto err_msg = std::string(
"Not creating split because essentials not set properly:");
722 auto add_bullet_item = [](std::string& a, std::string& b)->std::string {
return std::move(a) +
"\n• " + b; };
723 err_msg = std::accumulate (check.begin(), check.end(), std::move (err_msg), add_bullet_item);
724 PWARN (
"%s", err_msg.c_str());
728 auto splits_created = 0;
734 account = *m_account;
736 taccount = *m_taccount;
740 amount -= *m_amount_neg;
742 std::optional<GncNumeric> tamount;
743 if (m_tamount || m_tamount_neg)
747 *tamount += *m_tamount;
749 *tamount -= *m_tamount_neg;
760 if (m_value || m_value_neg)
765 value -= *m_value_neg;
772 value = amount * *m_price;
780 acct_comm, trans_curr, time);
781 GncNumeric rate = nprice ? gnc_price_get_value (nprice): gnc_numeric_zero();
787 value = amount * rate;
789 value = amount * rate.
inv();
792 PERR(
"No price found, can't create this split.");
796 trans_add_split (draft_trans->trans, account, amount, value, m_action, m_memo, m_rec_state, m_rec_date);
808 auto tvalue = -value;
816 tamount = tvalue * m_price->inv();
824 acct_comm, trans_curr, time);
825 GncNumeric rate = nprice ? gnc_price_get_value (nprice): gnc_numeric_zero();
831 tamount = tvalue * rate.
inv();
833 tamount = tvalue * rate;
838 trans_add_split (draft_trans->trans, taccount, *tamount, tvalue, m_taction, m_tmemo, m_trec_state, m_trec_date);
842 PWARN(
"No price found, defer creation of second split to generic import matcher.");
845 if (splits_created == 1)
855 draft_trans->m_price = m_price;
856 draft_trans->m_taction = m_taction;
857 draft_trans->m_tmemo = m_tmemo;
858 draft_trans->m_tamount = tamount;
859 draft_trans->m_taccount = m_taccount;
860 draft_trans->m_trec_state = m_trec_state;
861 draft_trans->m_trec_date = m_trec_date;
867 ErrMap GncPreSplit::errors (
void)
873 void GncPreSplit::set_account (
Account* acct)
880 UpdateCrossSplitCounters();
void xaccSplitSetValue(Split *split, gnc_numeric val)
The xaccSplitSetValue() method sets the value of this split in the transaction's commodity.
gnc_commodity_table * gnc_commodity_table_get_table(QofBook *book)
Returns the commodity table associated with a book.
Transaction * xaccMallocTransaction(QofBook *book)
The xaccMallocTransaction() will malloc memory and initialize it.
gboolean xaccParseAmountImport(const char *in_str, gboolean monetary, gnc_numeric *result, char **endstr, gboolean skip)
Similar to xaccParseAmount, but with two differences.
void xaccTransSetDatePostedSecsNormalized(Transaction *trans, time64 time)
This function sets the posted date of the transaction, specified by a time64 (see ctime(3))...
void xaccSplitSetAction(Split *split, const char *actn)
The Action is an arbitrary user-assigned string.
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...
a simple price database for gnucash
utility functions for the GnuCash UI
void xaccTransSetNotes(Transaction *trans, const char *notes)
Sets the transaction Notes.
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, see 'gnc_set_num_action' in engine/engine-helpers.c & .h which takes a user-set book option for selecting the source for the num-cell (the transaction-number or the split-action field) in registers/reports into account automatically.
gboolean gnc_numeric_zero_p(gnc_numeric a)
Returns 1 if the given gnc_numeric is 0 (zero), else returns 0.
The primary numeric class for representing amounts and values.
void xaccSplitSetReconcile(Split *split, char recn)
Set the reconcile flag.
#define PERR(format, args...)
Log a serious error.
GNCPriceDB * gnc_pricedb_get_db(QofBook *book)
Return the pricedb associated with the book.
#define VREC
split is void
void xaccTransSetCurrency(Transaction *trans, gnc_commodity *curr)
Set a new currency on a transaction.
#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.
void xaccSplitSetAmount(Split *split, gnc_numeric amt)
The xaccSplitSetAmount() method sets the amount in the account's commodity that the split should have...
Account handling public routines.
#define YREC
The Split has been reconciled.
void xaccSplitSetMemo(Split *split, const char *memo)
The memo is an arbitrary string associated with a split.
GList * gnc_commodity_table_get_namespaces(const gnc_commodity_table *table)
Return a list of all namespaces in the commodity table.
#define FREC
frozen into accounting period
time64 xaccTransRetDatePosted(const Transaction *trans)
Retrieve the posted date of the transaction.
Account * gnc_account_lookup_by_full_name(const Account *any_acc, const gchar *name)
The gnc_account_lookup_full_name() subroutine works like gnc_account_lookup_by_name, but uses fully-qualified names using the given separator.
#define xaccTransGetBook(X)
void xaccTransBeginEdit(Transaction *trans)
The xaccTransBeginEdit() method must be called before any changes are made to a transaction or any of...
#define CREC
The Split has been cleared.
gnc_commodity * gnc_account_get_currency_or_parent(const Account *account)
Returns a gnc_commodity that is a currency, suitable for being a Transaction's currency.
Split * xaccMallocSplit(QofBook *book)
Constructor.
GNCPrice * gnc_pricedb_lookup_nearest_in_time64(GNCPriceDB *db, const gnc_commodity *c, const gnc_commodity *currency, time64 t)
Return the price between the two commoditiesz nearest to the given time.
void xaccSplitSetDateReconciledSecs(Split *split, time64 secs)
Set the date on which this split was reconciled by specifying the time as time64. ...
bool is_part_of(std::shared_ptr< GncPreTrans > parent)
Check whether the harvested transaction properties for this instance match those of another one (the ...
gnc_commodity * xaccAccountGetCommodity(const Account *acc)
Get the account's commodity.
gnc_commodity * xaccTransGetCurrency(const Transaction *trans)
Returns the valuation commodity of this transaction.
GncNumeric inv() const noexcept
API for Transactions and Splits (journal entries)
gboolean gnc_commodity_equiv(const gnc_commodity *a, const gnc_commodity *b)
This routine returns TRUE if the two commodities are equivalent.
static const std::vector< GncDateFormat > c_formats
A vector with all the date formats supported by the string constructor.
#define NREC
not reconciled or cleared