[report-utilities.scm] convert account value accumulator to c++

This commit is contained in:
Christopher Lam
2026-06-29 10:21:11 +08:00
parent b6e4672ab6
commit 6070bac87b
2 changed files with 36 additions and 28 deletions
+29
View File
@@ -84,6 +84,10 @@ SCM gnc_account_accumulate_to_dates (const Account *acc, SCM dates,
AccountVec gnc_accounts_and_all_descendants (AccountVec accounts);
void gnc_account_foreach_split_between_dates (const Account* account,
SCM start_date, SCM end_date,
bool include_children, SCM scm_cb);
extern "C"
{
SCM scm_init_sw_engine_module (void);
@@ -240,6 +244,31 @@ gnc_accounts_and_all_descendants (AccountVec accounts)
return AccountVec (accset.begin(), accset.end());
}
void
gnc_account_foreach_split_between_dates (const Account* account,
SCM start_date, SCM end_date,
bool include_children, SCM scm_cb)
{
std::optional<time64> start, end;
if (scm_is_exact_integer (start_date)) start = scm_to_int64 (start_date);
if (scm_is_exact_integer (end_date)) end = scm_to_int64 (end_date);
auto maybe_call = [&](const Split* s)
{
if (!start || *start <= xaccTransGetDate (xaccSplitGetParent (s)))
scm_call_1 (scm_cb, gnc_split_to_scm (s));
};
std::function<void(const Account*)> scan_account;
if (end)
scan_account = [end, maybe_call](auto acc)
{ gnc_account_foreach_split_until_date (acc, *end, maybe_call); };
else
scan_account = [maybe_call](auto acc)
{ gnc_account_foreach_split (acc, maybe_call); };
scan_account (account);
if (include_children)
gnc_account_foreach_descendant (account, scan_account);
}
%}
/* NB: The object ownership annotations should already cover all the
+7 -28
View File
@@ -525,34 +525,13 @@
;; are returned in a commodity collector.
(define (gnc:account-get-comm-value-interval account start-date end-date
include-children?)
(let ((value-collector (gnc:make-commodity-collector))
(query (qof-query-create-for-splits))
(accounts (cons account
(if include-children?
(gnc-account-get-descendants account)
'()))))
;; Build a query to find all splits between the indicated dates.
(qof-query-set-book query (gnc-get-current-book))
(xaccQueryAddAccountMatch query accounts
QOF-GUID-MATCH-ANY
QOF-QUERY-AND)
(xaccQueryAddDateMatchTT query
(and start-date #t) (or start-date 0)
(and end-date #t) (or end-date 0)
QOF-QUERY-AND)
;; Get the query results.
(let ((splits (qof-query-run query)))
(qof-query-destroy query)
;; Add the "value" of each split returned (which is measured
;; in the transaction currency).
(for-each
(lambda (split)
(value-collector 'add
(xaccTransGetCurrency (xaccSplitGetParent split))
(xaccSplitGetValue split)))
splits))
(let ((value-collector (gnc:make-commodity-collector)))
(gnc-account-foreach-split-between-dates
account start-date end-date include-children?
(lambda (split)
(value-collector 'add
(xaccTransGetCurrency (xaccSplitGetParent split))
(xaccSplitGetValue split))))
value-collector))
;; Calculate the balance of the account in terms of "value" (rather