diff --git a/bindings/engine-common.i b/bindings/engine-common.i index b4d861dd67..d85e7ef8d1 100644 --- a/bindings/engine-common.i +++ b/bindings/engine-common.i @@ -48,10 +48,14 @@ AccountList * gnc_account_get_descendants (const Account *account); %newobject gnc_account_get_descendants_sorted; AccountList * gnc_account_get_descendants_sorted (const Account *account); +%newobject gnc_accounts_and_all_descendants; +AccountList * gnc_accounts_and_all_descendants (AccountList *accounts); + %ignore gnc_account_get_children; %ignore gnc_account_get_children_sorted; %ignore gnc_account_get_descendants; %ignore gnc_account_get_descendants_sorted; +%ignore gnc_accounts_and_all_descendants; %include %include diff --git a/gnucash/report/html-acct-table.scm b/gnucash/report/html-acct-table.scm index 6a04c2fcbc..9d8952fa38 100644 --- a/gnucash/report/html-acct-table.scm +++ b/gnucash/report/html-acct-table.scm @@ -555,6 +555,7 @@ (gnc:string-localeelt (gnc:make-gnc-monetary comm 0))))) ;; all selected accounts (of report-specific type), *and* ;; their descendants (of any type) need to be scanned. - (gnc:accounts-and-all-descendants accounts))) + (gnc-accounts-and-all-descendants accounts))) ;; Creates the to be used in the function ;; below. diff --git a/gnucash/report/reports/standard/net-charts.scm b/gnucash/report/reports/standard/net-charts.scm index 6d9d0f7d5e..1f58450f7b 100644 --- a/gnucash/report/reports/standard/net-charts.scm +++ b/gnucash/report/reports/standard/net-charts.scm @@ -299,7 +299,7 @@ (gnc:report-percent-done 1) (set! commodity-list (gnc:accounts-get-commodities - (gnc:accounts-and-all-descendants accounts) + (gnc-accounts-and-all-descendants accounts) report-currency)) (gnc:report-percent-done 10) (set! exchange-fn (gnc:case-exchange-time-fn diff --git a/gnucash/report/reports/standard/portfolio.scm b/gnucash/report/reports/standard/portfolio.scm index 45272d12ca..020e867ded 100644 --- a/gnucash/report/reports/standard/portfolio.scm +++ b/gnucash/report/reports/standard/portfolio.scm @@ -195,7 +195,7 @@ ;(gnc:debug "accounts" accounts) (if (not (null? accounts)) (let* ((commodity-list (gnc:accounts-get-commodities - (gnc:accounts-and-all-descendants accounts) + (gnc-accounts-and-all-descendants accounts) currency)) (pricedb (gnc-pricedb-get-db (gnc-get-current-book))) (exchange-fn (gnc:case-exchange-fn price-source currency to-date)) diff --git a/gnucash/report/reports/standard/test/test-income-gst.scm b/gnucash/report/reports/standard/test/test-income-gst.scm index a78bb4c649..8c069d8b9a 100644 --- a/gnucash/report/reports/standard/test/test-income-gst.scm +++ b/gnucash/report/reports/standard/test/test-income-gst.scm @@ -232,10 +232,10 @@ (define (default-testing-options) (let ((options (gnc:make-report-options rpt-uuid))) (set-option! options "Accounts" "Sales" - (gnc:accounts-and-all-descendants + (gnc-accounts-and-all-descendants (list (get-acct "Income")))) (set-option! options "Accounts" "Purchases" - (gnc:accounts-and-all-descendants + (gnc-accounts-and-all-descendants (list (get-acct "Expenses")))) (set-option! options "Accounts" "Tax Accounts" (list (get-acct "Purchases VAT") diff --git a/gnucash/report/test/test-report-utilities.scm b/gnucash/report/test/test-report-utilities.scm index 647540b2d9..ce38166731 100644 --- a/gnucash/report/test/test-report-utilities.scm +++ b/gnucash/report/test/test-report-utilities.scm @@ -546,9 +546,11 @@ (account-lookup "GBP Savings") (account-lookup "Expenses") (account-lookup "Fuel")) - (gnc:accounts-and-all-descendants - (list (account-lookup "Expenses") - (account-lookup "GBP Bank")))) + (sort + (gnc-accounts-and-all-descendants + (list (account-lookup "Expenses") + (account-lookup "GBP Bank"))) + gnc:account-full-name #include +#include static QofLogModule log_module = GNC_MOD_ACCOUNT; @@ -6228,6 +6229,21 @@ gboolean xaccAccountRegister (void) return qof_object_register (&account_object_def); } +using AccountSet = std::unordered_set; +static void maybe_add_descendants (Account* acc, gpointer arg) +{ + if (static_cast (arg)->insert (acc).second) + g_list_foreach (GET_PRIVATE(acc)->children, (GFunc) maybe_add_descendants, arg); +}; + +GList * +gnc_accounts_and_all_descendants (GList *accounts) +{ + AccountSet accset; + g_list_foreach (accounts, (GFunc) maybe_add_descendants, &accset); + return std::accumulate (accset.begin(), accset.end(), (GList*) nullptr, g_list_prepend); +} + /* ======================= UNIT TESTING ACCESS ======================= * The following functions are for unit testing use only. */ diff --git a/libgnucash/engine/Account.h b/libgnucash/engine/Account.h index 19093036c1..868c1b9557 100644 --- a/libgnucash/engine/Account.h +++ b/libgnucash/engine/Account.h @@ -1620,6 +1620,7 @@ void dxaccAccountSetQuoteTZ (Account *account, const char *tz); const char * dxaccAccountGetQuoteTZ (const Account *account); /** @} */ +GList * gnc_accounts_and_all_descendants (GList *accounts); /** @name Account parameter names @{