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;
143  boost::u32regex_replace(icu::UnicodeString::fromUTF8(str), expr, "").toUTF8String(str_no_symbols);
144 
145  /* Convert based on user chosen currency format */
146  gnc_numeric val = gnc_numeric_zero();
147  char *endptr;
148  switch (currency_format)
149  {
150  case 0:
151  /* Currency locale */
152  if (!(xaccParseAmountImport (str_no_symbols.c_str(), TRUE, &val, &endptr, TRUE)))
153  throw std::invalid_argument (_("Value can't be parsed into a number using the selected currency format."));
154  break;
155  case 1:
156  /* Currency decimal period */
157  if (!(xaccParseAmountExtImport (str_no_symbols.c_str(), TRUE, '-', '.', ',', "$+", &val, &endptr)))
158  throw std::invalid_argument (_("Value can't be parsed into a number using the selected currency format."));
159  break;
160  case 2:
161  /* Currency decimal comma */
162  if (!(xaccParseAmountExtImport (str_no_symbols.c_str(), TRUE, '-', ',', '.', "$+", &val, &endptr)))
163  throw std::invalid_argument (_("Value can't be parsed into a number using the selected currency format."));
164  break;
165  }
166 
167  return GncNumeric(val);
168 }
169 
170 static char parse_reconciled (const std::string& reconcile)
171 {
172  if (g_strcmp0 (reconcile.c_str(), gnc_get_reconcile_str(NREC)) == 0) // Not reconciled
173  return NREC;
174  else if (g_strcmp0 (reconcile.c_str(), gnc_get_reconcile_str(CREC)) == 0) // Cleared
175  return CREC;
176  else if (g_strcmp0 (reconcile.c_str(), gnc_get_reconcile_str(YREC)) == 0) // Reconciled
177  return YREC;
178  else if (g_strcmp0 (reconcile.c_str(), gnc_get_reconcile_str(FREC)) == 0) // Frozen
179  return FREC;
180  else if (g_strcmp0 (reconcile.c_str(), gnc_get_reconcile_str(VREC)) == 0) // Voided will be handled at the transaction level
181  return NREC; // so return not reconciled here
182  else
183  throw std::invalid_argument (_("Value can't be parsed into a valid reconcile state."));
184 }
185 
186 gnc_commodity* parse_commodity (const std::string& comm_str)
187 {
188  if (comm_str.empty())
189  return nullptr;
190 
191  auto table = gnc_commodity_table_get_table (gnc_get_current_book());
192  gnc_commodity* comm = nullptr;
193 
194  /* First try commodity as a unique name, returns null if not found */
195  comm = gnc_commodity_table_lookup_unique (table, comm_str.c_str());
196 
197  /* Then try mnemonic in the currency namespace */
198  if (!comm)
199  comm = gnc_commodity_table_lookup (table,
200  GNC_COMMODITY_NS_CURRENCY, comm_str.c_str());
201 
202  if (!comm)
203  {
204  /* If that fails try mnemonic in all other namespaces */
205  auto namespaces = gnc_commodity_table_get_namespaces(table);
206  for (auto ns = namespaces; ns; ns = ns->next)
207  {
208  gchar* ns_str = (gchar*)ns->data;
209  if (g_utf8_collate(ns_str, GNC_COMMODITY_NS_CURRENCY) == 0)
210  continue;
211 
212  comm = gnc_commodity_table_lookup (table,
213  ns_str, comm_str.c_str());
214  if (comm)
215  break;
216  }
217  g_list_free (namespaces);
218  }
219 
220  if (!comm)
221  throw std::invalid_argument (_("Value can't be parsed into a valid commodity."));
222  else
223  return comm;
224 }
225 
226 void GncPreTrans::set (GncTransPropType prop_type, const std::string& value)
227 {
228  try
229  {
230  // Drop any existing error for the prop_type we're about to set
231  m_errors.erase(prop_type);
232 
233  switch (prop_type)
234  {
235  case GncTransPropType::UNIQUE_ID:
236  m_differ.reset();
237  if (!value.empty())
238  m_differ = value;
239  break;
240 
241  case GncTransPropType::DATE:
242  m_date.reset();
243  if (!value.empty())
244  m_date = GncDate(value, GncDate::c_formats[m_date_format].m_fmt); // Throws if parsing fails
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());
249  break;
250 
251  case GncTransPropType::NUM:
252  m_num.reset();
253  if (!value.empty())
254  m_num = value;
255  break;
256 
257  case GncTransPropType::DESCRIPTION:
258  m_desc.reset();
259  if (!value.empty())
260  m_desc = value;
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());
265  break;
266 
267  case GncTransPropType::NOTES:
268  m_notes.reset();
269  if (!value.empty())
270  m_notes = value;
271  break;
272 
273  case GncTransPropType::COMMODITY:
274  m_currency = nullptr;
275  m_currency = parse_commodity (value);
276  break;
277 
278  case GncTransPropType::VOID_REASON:
279  m_void_reason.reset();
280  if (!value.empty())
281  m_void_reason = value;
282  break;
283 
284  default:
285  /* Issue a warning for all other prop_types. */
286  PWARN ("%d is an invalid property for a transaction", static_cast<int>(prop_type));
287  break;
288  }
289  }
290  catch (const std::exception& e)
291  {
292  auto err_str = (bl::format (std::string{_("{1}: {2}")}) %
293  std::string{_(gnc_csv_col_type_strs[prop_type])} %
294  e.what()).str();
295  m_errors.emplace(prop_type, err_str);
296  }
297 
298 }
299 
300 void GncPreTrans::reset (GncTransPropType prop_type)
301 {
302  set (prop_type, std::string());
303  // Set with an empty string will effectively clear the property
304  // but can also set an error for the property. Clear that error here.
305  m_errors.erase(prop_type);
306 }
307 
308 StrVec GncPreTrans::verify_essentials (void)
309 {
310  auto errors = StrVec();
311 
312  if (!m_date)
313  errors.emplace_back(_("No valid date."));
314 
315  if (!m_desc)
316  errors.emplace_back(_("No valid description."));
317 
318  return errors;
319 }
320 
321 std::shared_ptr<DraftTransaction> GncPreTrans::create_trans (QofBook* book, gnc_commodity* currency)
322 {
323  if (created)
324  return nullptr;
325 
326  /* Gently refuse to create the transaction if the basics are not set correctly
327  * This should have been tested before calling this function though!
328  */
329  auto check = verify_essentials();
330  if (!check.empty())
331  {
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());
336  return nullptr;
337  }
338 
339  auto trans = xaccMallocTransaction (book);
340  xaccTransBeginEdit (trans);
341 
342  if (gnc_commodity_is_currency(m_currency))
343  xaccTransSetCurrency (trans, m_currency);
344  else
345  xaccTransSetCurrency (trans, currency);
347  static_cast<time64>(GncDateTime(*m_date, DayPart::neutral)));
348 
349  if (m_num)
350  xaccTransSetNum (trans, m_num->c_str());
351 
352  if (m_desc)
353  xaccTransSetDescription (trans, m_desc->c_str());
354 
355  if (m_notes)
356  xaccTransSetNotes (trans, m_notes->c_str());
357 
358  created = true;
359  return std::make_shared<DraftTransaction>(trans);
360 }
361 
362 bool GncPreTrans::is_part_of (std::shared_ptr<GncPreTrans> parent)
363 {
364  if (!parent)
365  return false;
366 
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(); // A GncPreTrans with errors can never be a parent
375 }
376 
377 ErrMap GncPreTrans::errors ()
378 {
379  return m_errors;
380 }
381 
382 void GncPreTrans::reset_cross_split_counters()
383 {
384  m_alt_currencies.clear();
385  m_acct_commodities.clear();
386 }
387 
388 
389 bool GncPreTrans::is_multi_currency()
390 {
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()))
393  num_comm++;
394  return (num_comm > 1);
395 }
396 
397 
398 void GncPreSplit::UpdateCrossSplitCounters ()
399 {
400  if (m_account && *m_account)
401  {
402  auto acct = *m_account;
403  auto comm = xaccAccountGetCommodity (acct);
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);
407  if (gnc_commodity_is_currency (comm))
408  {
409  curr = comm;
410  comm = nullptr;
411  }
412  else
414 
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);
421  }
422 }
423 
424 void GncPreSplit::set (GncTransPropType prop_type, const std::string& value)
425 {
426  try
427  {
428  // Drop any existing error for the prop_type we're about to set
429  m_errors.erase(prop_type);
430 
431  Account *acct = nullptr;
432  switch (prop_type)
433  {
434  case GncTransPropType::ACTION:
435  m_action.reset();
436  if (!value.empty())
437  m_action = value;
438  break;
439 
440  case GncTransPropType::TACTION:
441  m_taction.reset();
442  if (!value.empty())
443  m_taction = value;
444  break;
445 
446  case GncTransPropType::ACCOUNT:
447  m_account.reset();
448  if (value.empty())
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())) ||
451  (acct = gnc_account_lookup_by_full_name (gnc_get_current_root_account(), value.c_str())))
452  m_account = acct;
453  else
454  throw std::invalid_argument (_("Account value can't be mapped back to an account."));
455  break;
456 
457  case GncTransPropType::TACCOUNT:
458  m_taccount.reset();
459  if (value.empty())
460  throw std::invalid_argument (_("Transfer account value can't be empty."));
461 
462  if ((acct = gnc_account_imap_find_any (gnc_get_current_book(), IMAP_CAT_CSV,value.c_str())) ||
463  (acct = gnc_account_lookup_by_full_name (gnc_get_current_root_account(), value.c_str())))
464  m_taccount = acct;
465  else
466  throw std::invalid_argument (_("Transfer account value can't be mapped back to an account."));
467  break;
468 
469  case GncTransPropType::MEMO:
470  m_memo.reset();
471  if (!value.empty())
472  m_memo = value;
473  break;
474 
475  case GncTransPropType::TMEMO:
476  m_tmemo.reset();
477  if (!value.empty())
478  m_tmemo = value;
479  break;
480 
481  case GncTransPropType::AMOUNT:
482  m_amount.reset();
483  m_amount = parse_monetary (value, m_currency_format); // Will throw if parsing fails
484  break;
485 
486  case GncTransPropType::AMOUNT_NEG:
487  m_amount_neg.reset();
488  m_amount_neg = parse_monetary (value, m_currency_format); // Will throw if parsing fails
489  break;
490 
491  case GncTransPropType::VALUE:
492  m_value.reset();
493  m_value = parse_monetary (value, m_currency_format); // Will throw if parsing fails
494  break;
495 
496  case GncTransPropType::VALUE_NEG:
497  m_value_neg.reset();
498  m_value_neg = parse_monetary (value, m_currency_format); // Will throw if parsing fails
499  break;
500 
501  case GncTransPropType::TAMOUNT:
502  m_tamount.reset();
503  m_tamount = parse_monetary (value, m_currency_format); // Will throw if parsing fails
504  break;
505 
506  case GncTransPropType::TAMOUNT_NEG:
507  m_tamount_neg.reset();
508  m_tamount_neg = parse_monetary (value, m_currency_format); // Will throw if parsing fails
509  break;
510 
511  case GncTransPropType::PRICE:
512  /* Note while a price is not stricly a currency, it will likely use
513  * the same decimal point as currencies in the csv file, so parse
514  * using the same parser */
515  m_price.reset();
516  m_price = parse_monetary (value, m_currency_format); // Will throw if parsing fails
517  break;
518 
519  case GncTransPropType::REC_STATE:
520  m_rec_state.reset();
521  m_rec_state = parse_reconciled (value); // Throws if parsing fails
522  break;
523 
524  case GncTransPropType::TREC_STATE:
525  m_trec_state.reset();
526  m_trec_state = parse_reconciled (value); // Throws if parsing fails
527  break;
528 
529  case GncTransPropType::REC_DATE:
530  m_rec_date.reset();
531  if (!value.empty())
532  m_rec_date = GncDate (value,
533  GncDate::c_formats[m_date_format].m_fmt); // Throws if parsing fails
534  break;
535 
536  case GncTransPropType::TREC_DATE:
537  m_trec_date.reset();
538  if (!value.empty())
539  m_trec_date = GncDate (value,
540  GncDate::c_formats[m_date_format].m_fmt); // Throws if parsing fails
541  break;
542 
543  default:
544  /* Issue a warning for all other prop_types. */
545  PWARN ("%d is an invalid property for a split", static_cast<int>(prop_type));
546  break;
547  }
548  }
549  catch (const std::exception& e)
550  {
551  auto err_str = (bl::format (std::string{_("{1}: {2}")}) %
552  std::string{_(gnc_csv_col_type_strs[prop_type])} %
553  e.what()).str();
554  m_errors.emplace(prop_type, err_str);
555  }
556 
557  /* Extra currency related postprocessing for account type */
558  if (prop_type == GncTransPropType::ACCOUNT)
559  UpdateCrossSplitCounters();
560 }
561 
562 void GncPreSplit::reset (GncTransPropType prop_type)
563 {
564  set (prop_type, std::string());
565  // Set with an empty string will effectively clear the property
566  // but can also set an error for the property. Clear that error here.
567  m_errors.erase(prop_type);
568 }
569 
570 void GncPreSplit::add (GncTransPropType prop_type, const std::string& value)
571 {
572  try
573  {
574  /* Don't try to add to a property that has an error already */
575  if (m_errors.find(prop_type) != m_errors.cend())
576  return;
577 
578  auto num_val = GncNumeric();
579  switch (prop_type)
580  {
581  case GncTransPropType::AMOUNT:
582  num_val = parse_monetary (value, m_currency_format); // Will throw if parsing fails
583  if (m_amount)
584  num_val += *m_amount;
585  m_amount = num_val;
586  break;
587 
588  case GncTransPropType::AMOUNT_NEG:
589  num_val = parse_monetary (value, m_currency_format); // Will throw if parsing fails
590  if (m_amount_neg)
591  num_val += *m_amount_neg;
592  m_amount_neg = num_val;
593  break;
594 
595  case GncTransPropType::VALUE:
596  num_val = parse_monetary (value, m_currency_format); // Will throw if parsing fails
597  if (m_value)
598  num_val += *m_value;
599  m_value = num_val;
600  break;
601 
602  case GncTransPropType::VALUE_NEG:
603  num_val = parse_monetary (value, m_currency_format); // Will throw if parsing fails
604  if (m_value_neg)
605  num_val += *m_value_neg;
606  m_value_neg = num_val;
607  break;
608 
609  case GncTransPropType::TAMOUNT:
610  num_val = parse_monetary (value, m_currency_format); // Will throw if parsing fails
611  if (m_tamount)
612  num_val += *m_tamount;
613  m_tamount = num_val;
614  break;
615 
616  case GncTransPropType::TAMOUNT_NEG:
617  num_val = parse_monetary (value, m_currency_format); // Will throw if parsing fails
618  if (m_tamount_neg)
619  num_val += *m_tamount_neg;
620  m_tamount_neg = num_val;
621  break;
622 
623  default:
624  /* Issue a warning for all other prop_types. */
625  PWARN ("%d can't be used to add values in a split", static_cast<int>(prop_type));
626  break;
627  }
628  }
629  catch (const std::exception& e)
630  {
631  auto err_str = (bl::format (std::string{_("{1}: {2}")}) %
632  std::string{_(gnc_csv_col_type_strs[prop_type])} %
633  e.what()).str();
634  m_errors.emplace(prop_type, err_str);
635  }
636 }
637 
638 StrVec GncPreSplit::verify_essentials()
639 {
640  auto err_msg = StrVec();
641  /* Make sure this split has the minimum required set of properties defined. */
642  if (!m_amount && !m_amount_neg)
643  err_msg.emplace_back (_("No amount or negated amount column."));
644 
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."));
647 
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."));
650 
651 
652  /* When current account selections imply multi-currency
653  * transactions, we require extra columns to ensure each split is
654  * fully defined.
655  * Note this check only involves splits created by the csv importer
656  * code. The generic import matcher may add a balancing split
657  * optionally using Transfer <something> properties. The generic
658  * import matcher has its own tools to balance that split so
659  * we won't concern ourselves with that one here.
660  */
661  if (m_pre_trans->is_multi_currency())
662  {
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."));
668  }
669 
670  return err_msg;
671 }
672 
681 static void trans_add_split (Transaction* trans, Account* account,
682  GncNumeric amount, GncNumeric value,
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)
687 {
688  QofBook* book = xaccTransGetBook (trans);
689  auto split = xaccMallocSplit (book);
690  xaccSplitSetAccount (split, account);
691  xaccSplitSetParent (split, trans);
692  xaccSplitSetAmount (split, static_cast<gnc_numeric>(amount));
693  xaccSplitSetValue (split, static_cast<gnc_numeric>(value));
694 
695  if (memo)
696  xaccSplitSetMemo (split, memo->c_str());
697  /* Note, this function assumes the num/action switch is done at a higher level
698  * if needed by the book option */
699  if (action)
700  xaccSplitSetAction (split, action->c_str());
701 
702  if (rec_state && *rec_state != 'n')
703  xaccSplitSetReconcile (split, *rec_state);
704  if (rec_state && *rec_state == YREC && rec_date)
706  static_cast<time64>(GncDateTime(*rec_date, DayPart::neutral)));
707 
708 }
709 
710 void GncPreSplit::create_split (std::shared_ptr<DraftTransaction> draft_trans)
711 {
712  if (created)
713  return;
714 
715  /* Gently refuse to create the split if the basics are not set correctly
716  * This should have been tested before calling this function though!
717  */
718  auto check = verify_essentials();
719  if (!check.empty())
720  {
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());
725  return;
726  }
727 
728  auto splits_created = 0;
729  Account *account = nullptr;
730  Account *taccount = nullptr;
731  auto amount = GncNumeric();
732 
733  if (m_account)
734  account = *m_account;
735  if (m_taccount)
736  taccount = *m_taccount;
737  if (m_amount)
738  amount += *m_amount;
739  if (m_amount_neg)
740  amount -= *m_amount_neg;
741 
742  std::optional<GncNumeric> tamount;
743  if (m_tamount || m_tamount_neg)
744  {
745  tamount = GncNumeric();
746  if (m_tamount)
747  *tamount += *m_tamount;
748  if (m_tamount_neg)
749  *tamount -= *m_tamount_neg;
750  }
751 
752  /* Value can be calculated in several ways, depending on what
753  * data was available in the csv import file.
754  * Below code will prefer the method with the least
755  * risk on rounding errors.
756  * */
757  auto value = GncNumeric();
758  auto trans_curr = xaccTransGetCurrency(draft_trans->trans);
759  auto acct_comm = xaccAccountGetCommodity(account);
760  if (m_value || m_value_neg)
761  {
762  if (m_value)
763  value += *m_value;
764  if (m_value_neg)
765  value -= *m_value_neg;
766  }
767  else if (gnc_commodity_equiv(trans_curr, acct_comm))
768  value = amount;
769  else if (tamount)
770  value = -*tamount;
771  else if (m_price)
772  value = amount * *m_price;
773  else
774  {
775  QofBook* book = xaccTransGetBook (draft_trans->trans);
776  auto time = xaccTransRetDatePosted (draft_trans->trans);
777  /* Import data didn't specify price, let's lookup the nearest in time */
778  auto nprice =
780  acct_comm, trans_curr, time);
781  GncNumeric rate = nprice ? gnc_price_get_value (nprice): gnc_numeric_zero();
782  if (!gnc_numeric_zero_p (rate))
783  {
784  /* Found a usable price. Let's check if the conversion direction is right
785  * Reminder: value = amount * price, or amount = value / price */
786  if (gnc_commodity_equiv(gnc_price_get_currency(nprice), trans_curr))
787  value = amount * rate;
788  else
789  value = amount * rate.inv();
790  }
791  else
792  PERR("No price found, can't create this split.");
793  }
794 
795  /* Add a split with the cumulative amount value. */
796  trans_add_split (draft_trans->trans, account, amount, value, m_action, m_memo, m_rec_state, m_rec_date);
797  splits_created++;
798 
799  if (taccount)
800  {
801  /* If a taccount is set that forcibly means we're processing a single-line
802  * transaction. The csv importer will assume this can only create a
803  * two-split transaction, so whatever transfer data is available, the
804  * transfer split's value must balance the first split value. Remains
805  * to determine: the transfer amount. As with value above, for single
806  * currency case use transfer value. Otherwise calculate from whatever
807  * is found in the csv data preferring minimal rounding calculations. */
808  auto tvalue = -value;
809  auto trans_curr = xaccTransGetCurrency(draft_trans->trans);
810  auto acct_comm = xaccAccountGetCommodity(taccount);
811  if (gnc_commodity_equiv(trans_curr, acct_comm))
812  tamount = tvalue;
813  else if (tamount)
814  ; // Nothing to do, was already calculated
815  else if (m_price)
816  tamount = tvalue * m_price->inv();
817  else
818  {
819  QofBook* book = xaccTransGetBook (draft_trans->trans);
820  auto time = xaccTransRetDatePosted (draft_trans->trans);
821  /* Import data didn't specify price, let's lookup the nearest in time */
822  auto nprice =
824  acct_comm, trans_curr, time);
825  GncNumeric rate = nprice ? gnc_price_get_value (nprice): gnc_numeric_zero();
826  if (!gnc_numeric_zero_p (rate))
827  {
828  /* Found a usable price. Let's check if the conversion direction is right
829  * Reminder: value = amount * price, or amount = value / price */
830  if (gnc_commodity_equiv(gnc_price_get_currency(nprice), trans_curr))
831  tamount = tvalue * rate.inv();
832  else
833  tamount = tvalue * rate;
834  }
835  }
836  if (tamount)
837  {
838  trans_add_split (draft_trans->trans, taccount, *tamount, tvalue, m_taction, m_tmemo, m_trec_state, m_trec_date);
839  splits_created++;
840  }
841  else
842  PWARN("No price found, defer creation of second split to generic import matcher.");
843  }
844 
845  if (splits_created == 1)
846  {
847  /* If we get here, we're either
848  * - in multi-line mode
849  * - or single-line mode but didn't have enough details to create the
850  * transfer split.
851  * For the latter we will pass what we know about the transfer split to
852  * allow the generic import matcher to ask the user for the final
853  * details before creating this split.
854  */
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;
862  }
863 
864  created = true;
865 }
866 
867 ErrMap GncPreSplit::errors (void)
868 {
869  return m_errors;
870 }
871 
872 
873 void GncPreSplit::set_account (Account* acct)
874 {
875  if (acct)
876  m_account = acct;
877  else
878  m_account.reset();
879 
880  UpdateCrossSplitCounters();
881 }
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:1750
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:3137
#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:3382
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:3375
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