GnuCash  5.6-150-g038405b370+
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  xaccDisableDataScrubbing();
570 
571  xaccTransBeginEdit (trans);
572  xaccSplitSetParent(balance_split, trans);
573  xaccSplitSetAccount(balance_split, account);
574  xaccTransCommitEdit (trans);
575  xaccEnableDataScrubbing();
576  }
577 
578  return balance_split;
579 }
580 
581 static void
582 add_balance_split (Transaction *trans, gnc_numeric imbalance,
583  Account *root, Account *account)
584 {
585  const gnc_commodity *commodity;
586  gnc_numeric old_value, new_value;
587  Split *balance_split;
588  gnc_commodity *currency = xaccTransGetCurrency (trans);
589 
590  balance_split = get_balance_split(trans, root, account, currency);
591  if (!balance_split)
592  {
593  /* Error already logged */
594  LEAVE("");
595  return;
596  }
597  account = xaccSplitGetAccount(balance_split);
598 
599  xaccTransBeginEdit (trans);
600 
601  old_value = xaccSplitGetValue (balance_split);
602 
603  /* Note: We have to round for the commodity's fraction, NOT any
604  * already existing denominator (bug #104343), because either one
605  * of the denominators might already be reduced. */
606  new_value = gnc_numeric_sub (old_value, imbalance,
607  gnc_commodity_get_fraction(currency),
609 
610  xaccSplitSetValue (balance_split, new_value);
611 
612  commodity = xaccAccountGetCommodity (account);
613  if (gnc_commodity_equiv (currency, commodity))
614  {
615  xaccSplitSetAmount (balance_split, new_value);
616  }
617 
618  xaccSplitScrub (balance_split);
619  xaccTransCommitEdit (trans);
620 }
621 
622 /* Balance a transaction without trading accounts. */
623 static void
624 gnc_transaction_balance_no_trading (Transaction *trans, Account *root,
625  Account *account)
626 {
627  gnc_numeric imbalance = xaccTransGetImbalanceValue (trans);
628 
629  /* Make the value sum to zero */
630  if (! gnc_numeric_zero_p (imbalance))
631  {
632  PINFO ("Value unbalanced transaction");
633 
634  add_balance_split (trans, imbalance, root, account);
635  }
636 
637 }
638 
639 static gnc_numeric
640 gnc_transaction_get_commodity_imbalance (Transaction *trans,
641  gnc_commodity *commodity)
642 {
643  /* Find the value imbalance in this commodity */
644  gnc_numeric val_imbalance = gnc_numeric_zero();
645  GList *splits = nullptr;
646  for (splits = trans->splits; splits; splits = splits->next)
647  {
648  Split *split = GNC_SPLIT(splits->data);
649  gnc_commodity *split_commodity =
651  if (xaccTransStillHasSplit (trans, split) &&
652  gnc_commodity_equal (commodity, split_commodity))
653  val_imbalance = gnc_numeric_add (val_imbalance,
654  xaccSplitGetValue (split),
657  }
658  return val_imbalance;
659 }
660 
661 /* GFunc wrapper for xaccSplitDestroy */
662 static void
663 destroy_split (void* ptr)
664 {
665  Split *split = GNC_SPLIT (ptr);
666  if (split)
667  xaccSplitDestroy (split);
668 }
669 
670 /* Balancing transactions with trading accounts works best when
671  * starting with no trading splits.
672  */
673 static void
674 xaccTransClearTradingSplits (Transaction *trans)
675 {
676  GList *trading_splits = nullptr;
677 
678  for (GList* node = trans->splits; node; node = node->next)
679  {
680  Split* split = GNC_SPLIT(node->data);
681  Account* acc = nullptr;
682  if (!split)
683  continue;
684  acc = xaccSplitGetAccount(split);
685  if (acc && xaccAccountGetType(acc) == ACCT_TYPE_TRADING)
686  trading_splits = g_list_prepend (trading_splits, node->data);
687  }
688 
689  if (!trading_splits)
690  return;
691 
692  xaccTransBeginEdit (trans);
693  /* destroy_splits doesn't actually free the splits but this gets
694  * the list itself freed.
695  */
696  g_list_free_full (trading_splits, destroy_split);
697  xaccTransCommitEdit (trans);
698 }
699 
700 static void
701 gnc_transaction_balance_trading (Transaction *trans, Account *root)
702 {
703  MonetaryList *imbal_list;
704  MonetaryList *imbalance_commod;
705  Split *balance_split = nullptr;
706 
707  /* If the transaction is balanced, nothing more to do */
708  imbal_list = xaccTransGetImbalance (trans);
709  if (!imbal_list)
710  {
711  LEAVE("transaction is balanced");
712  return;
713  }
714 
715  PINFO ("Currency unbalanced transaction");
716 
717  for (imbalance_commod = imbal_list; imbalance_commod;
718  imbalance_commod = imbalance_commod->next)
719  {
720  auto imbal_mon = static_cast<gnc_monetary*>(imbalance_commod->data);
721  gnc_commodity *commodity;
722  gnc_numeric old_amount, new_amount;
723  const gnc_commodity *txn_curr = xaccTransGetCurrency (trans);
724 
725  commodity = gnc_monetary_commodity (*imbal_mon);
726 
727  balance_split = get_trading_split(trans, root, commodity);
728  if (!balance_split)
729  {
730  /* Error already logged */
731  gnc_monetary_list_free(imbal_list);
732  LEAVE("");
733  return;
734  }
735 
736  xaccTransBeginEdit (trans);
737 
738  old_amount = xaccSplitGetAmount (balance_split);
739  new_amount = gnc_numeric_sub (old_amount, gnc_monetary_value(*imbal_mon),
740  gnc_commodity_get_fraction(commodity),
742 
743  xaccSplitSetAmount (balance_split, new_amount);
744 
745  if (gnc_commodity_equal (txn_curr, commodity))
746  {
747  /* Imbalance commodity is the transaction currency, value in the
748  split must be the same as the amount */
749  xaccSplitSetValue (balance_split, new_amount);
750  }
751  else
752  {
753  gnc_numeric val_imbalance = gnc_transaction_get_commodity_imbalance (trans, commodity);
754 
755  gnc_numeric old_value = xaccSplitGetValue (balance_split);
756  gnc_numeric new_value = gnc_numeric_sub (old_value, val_imbalance,
757  gnc_commodity_get_fraction(txn_curr),
759 
760  xaccSplitSetValue (balance_split, new_value);
761  }
762 
763  xaccSplitScrub (balance_split);
764  xaccTransCommitEdit (trans);
765  }
766 
767  gnc_monetary_list_free(imbal_list);
768 }
769 
775 static void
776 gnc_transaction_balance_trading_more_splits (Transaction *trans, Account *root)
777 {
778  /* Copy the split list so we don't see the splits we're adding */
779  GList *splits_dup = g_list_copy(trans->splits), *splits = nullptr;
780  const gnc_commodity *txn_curr = xaccTransGetCurrency (trans);
781  for (splits = splits_dup; splits; splits = splits->next)
782  {
783  Split *split = GNC_SPLIT(splits->data);
784  if (! xaccTransStillHasSplit(trans, split)) continue;
785  if (!gnc_numeric_zero_p(xaccSplitGetValue(split)) &&
787  {
788  gnc_commodity *commodity;
789  gnc_numeric old_value, new_value;
790  Split *balance_split;
791 
792  commodity = xaccAccountGetCommodity(xaccSplitGetAccount(split));
793  if (!commodity)
794  {
795  PERR("Split has no commodity");
796  continue;
797  }
798  balance_split = get_trading_split(trans, root, commodity);
799  if (!balance_split)
800  {
801  /* Error already logged */
802  LEAVE("");
803  return;
804  }
805  xaccTransBeginEdit (trans);
806 
807  old_value = xaccSplitGetValue (balance_split);
808  new_value = gnc_numeric_sub (old_value, xaccSplitGetValue(split),
809  gnc_commodity_get_fraction(txn_curr),
811  xaccSplitSetValue (balance_split, new_value);
812 
813  /* Don't change the balance split's amount since the amount
814  is zero in the split we're working on */
815 
816  xaccSplitScrub (balance_split);
817  xaccTransCommitEdit (trans);
818  }
819  }
820 
821  g_list_free(splits_dup);
822 }
823 
830 void
831 xaccTransScrubImbalance (Transaction *trans, Account *root,
832  Account *account)
833 {
834  gnc_numeric imbalance;
835 
836  if (!trans) return;
837 
838  ENTER ("()");
839 
840  /* Must look for orphan splits even if there is no imbalance. */
841  xaccTransScrubSplits (trans);
842 
843  /* Return immediately if things are balanced. */
844  if (xaccTransIsBalanced (trans))
845  {
846  LEAVE ("transaction is balanced");
847  return;
848  }
849 
850  if (! xaccTransUseTradingAccounts (trans))
851  {
852  gnc_transaction_balance_no_trading (trans, root, account);
853  LEAVE ("transaction balanced, no managed trading accounts");
854  return;
855  }
856 
857  xaccTransClearTradingSplits (trans);
858  imbalance = xaccTransGetImbalanceValue (trans);
859  if (! gnc_numeric_zero_p (imbalance))
860  {
861  PINFO ("Value unbalanced transaction");
862 
863  add_balance_split (trans, imbalance, root, account);
864  }
865 
866  gnc_transaction_balance_trading (trans, root);
868  {
869  LEAVE ("()");
870  return;
871  }
872  /* If the transaction is still not balanced, it's probably because there
873  are splits with zero amount and non-zero value. These are usually
874  realized gain/loss splits. Add a reversing split for each of them to
875  balance the value. */
876 
877  gnc_transaction_balance_trading_more_splits (trans, root);
879  PERR("Balancing currencies unbalanced value");
880 
881 }
882 
883 /* ================================================================ */
884 /* The xaccTransFindCommonCurrency () method returns
885  * a gnc_commodity indicating a currency denomination that all
886  * of the splits in this transaction have in common, using the
887  * old/obsolete currency/security fields of the split accounts.
888  */
889 
890 static gnc_commodity *
891 FindCommonExclSCurrency (SplitList *splits,
892  gnc_commodity * ra, gnc_commodity * rb,
893  Split *excl_split)
894 {
895  GList *node;
896 
897  if (!splits) return nullptr;
898 
899  for (node = splits; node; node = node->next)
900  {
901  Split *s = GNC_SPLIT(node->data);
902  gnc_commodity * sa, * sb;
903 
904  if (s == excl_split) continue;
905 
906  g_return_val_if_fail (s->acc, nullptr);
907 
908  sa = DxaccAccountGetCurrency (s->acc);
909  sb = xaccAccountGetCommodity (s->acc);
910 
911  if (ra && rb)
912  {
913  int aa = !gnc_commodity_equiv(ra, sa);
914  int ab = !gnc_commodity_equiv(ra, sb);
915  int ba = !gnc_commodity_equiv(rb, sa);
916  int bb = !gnc_commodity_equiv(rb, sb);
917 
918  if ( (!aa) && bb) rb = nullptr;
919  else if ( (!ab) && ba) rb = nullptr;
920  else if ( (!ba) && ab) ra = nullptr;
921  else if ( (!bb) && aa) ra = nullptr;
922  else if ( aa && bb && ab && ba )
923  {
924  ra = nullptr;
925  rb = nullptr;
926  }
927 
928  if (!ra)
929  {
930  ra = rb;
931  rb = nullptr;
932  }
933  }
934  else if (ra && !rb)
935  {
936  int aa = !gnc_commodity_equiv(ra, sa);
937  int ab = !gnc_commodity_equiv(ra, sb);
938  if ( aa && ab ) ra = nullptr;
939  }
940  else if (!ra && rb)
941  {
942  int aa = !gnc_commodity_equiv(rb, sa);
943  int ab = !gnc_commodity_equiv(rb, sb);
944  ra = ( aa && ab ) ? nullptr : rb;
945  }
946 
947  if ((!ra) && (!rb)) return nullptr;
948  }
949 
950  return (ra);
951 }
952 
953 /* This is the wrapper for those calls (i.e. the older ones) which
954  * don't exclude one split from the splitlist when looking for a
955  * common currency.
956  */
957 static gnc_commodity *
958 FindCommonCurrency (GList *splits, gnc_commodity * ra, gnc_commodity * rb)
959 {
960  return FindCommonExclSCurrency(splits, ra, rb, nullptr);
961 }
962 
963 static gnc_commodity *
964 xaccTransFindOldCommonCurrency (Transaction *trans, QofBook *book)
965 {
966  gnc_commodity *ra, *rb, *retval;
967  Split *split;
968 
969  if (!trans) return nullptr;
970 
971  if (trans->splits == nullptr) return nullptr;
972 
973  g_return_val_if_fail (book, nullptr);
974 
975  split = GNC_SPLIT(trans->splits->data);
976 
977  if (!split || nullptr == split->acc) return nullptr;
978 
979  ra = DxaccAccountGetCurrency (split->acc);
980  rb = xaccAccountGetCommodity (split->acc);
981 
982  retval = FindCommonCurrency (trans->splits, ra, rb);
983 
984  if (retval && !gnc_commodity_is_currency(retval))
985  retval = nullptr;
986 
987  return retval;
988 }
989 
990 /* Test the currency of the splits and find the most common and return
991  * it, or nullptr if there is no currency more common than the
992  * others -- or none at all.
993  */
994 typedef struct
995 {
996  gnc_commodity *commodity;
997  unsigned int count;
999 
1000 static gint
1001 commodity_equal (gconstpointer a, gconstpointer b)
1002 {
1003  CommodityCount *cc = (CommodityCount*)a;
1004  gnc_commodity *com = (gnc_commodity*)b;
1005  if ( cc == nullptr || cc->commodity == nullptr ||
1006  !GNC_IS_COMMODITY( cc->commodity ) ) return -1;
1007  if ( com == nullptr || !GNC_IS_COMMODITY( com ) ) return 1;
1008  if ( gnc_commodity_equal(cc->commodity, com) )
1009  return 0;
1010  return 1;
1011 }
1012 
1013 static gint
1014 commodity_compare( gconstpointer a, gconstpointer b)
1015 {
1016  CommodityCount *ca = (CommodityCount*)a, *cb = (CommodityCount*)b;
1017  if (ca == nullptr || ca->commodity == nullptr ||
1018  !GNC_IS_COMMODITY( ca->commodity ) )
1019  {
1020  if (cb == nullptr || cb->commodity == nullptr ||
1021  !GNC_IS_COMMODITY( cb->commodity ) )
1022  return 0;
1023  return -1;
1024  }
1025  if (cb == nullptr || cb->commodity == nullptr ||
1026  !GNC_IS_COMMODITY( cb->commodity ) )
1027  return 1;
1028  if (ca->count == cb->count)
1029  return 0;
1030  return ca->count > cb->count ? 1 : -1;
1031 }
1032 
1033 /* Find the commodities in the account of each of the splits of a
1034  * transaction, and rank them by how many splits in which they
1035  * occur. Commodities which are currencies count more than those which
1036  * aren't, because for simple buy and sell transactions it makes
1037  * slightly more sense for the transaction commodity to be the
1038  * currency -- to the extent that it makes sense for a transaction to
1039  * have a currency at all. jralls, 2010-11-02 */
1040 
1041 static gnc_commodity *
1042 xaccTransFindCommonCurrency (Transaction *trans, QofBook *book)
1043 {
1044  gnc_commodity *com_scratch;
1045  GList *node = nullptr;
1046  GSList *comlist = nullptr, *found = nullptr;
1047 
1048  if (!trans) return nullptr;
1049 
1050  if (trans->splits == nullptr) return nullptr;
1051 
1052  g_return_val_if_fail (book, nullptr);
1053 
1054  /* Find the most commonly used currency among the splits. If a given split
1055  is in a non-currency commodity, then look for an ancestor account in a
1056  currency, but prefer currencies used directly in splits. Ignore trading
1057  account splits in this whole process, they don't add any value to this algorithm. */
1058  for (node = trans->splits; node; node = node->next)
1059  {
1060  Split *s = GNC_SPLIT(node->data);
1061  unsigned int curr_weight;
1062 
1063  if (s == nullptr || s->acc == nullptr) continue;
1064  if (xaccAccountGetType(s->acc) == ACCT_TYPE_TRADING) continue;
1065  com_scratch = xaccAccountGetCommodity(s->acc);
1066  if (com_scratch && gnc_commodity_is_currency(com_scratch))
1067  {
1068  curr_weight = 3;
1069  }
1070  else
1071  {
1072  com_scratch = gnc_account_get_currency_or_parent(s->acc);
1073  if (com_scratch == nullptr) continue;
1074  curr_weight = 1;
1075  }
1076  if ( comlist )
1077  {
1078  found = g_slist_find_custom(comlist, com_scratch, commodity_equal);
1079  }
1080  if (comlist == nullptr || found == nullptr)
1081  {
1082  CommodityCount *count = g_slice_new0(CommodityCount);
1083  count->commodity = com_scratch;
1084  count->count = curr_weight;
1085  comlist = g_slist_append(comlist, count);
1086  }
1087  else
1088  {
1089  CommodityCount *count = (CommodityCount*)(found->data);
1090  count->count += curr_weight;
1091  }
1092  }
1093  found = g_slist_sort( comlist, commodity_compare);
1094 
1095  if ( found && found->data && (((CommodityCount*)(found->data))->commodity != nullptr))
1096  {
1097  return ((CommodityCount*)(found->data))->commodity;
1098  }
1099  /* We didn't find a currency in the current account structure, so try
1100  * an old one. */
1101  return xaccTransFindOldCommonCurrency( trans, book );
1102 }
1103 
1104 /* ================================================================ */
1105 
1106 void
1107 xaccTransScrubCurrency (Transaction *trans)
1108 {
1109  SplitList *node;
1110  gnc_commodity *currency;
1111 
1112  if (!trans) return;
1113 
1114  /* If there are any orphaned splits in a transaction, then the
1115  * this routine will fail. Therefore, we want to make sure that
1116  * there are no orphans (splits without parent account).
1117  */
1118  xaccTransScrubOrphans (trans);
1119 
1120  currency = xaccTransGetCurrency (trans);
1121  if (currency && gnc_commodity_is_currency(currency)) return;
1122 
1123  currency = xaccTransFindCommonCurrency (trans, qof_instance_get_book(trans));
1124  if (currency)
1125  {
1126  xaccTransBeginEdit (trans);
1127  xaccTransSetCurrency (trans, currency);
1128  xaccTransCommitEdit (trans);
1129  }
1130  else
1131  {
1132  if (nullptr == trans->splits)
1133  {
1134  PWARN ("Transaction \"%s\" has no splits in it!", trans->description);
1135  }
1136  else
1137  {
1138  SplitList *node;
1139  char guid_str[GUID_ENCODING_LENGTH + 1];
1140  guid_to_string_buff(xaccTransGetGUID(trans), guid_str);
1141  PWARN ("no common transaction currency found for trans=\"%s\" (%s);",
1142  trans->description, guid_str);
1143 
1144  for (node = trans->splits; node; node = node->next)
1145  {
1146  Split *split = GNC_SPLIT(node->data);
1147  if (nullptr == split->acc)
1148  {
1149  PWARN (" split=\"%s\" is not in any account!", split->memo);
1150  }
1151  else
1152  {
1153  gnc_commodity *currency = xaccAccountGetCommodity(split->acc);
1154  PWARN ("setting to split=\"%s\" account=\"%s\" commodity=\"%s\"",
1155  split->memo, xaccAccountGetName(split->acc),
1156  gnc_commodity_get_mnemonic(currency));
1157 
1158  xaccTransBeginEdit (trans);
1159  xaccTransSetCurrency (trans, currency);
1160  xaccTransCommitEdit (trans);
1161  return;
1162  }
1163  }
1164  }
1165  return;
1166  }
1167 
1168  for (node = trans->splits; node; node = node->next)
1169  {
1170  Split *sp = GNC_SPLIT(node->data);
1171 
1173  xaccSplitGetValue (sp)))
1174  {
1175  gnc_commodity *acc_currency;
1176 
1177  acc_currency = sp->acc ? xaccAccountGetCommodity(sp->acc) : nullptr;
1178  if (acc_currency == currency)
1179  {
1180  /* This Split needs fixing: The transaction-currency equals
1181  * the account-currency/commodity, but the amount/values are
1182  * inequal i.e. they still correspond to the security
1183  * (amount) and the currency (value). In the new model, the
1184  * value is the amount in the account-commodity -- so it
1185  * needs to be set to equal the amount (since the
1186  * account-currency doesn't exist anymore).
1187  *
1188  * Note: Nevertheless we lose some information here. Namely,
1189  * the information that the 'amount' in 'account-old-security'
1190  * was worth 'value' in 'account-old-currency'. Maybe it would
1191  * be better to store that information in the price database?
1192  * But then, for old currency transactions there is still the
1193  * 'other' transaction, which is going to keep that
1194  * information. So I don't bother with that here. -- cstim,
1195  * 2002/11/20. */
1196 
1197  PWARN ("Adjusted split with mismatched values, desc=\"%s\" memo=\"%s\""
1198  " old amount %s %s, new amount %s",
1199  trans->description, sp->memo,
1201  gnc_commodity_get_mnemonic (currency),
1203  xaccTransBeginEdit (trans);
1205  xaccTransCommitEdit (trans);
1206  }
1207  /*else
1208  {
1209  PINFO ("Ok: Split '%s' Amount %s %s, value %s %s",
1210  xaccSplitGetMemo (sp),
1211  gnc_num_dbg_to_string (amount),
1212  gnc_commodity_get_mnemonic (currency),
1213  gnc_num_dbg_to_string (value),
1214  gnc_commodity_get_mnemonic (acc_currency));
1215  }*/
1216  }
1217  }
1218 
1219 }
1220 
1221 /* ================================================================ */
1222 
1223 void
1225 {
1226  gnc_commodity *commodity;
1227 
1228  if (!account) return;
1229  if (xaccAccountGetType(account) == ACCT_TYPE_ROOT) return;
1230 
1231  commodity = xaccAccountGetCommodity (account);
1232  if (commodity) return;
1233 
1234  /* Use the 'obsolete' routines to try to figure out what the
1235  * account commodity should have been. */
1236  commodity = xaccAccountGetCommodity (account);
1237  if (commodity)
1238  {
1239  xaccAccountSetCommodity (account, commodity);
1240  return;
1241  }
1242 
1243  commodity = DxaccAccountGetCurrency (account);
1244  if (commodity)
1245  {
1246  xaccAccountSetCommodity (account, commodity);
1247  return;
1248  }
1249 
1250  PERR ("Account \"%s\" does not have a commodity!",
1251  xaccAccountGetName(account));
1252 }
1253 
1254 /* ================================================================ */
1255 
1256 /* EFFECTIVE FRIEND FUNCTION declared in qofinstance-p.h */
1257 extern void qof_instance_set_dirty (QofInstance*);
1258 
1259 static void
1260 xaccAccountDeleteOldData (Account *account)
1261 {
1262  if (!account) return;
1263  xaccAccountBeginEdit (account);
1264  qof_instance_set_kvp (QOF_INSTANCE (account), nullptr, 1, "old-currency");
1265  qof_instance_set_kvp (QOF_INSTANCE (account), nullptr, 1, "old-security");
1266  qof_instance_set_kvp (QOF_INSTANCE (account), nullptr, 1, "old-currency-scu");
1267  qof_instance_set_kvp (QOF_INSTANCE (account), nullptr, 1, "old-security-scu");
1268  qof_instance_set_dirty (QOF_INSTANCE (account));
1269  xaccAccountCommitEdit (account);
1270 }
1271 
1272 static int
1273 scrub_trans_currency_helper (Transaction *t, gpointer data)
1274 {
1276  return 0;
1277 }
1278 
1279 static void
1280 scrub_account_commodity_helper (Account *account, gpointer data)
1281 {
1282  scrub_depth++;
1283  xaccAccountScrubCommodity (account);
1284  xaccAccountDeleteOldData (account);
1285  scrub_depth--;
1286 }
1287 
1288 void
1290 {
1291  if (!acc) return;
1292  scrub_depth++;
1293  xaccAccountTreeForEachTransaction (acc, scrub_trans_currency_helper, nullptr);
1294 
1295  scrub_account_commodity_helper (acc, nullptr);
1296  gnc_account_foreach_descendant (acc, scrub_account_commodity_helper, nullptr);
1297  scrub_depth--;
1298 }
1299 
1300 /* ================================================================ */
1301 
1302 static gboolean
1303 check_quote_source (gnc_commodity *com, gpointer data)
1304 {
1305  gboolean *commodity_has_quote_src = (gboolean *)data;
1306  if (com && !gnc_commodity_is_iso(com))
1307  *commodity_has_quote_src |= gnc_commodity_get_quote_flag(com);
1308  return TRUE;
1309 }
1310 
1311 static void
1312 move_quote_source (Account *account, gpointer data)
1313 {
1314  gnc_commodity *com;
1315  gnc_quote_source *quote_source;
1316  gboolean new_style = GPOINTER_TO_INT(data);
1317  const char *source, *tz;
1318 
1319  com = xaccAccountGetCommodity(account);
1320  if (!com)
1321  return;
1322 
1323  if (!new_style)
1324  {
1325  source = dxaccAccountGetPriceSrc(account);
1326  if (!source || !*source)
1327  return;
1328  tz = dxaccAccountGetQuoteTZ(account);
1329 
1330  PINFO("to %8s from %s", gnc_commodity_get_mnemonic(com),
1331  xaccAccountGetName(account));
1332  gnc_commodity_set_quote_flag(com, TRUE);
1333  quote_source = gnc_quote_source_lookup_by_internal(source);
1334  if (!quote_source)
1335  quote_source = gnc_quote_source_add_new(source, FALSE);
1336  gnc_commodity_set_quote_source(com, quote_source);
1337  gnc_commodity_set_quote_tz(com, tz);
1338  }
1339 
1340  dxaccAccountSetPriceSrc(account, nullptr);
1341  dxaccAccountSetQuoteTZ(account, nullptr);
1342  return;
1343 }
1344 
1345 
1346 void
1347 xaccAccountTreeScrubQuoteSources (Account *root, gnc_commodity_table *table)
1348 {
1349  gboolean new_style = FALSE;
1350  ENTER(" ");
1351 
1352  if (!root || !table)
1353  {
1354  LEAVE("Oops");
1355  return;
1356  }
1357  scrub_depth++;
1358  gnc_commodity_table_foreach_commodity (table, check_quote_source, &new_style);
1359 
1360  move_quote_source(root, GINT_TO_POINTER(new_style));
1361  gnc_account_foreach_descendant (root, move_quote_source,
1362  GINT_TO_POINTER(new_style));
1363  LEAVE("Migration done");
1364  scrub_depth--;
1365 }
1366 
1367 /* ================================================================ */
1368 
1369 void
1371 {
1372  GValue v = G_VALUE_INIT;
1373  gchar *str2;
1374 
1375  if (!account) return;
1376  scrub_depth++;
1377 
1378  qof_instance_get_kvp (QOF_INSTANCE (account), &v, 1, "notes");
1379  if (G_VALUE_HOLDS_STRING (&v))
1380  {
1381  str2 = g_strstrip(g_value_dup_string(&v));
1382  if (strlen(str2) == 0)
1383  qof_instance_slot_delete (QOF_INSTANCE (account), "notes");
1384  g_free(str2);
1385  }
1386 
1387  qof_instance_get_kvp (QOF_INSTANCE (account), &v, 1, "placeholder");
1388  if ((G_VALUE_HOLDS_STRING (&v) &&
1389  strcmp(g_value_get_string (&v), "false") == 0) ||
1390  (G_VALUE_HOLDS_BOOLEAN (&v) && ! g_value_get_boolean (&v)))
1391  qof_instance_slot_delete (QOF_INSTANCE (account), "placeholder");
1392 
1393  g_value_unset (&v);
1394  qof_instance_slot_delete_if_empty (QOF_INSTANCE (account), "hbci");
1395  scrub_depth--;
1396 }
1397 
1398 /* ================================================================ */
1399 
1400 void
1402 {
1403  GValue value_s = G_VALUE_INIT;
1404  gboolean already_scrubbed;
1405 
1406  // get the run-once value
1407  qof_instance_get_kvp (QOF_INSTANCE (book), &value_s, 1, "remove-color-not-set-slots");
1408 
1409  already_scrubbed = (G_VALUE_HOLDS_STRING (&value_s) &&
1410  !g_strcmp0 (g_value_get_string (&value_s), "true"));
1411  g_value_unset (&value_s);
1412 
1413  if (already_scrubbed)
1414  return;
1415  else
1416  {
1417  GValue value_b = G_VALUE_INIT;
1418  Account *root = gnc_book_get_root_account (book);
1419  GList *accts = gnc_account_get_descendants_sorted (root);
1420  GList *ptr;
1421 
1422  for (ptr = accts; ptr; ptr = g_list_next (ptr))
1423  {
1424  auto acct = GNC_ACCOUNT(ptr->data);
1425  auto color = xaccAccountGetColor (acct);
1426 
1427  if (g_strcmp0 (color, "Not Set") == 0)
1428  xaccAccountSetColor (acct, "");
1429  }
1430  g_list_free (accts);
1431 
1432  g_value_init (&value_b, G_TYPE_BOOLEAN);
1433  g_value_set_boolean (&value_b, TRUE);
1434 
1435  // set the run-once value
1436  qof_instance_set_kvp (QOF_INSTANCE (book), &value_b, 1, "remove-color-not-set-slots");
1437  g_value_unset (&value_b);
1438  }
1439 }
1440 
1441 /* ================================================================ */
1442 
1443 static Account*
1444 construct_account (Account *root, gnc_commodity *currency, const char *accname,
1445  GNCAccountType acctype, gboolean placeholder)
1446 {
1447  gnc_commodity* root_currency = find_root_currency ();
1448  Account *acc = xaccMallocAccount(gnc_account_get_book (root));
1449  xaccAccountBeginEdit (acc);
1450  if (accname && *accname)
1451  xaccAccountSetName (acc, accname);
1452  if (currency || root_currency)
1453  xaccAccountSetCommodity (acc, currency ? currency : root_currency);
1454  xaccAccountSetType (acc, acctype);
1455  xaccAccountSetPlaceholder (acc, placeholder);
1456 
1457  /* Hang the account off the root. */
1458  gnc_account_append_child (root, acc);
1459  xaccAccountCommitEdit (acc);
1460  return acc;
1461 }
1462 
1463 static Account*
1464 find_root_currency_account_in_list (GList *acc_list)
1465 {
1466  gnc_commodity* root_currency = find_root_currency();
1467  for (GList *node = acc_list; node; node = g_list_next (node))
1468  {
1469  Account *acc = GNC_ACCOUNT (node->data);
1470  gnc_commodity *acc_commodity = nullptr;
1471  if (G_UNLIKELY (!acc)) continue;
1472  acc_commodity = xaccAccountGetCommodity(acc);
1473  if (gnc_commodity_equiv (acc_commodity, root_currency))
1474  return acc;
1475  }
1476 
1477  return nullptr;
1478 }
1479 
1480 static Account*
1481 find_account_matching_name_in_list (GList *acc_list, const char* accname)
1482 {
1483  for (GList* node = acc_list; node; node = g_list_next(node))
1484  {
1485  Account *acc = GNC_ACCOUNT (node->data);
1486  if (G_UNLIKELY (!acc)) continue;
1487  if (g_strcmp0 (accname, xaccAccountGetName (acc)) == 0)
1488  return acc;
1489  }
1490  return nullptr;
1491 }
1492 
1493 Account *
1494 xaccScrubUtilityGetOrMakeAccount (Account *root, gnc_commodity * currency,
1495  const char *accname, GNCAccountType acctype,
1496  gboolean placeholder, gboolean checkname)
1497 {
1498  GList* acc_list;
1499  Account *acc = nullptr;
1500 
1501  g_return_val_if_fail (root, nullptr);
1502 
1503  acc_list =
1505  checkname ? accname : nullptr,
1506  acctype, currency);
1507 
1508  if (!acc_list)
1509  return construct_account (root, currency, accname,
1510  acctype, placeholder);
1511 
1512  if (g_list_next(acc_list))
1513  {
1514  if (!currency)
1515  acc = find_root_currency_account_in_list (acc_list);
1516 
1517  if (!acc)
1518  acc = find_account_matching_name_in_list (acc_list, accname);
1519  }
1520 
1521  if (!acc)
1522  acc = GNC_ACCOUNT (acc_list->data);
1523 
1524  g_list_free (acc_list);
1525  return acc;
1526 }
1527 
1528 void
1529 xaccTransScrubPostedDate (Transaction *trans)
1530 {
1531  time64 orig = xaccTransGetDate(trans);
1532  if(orig == INT64_MAX)
1533  {
1534  GDate date = xaccTransGetDatePostedGDate(trans);
1535  time64 time = gdate_to_time64(date);
1536  if(time != INT64_MAX)
1537  {
1538  // xaccTransSetDatePostedSecs handles committing the change.
1539  xaccTransSetDatePostedSecs(trans, time);
1540  }
1541  }
1542 }
1543 
1544 /* ==================== END OF FILE ==================== */
void xaccAccountSetType(Account *acc, GNCAccountType tip)
Set the account&#39;s type.
Definition: Account.cpp:2422
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:1370
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:1107
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:2807
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:3022
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:1289
gnc_commodity * DxaccAccountGetCurrency(const Account *acc)
Definition: Account.cpp:3359
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:3237
gboolean xaccSplitDestroy(Split *split)
Destructor.
Definition: Split.cpp:1470
void xaccAccountScrubCommodity(Account *account)
The xaccAccountScrubCommodity method fixed accounts without a commodity by using the old account curr...
Definition: Scrub.cpp:1224
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:2716
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:1529
#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:3320
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:608
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:575
Account handling public routines.
void xaccAccountSetPlaceholder(Account *acc, gboolean val)
Set the "placeholder" flag for an account.
Definition: Account.cpp:4080
void xaccAccountSetColor(Account *acc, const char *str)
Set the account&#39;s Color.
Definition: Account.cpp:2591
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:4790
#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:831
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:4829
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:1259
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:4818
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:3378
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:4802
GList * gnc_account_get_children(const Account *account)
This routine returns a GList of all children accounts of the specified account.
Definition: Account.cpp:2931
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:1477
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:3371
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:1273
GNCNumericErrorCode gnc_numeric_check(gnc_numeric a)
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:2913
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:1401
const char * xaccAccountGetName(const Account *acc)
Get the account&#39;s name.
Definition: Account.cpp:3259
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:1347
gint 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:1518
void xaccAccountSetName(Account *acc, const char *str)
Set the account&#39;s name.
Definition: Account.cpp:2443
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:2649
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:3158