GnuCash  5.6-150-g038405b370+
Scrub.cpp
1 /********************************************************************\
2  * Scrub.c -- convert single-entry accounts into clean double-entry *
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 /*
24  * FILE:
25  * Scrub.c
26  *
27  * FUNCTION:
28  * Provides a set of functions and utilities for scrubbing clean
29  * single-entry accounts so that they can be promoted into
30  * self-consistent, clean double-entry accounts.
31  *
32  * HISTORY:
33  * Created by Linas Vepstas December 1998
34  * Copyright (c) 1998-2000, 2003 Linas Vepstas <linas@linas.org>
35  * Copyright (c) 2002 Christian Stimming
36  * Copyright (c) 2006 David Hampton
37  */
38 
39 #include <config.h>
40 
41 #include <glib.h>
42 #include <glib/gi18n.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <stdint.h>
46 #include <stdbool.h>
47 #include <unordered_set>
48 
49 #include "Account.h"
50 #include "AccountP.hpp"
51 #include "Account.hpp"
52 #include "Scrub.h"
53 #include "Transaction.h"
54 #include "TransactionP.hpp"
55 #include "gnc-commodity.h"
56 #include "qofinstance-p.h"
57 #include "gnc-session.h"
58 
59 #undef G_LOG_DOMAIN
60 #define G_LOG_DOMAIN "gnc.engine.scrub"
61 
62 static QofLogModule log_module = G_LOG_DOMAIN;
63 static gboolean abort_now = FALSE;
64 static gint scrub_depth = 0;
65 
66 
67 static Account* xaccScrubUtilityGetOrMakeAccount (Account *root,
68  gnc_commodity* currency,
69  const char* accname,
70  GNCAccountType acctype,
71  gboolean placeholder,
72  gboolean checkname);
73 
74 void
75 gnc_set_abort_scrub (gboolean abort)
76 {
77  abort_now = abort;
78 }
79 
80 gboolean
81 gnc_get_abort_scrub (void)
82 {
83  return abort_now;
84 }
85 
86 gboolean
88 {
89  return scrub_depth > 0;
90 }
91 
92 /* ================================================================ */
93 
94 using TransSet = std::unordered_set<Transaction*>;
95 
96 static TransSet
97 get_all_transactions (Account *account, bool descendants)
98 {
99  TransSet set;
100  auto add_transactions = [&set](auto a)
101  { gnc_account_foreach_split (a, [&set](auto s){ set.insert (xaccSplitGetParent (s)); }, false); };
102  add_transactions (account);
103  if (descendants)
104  gnc_account_foreach_descendant (account, add_transactions);
105  return set;
106 }
107 
108 /* ================================================================ */
109 
110 static void
111 TransScrubOrphansFast (Transaction *trans, Account *root)
112 {
113  g_return_if_fail (trans && trans->common_currency && root);
114 
115  for (GList *node = trans->splits; node; node = node->next)
116  {
117  Split *split = GNC_SPLIT(node->data);
118  if (abort_now) break;
119 
120  if (split->acc) continue;
121 
122  DEBUG ("Found an orphan\n");
123 
124  gchar *accname = g_strconcat
125  (_("Orphan"), "-", gnc_commodity_get_mnemonic (trans->common_currency),
126  nullptr);
127 
128  Account *orph = xaccScrubUtilityGetOrMakeAccount
129  (root, trans->common_currency, accname, ACCT_TYPE_BANK, false, true);
130 
131  g_free (accname);
132  if (!orph) continue;
133 
134  xaccSplitSetAccount(split, orph);
135  }
136 }
137 
138 static void
139 AccountScrubOrphans (Account *acc, bool descendants, QofPercentageFunc percentagefunc)
140 {
141  if (!acc) return;
142  scrub_depth++;
143 
144  auto transactions = get_all_transactions (acc, descendants);
145  auto total_trans = transactions.size();
146  const char *message = _("Looking for orphans in transaction: %u of %zu");
147  guint current_trans = 0;
148 
149  for (auto trans : transactions)
150  {
151  if (current_trans % 10 == 0)
152  {
153  char *progress_msg = g_strdup_printf (message, current_trans, total_trans);
154  (percentagefunc)(progress_msg, (100 * current_trans) / total_trans);
155  g_free (progress_msg);
156  if (abort_now) break;
157  }
158 
159  TransScrubOrphansFast (trans, gnc_account_get_root (acc));
160  current_trans++;
161  }
162  (percentagefunc)(nullptr, -1.0);
163  scrub_depth--;
164 }
165 
166 void
168 {
169  AccountScrubOrphans (acc, false, percentagefunc);
170 }
171 
172 void
174 {
175  AccountScrubOrphans (acc, true, percentagefunc);
176 }
177 
178 void
179 xaccTransScrubOrphans (Transaction *trans)
180 {
181  SplitList *node;
182  QofBook *book = nullptr;
183  Account *root = nullptr;
184 
185  if (!trans) return;
186 
187  for (node = trans->splits; node; node = node->next)
188  {
189  Split *split = GNC_SPLIT(node->data);
190  if (abort_now) break;
191 
192  if (split->acc)
193  {
194  TransScrubOrphansFast (trans, gnc_account_get_root(split->acc));
195  return;
196  }
197  }
198 
199  /* If we got to here, then *none* of the splits belonged to an
200  * account. Not a happy situation. We should dig an account
201  * out of the book the transaction belongs to.
202  * XXX we should probably *always* to this, instead of the above loop!
203  */
204  PINFO ("Free Floating Transaction!");
205  book = xaccTransGetBook (trans);
206  root = gnc_book_get_root_account (book);
207  TransScrubOrphansFast (trans, root);
208 }
209 
210 /* ================================================================ */
211 
212 void
213 xaccAccountTreeScrubSplits (Account *account)
214 {
215  if (!account) return;
216 
217  xaccAccountScrubSplits (account);
218  gnc_account_foreach_descendant(account,
219  (AccountCb)xaccAccountScrubSplits, nullptr);
220 }
221 
222 void
223 xaccAccountScrubSplits (Account *account)
224 {
225  scrub_depth++;
226  for (auto s : xaccAccountGetSplits (account))
227  {
228  if (abort_now) break;
229  xaccSplitScrub (s);
230  }
231  scrub_depth--;
232 }
233 
234 /* if dry_run is true, this function will analyze the split and
235  return true if the split will be modified during the actual scrub. */
236 static bool
237 split_scrub_or_dry_run (Split *split, bool dry_run)
238 {
239  Account *account;
240  Transaction *trans;
241  gnc_numeric value, amount;
242  gnc_commodity *currency, *acc_commodity;
243  int scu;
244 
245  if (!split) return false;
246  ENTER ("(split=%p)", split);
247 
248  trans = xaccSplitGetParent (split);
249  if (!trans)
250  {
251  LEAVE("no trans");
252  return false;
253  }
254 
255  account = xaccSplitGetAccount (split);
256 
257  /* If there's no account, this split is an orphan.
258  * We need to fix that first, before proceeding.
259  */
260  if (!account)
261  {
262  if (dry_run)
263  return true;
264  else
265  xaccTransScrubOrphans (trans);
266  account = xaccSplitGetAccount (split);
267  }
268 
269  /* Grrr... the register gnc_split_register_load() line 203 of
270  * src/register/ledger-core/split-register-load.c will create
271  * free-floating bogus transactions. Ignore these for now ...
272  */
273  if (!account)
274  {
275  PINFO ("Free Floating Transaction!");
276  LEAVE ("no account");
277  return false;
278  }
279 
280  /* Split amounts and values should be valid numbers */
281  value = xaccSplitGetValue (split);
282  if (gnc_numeric_check (value))
283  {
284  value = gnc_numeric_zero();
285  if (dry_run)
286  return true;
287  else
288  xaccSplitSetValue (split, value);
289  }
290 
291  amount = xaccSplitGetAmount (split);
292  if (gnc_numeric_check (amount))
293  {
294  amount = gnc_numeric_zero();
295  if (dry_run)
296  return true;
297  else
298  xaccSplitSetAmount (split, amount);
299  }
300 
301  currency = xaccTransGetCurrency (trans);
302 
303  /* If the account doesn't have a commodity,
304  * we should attempt to fix that first.
305  */
306  acc_commodity = xaccAccountGetCommodity(account);
307  if (!acc_commodity)
308  {
309  if (dry_run)
310  return true;
311  else
312  xaccAccountScrubCommodity (account);
313  }
314  if (!acc_commodity || !gnc_commodity_equiv(acc_commodity, currency))
315  {
316  LEAVE ("(split=%p) inequiv currency", split);
317  return false;
318  }
319 
320  scu = MIN (xaccAccountGetCommoditySCU (account),
321  gnc_commodity_get_fraction (currency));
322 
323  if (gnc_numeric_same (amount, value, scu, GNC_HOW_RND_ROUND_HALF_UP))
324  {
325  LEAVE("(split=%p) different values", split);
326  return false;
327  }
328 
329  if (dry_run)
330  return true;
331 
332  /*
333  * This will be hit every time you answer yes to the dialog "The
334  * current transaction has changed. Would you like to record it.
335  */
336  PINFO ("Adjusted split with mismatched values, desc=\"%s\" memo=\"%s\""
337  " old amount %s %s, new amount %s",
338  trans->description, split->memo,
340  gnc_commodity_get_mnemonic (currency),
342 
343  xaccTransBeginEdit (trans);
344  xaccSplitSetAmount (split, value);
345  xaccTransCommitEdit (trans);
346  LEAVE ("(split=%p)", split);
347  return true;
348 }
349 
350 /* ================================================================ */
351 
352 
353 static void
354 AccountScrubImbalance (Account *acc, bool descendants,
355  QofPercentageFunc percentagefunc)
356 {
357  const char *message = _("Looking for imbalances in transaction date %s: %u of %zu");
358 
359  if (!acc) return;
360 
361  QofBook *book = qof_session_get_book (gnc_get_current_session ());
362  Account *root = gnc_book_get_root_account (book);
363  auto transactions = get_all_transactions (acc, descendants);
364  auto count = transactions.size();
365  auto curr_trans = 0;
366 
367  scrub_depth++;
368  for (auto trans : transactions)
369  {
370  if (abort_now) break;
371 
372  PINFO("Start processing transaction %d of %zu", curr_trans + 1, count);
373 
374  if (curr_trans % 10 == 0)
375  {
376  char *date = qof_print_date (xaccTransGetDate (trans));
377  char *progress_msg = g_strdup_printf (message, date, curr_trans, count);
378  (percentagefunc)(progress_msg, (100 * curr_trans) / count);
379  g_free (progress_msg);
380  g_free (date);
381  }
382 
383  TransScrubOrphansFast (trans, root);
384  xaccTransScrubCurrency(trans);
385  xaccTransScrubImbalance (trans, root, nullptr);
386 
387  PINFO("Finished processing transaction %d of %zu", curr_trans + 1, count);
388  curr_trans++;
389  }
390  (percentagefunc)(nullptr, -1.0);
391  scrub_depth--;
392 }
393 
394 void
395 xaccTransScrubSplits (Transaction *trans)
396 {
397  if (!trans) return;
398 
399  gnc_commodity *currency = xaccTransGetCurrency (trans);
400  if (!currency)
401  PERR ("Transaction doesn't have a currency!");
402 
403  bool must_scrub = false;
404 
405  for (GList *n = xaccTransGetSplitList (trans); !must_scrub && n; n = g_list_next (n))
406  if (split_scrub_or_dry_run (GNC_SPLIT(n->data), true))
407  must_scrub = true;
408 
409  if (!must_scrub)
410  return;
411 
412  xaccTransBeginEdit(trans);
413  /* The split scrub expects the transaction to have a currency! */
414 
415  for (GList *n = xaccTransGetSplitList (trans); n; n = g_list_next (n))
416  xaccSplitScrub (GNC_SPLIT(n->data));
417 
418  xaccTransCommitEdit(trans);
419 }
420 
421 /* ================================================================ */
422 
423 void
424 xaccSplitScrub (Split *split)
425 {
426  split_scrub_or_dry_run (split, false);
427 }
428 
429 /* ================================================================ */
430 
431 
432 void
433 xaccAccountTreeScrubImbalance (Account *acc, QofPercentageFunc percentagefunc)
434 {
435  AccountScrubImbalance (acc, true, percentagefunc);
436 }
437 
438 void
439 xaccAccountScrubImbalance (Account *acc, QofPercentageFunc percentagefunc)
440 {
441  AccountScrubImbalance (acc, false, percentagefunc);
442 }
443 
444 static Split *
445 get_balance_split (Transaction *trans, Account *root, Account *account,
446  gnc_commodity *commodity)
447 {
448  Split *balance_split;
449  gchar *accname;
450 
451  if (!account ||
452  !gnc_commodity_equiv (commodity, xaccAccountGetCommodity(account)))
453  {
454  if (!root)
455  {
456  root = gnc_book_get_root_account (xaccTransGetBook (trans));
457  if (nullptr == root)
458  {
459  /* This can't occur, things should be in books */
460  PERR ("Bad data corruption, no root account in book");
461  return nullptr;
462  }
463  }
464  accname = g_strconcat (_("Imbalance"), "-",
465  gnc_commodity_get_mnemonic (commodity), nullptr);
466  account = xaccScrubUtilityGetOrMakeAccount (root, commodity,
467  accname, ACCT_TYPE_BANK,
468  FALSE, TRUE);
469  g_free (accname);
470  if (!account)
471  {
472  PERR ("Can't get balancing account");
473  return nullptr;
474  }
475  }
476 
477  balance_split = xaccTransFindSplitByAccount(trans, account);
478 
479  /* Put split into account before setting split value */
480  if (!balance_split)
481  {
482  balance_split = xaccMallocSplit (qof_instance_get_book(trans));
483 
484  xaccTransBeginEdit (trans);
485  xaccSplitSetParent(balance_split, trans);
486  xaccSplitSetAccount(balance_split, account);
487  xaccTransCommitEdit (trans);
488  }
489 
490  return balance_split;
491 }
492 
493 static gnc_commodity*
494 find_root_currency(void)
495 {
496  QofSession *sess = gnc_get_current_session ();
497  Account *root = gnc_book_get_root_account (qof_session_get_book (sess));
498  gnc_commodity *root_currency = xaccAccountGetCommodity (root);
499 
500  /* Some older books may not have a currency set on the root
501  * account. In that case find the first top-level INCOME account
502  * and use its currency. */
503  if (!root_currency)
504  {
505  GList *children = gnc_account_get_children (root);
506  for (GList *node = children; node && !root_currency;
507  node = g_list_next (node))
508  {
509  Account *child = GNC_ACCOUNT (node->data);
510  if (xaccAccountGetType (child) == ACCT_TYPE_INCOME)
511  root_currency = xaccAccountGetCommodity (child);
512  }
513  g_list_free (children);
514  }
515  return root_currency;
516 }
517 
518 /* Get the trading split for a given commodity, creating it (and the
519  necessary parent accounts) if it doesn't exist. */
520 static Split *
521 get_trading_split (Transaction *trans, Account *base,
522  gnc_commodity *commodity)
523 {
524  Split *balance_split;
525  Account *trading_account;
526  Account *ns_account;
527  Account *account;
528  Account* root = gnc_book_get_root_account (xaccTransGetBook (trans));
529 
530  trading_account = xaccScrubUtilityGetOrMakeAccount (root,
531  nullptr,
532  _("Trading"),
534  TRUE, FALSE);
535  if (!trading_account)
536  {
537  PERR ("Can't get trading account");
538  return nullptr;
539  }
540 
541  ns_account = xaccScrubUtilityGetOrMakeAccount (trading_account,
542  nullptr,
543  gnc_commodity_get_namespace(commodity),
545  TRUE, TRUE);
546  if (!ns_account)
547  {
548  PERR ("Can't get namespace account");
549  return nullptr;
550  }
551 
552  account = xaccScrubUtilityGetOrMakeAccount (ns_account, commodity,
553  gnc_commodity_get_mnemonic(commodity),
555  FALSE, FALSE);
556  if (!account)
557  {
558  PERR ("Can't get commodity account");
559  return nullptr;
560  }
561 
562 
563  balance_split = xaccTransFindSplitByAccount(trans, account);
564 
565  /* Put split into account before setting split value */
566  if (!balance_split)
567  {
568  balance_split = xaccMallocSplit (qof_instance_get_book(trans));
569 
570  xaccTransBeginEdit (trans);
571  xaccSplitSetParent(balance_split, trans);
572  xaccSplitSetAccount(balance_split, account);
573  xaccTransCommitEdit (trans);
574  }
575 
576  return balance_split;
577 }
578 
579 static void
580 add_balance_split (Transaction *trans, gnc_numeric imbalance,
581  Account *root, Account *account)
582 {
583  const gnc_commodity *commodity;
584  gnc_numeric old_value, new_value;
585  Split *balance_split;
586  gnc_commodity *currency = xaccTransGetCurrency (trans);
587 
588  balance_split = get_balance_split(trans, root, account, currency);
589  if (!balance_split)
590  {
591  /* Error already logged */
592  LEAVE("");
593  return;
594  }
595  account = xaccSplitGetAccount(balance_split);
596 
597  xaccTransBeginEdit (trans);
598 
599  old_value = xaccSplitGetValue (balance_split);
600 
601  /* Note: We have to round for the commodity's fraction, NOT any
602  * already existing denominator (bug #104343), because either one
603  * of the denominators might already be reduced. */
604  new_value = gnc_numeric_sub (old_value, imbalance,
605  gnc_commodity_get_fraction(currency),
607 
608  xaccSplitSetValue (balance_split, new_value);
609 
610  commodity = xaccAccountGetCommodity (account);
611  if (gnc_commodity_equiv (currency, commodity))
612  {
613  xaccSplitSetAmount (balance_split, new_value);
614  }
615 
616  xaccSplitScrub (balance_split);
617  xaccTransCommitEdit (trans);
618 }
619 
620 /* Balance a transaction without trading accounts. */
621 static void
622 gnc_transaction_balance_no_trading (Transaction *trans, Account *root,
623  Account *account)
624 {
625  gnc_numeric imbalance = xaccTransGetImbalanceValue (trans);
626 
627  /* Make the value sum to zero */
628  if (! gnc_numeric_zero_p (imbalance))
629  {
630  PINFO ("Value unbalanced transaction");
631 
632  add_balance_split (trans, imbalance, root, account);
633  }
634 
635 }
636 
637 static gnc_numeric
638 gnc_transaction_get_commodity_imbalance (Transaction *trans,
639  gnc_commodity *commodity)
640 {
641  /* Find the value imbalance in this commodity */
642  gnc_numeric val_imbalance = gnc_numeric_zero();
643  GList *splits = nullptr;
644  for (splits = trans->splits; splits; splits = splits->next)
645  {
646  Split *split = GNC_SPLIT(splits->data);
647  gnc_commodity *split_commodity =
649  if (xaccTransStillHasSplit (trans, split) &&
650  gnc_commodity_equal (commodity, split_commodity))
651  val_imbalance = gnc_numeric_add (val_imbalance,
652  xaccSplitGetValue (split),
655  }
656  return val_imbalance;
657 }
658 
659 /* GFunc wrapper for xaccSplitDestroy */
660 static void
661 destroy_split (void* ptr)
662 {
663  Split *split = GNC_SPLIT (ptr);
664  if (split)
665  xaccSplitDestroy (split);
666 }
667 
668 /* Balancing transactions with trading accounts works best when
669  * starting with no trading splits.
670  */
671 static void
672 xaccTransClearTradingSplits (Transaction *trans)
673 {
674  GList *trading_splits = nullptr;
675 
676  for (GList* node = trans->splits; node; node = node->next)
677  {
678  Split* split = GNC_SPLIT(node->data);
679  Account* acc = nullptr;
680  if (!split)
681  continue;
682  acc = xaccSplitGetAccount(split);
683  if (acc && xaccAccountGetType(acc) == ACCT_TYPE_TRADING)
684  trading_splits = g_list_prepend (trading_splits, node->data);
685  }
686 
687  if (!trading_splits)
688  return;
689 
690  xaccTransBeginEdit (trans);
691  /* destroy_splits doesn't actually free the splits but this gets
692  * the list itself freed.
693  */
694  g_list_free_full (trading_splits, destroy_split);
695  xaccTransCommitEdit (trans);
696 }
697 
698 static void
699 gnc_transaction_balance_trading (Transaction *trans, Account *root)
700 {
701  MonetaryList *imbal_list;
702  MonetaryList *imbalance_commod;
703  Split *balance_split = nullptr;
704 
705  /* If the transaction is balanced, nothing more to do */
706  imbal_list = xaccTransGetImbalance (trans);
707  if (!imbal_list)
708  {
709  LEAVE("transaction is balanced");
710  return;
711  }
712 
713  PINFO ("Currency unbalanced transaction");
714 
715  for (imbalance_commod = imbal_list; imbalance_commod;
716  imbalance_commod = imbalance_commod->next)
717  {
718  auto imbal_mon = static_cast<gnc_monetary*>(imbalance_commod->data);
719  gnc_commodity *commodity;
720  gnc_numeric old_amount, new_amount;
721  const gnc_commodity *txn_curr = xaccTransGetCurrency (trans);
722 
723  commodity = gnc_monetary_commodity (*imbal_mon);
724 
725  balance_split = get_trading_split(trans, root, commodity);
726  if (!balance_split)
727  {
728  /* Error already logged */
729  gnc_monetary_list_free(imbal_list);
730  LEAVE("");
731  return;
732  }
733 
734  xaccTransBeginEdit (trans);
735 
736  old_amount = xaccSplitGetAmount (balance_split);
737  new_amount = gnc_numeric_sub (old_amount, gnc_monetary_value(*imbal_mon),
738  gnc_commodity_get_fraction(commodity),
740 
741  xaccSplitSetAmount (balance_split, new_amount);
742 
743  if (gnc_commodity_equal (txn_curr, commodity))
744  {
745  /* Imbalance commodity is the transaction currency, value in the
746  split must be the same as the amount */
747  xaccSplitSetValue (balance_split, new_amount);
748  }
749  else
750  {
751  gnc_numeric val_imbalance = gnc_transaction_get_commodity_imbalance (trans, commodity);
752 
753  gnc_numeric old_value = xaccSplitGetValue (balance_split);
754  gnc_numeric new_value = gnc_numeric_sub (old_value, val_imbalance,
755  gnc_commodity_get_fraction(txn_curr),
757 
758  xaccSplitSetValue (balance_split, new_value);
759  }
760 
761  xaccSplitScrub (balance_split);
762  xaccTransCommitEdit (trans);
763  }
764 
765  gnc_monetary_list_free(imbal_list);
766 }
767 
773 static void
774 gnc_transaction_balance_trading_more_splits (Transaction *trans, Account *root)
775 {
776  /* Copy the split list so we don't see the splits we're adding */
777  GList *splits_dup = g_list_copy(trans->splits), *splits = nullptr;
778  const gnc_commodity *txn_curr = xaccTransGetCurrency (trans);
779  for (splits = splits_dup; splits; splits = splits->next)
780  {
781  Split *split = GNC_SPLIT(splits->data);
782  if (! xaccTransStillHasSplit(trans, split)) continue;
783  if (!gnc_numeric_zero_p(xaccSplitGetValue(split)) &&
785  {
786  gnc_commodity *commodity;
787  gnc_numeric old_value, new_value;
788  Split *balance_split;
789 
790  commodity = xaccAccountGetCommodity(xaccSplitGetAccount(split));
791  if (!commodity)
792  {
793  PERR("Split has no commodity");
794  continue;
795  }
796  balance_split = get_trading_split(trans, root, commodity);
797  if (!balance_split)
798  {
799  /* Error already logged */
800  LEAVE("");
801  return;
802  }
803  xaccTransBeginEdit (trans);
804 
805  old_value = xaccSplitGetValue (balance_split);
806  new_value = gnc_numeric_sub (old_value, xaccSplitGetValue(split),
807  gnc_commodity_get_fraction(txn_curr),
809  xaccSplitSetValue (balance_split, new_value);
810 
811  /* Don't change the balance split's amount since the amount
812  is zero in the split we're working on */
813 
814  xaccSplitScrub (balance_split);
815  xaccTransCommitEdit (trans);
816  }
817  }
818 
819  g_list_free(splits_dup);
820 }
821 
828 void
829 xaccTransScrubImbalance (Transaction *trans, Account *root,
830  Account *account)
831 {
832  gnc_numeric imbalance;
833 
834  if (!trans) return;
835 
836  ENTER ("()");
837 
838  /* Must look for orphan splits even if there is no imbalance. */
839  xaccTransScrubSplits (trans);
840 
841  /* Return immediately if things are balanced. */
842  if (xaccTransIsBalanced (trans))
843  {
844  LEAVE ("transaction is balanced");
845  return;
846  }
847 
848  if (! xaccTransUseTradingAccounts (trans))
849  {
850  gnc_transaction_balance_no_trading (trans, root, account);
851  LEAVE ("transaction balanced, no managed trading accounts");
852  return;
853  }
854 
855  xaccTransClearTradingSplits (trans);
856  imbalance = xaccTransGetImbalanceValue (trans);
857  if (! gnc_numeric_zero_p (imbalance))
858  {
859  PINFO ("Value unbalanced transaction");
860 
861  add_balance_split (trans, imbalance, root, account);
862  }
863 
864  gnc_transaction_balance_trading (trans, root);
866  {
867  LEAVE ("()");
868  return;
869  }
870  /* If the transaction is still not balanced, it's probably because there
871  are splits with zero amount and non-zero value. These are usually
872  realized gain/loss splits. Add a reversing split for each of them to
873  balance the value. */
874 
875  gnc_transaction_balance_trading_more_splits (trans, root);
877  PERR("Balancing currencies unbalanced value");
878 
879 }
880 
881 /* ================================================================ */
882 /* The xaccTransFindCommonCurrency () method returns
883  * a gnc_commodity indicating a currency denomination that all
884  * of the splits in this transaction have in common, using the
885  * old/obsolete currency/security fields of the split accounts.
886  */
887 
888 static gnc_commodity *
889 FindCommonExclSCurrency (SplitList *splits,
890  gnc_commodity * ra, gnc_commodity * rb,
891  Split *excl_split)
892 {
893  GList *node;
894 
895  if (!splits) return nullptr;
896 
897  for (node = splits; node; node = node->next)
898  {
899  Split *s = GNC_SPLIT(node->data);
900  gnc_commodity * sa, * sb;
901 
902  if (s == excl_split) continue;
903 
904  g_return_val_if_fail (s->acc, nullptr);
905 
906  sa = DxaccAccountGetCurrency (s->acc);
907  sb = xaccAccountGetCommodity (s->acc);
908 
909  if (ra && rb)
910  {
911  int aa = !gnc_commodity_equiv(ra, sa);
912  int ab = !gnc_commodity_equiv(ra, sb);
913  int ba = !gnc_commodity_equiv(rb, sa);
914  int bb = !gnc_commodity_equiv(rb, sb);
915 
916  if ( (!aa) && bb) rb = nullptr;
917  else if ( (!ab) && ba) rb = nullptr;
918  else if ( (!ba) && ab) ra = nullptr;
919  else if ( (!bb) && aa) ra = nullptr;
920  else if ( aa && bb && ab && ba )
921  {
922  ra = nullptr;
923  rb = nullptr;
924  }
925 
926  if (!ra)
927  {
928  ra = rb;
929  rb = nullptr;
930  }
931  }
932  else if (ra && !rb)
933  {
934  int aa = !gnc_commodity_equiv(ra, sa);
935  int ab = !gnc_commodity_equiv(ra, sb);
936  if ( aa && ab ) ra = nullptr;
937  }
938  else if (!ra && rb)
939  {
940  int aa = !gnc_commodity_equiv(rb, sa);
941  int ab = !gnc_commodity_equiv(rb, sb);
942  ra = ( aa && ab ) ? nullptr : rb;
943  }
944 
945  if ((!ra) && (!rb)) return nullptr;
946  }
947 
948  return (ra);
949 }
950 
951 /* This is the wrapper for those calls (i.e. the older ones) which
952  * don't exclude one split from the splitlist when looking for a
953  * common currency.
954  */
955 static gnc_commodity *
956 FindCommonCurrency (GList *splits, gnc_commodity * ra, gnc_commodity * rb)
957 {
958  return FindCommonExclSCurrency(splits, ra, rb, nullptr);
959 }
960 
961 static gnc_commodity *
962 xaccTransFindOldCommonCurrency (Transaction *trans, QofBook *book)
963 {
964  gnc_commodity *ra, *rb, *retval;
965  Split *split;
966 
967  if (!trans) return nullptr;
968 
969  if (trans->splits == nullptr) return nullptr;
970 
971  g_return_val_if_fail (book, nullptr);
972 
973  split = GNC_SPLIT(trans->splits->data);
974 
975  if (!split || nullptr == split->acc) return nullptr;
976 
977  ra = DxaccAccountGetCurrency (split->acc);
978  rb = xaccAccountGetCommodity (split->acc);
979 
980  retval = FindCommonCurrency (trans->splits, ra, rb);
981 
982  if (retval && !gnc_commodity_is_currency(retval))
983  retval = nullptr;
984 
985  return retval;
986 }
987 
988 /* Test the currency of the splits and find the most common and return
989  * it, or nullptr if there is no currency more common than the
990  * others -- or none at all.
991  */
992 typedef struct
993 {
994  gnc_commodity *commodity;
995  unsigned int count;
997 
998 static gint
999 commodity_equal (gconstpointer a, gconstpointer b)
1000 {
1001  CommodityCount *cc = (CommodityCount*)a;
1002  gnc_commodity *com = (gnc_commodity*)b;
1003  if ( cc == nullptr || cc->commodity == nullptr ||
1004  !GNC_IS_COMMODITY( cc->commodity ) ) return -1;
1005  if ( com == nullptr || !GNC_IS_COMMODITY( com ) ) return 1;
1006  if ( gnc_commodity_equal(cc->commodity, com) )
1007  return 0;
1008  return 1;
1009 }
1010 
1011 static gint
1012 commodity_compare( gconstpointer a, gconstpointer b)
1013 {
1014  CommodityCount *ca = (CommodityCount*)a, *cb = (CommodityCount*)b;
1015  if (ca == nullptr || ca->commodity == nullptr ||
1016  !GNC_IS_COMMODITY( ca->commodity ) )
1017  {
1018  if (cb == nullptr || cb->commodity == nullptr ||
1019  !GNC_IS_COMMODITY( cb->commodity ) )
1020  return 0;
1021  return -1;
1022  }
1023  if (cb == nullptr || cb->commodity == nullptr ||
1024  !GNC_IS_COMMODITY( cb->commodity ) )
1025  return 1;
1026  if (ca->count == cb->count)
1027  return 0;
1028  return ca->count > cb->count ? 1 : -1;
1029 }
1030 
1031 /* Find the commodities in the account of each of the splits of a
1032  * transaction, and rank them by how many splits in which they
1033  * occur. Commodities which are currencies count more than those which
1034  * aren't, because for simple buy and sell transactions it makes
1035  * slightly more sense for the transaction commodity to be the
1036  * currency -- to the extent that it makes sense for a transaction to
1037  * have a currency at all. jralls, 2010-11-02 */
1038 
1039 static gnc_commodity *
1040 xaccTransFindCommonCurrency (Transaction *trans, QofBook *book)
1041 {
1042  gnc_commodity *com_scratch;
1043  GList *node = nullptr;
1044  GSList *comlist = nullptr, *found = nullptr;
1045 
1046  if (!trans) return nullptr;
1047 
1048  if (trans->splits == nullptr) return nullptr;
1049 
1050  g_return_val_if_fail (book, nullptr);
1051 
1052  /* Find the most commonly used currency among the splits. If a given split
1053  is in a non-currency commodity, then look for an ancestor account in a
1054  currency, but prefer currencies used directly in splits. Ignore trading
1055  account splits in this whole process, they don't add any value to this algorithm. */
1056  for (node = trans->splits; node; node = node->next)
1057  {
1058  Split *s = GNC_SPLIT(node->data);
1059  unsigned int curr_weight;
1060 
1061  if (s == nullptr || s->acc == nullptr) continue;
1062  if (xaccAccountGetType(s->acc) == ACCT_TYPE_TRADING) continue;
1063  com_scratch = xaccAccountGetCommodity(s->acc);
1064  if (com_scratch && gnc_commodity_is_currency(com_scratch))
1065  {
1066  curr_weight = 3;
1067  }
1068  else
1069  {
1070  com_scratch = gnc_account_get_currency_or_parent(s->acc);
1071  if (com_scratch == nullptr) continue;
1072  curr_weight = 1;
1073  }
1074  if ( comlist )
1075  {
1076  found = g_slist_find_custom(comlist, com_scratch, commodity_equal);
1077  }
1078  if (comlist == nullptr || found == nullptr)
1079  {
1080  CommodityCount *count = g_slice_new0(CommodityCount);
1081  count->commodity = com_scratch;
1082  count->count = curr_weight;
1083  comlist = g_slist_append(comlist, count);
1084  }
1085  else
1086  {
1087  CommodityCount *count = (CommodityCount*)(found->data);
1088  count->count += curr_weight;
1089  }
1090  }
1091  found = g_slist_sort( comlist, commodity_compare);
1092 
1093  if ( found && found->data && (((CommodityCount*)(found->data))->commodity != nullptr))
1094  {
1095  return ((CommodityCount*)(found->data))->commodity;
1096  }
1097  /* We didn't find a currency in the current account structure, so try
1098  * an old one. */
1099  return xaccTransFindOldCommonCurrency( trans, book );
1100 }
1101 
1102 /* ================================================================ */
1103 
1104 void
1105 xaccTransScrubCurrency (Transaction *trans)
1106 {
1107  SplitList *node;
1108  gnc_commodity *currency;
1109 
1110  if (!trans) return;
1111 
1112  /* If there are any orphaned splits in a transaction, then the
1113  * this routine will fail. Therefore, we want to make sure that
1114  * there are no orphans (splits without parent account).
1115  */
1116  xaccTransScrubOrphans (trans);
1117 
1118  currency = xaccTransGetCurrency (trans);
1119  if (currency && gnc_commodity_is_currency(currency)) return;
1120 
1121  currency = xaccTransFindCommonCurrency (trans, qof_instance_get_book(trans));
1122  if (currency)
1123  {
1124  xaccTransBeginEdit (trans);
1125  xaccTransSetCurrency (trans, currency);
1126  xaccTransCommitEdit (trans);
1127  }
1128  else
1129  {
1130  if (nullptr == trans->splits)
1131  {
1132  PWARN ("Transaction \"%s\" has no splits in it!", trans->description);
1133  }
1134  else
1135  {
1136  SplitList *node;
1137  char guid_str[GUID_ENCODING_LENGTH + 1];
1138  guid_to_string_buff(xaccTransGetGUID(trans), guid_str);
1139  PWARN ("no common transaction currency found for trans=\"%s\" (%s);",
1140  trans->description, guid_str);
1141 
1142  for (node = trans->splits; node; node = node->next)
1143  {
1144  Split *split = GNC_SPLIT(node->data);
1145  if (nullptr == split->acc)
1146  {
1147  PWARN (" split=\"%s\" is not in any account!", split->memo);
1148  }
1149  else
1150  {
1151  gnc_commodity *currency = xaccAccountGetCommodity(split->acc);
1152  PWARN ("setting to split=\"%s\" account=\"%s\" commodity=\"%s\"",
1153  split->memo, xaccAccountGetName(split->acc),
1154  gnc_commodity_get_mnemonic(currency));
1155 
1156  xaccTransBeginEdit (trans);
1157  xaccTransSetCurrency (trans, currency);
1158  xaccTransCommitEdit (trans);
1159  return;
1160  }
1161  }
1162  }
1163  return;
1164  }
1165 
1166  for (node = trans->splits; node; node = node->next)
1167  {
1168  Split *sp = GNC_SPLIT(node->data);
1169 
1171  xaccSplitGetValue (sp)))
1172  {
1173  gnc_commodity *acc_currency;
1174 
1175  acc_currency = sp->acc ? xaccAccountGetCommodity(sp->acc) : nullptr;
1176  if (acc_currency == currency)
1177  {
1178  /* This Split needs fixing: The transaction-currency equals
1179  * the account-currency/commodity, but the amount/values are
1180  * inequal i.e. they still correspond to the security
1181  * (amount) and the currency (value). In the new model, the
1182  * value is the amount in the account-commodity -- so it
1183  * needs to be set to equal the amount (since the
1184  * account-currency doesn't exist anymore).
1185  *
1186  * Note: Nevertheless we lose some information here. Namely,
1187  * the information that the 'amount' in 'account-old-security'
1188  * was worth 'value' in 'account-old-currency'. Maybe it would
1189  * be better to store that information in the price database?
1190  * But then, for old currency transactions there is still the
1191  * 'other' transaction, which is going to keep that
1192  * information. So I don't bother with that here. -- cstim,
1193  * 2002/11/20. */
1194 
1195  PWARN ("Adjusted split with mismatched values, desc=\"%s\" memo=\"%s\""
1196  " old amount %s %s, new amount %s",
1197  trans->description, sp->memo,
1199  gnc_commodity_get_mnemonic (currency),
1201  xaccTransBeginEdit (trans);
1203  xaccTransCommitEdit (trans);
1204  }
1205  /*else
1206  {
1207  PINFO ("Ok: Split '%s' Amount %s %s, value %s %s",
1208  xaccSplitGetMemo (sp),
1209  gnc_num_dbg_to_string (amount),
1210  gnc_commodity_get_mnemonic (currency),
1211  gnc_num_dbg_to_string (value),
1212  gnc_commodity_get_mnemonic (acc_currency));
1213  }*/
1214  }
1215  }
1216 
1217 }
1218 
1219 /* ================================================================ */
1220 
1221 void
1223 {
1224  gnc_commodity *commodity;
1225 
1226  if (!account) return;
1227  if (xaccAccountGetType(account) == ACCT_TYPE_ROOT) return;
1228 
1229  commodity = xaccAccountGetCommodity (account);
1230  if (commodity) return;
1231 
1232  /* Use the 'obsolete' routines to try to figure out what the
1233  * account commodity should have been. */
1234  commodity = xaccAccountGetCommodity (account);
1235  if (commodity)
1236  {
1237  xaccAccountSetCommodity (account, commodity);
1238  return;
1239  }
1240 
1241  commodity = DxaccAccountGetCurrency (account);
1242  if (commodity)
1243  {
1244  xaccAccountSetCommodity (account, commodity);
1245  return;
1246  }
1247 
1248  PERR ("Account \"%s\" does not have a commodity!",
1249  xaccAccountGetName(account));
1250 }
1251 
1252 /* ================================================================ */
1253 
1254 /* EFFECTIVE FRIEND FUNCTION declared in qofinstance-p.h */
1255 extern void qof_instance_set_dirty (QofInstance*);
1256 
1257 static void
1258 xaccAccountDeleteOldData (Account *account)
1259 {
1260  if (!account) return;
1261  xaccAccountBeginEdit (account);
1262  qof_instance_set_kvp (QOF_INSTANCE (account), nullptr, 1, "old-currency");
1263  qof_instance_set_kvp (QOF_INSTANCE (account), nullptr, 1, "old-security");
1264  qof_instance_set_kvp (QOF_INSTANCE (account), nullptr, 1, "old-currency-scu");
1265  qof_instance_set_kvp (QOF_INSTANCE (account), nullptr, 1, "old-security-scu");
1266  qof_instance_set_dirty (QOF_INSTANCE (account));
1267  xaccAccountCommitEdit (account);
1268 }
1269 
1270 static int
1271 scrub_trans_currency_helper (Transaction *t, gpointer data)
1272 {
1274  return 0;
1275 }
1276 
1277 static void
1278 scrub_account_commodity_helper (Account *account, gpointer data)
1279 {
1280  scrub_depth++;
1281  xaccAccountScrubCommodity (account);
1282  xaccAccountDeleteOldData (account);
1283  scrub_depth--;
1284 }
1285 
1286 void
1288 {
1289  if (!acc) return;
1290  scrub_depth++;
1291  xaccAccountTreeForEachTransaction (acc, scrub_trans_currency_helper, nullptr);
1292 
1293  scrub_account_commodity_helper (acc, nullptr);
1294  gnc_account_foreach_descendant (acc, scrub_account_commodity_helper, nullptr);
1295  scrub_depth--;
1296 }
1297 
1298 /* ================================================================ */
1299 
1300 static gboolean
1301 check_quote_source (gnc_commodity *com, gpointer data)
1302 {
1303  gboolean *commodity_has_quote_src = (gboolean *)data;
1304  if (com && !gnc_commodity_is_iso(com))
1305  *commodity_has_quote_src |= gnc_commodity_get_quote_flag(com);
1306  return TRUE;
1307 }
1308 
1309 static void
1310 move_quote_source (Account *account, gpointer data)
1311 {
1312  gnc_commodity *com;
1313  gnc_quote_source *quote_source;
1314  gboolean new_style = GPOINTER_TO_INT(data);
1315  const char *source, *tz;
1316 
1317  com = xaccAccountGetCommodity(account);
1318  if (!com)
1319  return;
1320 
1321  if (!new_style)
1322  {
1323  source = dxaccAccountGetPriceSrc(account);
1324  if (!source || !*source)
1325  return;
1326  tz = dxaccAccountGetQuoteTZ(account);
1327 
1328  PINFO("to %8s from %s", gnc_commodity_get_mnemonic(com),
1329  xaccAccountGetName(account));
1330  gnc_commodity_set_quote_flag(com, TRUE);
1331  quote_source = gnc_quote_source_lookup_by_internal(source);
1332  if (!quote_source)
1333  quote_source = gnc_quote_source_add_new(source, FALSE);
1334  gnc_commodity_set_quote_source(com, quote_source);
1335  gnc_commodity_set_quote_tz(com, tz);
1336  }
1337 
1338  dxaccAccountSetPriceSrc(account, nullptr);
1339  dxaccAccountSetQuoteTZ(account, nullptr);
1340  return;
1341 }
1342 
1343 
1344 void
1345 xaccAccountTreeScrubQuoteSources (Account *root, gnc_commodity_table *table)
1346 {
1347  gboolean new_style = FALSE;
1348  ENTER(" ");
1349 
1350  if (!root || !table)
1351  {
1352  LEAVE("Oops");
1353  return;
1354  }
1355  scrub_depth++;
1356  gnc_commodity_table_foreach_commodity (table, check_quote_source, &new_style);
1357 
1358  move_quote_source(root, GINT_TO_POINTER(new_style));
1359  gnc_account_foreach_descendant (root, move_quote_source,
1360  GINT_TO_POINTER(new_style));
1361  LEAVE("Migration done");
1362  scrub_depth--;
1363 }
1364 
1365 /* ================================================================ */
1366 
1367 void
1369 {
1370  GValue v = G_VALUE_INIT;
1371  gchar *str2;
1372 
1373  if (!account) return;
1374  scrub_depth++;
1375 
1376  qof_instance_get_kvp (QOF_INSTANCE (account), &v, 1, "notes");
1377  if (G_VALUE_HOLDS_STRING (&v))
1378  {
1379  str2 = g_strstrip(g_value_dup_string(&v));
1380  if (strlen(str2) == 0)
1381  qof_instance_slot_delete (QOF_INSTANCE (account), "notes");
1382  g_free(str2);
1383  }
1384 
1385  qof_instance_get_kvp (QOF_INSTANCE (account), &v, 1, "placeholder");
1386  if ((G_VALUE_HOLDS_STRING (&v) &&
1387  strcmp(g_value_get_string (&v), "false") == 0) ||
1388  (G_VALUE_HOLDS_BOOLEAN (&v) && ! g_value_get_boolean (&v)))
1389  qof_instance_slot_delete (QOF_INSTANCE (account), "placeholder");
1390 
1391  g_value_unset (&v);
1392  qof_instance_slot_delete_if_empty (QOF_INSTANCE (account), "hbci");
1393  scrub_depth--;
1394 }
1395 
1396 /* ================================================================ */
1397 
1398 void
1400 {
1401  GValue value_s = G_VALUE_INIT;
1402  gboolean already_scrubbed;
1403 
1404  // get the run-once value
1405  qof_instance_get_kvp (QOF_INSTANCE (book), &value_s, 1, "remove-color-not-set-slots");
1406 
1407  already_scrubbed = (G_VALUE_HOLDS_STRING (&value_s) &&
1408  !g_strcmp0 (g_value_get_string (&value_s), "true"));
1409  g_value_unset (&value_s);
1410 
1411  if (already_scrubbed)
1412  return;
1413  else
1414  {
1415  GValue value_b = G_VALUE_INIT;
1416  Account *root = gnc_book_get_root_account (book);
1417  GList *accts = gnc_account_get_descendants_sorted (root);
1418  GList *ptr;
1419 
1420  for (ptr = accts; ptr; ptr = g_list_next (ptr))
1421  {
1422  auto acct = GNC_ACCOUNT(ptr->data);
1423  auto color = xaccAccountGetColor (acct);
1424 
1425  if (g_strcmp0 (color, "Not Set") == 0)
1426  xaccAccountSetColor (acct, "");
1427  }
1428  g_list_free (accts);
1429 
1430  g_value_init (&value_b, G_TYPE_BOOLEAN);
1431  g_value_set_boolean (&value_b, TRUE);
1432 
1433  // set the run-once value
1434  qof_instance_set_kvp (QOF_INSTANCE (book), &value_b, 1, "remove-color-not-set-slots");
1435  g_value_unset (&value_b);
1436  }
1437 }
1438 
1439 /* ================================================================ */
1440 
1441 static Account*
1442 construct_account (Account *root, gnc_commodity *currency, const char *accname,
1443  GNCAccountType acctype, gboolean placeholder)
1444 {
1445  gnc_commodity* root_currency = find_root_currency ();
1446  Account *acc = xaccMallocAccount(gnc_account_get_book (root));
1447  xaccAccountBeginEdit (acc);
1448  if (accname && *accname)
1449  xaccAccountSetName (acc, accname);
1450  if (currency || root_currency)
1451  xaccAccountSetCommodity (acc, currency ? currency : root_currency);
1452  xaccAccountSetType (acc, acctype);
1453  xaccAccountSetPlaceholder (acc, placeholder);
1454 
1455  /* Hang the account off the root. */
1456  gnc_account_append_child (root, acc);
1457  xaccAccountCommitEdit (acc);
1458  return acc;
1459 }
1460 
1461 static Account*
1462 find_root_currency_account_in_list (GList *acc_list)
1463 {
1464  gnc_commodity* root_currency = find_root_currency();
1465  for (GList *node = acc_list; node; node = g_list_next (node))
1466  {
1467  Account *acc = GNC_ACCOUNT (node->data);
1468  gnc_commodity *acc_commodity = nullptr;
1469  if (G_UNLIKELY (!acc)) continue;
1470  acc_commodity = xaccAccountGetCommodity(acc);
1471  if (gnc_commodity_equiv (acc_commodity, root_currency))
1472  return acc;
1473  }
1474 
1475  return nullptr;
1476 }
1477 
1478 static Account*
1479 find_account_matching_name_in_list (GList *acc_list, const char* accname)
1480 {
1481  for (GList* node = acc_list; node; node = g_list_next(node))
1482  {
1483  Account *acc = GNC_ACCOUNT (node->data);
1484  if (G_UNLIKELY (!acc)) continue;
1485  if (g_strcmp0 (accname, xaccAccountGetName (acc)) == 0)
1486  return acc;
1487  }
1488  return nullptr;
1489 }
1490 
1491 Account *
1492 xaccScrubUtilityGetOrMakeAccount (Account *root, gnc_commodity * currency,
1493  const char *accname, GNCAccountType acctype,
1494  gboolean placeholder, gboolean checkname)
1495 {
1496  GList* acc_list;
1497  Account *acc = nullptr;
1498 
1499  g_return_val_if_fail (root, nullptr);
1500 
1501  acc_list =
1503  checkname ? accname : nullptr,
1504  acctype, currency);
1505 
1506  if (!acc_list)
1507  return construct_account (root, currency, accname,
1508  acctype, placeholder);
1509 
1510  if (g_list_next(acc_list))
1511  {
1512  if (!currency)
1513  acc = find_root_currency_account_in_list (acc_list);
1514 
1515  if (!acc)
1516  acc = find_account_matching_name_in_list (acc_list, accname);
1517  }
1518 
1519  if (!acc)
1520  acc = GNC_ACCOUNT (acc_list->data);
1521 
1522  g_list_free (acc_list);
1523  return acc;
1524 }
1525 
1526 void
1527 xaccTransScrubPostedDate (Transaction *trans)
1528 {
1529  time64 orig = xaccTransGetDate(trans);
1530  if(orig == INT64_MAX)
1531  {
1532  GDate date = xaccTransGetDatePostedGDate(trans);
1533  time64 time = gdate_to_time64(date);
1534  if(time != INT64_MAX)
1535  {
1536  // xaccTransSetDatePostedSecs handles committing the change.
1537  xaccTransSetDatePostedSecs(trans, time);
1538  }
1539  }
1540 }
1541 
1542 /* ==================== END OF FILE ==================== */
void xaccAccountSetType(Account *acc, GNCAccountType tip)
Set the account&#39;s type.
Definition: Account.cpp:2406
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
int xaccAccountTreeForEachTransaction(Account *acc, TransactionCallback proc, void *data)
Traverse all of the transactions in the given account group.
This is the private header for the account structure.
void xaccAccountScrubKvp(Account *account)
Removes empty "notes", "placeholder", and "hbci" KVP slots from Accounts.
Definition: Scrub.cpp:1368
gboolean gnc_commodity_table_foreach_commodity(const gnc_commodity_table *table, gboolean(*f)(gnc_commodity *cm, gpointer user_data), gpointer user_data)
Call a function once for each commodity in the commodity table.
gboolean gnc_numeric_equal(gnc_numeric a, gnc_numeric b)
Equivalence predicate: Returns TRUE (1) if a and b represent the same number.
void xaccTransScrubCurrency(Transaction *trans)
The xaccTransScrubCurrency method fixes transactions without a common_currency by looking for the mos...
Definition: Scrub.cpp:1105
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...
gchar * gnc_num_dbg_to_string(gnc_numeric n)
Convert to string.
int gnc_commodity_get_fraction(const gnc_commodity *cm)
Retrieve the fraction for the specified commodity.
void(* QofPercentageFunc)(const char *message, double percent)
The qof_session_load() method causes the QofBook to be made ready to to use with this URL/datastore...
Definition: qofsession.h:199
void gnc_account_append_child(Account *new_parent, Account *child)
This function will remove from the child account any pre-existing parent relationship, and will then add the account as a child of the new parent.
Definition: Account.cpp:2776
time64 xaccTransGetDate(const Transaction *trans)
Retrieve the posted date of the transaction.
void qof_instance_set_kvp(QofInstance *, GValue const *value, unsigned count,...)
Sets a KVP slot to a value from a GValue.
GList * gnc_account_get_descendants_sorted(const Account *account)
This function returns a GList containing all the descendants of the specified account, sorted at each level.
Definition: Account.cpp:2997
gboolean xaccTransUseTradingAccounts(const Transaction *trans)
Determine whether this transaction should use commodity trading accounts.
const char * gnc_commodity_get_mnemonic(const gnc_commodity *cm)
Retrieve the mnemonic for the specified commodity.
void xaccAccountTreeScrubCommodities(Account *acc)
The xaccAccountTreeScrubCommodities will scrub the currency/commodity of all accounts & transactions ...
Definition: Scrub.cpp:1287
gnc_commodity * DxaccAccountGetCurrency(const Account *acc)
Definition: Account.cpp:3375
QofBook * qof_instance_get_book(gconstpointer inst)
Return the book pointer.
gnc_quote_source * gnc_quote_source_add_new(const char *source_name, gboolean supported)
Create a new quote source.
gboolean gnc_get_ongoing_scrub(void)
The gnc_get_ongoing_scrub () method returns TRUE if a scrub operation is ongoing. ...
Definition: Scrub.cpp:87
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
#define PINFO(format, args...)
Print an informational note.
Definition: qoflog.h:256
GNCAccountType xaccAccountGetType(const Account *acc)
Returns the account&#39;s account type.
Definition: Account.cpp:3212
gboolean xaccSplitDestroy(Split *split)
Destructor.
Definition: Split.cpp:1471
void xaccAccountScrubCommodity(Account *account)
The xaccAccountScrubCommodity method fixed accounts without a commodity by using the old account curr...
Definition: Scrub.cpp:1222
gboolean gnc_commodity_get_quote_flag(const gnc_commodity *cm)
Retrieve the automatic price quote flag for the specified commodity.
int xaccAccountGetCommoditySCU(const Account *acc)
Return the SCU for the account.
Definition: Account.cpp:2673
STRUCTS.
void gnc_commodity_set_quote_tz(gnc_commodity *cm, const char *tz)
Set the automatic price quote timezone for the specified commodity.
#define DEBUG(format, args...)
Print a debugging message.
Definition: qoflog.h:264
gboolean gnc_commodity_equal(const gnc_commodity *a, const gnc_commodity *b)
This routine returns TRUE if the two commodities are equal.
gnc_numeric gnc_numeric_add(gnc_numeric a, gnc_numeric b, gint64 denom, gint how)
Return a+b.
gboolean gnc_numeric_zero_p(gnc_numeric a)
Returns 1 if the given gnc_numeric is 0 (zero), else returns 0.
Transaction * xaccSplitGetParent(const Split *split)
Returns the parent transaction of the split.
const char * gnc_commodity_get_namespace(const gnc_commodity *cm)
Retrieve the namespace for the specified commodity.
gchar * guid_to_string_buff(const GncGUID *guid, gchar *str)
The guid_to_string_buff() routine puts a null-terminated string encoding of the id into the memory po...
Definition: guid.cpp:173
Use any denominator which gives an exactly correct ratio of numerator to denominator.
Definition: gnc-numeric.h:188
void gnc_commodity_set_quote_flag(gnc_commodity *cm, const gboolean flag)
Set the automatic price quote flag for the specified commodity.
gboolean xaccTransIsBalanced(const Transaction *trans)
Returns true if the transaction is balanced according to the rules currently in effect.
void xaccTransScrubPostedDate(Transaction *trans)
Changes Transaction date_posted timestamps from 00:00 local to 11:00 UTC.
Definition: Scrub.cpp:1527
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244
#define ENTER(format, args...)
Print a function entry debugging message.
Definition: qoflog.h:272
void qof_instance_get_kvp(QofInstance *, GValue *value, unsigned count,...)
Retrieves the contents of a KVP slot into a provided GValue.
void xaccTransSetCurrency(Transaction *trans, gnc_commodity *curr)
Set a new currency on a transaction.
Account used to record multiple commodity transactions.
Definition: Account.h:155
#define PWARN(format, args...)
Log a warning.
Definition: qoflog.h:250
const char * xaccAccountGetColor(const Account *acc)
Get the account&#39;s color.
Definition: Account.cpp:3303
void gnc_set_abort_scrub(gboolean abort)
The gnc_set_abort_scrub () method causes a currently running scrub operation to stop, if abort is TRUE; gnc_set_abort_scrub(FALSE) must be called before any scrubbing operation.
Definition: Scrub.cpp:75
convert single-entry accounts to clean double-entry
char * qof_print_date(time64 secs)
Convenience; calls through to qof_print_date_dmy_buff().
Definition: gnc-date.cpp:609
void gnc_commodity_set_quote_source(gnc_commodity *cm, gnc_quote_source *src)
Set the automatic price quote source for the specified commodity.
GList SplitList
GList of Split.
Definition: gnc-engine.h:207
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
QofBook * qof_session_get_book(const QofSession *session)
Returns the QofBook of this session.
Definition: qofsession.cpp:574
Account handling public routines.
void xaccAccountSetPlaceholder(Account *acc, gboolean val)
Set the "placeholder" flag for an account.
Definition: Account.cpp:4185
void xaccAccountSetColor(Account *acc, const char *str)
Set the account&#39;s Color.
Definition: Account.cpp:2530
gnc_numeric xaccTransGetImbalanceValue(const Transaction *trans)
The xaccTransGetImbalanceValue() method returns the total value of the transaction.
Income accounts are used to denote income.
Definition: Account.h:140
Account public routines (C++ api)
void xaccAccountTreeScrubOrphans(Account *acc, QofPercentageFunc percentagefunc)
The xaccAccountTreeScrubOrphans() method performs this scrub for the indicated account and its childr...
Definition: Scrub.cpp:173
void dxaccAccountSetPriceSrc(Account *acc, const char *src)
Set a string that identifies the Finance::Quote backend that should be used to retrieve online prices...
Definition: Account.cpp:5194
#define GUID_ENCODING_LENGTH
Number of characters needed to encode a guid as a string not including the null terminator.
Definition: guid.h:84
void gnc_monetary_list_free(MonetaryList *list)
Free a MonetaryList and all the monetaries it points to.
void xaccTransScrubImbalance(Transaction *trans, Account *root, Account *account)
Correct transaction imbalances.
Definition: Scrub.cpp:829
const char * dxaccAccountGetQuoteTZ(const Account *acc)
Get the timezone to be used when interpreting the results from a given Finance::Quote backend...
Definition: Account.cpp:5236
void xaccSplitScrub(Split *split)
The xaccSplitScrub method ensures that if this split has the same commodity and currency, then it will have the same amount and value.
Definition: Scrub.cpp:424
void xaccTransScrubSplits(Transaction *trans)
The xacc*ScrubSplits() calls xaccSplitScrub() on each split in the respective structure: transaction...
Definition: Scrub.cpp:395
The bank account type denotes a savings or checking account held at a bank.
Definition: Account.h:107
void xaccAccountScrubOrphans(Account *acc, QofPercentageFunc percentagefunc)
The xaccAccountScrubOrphans() method performs this scrub only for the indicated account, and not for any of its children.
Definition: Scrub.cpp:167
time64 gdate_to_time64(GDate d)
Turns a GDate into a time64, returning the first second of the day.
Definition: gnc-date.cpp:1253
void xaccTransScrubOrphans(Transaction *trans)
The xaccTransScrubOrphans() method scrubs only the splits in the given transaction.
Definition: Scrub.cpp:179
#define xaccTransGetBook(X)
Definition: Transaction.h:786
void xaccTransCommitEdit(Transaction *trans)
The xaccTransCommitEdit() method indicates that the changes to the transaction and its splits are com...
void xaccTransBeginEdit(Transaction *trans)
The xaccTransBeginEdit() method must be called before any changes are made to a transaction or any of...
void dxaccAccountSetQuoteTZ(Account *acc, const char *tz)
Set the timezone to be used when interpreting the results from a given Finance::Quote backend...
Definition: Account.cpp:5225
GNCAccountType
The account types are used to determine how the transaction data in the account is displayed...
Definition: Account.h:101
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:3404
Split * xaccMallocSplit(QofBook *book)
Constructor.
Definition: gmock-Split.cpp:37
#define xaccTransGetGUID(X)
Definition: Transaction.h:788
gnc_numeric gnc_numeric_sub(gnc_numeric a, gnc_numeric b, gint64 denom, gint how)
Return a-b.
gnc_quote_source * gnc_quote_source_lookup_by_internal(const char *name)
Given the internal (gnucash or F::Q) name of a quote source, find the data structure identified by th...
void xaccTransSetDatePostedSecs(Transaction *trans, time64 secs)
The xaccTransSetDatePostedSecs() method will modify the posted date of the transaction, specified by a time64 (see ctime(3)).
const char * dxaccAccountGetPriceSrc(const Account *acc)
Get a string that identifies the Finance::Quote backend that should be used to retrieve online prices...
Definition: Account.cpp:5206
GList * gnc_account_get_children(const Account *account)
This routine returns a GList of all children accounts of the specified account.
Definition: Account.cpp:2906
gnc_numeric xaccSplitGetValue(const Split *split)
Returns the value of this split in the transaction&#39;s commodity.
Definition: gmock-Split.cpp:84
void xaccAccountBeginEdit(Account *acc)
The xaccAccountBeginEdit() subroutine is the first phase of a two-phase-commit wrapper for account up...
Definition: Account.cpp:1479
Account * xaccSplitGetAccount(const Split *split)
Returns the account of this split, which was set through xaccAccountInsertSplit().
Definition: gmock-Split.cpp:53
gnc_commodity * xaccAccountGetCommodity(const Account *acc)
Get the account&#39;s commodity.
Definition: Account.cpp:3397
gnc_commodity * xaccTransGetCurrency(const Transaction *trans)
Returns the valuation commodity of this transaction.
MonetaryList * xaccTransGetImbalance(const Transaction *trans)
The xaccTransGetImbalance method returns a list giving the value of the transaction in each currency ...
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition: qoflog.h:282
Round to the nearest integer, rounding away from zero when there are two equidistant nearest integers...
Definition: gnc-numeric.h:165
Account * xaccMallocAccount(QofBook *book)
Constructor.
Definition: Account.cpp:1275
GNCNumericErrorCode gnc_numeric_check(gnc_numeric in)
Check for error signal in value.
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
Definition: gnc-date.h:87
Account * gnc_account_get_root(Account *acc)
This routine returns the root account of the account tree that the specified account belongs to...
Definition: Account.cpp:2882
void xaccAccountScrubColorNotSet(QofBook *book)
Remove color slots that have a "Not Set" value, since 2.4.0, fixed in 3.4 This should only be run onc...
Definition: Scrub.cpp:1399
const char * xaccAccountGetName(const Account *acc)
Get the account&#39;s name.
Definition: Account.cpp:3234
void xaccAccountTreeScrubQuoteSources(Account *root, gnc_commodity_table *table)
This routine will migrate the information about price quote sources from the account data structures ...
Definition: Scrub.cpp:1345
int gnc_numeric_same(gnc_numeric a, gnc_numeric b, gint64 denom, gint how)
Equivalence predicate: Convert both a and b to denom using the specified DENOM and method HOW...
GDate xaccTransGetDatePostedGDate(const Transaction *trans)
Retrieve the posted date of the transaction.
#define GNC_DENOM_AUTO
Values that can be passed as the &#39;denom&#39; argument.
Definition: gnc-numeric.h:245
API for Transactions and Splits (journal entries)
void xaccAccountCommitEdit(Account *acc)
ThexaccAccountCommitEdit() subroutine is the second phase of a two-phase-commit wrapper for account u...
Definition: Account.cpp:1520
void xaccAccountSetName(Account *acc, const char *str)
Set the account&#39;s name.
Definition: Account.cpp:2427
The hidden root account of an account tree.
Definition: Account.h:153
SplitList * xaccTransGetSplitList(const Transaction *trans)
The xaccTransGetSplitList() method returns a GList of the splits in a transaction.
Commodity handling public routines.
gboolean gnc_commodity_equiv(const gnc_commodity *a, const gnc_commodity *b)
This routine returns TRUE if the two commodities are equivalent.
gboolean gnc_commodity_is_iso(const gnc_commodity *cm)
Checks to see if the specified commodity is an ISO 4217 recognized currency.
void xaccAccountSetCommodity(Account *acc, gnc_commodity *com)
Set the account&#39;s commodity.
Definition: Account.cpp:2606
gnc_numeric xaccSplitGetAmount(const Split *split)
Returns the amount of the split in the account&#39;s commodity.
Definition: gmock-Split.cpp:69
GList * gnc_account_lookup_by_type_and_commodity(Account *root, const char *name, GNCAccountType acctype, gnc_commodity *commodity)
Find a direct child account matching name, GNCAccountType, and/or commodity.
Definition: Account.cpp:3133