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