r17377 - gnucash/branches/2.2/src/report - [r17287] Balance sheet report: Support calculation of unrealized gains/losses on liabilities.

Andreas Köhler andi5 at cvs.gnucash.org
Tue Jul 22 18:32:16 EDT 2008


Author: andi5
Date: 2008-07-22 18:32:15 -0400 (Tue, 22 Jul 2008)
New Revision: 17377
Trac: http://svn.gnucash.org/trac/changeset/17377

Modified:
   gnucash/branches/2.2/src/report/report-system/report-system.scm
   gnucash/branches/2.2/src/report/report-system/report-utilities.scm
   gnucash/branches/2.2/src/report/standard-reports/balance-sheet.scm
Log:
[r17287] Balance sheet report: Support calculation of unrealized gains/losses on liabilities.

Committed by cedayiv.


Modified: gnucash/branches/2.2/src/report/report-system/report-system.scm
===================================================================
--- gnucash/branches/2.2/src/report/report-system/report-system.scm	2008-07-22 22:32:04 UTC (rev 17376)
+++ gnucash/branches/2.2/src/report/report-system/report-system.scm	2008-07-22 22:32:15 UTC (rev 17377)
@@ -577,6 +577,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)

Modified: gnucash/branches/2.2/src/report/report-system/report-utilities.scm
===================================================================
--- gnucash/branches/2.2/src/report/report-system/report-utilities.scm	2008-07-22 22:32:04 UTC (rev 17376)
+++ gnucash/branches/2.2/src/report/report-system/report-utilities.scm	2008-07-22 22:32:15 UTC (rev 17377)
@@ -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

Modified: gnucash/branches/2.2/src/report/standard-reports/balance-sheet.scm
===================================================================
--- gnucash/branches/2.2/src/report/standard-reports/balance-sheet.scm	2008-07-22 22:32:04 UTC (rev 17376)
+++ gnucash/branches/2.2/src/report/standard-reports/balance-sheet.scm	2008-07-22 22:32:15 UTC (rev 17377)
@@ -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))



More information about the gnucash-changes mailing list