[report-utilities] improve (gnc:account-get-comm-value-interval)

This commit will marginally speed up this function when
include-children? is #t. The original code would create a new query
for each descendant. This commit will create one query only for all
accounts when include-children? is #t. Unfortunately there is no
actual live code whereby include-children? is enabled. Anyway this
code is cleaned up.
This commit is contained in:
Christopher Lam 2018-09-14 09:18:32 +08:00
parent 984501e951
commit 77063afa73

View File

@ -413,41 +413,35 @@ construct gnc:make-gnc-monetary and use gnc:monetary->string instead.")
;; just direct children) are are included in the calculation. The results ;; just direct children) are are included in the calculation. The results
;; are returned in a commodity collector. ;; are returned in a commodity collector.
(define (gnc:account-get-comm-value-interval account start-date end-date (define (gnc:account-get-comm-value-interval account start-date end-date
include-children?) include-children?)
(let ((value-collector (gnc:make-commodity-collector)) (let ((value-collector (gnc:make-commodity-collector))
(query (qof-query-create-for-splits)) (query (qof-query-create-for-splits))
(splits #f)) (accounts (cons account
(if include-children?
(if include-children? (gnc-account-get-descendants account)
(for-each '()))))
(lambda (x)
(value-collector 'merge x #f))
(gnc:account-map-descendants
(lambda (d)
(gnc:account-get-comm-value-interval d start-date end-date #f))
account)))
;; Build a query to find all splits between the indicated dates. ;; Build a query to find all splits between the indicated dates.
(qof-query-set-book query (gnc-get-current-book)) (qof-query-set-book query (gnc-get-current-book))
(xaccQueryAddSingleAccountMatch query account QOF-QUERY-AND) (xaccQueryAddAccountMatch query accounts
QOF-GUID-MATCH-ANY
QOF-QUERY-AND)
(xaccQueryAddDateMatchTT query (xaccQueryAddDateMatchTT query
(and start-date #t) (if start-date start-date 0) (and start-date #t) (or start-date 0)
(and end-date #t) (if end-date end-date 0) (and end-date #t) (or end-date 0)
QOF-QUERY-AND) QOF-QUERY-AND)
;; Get the query results. ;; Get the query results.
(set! splits (qof-query-run query)) (let ((splits (qof-query-run query)))
(qof-query-destroy query) (qof-query-destroy query)
;; Add the "value" of each split returned (which is measured
;; Add the "value" of each split returned (which is measured ;; in the transaction currency).
;; in the transaction currency). (for-each
(for-each (lambda (split)
(lambda (split) (value-collector 'add
(value-collector 'add (xaccTransGetCurrency (xaccSplitGetParent split))
(xaccTransGetCurrency (xaccSplitGetParent split)) (xaccSplitGetValue split)))
(xaccSplitGetValue split))) splits))
splits)
value-collector)) value-collector))
;; Calculate the balance of the account in terms of "value" (rather ;; Calculate the balance of the account in terms of "value" (rather