GnuCash  5.6-150-g038405b370+
gnc-imp-props-tx.cpp
1 /********************************************************************\
2  * gnc-imp-props-tx.cpp - import transactions from csv files *
3  * *
4  * This program is free software; you can redistribute it and/or *
5  * modify it under the terms of the GNU General Public License as *
6  * published by the Free Software Foundation; either version 2 of *
7  * the License, or (at your option) any later version. *
8  * *
9  * This program is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU General Public License*
15  * along with this program; if not, contact: *
16  * *
17  * Free Software Foundation Voice: +1-617-542-5942 *
18  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
19  * Boston, MA 02110-1301, USA gnu@gnu.org *
20  * *
21 \********************************************************************/
22 
23 #include <glib.h>
24 #include <glib/gi18n.h>
25 
26 #include <platform.h>
27 #if PLATFORM(WINDOWS)
28 #include <windows.h>
29 #endif
30 
31 #include "engine-helpers.h"
32 #include "gnc-ui-util.h"
33 #include "Account.h"
34 #include "Transaction.h"
35 #include "gnc-pricedb.h"
36 #include <gnc-exp-parser.h>
37 
38 #include <algorithm>
39 #include <exception>
40 #include <map>
41 #include <numeric>
42 #include <string>
43 #include <vector>
44 
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"
50 
51 namespace bl = boost::locale;
52 
53 G_GNUC_UNUSED static QofLogModule log_module = GNC_MOD_IMPORT;
54 
55 /* This map contains a set of strings representing the different column types. */
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") }
82 };
83 
84 /* Below two vectors define which properties the user *can't* select
85  * in two-split or multi-split mode (mostly because they don't make
86  * sense in that context).
87  */
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
98 };
99 /* List of properties that can be assigned to multiple columns at once */
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
107 };
108 
109 bool is_multi_col_prop (GncTransPropType prop)
110 {
111  return (std::find (multi_col_props.cbegin(),
112  multi_col_props.cend(), prop) != multi_col_props.cend());
113 }
114 
115 GncTransPropType sanitize_trans_prop (GncTransPropType prop, bool multi_split)
116 {
117  auto bl = multi_split ? multisplit_blacklist : twosplit_blacklist;
118  if (std::find(bl.begin(), bl.end(), prop) == bl.end())
119  return prop;
120  else
121  return GncTransPropType::NONE;
122 }
123 
124 
131 GncNumeric parse_monetary (const std::string &str, int currency_format)
132 {
133  /* An empty field is treated as zero */
134  if (str.empty())
135  return GncNumeric{};
136 
137  /* Strings otherwise containing no digits will be considered invalid */
138  if(!boost::regex_search(str, boost::regex("[0-9]")))
139  throw std::invalid_argument (_("Value doesn't appear to contain a valid number."));
140 
141  auto expr = boost::make_u32regex("[[:Sc:][:blank:]]|--");
142  std::string str_no_symbols = boost::u32regex_replace(str, expr, "");
143 
144  /* Convert based on user chosen currency format */
145  gnc_numeric val = gnc_numeric_zero();
146  char *endptr;
147  switch (currency_format)
148  {
149  case 0:
150  /* Currency locale */
151  if (!(xaccParseAmountImport (str_no_symbols.c_str(), TRUE, &val, &endptr, TRUE)))
152  throw std::invalid_argument (_("Value can't be parsed into a number using the selected currency format."));
153  break;
154  case 1:
155  /* Currency decimal period */
156  if (!(xaccParseAmountExtImport (str_no_symbols.c_str(), TRUE, '-', '.', ',', "$+", &val, &endptr)))
157  throw std::invalid_argument (_("Value can't be parsed into a number using the selected currency format."));
158  break;
159  case 2:
160  /* Currency decimal comma */
161  if (!(xaccParseAmountExtImport (str_no_symbols.c_str(), TRUE, '-', ',', '.', "$+", &val, &endptr)))
162  throw std::invalid_argument (_("Value can't be parsed into a number using the selected currency format."));
163  break;
164  }
165 
166  return GncNumeric(val);
167 }
168 
169 static char parse_reconciled (const std::string& reconcile)
170 {
171  if (g_strcmp0 (reconcile.c_str(), gnc_get_reconcile_str(NREC)) == 0) // Not reconciled
172  return NREC;
173  else if (g_strcmp0 (reconcile.c_str(), gnc_get_reconcile_str(CREC)) == 0) // Cleared
174  return CREC;
175  else if (g_strcmp0 (reconcile.c_str(), gnc_get_reconcile_str(YREC)) == 0) // Reconciled
176  return YREC;
177  else if (g_strcmp0 (reconcile.c_str(), gnc_get_reconcile_str(FREC)) == 0) // Frozen
178  return FREC;
179  else if (g_strcmp0 (reconcile.c_str(), gnc_get_reconcile_str(VREC)) == 0) // Voided will be handled at the transaction level
180  return NREC; // so return not reconciled here
181  else
182  throw std::invalid_argument (_("Value can't be parsed into a valid reconcile state."));
183 }
184 
185 gnc_commodity* parse_commodity (const std::string& comm_str)
186 {
187  if (comm_str.empty())
188  return nullptr;
189 
190  auto table = gnc_commodity_table_get_table (gnc_get_current_book());
191  gnc_commodity* comm = nullptr;
192 
193  /* First try commodity as a unique name, returns null if not found */
194  comm = gnc_commodity_table_lookup_unique (table, comm_str.c_str());
195 
196  /* Then try mnemonic in the currency namespace */
197  if (!comm)
198  comm = gnc_commodity_table_lookup (table,
199  GNC_COMMODITY_NS_CURRENCY, comm_str.c_str());
200 
201  if (!comm)
202  {
203  /* If that fails try mnemonic in all other namespaces */
204  auto namespaces = gnc_commodity_table_get_namespaces(table);
205  for (auto ns = namespaces; ns; ns = ns->next)
206  {
207  gchar* ns_str = (gchar*)ns->data;
208  if (g_utf8_collate(ns_str, GNC_COMMODITY_NS_CURRENCY) == 0)
209  continue;
210 
211  comm = gnc_commodity_table_lookup (table,
212  ns_str, comm_str.c_str());
213  if (comm)
214  break;
215  }
216  g_list_free (namespaces);
217  }
218 
219  if (!comm)
220  throw std::invalid_argument (_("Value can't be parsed into a valid commodity."));
221  else
222  return comm;
223 }
224 
225 void GncPreTrans::set (GncTransPropType prop_type, const std::string& value)
226 {
227  try
228  {
229  // Drop any existing error for the prop_type we're about to set
230  m_errors.erase(prop_type);
231 
232  switch (prop_type)
233  {
234  case GncTransPropType::UNIQUE_ID:
235  m_differ.reset();
236  if (!value.empty())
237  m_differ = value;
238  break;
239 
240  case GncTransPropType::DATE:
241  m_date.reset();
242  if (!value.empty())
243  m_date = GncDate(value, GncDate::c_formats[m_date_format].m_fmt); // Throws if parsing fails
244  else if (!m_multi_split)
245  throw std::invalid_argument (
246  (bl::format (std::string{_("Date field can not be empty if 'Multi-split' option is unset.\n")}) %
247  std::string{_(gnc_csv_col_type_strs[prop_type])}).str());
248  break;
249 
250  case GncTransPropType::NUM:
251  m_num.reset();
252  if (!value.empty())
253  m_num = value;
254  break;
255 
256  case GncTransPropType::DESCRIPTION:
257  m_desc.reset();
258  if (!value.empty())
259  m_desc = value;
260  else if (!m_multi_split)
261  throw std::invalid_argument (
262  (bl::format (std::string{_("Description field can not be empty if 'Multi-split' option is unset.\n")}) %
263  std::string{_(gnc_csv_col_type_strs[prop_type])}).str());
264  break;
265 
266  case GncTransPropType::NOTES:
267  m_notes.reset();
268  if (!value.empty())
269  m_notes = value;
270  break;
271 
272  case GncTransPropType::COMMODITY:
273  m_currency = nullptr;
274  m_currency = parse_commodity (value);
275  break;
276 
277  case GncTransPropType::VOID_REASON:
278  m_void_reason.reset();
279  if (!value.empty())
280  m_void_reason = value;
281  break;
282 
283  default:
284  /* Issue a warning for all other prop_types. */
285  PWARN ("%d is an invalid property for a transaction", static_cast<int>(prop_type));
286  break;
287  }
288  }
289  catch (const std::exception& e)
290  {
291  auto err_str = (bl::format (std::string{_("{1}: {2}")}) %
292  std::string{_(gnc_csv_col_type_strs[prop_type])} %
293  e.what()).str();
294  m_errors.emplace(prop_type, err_str);
295  }
296 
297 }
298 
299 void GncPreTrans::reset (GncTransPropType prop_type)
300 {
301  set (prop_type, std::string());
302  // Set with an empty string will effectively clear the property
303  // but can also set an error for the property. Clear that error here.
304  m_errors.erase(prop_type);
305 }
306 
307 StrVec GncPreTrans::verify_essentials (void)
308 {
309  auto errors = StrVec();
310 
311  if (!m_date)
312  errors.emplace_back(_("No valid date."));
313 
314  if (!m_desc)
315  errors.emplace_back(_("No valid description."));
316 
317  return errors;
318 }
319 
320 std::shared_ptr<DraftTransaction> GncPreTrans::create_trans (QofBook* book, gnc_commodity* currency)
321 {
322  if (created)
323  return nullptr;
324 
325  /* Gently refuse to create the transaction if the basics are not set correctly
326  * This should have been tested before calling this function though!
327  */
328  auto check = verify_essentials();
329  if (!check.empty())
330  {
331  auto err_msg = std::string("Not creating transaction because essentials not set properly:");
332  auto add_bullet_item = [](std::string& a, std::string& b)->std::string { return std::move(a) + "\n• " + b; };
333  err_msg = std::accumulate (check.begin(), check.end(), std::move (err_msg), add_bullet_item);
334  PWARN ("%s", err_msg.c_str());
335  return nullptr;
336  }
337 
338  auto trans = xaccMallocTransaction (book);
339  xaccTransBeginEdit (trans);
340 
341  if (gnc_commodity_is_currency(m_currency))
342  xaccTransSetCurrency (trans, m_currency);
343  else
344  xaccTransSetCurrency (trans, currency);
346  static_cast<time64>(GncDateTime(*m_date, DayPart::neutral)));
347 
348  if (m_num)
349  xaccTransSetNum (trans, m_num->c_str());
350 
351  if (m_desc)
352  xaccTransSetDescription (trans, m_desc->c_str());
353 
354  if (m_notes)
355  xaccTransSetNotes (trans, m_notes->c_str());
356 
357  created = true;
358  return std::make_shared<DraftTransaction>(trans);
359 }
360 
361 bool GncPreTrans::is_part_of (std::shared_ptr<GncPreTrans> parent)
362 {
363  if (!parent)
364  return false;
365 
366  return (!m_differ || m_differ == parent->m_differ) &&
367  (!m_date || m_date == parent->m_date) &&
368  (!m_num || m_num == parent->m_num) &&
369  (!m_desc || m_desc == parent->m_desc) &&
370  (!m_notes || m_notes == parent->m_notes) &&
371  (!m_currency || m_currency == parent->m_currency) &&
372  (!m_void_reason || m_void_reason == parent->m_void_reason) &&
373  parent->m_errors.empty(); // A GncPreTrans with errors can never be a parent
374 }
375 
376 ErrMap GncPreTrans::errors ()
377 {
378  return m_errors;
379 }
380 
381 void GncPreTrans::reset_cross_split_counters()
382 {
383  m_alt_currencies.clear();
384  m_acct_commodities.clear();
385 }
386 
387 
388 bool GncPreTrans::is_multi_currency()
389 {
390  auto num_comm = m_acct_commodities.size() + m_alt_currencies.size();
391  if (m_currency && (std::find (m_alt_currencies.cbegin(),m_alt_currencies.cend(), m_currency) == m_alt_currencies.cend()))
392  num_comm++;
393  return (num_comm > 1);
394 }
395 
396 
397 void GncPreSplit::UpdateCrossSplitCounters ()
398 {
399  if (m_account && *m_account)
400  {
401  auto acct = *m_account;
402  auto comm = xaccAccountGetCommodity (acct);
403  auto alt_currs = m_pre_trans->m_alt_currencies;
404  auto acct_comms = m_pre_trans->m_acct_commodities;
405  auto curr = static_cast<gnc_commodity*> (nullptr);
406  if (gnc_commodity_is_currency (comm))
407  {
408  curr = comm;
409  comm = nullptr;
410  }
411  else
413 
414  auto has_curr = [curr] (const gnc_commodity *vec_curr) { return gnc_commodity_equiv (curr, vec_curr); };
415  if (curr && std::none_of (alt_currs.cbegin(), alt_currs.cend(), has_curr))
416  m_pre_trans->m_alt_currencies.push_back(curr);
417  auto has_comm = [comm] (const gnc_commodity *vec_comm) { return gnc_commodity_equiv (comm, vec_comm); };
418  if (comm && std::none_of (acct_comms.cbegin(), acct_comms.cend(), has_comm))
419  m_pre_trans->m_alt_currencies.push_back(comm);
420  }
421 }
422 
423 void GncPreSplit::set (GncTransPropType prop_type, const std::string& value)
424 {
425  try
426  {
427  // Drop any existing error for the prop_type we're about to set
428  m_errors.erase(prop_type);
429 
430  Account *acct = nullptr;
431  switch (prop_type)
432  {
433  case GncTransPropType::ACTION:
434  m_action.reset();
435  if (!value.empty())
436  m_action = value;
437  break;
438 
439  case GncTransPropType::TACTION:
440  m_taction.reset();
441  if (!value.empty())
442  m_taction = value;
443  break;
444 
445  case GncTransPropType::ACCOUNT:
446  m_account.reset();
447  if (value.empty())
448  throw std::invalid_argument (_("Account value can't be empty."));
449  if ((acct = gnc_account_imap_find_any (gnc_get_current_book(), IMAP_CAT_CSV, value.c_str())) ||
450  (acct = gnc_account_lookup_by_full_name (gnc_get_current_root_account(), value.c_str())))
451  m_account = acct;
452  else
453  throw std::invalid_argument (_("Account value can't be mapped back to an account."));
454  break;
455 
456  case GncTransPropType::TACCOUNT:
457  m_taccount.reset();
458  if (value.empty())
459  throw std::invalid_argument (_("Transfer account value can't be empty."));
460 
461  if ((acct = gnc_account_imap_find_any (gnc_get_current_book(), IMAP_CAT_CSV,value.c_str())) ||
462  (acct = gnc_account_lookup_by_full_name (gnc_get_current_root_account(), value.c_str())))
463  m_taccount = acct;
464  else
465  throw std::invalid_argument (_("Transfer account value can't be mapped back to an account."));
466  break;
467 
468  case GncTransPropType::MEMO:
469  m_memo.reset();
470  if (!value.empty())
471  m_memo = value;
472  break;
473 
474  case GncTransPropType::TMEMO:
475  m_tmemo.reset();
476  if (!value.empty())
477  m_tmemo = value;
478  break;
479 
480  case GncTransPropType::AMOUNT:
481  m_amount.reset();
482  m_amount = parse_monetary (value, m_currency_format); // Will throw if parsing fails
483  break;
484 
485  case GncTransPropType::AMOUNT_NEG:
486  m_amount_neg.reset();
487  m_amount_neg = parse_monetary (value, m_currency_format); // Will throw if parsing fails
488  break;
489 
490  case GncTransPropType::VALUE:
491  m_value.reset();
492  m_value = parse_monetary (value, m_currency_format); // Will throw if parsing fails
493  break;
494 
495  case GncTransPropType::VALUE_NEG:
496  m_value_neg.reset();
497  m_value_neg = parse_monetary (value, m_currency_format); // Will throw if parsing fails
498  break;
499 
500  case GncTransPropType::TAMOUNT:
501  m_tamount.reset();
502  m_tamount = parse_monetary (value, m_currency_format); // Will throw if parsing fails
503  break;
504 
505  case GncTransPropType::TAMOUNT_NEG:
506  m_tamount_neg.reset();
507  m_tamount_neg = parse_monetary (value, m_currency_format); // Will throw if parsing fails
508  break;
509 
510  case GncTransPropType::PRICE:
511  /* Note while a price is not stricly a currency, it will likely use
512  * the same decimal point as currencies in the csv file, so parse
513  * using the same parser */
514  m_price.reset();
515  m_price = parse_monetary (value, m_currency_format); // Will throw if parsing fails
516  break;
517 
518  case GncTransPropType::REC_STATE:
519  m_rec_state.reset();
520  m_rec_state = parse_reconciled (value); // Throws if parsing fails
521  break;
522 
523  case GncTransPropType::TREC_STATE:
524  m_trec_state.reset();
525  m_trec_state = parse_reconciled (value); // Throws if parsing fails
526  break;
527 
528  case GncTransPropType::REC_DATE:
529  m_rec_date.reset();
530  if (!value.empty())
531  m_rec_date = GncDate (value,
532  GncDate::c_formats[m_date_format].m_fmt); // Throws if parsing fails
533  break;
534 
535  case GncTransPropType::TREC_DATE:
536  m_trec_date.reset();
537  if (!value.empty())
538  m_trec_date = GncDate (value,
539  GncDate::c_formats[m_date_format].m_fmt); // Throws if parsing fails
540  break;
541 
542  default:
543  /* Issue a warning for all other prop_types. */
544  PWARN ("%d is an invalid property for a split", static_cast<int>(prop_type));
545  break;
546  }
547  }
548  catch (const std::exception& e)
549  {
550  auto err_str = (bl::format (std::string{_("{1}: {2}")}) %
551  std::string{_(gnc_csv_col_type_strs[prop_type])} %
552  e.what()).str();
553  m_errors.emplace(prop_type, err_str);
554  }
555 
556  /* Extra currency related postprocessing for account type */
557  if (prop_type == GncTransPropType::ACCOUNT)
558  UpdateCrossSplitCounters();
559 }
560 
561 void GncPreSplit::reset (GncTransPropType prop_type)
562 {
563  set (prop_type, std::string());
564  // Set with an empty string will effectively clear the property
565  // but can also set an error for the property. Clear that error here.
566  m_errors.erase(prop_type);
567 }
568 
569 void GncPreSplit::add (GncTransPropType prop_type, const std::string& value)
570 {
571  try
572  {
573  /* Don't try to add to a property that has an error already */
574  if (m_errors.find(prop_type) != m_errors.cend())
575  return;
576 
577  auto num_val = GncNumeric();
578  switch (prop_type)
579  {
580  case GncTransPropType::AMOUNT:
581  num_val = parse_monetary (value, m_currency_format); // Will throw if parsing fails
582  if (m_amount)
583  num_val += *m_amount;
584  m_amount = num_val;
585  break;
586 
587  case GncTransPropType::AMOUNT_NEG:
588  num_val = parse_monetary (value, m_currency_format); // Will throw if parsing fails
589  if (m_amount_neg)
590  num_val += *m_amount_neg;
591  m_amount_neg = num_val;
592  break;
593 
594  case GncTransPropType::VALUE:
595  num_val = parse_monetary (value, m_currency_format); // Will throw if parsing fails
596  if (m_value)
597  num_val += *m_value;
598  m_value = num_val;
599  break;
600 
601  case GncTransPropType::VALUE_NEG:
602  num_val = parse_monetary (value, m_currency_format); // Will throw if parsing fails
603  if (m_value_neg)
604  num_val += *m_value_neg;
605  m_value_neg = num_val;
606  break;
607 
608  case GncTransPropType::TAMOUNT:
609  num_val = parse_monetary (value, m_currency_format); // Will throw if parsing fails
610  if (m_tamount)
611  num_val += *m_tamount;
612  m_tamount = num_val;
613  break;
614 
615  case GncTransPropType::TAMOUNT_NEG:
616  num_val = parse_monetary (value, m_currency_format); // Will throw if parsing fails
617  if (m_tamount_neg)
618  num_val += *m_tamount_neg;
619  m_tamount_neg = num_val;
620  break;
621 
622  default:
623  /* Issue a warning for all other prop_types. */
624  PWARN ("%d can't be used to add values in a split", static_cast<int>(prop_type));
625  break;
626  }
627  }
628  catch (const std::exception& e)
629  {
630  auto err_str = (bl::format (std::string{_("{1}: {2}")}) %
631  std::string{_(gnc_csv_col_type_strs[prop_type])} %
632  e.what()).str();
633  m_errors.emplace(prop_type, err_str);
634  }
635 }
636 
637 StrVec GncPreSplit::verify_essentials()
638 {
639  auto err_msg = StrVec();
640  /* Make sure this split has the minimum required set of properties defined. */
641  if (!m_amount && !m_amount_neg)
642  err_msg.emplace_back (_("No amount or negated amount column."));
643 
644  if (m_rec_state && *m_rec_state == YREC && !m_rec_date)
645  err_msg.emplace_back (_("Split is reconciled but reconcile date column is missing or invalid."));
646 
647  if (m_trec_state && *m_trec_state == YREC && !m_trec_date)
648  err_msg.emplace_back (_("Transfer split is reconciled but transfer reconcile date column is missing or invalid."));
649 
650 
651  /* When current account selections imply multi-currency
652  * transactions, we require extra columns to ensure each split is
653  * fully defined.
654  * Note this check only involves splits created by the csv importer
655  * code. The generic import matcher may add a balancing split
656  * optionally using Transfer <something> properties. The generic
657  * import matcher has its own tools to balance that split so
658  * we won't concern ourselves with that one here.
659  */
660  if (m_pre_trans->is_multi_currency())
661  {
662  if (m_pre_trans->m_multi_split && !m_price && !m_value && !m_value_neg)
663  err_msg.emplace_back( _("Choice of accounts makes this a multi-currency transaction but price or (negated) value column is missing or invalid."));
664  else if (!m_pre_trans->m_multi_split &&
665  !m_price && !m_value && !m_value_neg && !m_tamount && !m_tamount_neg )
666  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."));
667  }
668 
669  return err_msg;
670 }
671 
680 static void trans_add_split (Transaction* trans, Account* account,
681  GncNumeric amount, GncNumeric value,
682  const std::optional<std::string>& action,
683  const std::optional<std::string>& memo,
684  const std::optional<char>& rec_state,
685  const std::optional<GncDate>& rec_date)
686 {
687  QofBook* book = xaccTransGetBook (trans);
688  auto split = xaccMallocSplit (book);
689  xaccSplitSetAccount (split, account);
690  xaccSplitSetParent (split, trans);
691  xaccSplitSetAmount (split, static_cast<gnc_numeric>(amount));
692  xaccSplitSetValue (split, static_cast<gnc_numeric>(value));
693 
694  if (memo)
695  xaccSplitSetMemo (split, memo->c_str());
696  /* Note, this function assumes the num/action switch is done at a higher level
697  * if needed by the book option */
698  if (action)
699  xaccSplitSetAction (split, action->c_str());
700 
701  if (rec_state && *rec_state != 'n')
702  xaccSplitSetReconcile (split, *rec_state);
703  if (rec_state && *rec_state == YREC && rec_date)
705  static_cast<time64>(GncDateTime(*rec_date, DayPart::neutral)));
706 
707 }
708 
709 void GncPreSplit::create_split (std::shared_ptr<DraftTransaction> draft_trans)
710 {
711  if (created)
712  return;
713 
714  /* Gently refuse to create the split if the basics are not set correctly
715  * This should have been tested before calling this function though!
716  */
717  auto check = verify_essentials();
718  if (!check.empty())
719  {
720  auto err_msg = std::string("Not creating split because essentials not set properly:");
721  auto add_bullet_item = [](std::string& a, std::string& b)->std::string { return std::move(a) + "\n• " + b; };
722  err_msg = std::accumulate (check.begin(), check.end(), std::move (err_msg), add_bullet_item);
723  PWARN ("%s", err_msg.c_str());
724  return;
725  }
726 
727  auto splits_created = 0;
728  Account *account = nullptr;
729  Account *taccount = nullptr;
730  auto amount = GncNumeric();
731 
732  if (m_account)
733  account = *m_account;
734  if (m_taccount)
735  taccount = *m_taccount;
736  if (m_amount)
737  amount += *m_amount;
738  if (m_amount_neg)
739  amount -= *m_amount_neg;
740 
741  std::optional<GncNumeric> tamount;
742  if (m_tamount || m_tamount_neg)
743  {
744  tamount = GncNumeric();
745  if (m_tamount)
746  *tamount += *m_tamount;
747  if (m_tamount_neg)
748  *tamount -= *m_tamount_neg;
749  }
750 
751  /* Value can be calculated in several ways, depending on what
752  * data was available in the csv import file.
753  * Below code will prefer the method with the least
754  * risk on rounding errors.
755  * */
756  auto value = GncNumeric();
757  auto trans_curr = xaccTransGetCurrency(draft_trans->trans);
758  auto acct_comm = xaccAccountGetCommodity(account);
759  if (m_value || m_value_neg)
760  {
761  if (m_value)
762  value += *m_value;
763  if (m_value_neg)
764  value -= *m_value_neg;
765  }
766  else if (gnc_commodity_equiv(trans_curr, acct_comm))
767  value = amount;
768  else if (tamount)
769  value = -*tamount;
770  else if (m_price)
771  value = amount * *m_price;
772  else
773  {
774  QofBook* book = xaccTransGetBook (draft_trans->trans);
775  auto time = xaccTransRetDatePosted (draft_trans->trans);
776  /* Import data didn't specify price, let's lookup the nearest in time */
777  auto nprice =
779  acct_comm, trans_curr, time);
780  GncNumeric rate = nprice ? gnc_price_get_value (nprice): gnc_numeric_zero();
781  if (!gnc_numeric_zero_p (rate))
782  {
783  /* Found a usable price. Let's check if the conversion direction is right
784  * Reminder: value = amount * price, or amount = value / price */
785  if (gnc_commodity_equiv(gnc_price_get_currency(nprice), trans_curr))
786  value = amount * rate;
787  else
788  value = amount * rate.inv();
789  }
790  else
791  PERR("No price found, can't create this split.");
792  }
793 
794  /* Add a split with the cumulative amount value. */
795  trans_add_split (draft_trans->trans, account, amount, value, m_action, m_memo, m_rec_state, m_rec_date);
796  splits_created++;
797 
798  if (taccount)
799  {
800  /* If a taccount is set that forcibly means we're processing a single-line
801  * transaction. The csv importer will assume this can only create a
802  * two-split transaction, so whatever transfer data is available, the
803  * transfer split's value must balance the first split value. Remains
804  * to determine: the transfer amount. As with value above, for single
805  * currency case use transfer value. Otherwise calculate from whatever
806  * is found in the csv data preferring minimal rounding calculations. */
807  auto tvalue = -value;
808  auto trans_curr = xaccTransGetCurrency(draft_trans->trans);
809  auto acct_comm = xaccAccountGetCommodity(taccount);
810  if (gnc_commodity_equiv(trans_curr, acct_comm))
811  tamount = tvalue;
812  else if (tamount)
813  ; // Nothing to do, was already calculated
814  else if (m_price)
815  tamount = tvalue * m_price->inv();
816  else
817  {
818  QofBook* book = xaccTransGetBook (draft_trans->trans);
819  auto time = xaccTransRetDatePosted (draft_trans->trans);
820  /* Import data didn't specify price, let's lookup the nearest in time */
821  auto nprice =
823  acct_comm, trans_curr, time);
824  GncNumeric rate = nprice ? gnc_price_get_value (nprice): gnc_numeric_zero();
825  if (!gnc_numeric_zero_p (rate))
826  {
827  /* Found a usable price. Let's check if the conversion direction is right
828  * Reminder: value = amount * price, or amount = value / price */
829  if (gnc_commodity_equiv(gnc_price_get_currency(nprice), trans_curr))
830  tamount = tvalue * rate.inv();
831  else
832  tamount = tvalue * rate;
833  }
834  }
835  if (tamount)
836  {
837  trans_add_split (draft_trans->trans, taccount, *tamount, tvalue, m_taction, m_tmemo, m_trec_state, m_trec_date);
838  splits_created++;
839  }
840  else
841  PWARN("No price found, defer creation of second split to generic import matcher.");
842  }
843 
844  if (splits_created == 1)
845  {
846  /* If we get here, we're either
847  * - in multi-line mode
848  * - or single-line mode but didn't have enough details to create the
849  * transfer split.
850  * For the latter we will pass what we know about the transfer split to
851  * allow the generic import matcher to ask the user for the final
852  * details before creating this split.
853  */
854  draft_trans->m_price = m_price;
855  draft_trans->m_taction = m_taction;
856  draft_trans->m_tmemo = m_tmemo;
857  draft_trans->m_tamount = tamount;
858  draft_trans->m_taccount = m_taccount;
859  draft_trans->m_trec_state = m_trec_state;
860  draft_trans->m_trec_date = m_trec_date;
861  }
862 
863  created = true;
864 }
865 
866 ErrMap GncPreSplit::errors (void)
867 {
868  return m_errors;
869 }
870 
871 
872 void GncPreSplit::set_account (Account* acct)
873 {
874  if (acct)
875  m_account = acct;
876  else
877  m_account.reset();
878 
879  UpdateCrossSplitCounters();
880 }
void xaccSplitSetValue(Split *split, gnc_numeric val)
The xaccSplitSetValue() method sets the value of this split in the transaction&#39;s commodity.
Definition: gmock-Split.cpp:92
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.
Definition: Split.cpp:1752
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...
GnuCash DateTime class.
a simple price database for gnucash
utility functions for the GnuCash UI
void xaccTransSetNotes(Transaction *trans, const char *notes)
Sets the transaction Notes.
STRUCTS.
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 &#39;gnc_set_num_action&#39; 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.
Definition: gnc-numeric.hpp:60
void xaccSplitSetReconcile(Split *split, char recn)
Set the reconcile flag.
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244
GNCPriceDB * gnc_pricedb_get_db(QofBook *book)
Return the pricedb associated with the book.
#define VREC
split is void
Definition: Split.h:77
void xaccTransSetCurrency(Transaction *trans, gnc_commodity *curr)
Set a new currency on a transaction.
#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.
void xaccSplitSetAmount(Split *split, gnc_numeric amt)
The xaccSplitSetAmount() method sets the amount in the account&#39;s commodity that the split should have...
Definition: gmock-Split.cpp:77
Account handling public routines.
#define YREC
The Split has been reconciled.
Definition: Split.h:74
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
Definition: Split.h:75
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.
Definition: Account.cpp:3113
#define xaccTransGetBook(X)
Definition: Transaction.h:786
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.
Definition: Split.h:73
gnc_commodity * gnc_account_get_currency_or_parent(const Account *account)
Returns a gnc_commodity that is a currency, suitable for being a Transaction&#39;s currency.
Definition: Account.cpp:3358
Split * xaccMallocSplit(QofBook *book)
Constructor.
Definition: gmock-Split.cpp:37
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&#39;s commodity.
Definition: Account.cpp:3351
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.
GnuCash Date class.
#define NREC
not reconciled or cleared
Definition: Split.h:76