Balance sheet report: Support calculation of unrealized gains/losses on liabilities.

BP


git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@17287 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Charles Day
2008-07-08 17:26:34 +00:00
parent 4545e8054e
commit 2e7af494c3
3 changed files with 66 additions and 23 deletions

View File

@@ -580,6 +580,8 @@
(export gnc-commodity-collector-commodity-count)
(export gnc:account-get-balance-at-date)
(export gnc:account-get-comm-balance-at-date)
(export gnc:account-get-comm-value-interval)
(export gnc:account-get-comm-value-at-date)
(export gnc:accounts-get-balance-helper)
(export gnc:accounts-get-comm-total-profit)
(export gnc:accounts-get-comm-total-income)

View File

@@ -488,6 +488,57 @@
(xaccSplitGetBalance (car splits))))
balance-collector))
;; Calculate the increase in the balance of the account in terms of
;; "value" (as opposed to "amount") between the specified dates.
;; If include-children? is true, the balances of all children (not
;; just direct children) are are included in the calculation. The results
;; 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))
(splits #f))
(if include-children?
(for-each
(lambda (x)
(gnc-commodity-collector-merge value-collector x))
(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.
(qof-query-set-book query (gnc-get-current-book))
(xaccQueryAddSingleAccountMatch query account QOF-QUERY-AND)
(xaccQueryAddDateMatchTS query
(and start-date #t) start-date
(and end-date #t) end-date
QOF-QUERY-AND)
;; Get the query results.
(set! 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)
value-collector))
;; Calculate the balance of the account in terms of "value" (rather
;; than "amount") at the specified date. If include-children? is
;; true, the balances of all children (not just direct children) are
;; are included in the calculation. The results are returned in a
;; commodity collector.
(define (gnc:account-get-comm-value-at-date account date include-children?)
(gnc:account-get-comm-value-interval account #f date include-children?))
;; Adds all accounts' balances, where the balances are determined with
;; the get-balance-fn. The reverse-balance-fn
;; (e.g. gnc-reverse-balance) should return #t if the

View File

@@ -416,22 +416,6 @@
(+ (* 2 tree-depth)
(if (equal? tabbing 'canonically-tabbed) 1 0))))
;; Get the value of all transactions (in transaction currency)
;; in the given account before the given date
(define (get-account-value-at-date account to-date)
(let ((value (gnc:make-commodity-collector)))
(for-each
(lambda (split)
(let* ((parent (xaccSplitGetParent split))
(currency (xaccTransGetCurrency parent)))
(if (gnc:timepair-le (gnc-transaction-get-date-posted parent) to-date)
(value 'add currency (xaccSplitGetValue split)))))
(xaccAccountGetSplitList account)
)
value
)
)
;;(gnc:warn "account names" liability-account-names)
(gnc:html-document-set-title!
doc (string-append company-name " " report-title " "
@@ -451,7 +435,6 @@
;; Get all the balances for each of the account types.
(let* ((asset-balance #f)
(asset-basis #f)
(neg-liability-balance #f) ;; credit balances are < 0
(liability-balance #f)
(neg-equity-balance #f)
@@ -478,7 +461,7 @@
account date-tp #f)))
(get-total-value-fn
(lambda (account)
(get-account-value-at-date account date-tp)))
(gnc:account-get-comm-value-at-date account date-tp #f)))
)
;; If you ask me, any outstanding(TM) retained earnings and
@@ -490,9 +473,6 @@
(set! asset-balance
(gnc:accounts-get-comm-total-assets
asset-accounts get-total-balance-fn))
(set! asset-basis
(gnc:accounts-get-comm-total-assets
asset-accounts get-total-value-fn))
(gnc:report-percent-done 6)
;; sum liabilities
(set! neg-liability-balance
@@ -533,8 +513,18 @@
;; commodity trading accounts they will automatically accumulate the gains.
(set! unrealized-gain-collector (gnc:make-commodity-collector))
(if compute-unrealized-gains?
(begin (unrealized-gain-collector 'merge asset-balance #f)
(unrealized-gain-collector 'minusmerge asset-basis #f)))
(let ((asset-basis
(gnc:accounts-get-comm-total-assets asset-accounts
get-total-value-fn))
(neg-liability-basis
(gnc:accounts-get-comm-total-assets liability-accounts
get-total-value-fn)))
;; Calculate unrealized gains from assets.
(unrealized-gain-collector 'merge asset-balance #f)
(unrealized-gain-collector 'minusmerge asset-basis #f)
;; Combine with unrealized gains from liabilities
(unrealized-gain-collector 'merge neg-liability-balance #f)
(unrealized-gain-collector 'minusmerge neg-liability-basis #f)))
;; calculate equity and liability+equity totals
(set! total-equity-balance (gnc:make-commodity-collector))