[trial-balance] compact (collect-unrealized-gains)

use functional style
This commit is contained in:
Christopher Lam 2019-09-19 21:21:35 +08:00
parent 6b573de128
commit 656d2718d8

View File

@ -523,36 +523,21 @@
;; ;;
;; This procedure returns a commodity collector. ;; This procedure returns a commodity collector.
(define (collect-unrealized-gains) (define (collect-unrealized-gains)
(define (acct->bal acct)
(gnc:account-get-comm-balance-at-date acct end-date #f))
(if (eq? price-source 'average-cost) (if (eq? price-source 'average-cost)
;; No need to calculate if doing valuation at cost. ;; No need to calculate if doing valuation at cost.
(gnc:make-commodity-collector) (gnc:make-commodity-collector)
(let ((book-balance (gnc:make-commodity-collector)) (let* ((cost-fn (gnc:case-exchange-fn
(unrealized-gain-collector (gnc:make-commodity-collector)) 'average-cost report-commodity end-date))
(cost-fn (gnc:case-exchange-fn (acct-balances (map acct->bal all-accounts))
'average-cost report-commodity end-date))) (book-balance (apply coll-plus acct-balances))
(value (gnc:sum-collector-commodity
;; Calculate book balance. book-balance report-commodity exchange-fn))
;; assets - liabilities - equity; normally 0 (cost (gnc:sum-collector-commodity
(for-each book-balance report-commodity cost-fn)))
(lambda (acct) ;; Get the unrealized gain or loss (value minus cost).
(book-balance (gnc:monetaries-add value (gnc:monetary-neg cost)))))
'merge
(gnc:account-get-comm-balance-at-date acct end-date #f)
#f))
all-accounts)
(let ((value (gnc:gnc-monetary-amount
(gnc:sum-collector-commodity
book-balance report-commodity exchange-fn)))
(cost (gnc:gnc-monetary-amount
(gnc:sum-collector-commodity
book-balance report-commodity cost-fn))))
;; Get the unrealized gain or loss (value minus cost).
(unrealized-gain-collector
'add report-commodity (- value cost))
unrealized-gain-collector))))
;; set default cell alignment ;; set default cell alignment
(gnc:html-table-set-style! (gnc:html-table-set-style!